/ adafruit_max7219 / bcddigits.py
bcddigits.py
  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  `adafruit_max7219.bcddigits.BCDDigits`
 26  ====================================================
 27  """
 28  from micropython import const
 29  from adafruit_max7219 import max7219
 30  
 31  __version__ = "0.0.0-auto.0"
 32  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MAX7219.git"
 33  
 34  _DECODEMODE = const(9)
 35  _SCANLIMIT = const(11)
 36  _SHUTDOWN = const(12)
 37  _DISPLAYTEST = const(15)
 38  
 39  
 40  class BCDDigits(max7219.MAX7219):
 41      """
 42      Basic support for display on a 7-Segment BCD display controlled
 43      by a Max7219 chip using SPI.
 44  
 45      :param object spi: an spi busio or spi bitbangio object
 46      :param ~digitalio.DigitalInOut cs: digital in/out to use as chip select signal
 47      :param int nDigits: number of led 7-segment digits; default 1; max 8
 48      """
 49  
 50      def __init__(self, spi, cs, nDigits=1):
 51          self._ndigits = nDigits
 52          super().__init__(self._ndigits, 8, spi, cs)
 53  
 54      def init_display(self):
 55  
 56          for cmd, data in (
 57              (_SHUTDOWN, 0),
 58              (_DISPLAYTEST, 0),
 59              (_SCANLIMIT, 7),
 60              (_DECODEMODE, (2 ** self._ndigits) - 1),
 61              (_SHUTDOWN, 1),
 62          ):
 63              self.write_cmd(cmd, data)
 64  
 65          self.clear_all()
 66          self.show()
 67  
 68      def set_digit(self, dpos, value):
 69          """
 70          Display one digit.
 71  
 72          :param int dpos: the digit position; zero-based
 73          :param int value: integer ranging from 0->15
 74          """
 75          dpos = self._ndigits - dpos - 1
 76          for i in range(4):
 77              # print('digit {} pixel {} value {}'.format(dpos,i+4,v & 0x01))
 78              self.pixel(dpos, i, value & 0x01)
 79              value >>= 1
 80  
 81      def set_digits(self, start, values):
 82          """
 83          Display digits from a list.
 84  
 85          :param int s: digit to start display zero-based
 86          :param list ds: list of integer values ranging from 0->15
 87          """
 88          for value in values:
 89              # print('set digit {} start {}'.format(d,start))
 90              self.set_digit(start, value)
 91              start += 1
 92  
 93      def show_dot(self, dpos, bit_value=None):
 94          """
 95          The decimal point for a digit.
 96  
 97          :param int dpos: the digit to set the decimal point zero-based
 98          :param int value: value > zero lights the decimal point, else unlights the point
 99          """
100          if 0 <= dpos < self._ndigits:
101              # print('set dot {} = {}'.format((self._ndigits - d -1),col))
102              self.pixel(self._ndigits - dpos - 1, 7, bit_value)
103  
104      def clear_all(self):
105          """
106          Clear all digits and decimal points.
107          """
108          self.fill(1)
109          for i in range(self._ndigits):
110              self.show_dot(i)
111  
112      def show_str(self, start, strg):
113          """
114          Displays a numeric str in the display.  Shows digits ``0-9``, ``-``, and ``.``.
115  
116          :param int start: start position to show the numeric string
117          :param string str: the numeric string
118          """
119          cpos = start
120          for char in strg:
121              # print('c {}'.format(c))
122              value = 0x0F  # assume blank
123              if "0" <= char <= "9":
124                  value = int(char)
125              elif char == "-":
126                  value = 10  # minus sign
127              elif char == ".":
128                  self.show_dot(cpos - 1, 1)
129                  continue
130              self.set_digit(cpos, value)
131              cpos += 1
132  
133      def show_help(self, start):
134          """
135          Display the word HELP in the display.
136  
137          :param int start: start position to show HELP
138          """
139          digits = [12, 11, 13, 14]
140          self.set_digits(start, digits)