更新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

@@ -0,0 +1,121 @@
"""
TM1637 for Four Digit LED Display
Micropython library for the TM1637
=======================================================
@dahanzimin From the Mixly Team
"""
from machine import Pin
from time import sleep_us
from micropython import const
TM1637_CMD1 = const(0x40)
TM1637_CMD2 = const(0xC0)
TM1637_CMD3 = const(0x80)
TM1637_DSP_ON = const(0x08)
TM1637_DELAY = const(0x0A)
_SEGMENTS = b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71'
class TM1637:
def __init__(self, clk, dio, brightness=2):
self.clk = Pin(clk, Pin.OUT, value=0)
self.dio = Pin(dio, Pin.OUT, value=0)
sleep_us(TM1637_DELAY)
self._intensity = brightness
self.dbuf = [0, 0, 0, 0]
self.on()
def _start(self):
self.dio(0)
sleep_us(TM1637_DELAY)
self.clk(0)
sleep_us(TM1637_DELAY)
def _stop(self):
self.dio(0)
sleep_us(TM1637_DELAY)
self.clk(1)
sleep_us(TM1637_DELAY)
self.dio(1)
def _write(self, b):
for i in range(8):
self.dio((b >> i) & 1)
sleep_us(TM1637_DELAY)
self.clk(1)
sleep_us(TM1637_DELAY)
self.clk(0)
sleep_us(TM1637_DELAY)
self.clk(0)
sleep_us(TM1637_DELAY)
self.clk(1)
sleep_us(TM1637_DELAY)
self.clk(0)
sleep_us(TM1637_DELAY)
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._start()
self._write(TM1637_CMD1)
self._stop()
self._start()
self._write(TM1637_CMD3 | TM1637_DSP_ON | self._intensity)
self._stop()
def off(self):
self._start()
self._write(TM1637_CMD3)
self._stop()
def dat(self, bit, data):
self._start()
self._write(TM1637_CMD2 | bit)
self._write(data)
self._stop()
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)

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: