Skip to content

Commit

Permalink
Add temperature, fix brightness, change how pins are toggled
Browse files Browse the repository at this point in the history
  • Loading branch information
mcauser committed Dec 5, 2017
1 parent 0b6b804 commit 04713d7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
103 changes: 60 additions & 43 deletions tm1637.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,75 +23,80 @@ 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.
The MSB in the 2nd segment controls the colon between the 2nd
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."""
Expand Down Expand Up @@ -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
16 changes: 10 additions & 6 deletions tm1637_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -149,5 +146,12 @@
# show "12:59"
tm.numbers(12,59)

# show temperature
tm.temperature(24) '24*C'
# 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

0 comments on commit 04713d7

Please sign in to comment.