From 04713d7ff33be44da73a61da618e3b07e5c0ea22 Mon Sep 17 00:00:00 2001 From: Mike Causer Date: Wed, 6 Dec 2017 09:11:42 +1100 Subject: [PATCH] Add temperature, fix brightness, change how pins are toggled --- setup.py | 2 +- tm1637.py | 103 ++++++++++++++++++++++++++++--------------------- tm1637_test.py | 16 +++++--- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/setup.py b/setup.py index 86a94dd..f0c60b2 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( name='micropython-tm1637', py_modules=['tm1637'], - version='1.1.0', + version='1.2.0', description='MicroPython library for TM1637 LED driver.', long_description='This library lets you operate quad 7-segment LED display modules based on the TM1637 LED driver.', keywords='tm1637 seven segment led micropython', diff --git a/tm1637.py b/tm1637.py index 51314d6..4b56064 100644 --- a/tm1637.py +++ b/tm1637.py @@ -1,18 +1,20 @@ # MicroPython TM1637 quad 7-segment LED display driver +from micropython import const from machine import Pin from time import sleep_us -_CMD_SET1 = const(64) # 0x40 data set -_CMD_SET2 = const(192) # 0xC0 address command set -_CMD_SET3 = const(128) # 0x80 data control command set +TM1637_CMD1 = const(64) # 0x40 data command +TM1637_CMD2 = const(192) # 0xC0 address command +TM1637_CMD3 = const(128) # 0x80 display control command +TM1637_DSP_ON = const(8) # 0x08 display on +TM1637_DELAY = const(10) # 10us delay between clk/dio pulses # 0-9, a-f, blank, dash _SEGMENTS = [63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113,0,64] class TM1637(object): - """Library for the quad 7-segment LED display modules based on the TM1637 - LED driver.""" + """Library for quad 7-segment LED modules based on the TM1637 LED driver.""" def __init__(self, clk, dio, brightness=7): self.clk = clk self.dio = dio @@ -21,59 +23,65 @@ def __init__(self, clk, dio, brightness=7): raise ValueError("Brightness out of range") self._brightness = brightness - self.clk.init(Pin.IN) - self.dio.init(Pin.IN) - self.clk(0) - self.dio(0) + self.clk.init(Pin.OUT, value=0) + self.dio.init(Pin.OUT, value=0) + sleep_us(TM1637_DELAY) + + self._write_data_cmd() + self._write_dsp_ctrl() def _start(self): - self.dio.init(Pin.OUT) - sleep_us(50) + self.dio(0) + sleep_us(TM1637_DELAY) + self.clk(0) + sleep_us(TM1637_DELAY) def _stop(self): - self.dio.init(Pin.OUT) - sleep_us(50) - self.clk.init(Pin.IN) - sleep_us(50) - self.dio.init(Pin.IN) - sleep_us(50) - - def _write_comm1(self): + self.dio(0) + sleep_us(TM1637_DELAY) + self.clk(1) + sleep_us(TM1637_DELAY) + self.dio(1) + + def _write_data_cmd(self): + # automatic address increment, normal mode self._start() - self._write_byte(_CMD_SET1) + self._write_byte(TM1637_CMD1) self._stop() - def _write_comm3(self): + def _write_dsp_ctrl(self): + # display on, set brightness self._start() - self._write_byte(_CMD_SET3 + self._brightness + 7) + self._write_byte(TM1637_CMD3 | TM1637_DSP_ON | self._brightness) self._stop() def _write_byte(self, b): - # send each bit for i in range(8): - self.clk.init(Pin.OUT) - sleep_us(50) - self.dio.init(Pin.IN if b & 1 else Pin.OUT) - sleep_us(50) - self.clk.init(Pin.IN) - sleep_us(50) - b >>= 1 - self.clk.init(Pin.OUT) - sleep_us(50) - self.clk.init(Pin.IN) - sleep_us(50) - self.clk.init(Pin.OUT) - sleep_us(50) + 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 brightness(self, val=None): """Set the display brightness 0-7.""" + # brightness 0 = 1/16th pulse width + # brightness 7 = 14/16th pulse width if val is None: return self._brightness if not 0 <= val <= 7: raise ValueError("Brightness out of range") + self._brightness = val - self._write_comm1() - self._write_comm3() + self._write_data_cmd() + self._write_dsp_ctrl() def write(self, segments, pos=0): """Display up to 4 segments moving right from a given position. @@ -81,15 +89,14 @@ def write(self, segments, pos=0): and 3rd segments.""" if not 0 <= pos <= 3: raise ValueError("Position out of range") - self._write_comm1() - - # write COMM2 + first digit address + self._write_data_cmd() self._start() - self._write_byte(_CMD_SET2 + pos) + + self._write_byte(TM1637_CMD2 | pos) for seg in segments: self._write_byte(seg) self._stop() - self._write_comm3() + self._write_dsp_ctrl() def encode_digit(self, digit): """Convert a character 0-9, a-f to a segment.""" @@ -146,3 +153,13 @@ def numbers(self, num1, num2, colon=True): if colon: segments[1] |= 0x80 self.write(segments) + + def temperature(self, num): + if num < -9: + self.write([0x38, 0x3F]) # LO + elif num > 99: + self.write([0x76, 0x06]) # HI + else: + string = '{0: >2d}'.format(num) + self.write(self.encode_string(string)) + self.write([0x63, 0x39], 2) # degrees C diff --git a/tm1637_test.py b/tm1637_test.py index c2bc959..3964b39 100644 --- a/tm1637_test.py +++ b/tm1637_test.py @@ -39,11 +39,8 @@ # show "COOL" tm.write([0b00111001, 0b00111111, 0b00111111, 0b00111000]) -# all LEDs off -tm.brightness(0) - # all LEDs dim -tm.brightness(1) +tm.brightness(0) # all LEDs bright tm.brightness(7) @@ -149,5 +146,12 @@ # show "12:59" tm.numbers(12,59) -# show temperature -tm.temperature(24) '24*C' \ No newline at end of file +# show temperature '24*C' +tm.temperature(24) + +# show temperature works for range -9 to +99 +tm.temperature(-10) # LO*C +tm.temperature(-9) # -9*C +tm.temperature(5) # 5*C +tm.temperature(99) # 99*C +tm.temperature(100) # HI*C