Files
mixly3/boards/default_src/micropython/origin/build/lib/tm1650.py

84 lines
1.9 KiB
Python

"""
TM1650 for Four Digit LED Display
Micropython library for the TM1650
=======================================================
@dahanzimin From the Mixly Team
"""
from micropython import const
from machine import Pin, SoftI2C
TM1650_CMD = const(0x24)
TM1650_DSP = const(0x34)
_SEGMENTS = b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71'
class TM1650:
def __init__(self, i2c_bus=None, clk=0, dio=1, brightness=2):
if i2c_bus is None:
self.i2c = SoftI2C(scl=Pin(clk), sda=Pin(dio), freq=100000)
else:
self.i2c = i2c_bus
self._intensity = brightness
self.dbuf = [0, 0, 0, 0]
self.on()
def _wreg(self, val):
self.i2c.writeto(TM1650_CMD, val.to_bytes(1, 'little'))
def intensity(self, val=None):
"""Set the display brightness 0-7."""
if val is None:
return self._intensity
val = max(min(val, 7), 0)
self._intensity = val
if val == 0:
self.off()
else:
self.on()
def on(self):
self._wreg((self._intensity << 4) | 0x01)
def off(self):
self._wreg(0)
def dat(self, bit, val):
self.i2c.writeto(TM1650_DSP + bit % 4, val.to_bytes(1, 'little'))
def clear(self):
self.dat(0, 0)
self.dat(1, 0)
self.dat(2, 0)
self.dat(3, 0)
self.dbuf = [0, 0, 0, 0]
def showbit(self, num, bit=0):
self.dbuf[bit % 4] = _SEGMENTS[num % 16]
self.dat(bit, _SEGMENTS[num % 16])
def shownum(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num // 1000) % 10)
self.showbit(num % 10, 3)
self.showbit((num // 10) % 10, 2)
self.showbit((num // 100) % 10, 1)
def showhex(self, num):
if num < 0:
self.dat(0, 0x40) # '-'
num = -num
else:
self.showbit((num >> 12) % 16)
self.showbit(num % 16, 3)
self.showbit((num >> 4) % 16, 2)
self.showbit((num >> 8) % 16, 1)
def showDP(self, bit=1, show=True):
if show:
self.dat(bit, self.dbuf[bit] | 0x80)
else:
self.dat(bit, self.dbuf[bit] & 0x7F)