更新mpy支持tm1637库,同时修改tm1650支持同样初始化

This commit is contained in:
dahanzimin
2025-07-22 16:08:50 +08:00
parent 047c028587
commit 24b5bc4304
2 changed files with 154 additions and 39 deletions

View File

@@ -1,55 +1,49 @@
"""
TM1650 for Four Digit LED Display
CircuitPython library for the TM1650
Micropython library for the TM1650
=======================================================
#Changed from circuitpython to micropython 20220216
dahanzimin From the Mixly Team
@dahanzimin From the Mixly Team
"""
import time
from micropython import const
from machine import Pin, SoftI2C
COMMAND_I2C_ADDRESS = const(0x24)
DISPLAY_I2C_ADDRESS = const(0x34)
buf = (0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71)
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):
self.i2c = i2c_bus
self._intensity = 2
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.tbuf = bytearray(1)
self.on()
def intensity(self, dat = -1):
if dat < 0 or dat > 8:
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
if dat == 0:
val = max(min(val, 7), 0)
self._intensity = val
if val == 0:
self.off()
else:
self._intensity = dat
self.cmd((dat<<4)|0x01)
def cmd(self,command):
self.tbuf[0] = command &0xff
self.i2c.writeto(COMMAND_I2C_ADDRESS, self.tbuf)
def dat(self,bit, data):
self.tbuf[0] = data&0xff
self.i2c.writeto(DISPLAY_I2C_ADDRESS + bit%4, self.tbuf)
self.on()
def on(self):
self.cmd((self._intensity<<4)|0x01)
self._wreg((self._intensity << 4) | 0x01)
def off(self):
self._intensity = 0
self.cmd(0)
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)
@@ -58,9 +52,9 @@ class TM1650:
self.dat(3, 0)
self.dbuf = [0, 0, 0, 0]
def showbit(self, num, bit = 0):
self.dbuf[bit%4] = buf[num%16]
self.dat(bit, buf[num%16])
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:
@@ -81,8 +75,8 @@ class TM1650:
self.showbit(num % 16, 3)
self.showbit((num >> 4) % 16, 2)
self.showbit((num >> 8) % 16, 1)
def showDP(self, bit = 1, show = True):
def showDP(self, bit=1, show=True):
if show:
self.dat(bit, self.dbuf[bit] | 0x80)
else: