|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2018 Carter Nelson for Adafruit Industries |
| 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 all |
| 13 | +# 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 THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +""" |
| 24 | +`adafruit_ht16k33.bargraph` |
| 25 | +=========================== |
| 26 | +
|
| 27 | +* Authors: Carter Nelson for Adafruit Industries |
| 28 | +
|
| 29 | +""" |
| 30 | + |
| 31 | +from adafruit_ht16k33.ht16k33 import HT16K33 |
| 32 | + |
| 33 | +class Bicolor24(HT16K33): |
| 34 | + """Bi-color 24-bar bargraph display.""" |
| 35 | + |
| 36 | + LED_OFF = 0 |
| 37 | + LED_RED = 1 |
| 38 | + LED_GREEN = 2 |
| 39 | + LED_YELLOW = 3 |
| 40 | + |
| 41 | + def __getitem__(self, key): |
| 42 | + # map to HT16K33 row (x) and column (y), see schematic |
| 43 | + x = key % 4 + 4 * (key // 12) |
| 44 | + y = key // 4 - 3 * (key // 12) |
| 45 | + # construct the color value and return it |
| 46 | + return self._pixel(x, y) | self._pixel(x+8, y) << 1 |
| 47 | + |
| 48 | + def __setitem__(self, key, value): |
| 49 | + # map to HT16K33 row (x) and column (y), see schematic |
| 50 | + x = key % 4 + 4 * (key // 12) |
| 51 | + y = key // 4 - 3 * (key // 12) |
| 52 | + # conditionally turn on red LED |
| 53 | + self._pixel(x, y, value & 0x01) |
| 54 | + # conditionally turn on green LED |
| 55 | + self._pixel(x+8, y, value >> 1) |
| 56 | + |
| 57 | + def fill(self, color): |
| 58 | + """Fill the whole display with the given color.""" |
| 59 | + what_it_was = self.auto_write |
| 60 | + self.auto_write = False |
| 61 | + for i in range(24): |
| 62 | + self[i] = color |
| 63 | + self.show() |
| 64 | + self.auto_write = what_it_was |
0 commit comments