Skip to content

Commit 775c44e

Browse files
authored
Merge pull request #16 from caternuson/iss7
Bi-bolor 24-bar bargraph support
2 parents fa1275c + 64bcf8e commit 775c44e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

adafruit_ht16k33/bargraph.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

examples/bicolor24.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Basic example of using the Bi-color 24 segment bargraph display.
2+
# This example and library is meant to work with Adafruit CircuitPython API.
3+
# Author: Carter Nelson
4+
# License: Public Domain
5+
6+
import time
7+
8+
# Import board related modules
9+
import board
10+
import busio
11+
12+
# Import the Bicolor24 driver from the HT16K33 module
13+
from adafruit_ht16k33.bargraph import Bicolor24
14+
15+
# Create the I2C interface
16+
i2c = busio.I2C(board.SCL, board.SDA)
17+
18+
# Create the LED bargraph class.
19+
bc24 = Bicolor24(i2c)
20+
21+
# Set individual segments of bargraph
22+
bc24[0] = bc24.LED_RED
23+
bc24[1] = bc24.LED_GREEN
24+
bc24[2] = bc24.LED_YELLOW
25+
26+
time.sleep(2)
27+
28+
# Turn them all off
29+
bc24.fill(bc24.LED_OFF)
30+
31+
# Turn them on in a loop
32+
for i in range(24):
33+
bc24[i] = bc24.LED_RED
34+
time.sleep(0.1)
35+
bc24[i] = bc24.LED_OFF
36+
37+
time.sleep(1)
38+
39+
# Fill the entrire bargraph
40+
bc24.fill(bc24.LED_GREEN)

0 commit comments

Comments
 (0)