|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Dan Halbert |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +# |
| 23 | + |
| 24 | +""" |
| 25 | +:mod:`adafruit_max7219.bcddigits.BCDDigits` |
| 26 | +==================================================== |
| 27 | +""" |
| 28 | +from adafruit_max7219 import max7219 |
| 29 | + |
| 30 | +_DECODEMODE = const(9) |
| 31 | +_SCANLIMIT = const(11) |
| 32 | +_SHUTDOWN = const(12) |
| 33 | +_DISPLAYTEST = const(15) |
| 34 | + |
| 35 | +class BCDDigits(max7219.MAX7219): |
| 36 | + """ |
| 37 | + Basic support for display on a 7-Segment BCD display controlled |
| 38 | + by a Max7219 chip using SPI. |
| 39 | + """ |
| 40 | + def __init__(self, spi, cs, nDigits=1): |
| 41 | + """ |
| 42 | + :param object spi: an spi busio or spi bitbangio object |
| 43 | + :param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal |
| 44 | + :param int nDigits: number of led 7-segment digits; default 1; max 8 |
| 45 | + """ |
| 46 | + self.nD = nDigits |
| 47 | + super().__init__(self.nD, 8 ,spi ,cs) |
| 48 | + |
| 49 | + def init_display(self): |
| 50 | + |
| 51 | + for cmd, data in ( |
| 52 | + (_SHUTDOWN, 0), |
| 53 | + (_DISPLAYTEST, 0), |
| 54 | + (_SCANLIMIT, 7), |
| 55 | + (_DECODEMODE, (2**self.nD)-1), |
| 56 | + (_SHUTDOWN, 1), |
| 57 | + ): |
| 58 | + self.write_cmd(cmd, data) |
| 59 | + |
| 60 | + self.clear_all() |
| 61 | + self.show() |
| 62 | + |
| 63 | + def set_digit(self, d, v): |
| 64 | + """ |
| 65 | + set one digit in the display |
| 66 | + :param int d: the digit position; zero-based |
| 67 | + :param int v: integer ranging from 0->15 |
| 68 | + """ |
| 69 | + d = self.nD - d - 1 |
| 70 | + for i in range(4): |
| 71 | + #print('digit {} pixel {} value {}'.format(d,i+4,v & 0x01)) |
| 72 | + self.pixel(d,i,v & 0x01) |
| 73 | + v >>= 1 |
| 74 | + |
| 75 | + def set_digits(self, s, ds): |
| 76 | + """ |
| 77 | + set the display from a list |
| 78 | + :param int s: digit to start display zero-based |
| 79 | + :param list ds: list of integer values ranging from 0->15 |
| 80 | + """ |
| 81 | + for d in ds: |
| 82 | + #print('set digit {} start {}'.format(d,start)) |
| 83 | + self.set_digit(s,d) |
| 84 | + s += 1 |
| 85 | + |
| 86 | + def show_dot(self,d, col=None): |
| 87 | + """ |
| 88 | + set the decimal point for a digit |
| 89 | + :param int d: the digit to set the decimal point zero-based |
| 90 | + :param int col: value > zero lights the decimal point, else unlights the point |
| 91 | + """ |
| 92 | + if d < self.nD and d >= 0: |
| 93 | + #print('set dot {} = {}'.format((self.nD - d -1),col)) |
| 94 | + self.pixel(self.nD-d-1, 7,col) |
| 95 | + |
| 96 | + def clear_all(self): |
| 97 | + """ |
| 98 | + clear all digits and decimal points |
| 99 | + """ |
| 100 | + self.fill(1) |
| 101 | + for i in range(self.nD): |
| 102 | + self.show_dot(i) |
| 103 | + |
| 104 | + def show_str(self,s,str): |
| 105 | + """ |
| 106 | + displays a numeric str in the display. shows digits 0-9, -, and . |
| 107 | + :param int s: start position to show the numeric string |
| 108 | + :param string str: the numeric string |
| 109 | + """ |
| 110 | + ci = s |
| 111 | + for i in range (len(str)): |
| 112 | + c = str[i] |
| 113 | + # print('c {}'.format(c)) |
| 114 | + v = 0x0f # assume blank |
| 115 | + if c >= '0' and c<='9': |
| 116 | + v = int(c) |
| 117 | + elif c == '-': |
| 118 | + v = 10 # minus sign |
| 119 | + elif c == '.': |
| 120 | + self.show_dot(ci-1,1) |
| 121 | + continue |
| 122 | + self.set_digit(ci,v) |
| 123 | + ci += 1 |
| 124 | + |
| 125 | + def show_help(self, s): |
| 126 | + """ |
| 127 | + display the word HELP in the display |
| 128 | + :param int s: start position to show HELP |
| 129 | + """ |
| 130 | + digits = [12,11,13,14] |
| 131 | + self.set_digits(s,digits) |
| 132 | + |
0 commit comments