Skip to content

Commit cfd4c75

Browse files
committed
Initial Commit
0 parents  commit cfd4c75

File tree

7 files changed

+469
-0
lines changed

7 files changed

+469
-0
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mike Causer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# MicroPython TM1640 LED Matrix
2+
3+
A MicroPython library for a LED matrix using the TM1740 LED driver.
4+
5+
![demo](docs/demo.jpg)
6+
7+
## Examples
8+
9+
Copy the file to your device, using ampy, webrepl or compiling and deploying. eg.
10+
11+
```
12+
$ ampy put tm1640.py
13+
```
14+
15+
**Basic usage**
16+
17+
```
18+
import tm1640
19+
from machine import Pin
20+
tm = tm1640.TM1640(clk=Pin(14), dio=Pin(13))
21+
22+
# line from bottom left to top right
23+
tm.write([1, 2, 4, 8, 16, 32, 64, 128])
24+
25+
# all on
26+
tm.write([255, 255, 255, 255, 255, 255, 255, 255])
27+
28+
# all off
29+
tm.write([0, 0, 0, 0, 0, 0, 0, 0])
30+
31+
# all LEDs dim
32+
tm.brightness(1)
33+
34+
# all LEDs bright
35+
tm.brightness(7)
36+
37+
# the number 3
38+
tm.write([0b00000000, 0b00011110, 0b00110011, 0b00110000, 0b00011100, 0b00110000, 0b00110011, 0b00011110])
39+
40+
# cross
41+
tm.write(b'\x81\x42\x24\x18\x18\x24\x42\x81')
42+
43+
# squares
44+
tm.write([255, 129, 189, 165, 165, 189, 129, 255])
45+
46+
# 50% on
47+
tm.write_int(0x55aa55aa55aa55aa)
48+
```
49+
50+
For more detailed examples, see ![tm1640_test.py](tm1640_test.py)
51+
52+
## Parts
53+
54+
* [WeMos D1 Mini](https://www.aliexpress.com/store/product/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/1331105_32529101036.html) $3.50 USD
55+
* [Clone DIY More WeMos Matrix LED Shield](https://www.aliexpress.com/item/Matrix-LED-Shield-V1-0-0-For-WEMOS-D1-Mini-Digital-Signal-Output-Module-8-X/32821752799.html) $1.35 USD
56+
* [Dual Base for WeMos D1 Mini](https://www.aliexpress.com/store/product/Dual-Base-for-WeMos-D1-mini/1331105_32642733925.html) $1.00 USD
57+
58+
## Connections
59+
60+
WeMos D1 Mini | TM1640 LED Matrix
61+
------------- | -----------------
62+
D5 (GPIO14) | CLK
63+
D7 (GPIO13) | DIO
64+
3V3 (or 5V) | VCC
65+
G | GND
66+
67+
## Links
68+
69+
* [WeMos D1 Mini](https://wiki.wemos.cc/products:d1:d1_mini)
70+
* [micropython.org](http://micropython.org)
71+
* [TM1640 datasheet](http://www.titanmic.com/pic/other/2014-11-20-15-36-028.pdf)
72+
* [Titan Micro TM1640 product page](http://www.titanmec.com/index.php/en/project/view/id/305.html)
73+
* [MicroPython framebuf](http://docs.micropython.org/en/latest/esp8266/library/framebuf.html)
74+
* [Adafruit Ampy](https://learn.adafruit.com/micropython-basics-load-files-and-run-code/install-ampy)

docs/demo.jpg

251 KB
Loading

docs/tm1640_datasheet_v1.0.pdf

404 KB
Binary file not shown.

setup.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
# Remove current dir from sys.path, otherwise setuptools will peek up our
3+
# module instead of system.
4+
sys.path.pop(0)
5+
from setuptools import setup
6+
7+
setup(
8+
name='micropython-tm1640',
9+
py_modules=['tm1640'],
10+
version='1.0.0',
11+
description='MicroPython library for TM1640 LED driver.',
12+
long_description='This library lets you operate a LED matrix display module based on a TM1640 LED driver.',
13+
keywords='tm1640 led matrix micropython',
14+
url='https://github.com/mcauser/micropython-tm1640',
15+
author='Mike Causer',
16+
author_email='[email protected]',
17+
maintainer='Mike Causer',
18+
maintainer_email='[email protected]',
19+
license='MIT',
20+
classifiers = [
21+
'Development Status :: 5 - Production/Stable',
22+
'Programming Language :: Python :: Implementation :: MicroPython',
23+
'License :: OSI Approved :: MIT License',
24+
],
25+
)

tm1640.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# MicroPython TM1640 LED matrix display driver
2+
3+
from micropython import const
4+
from machine import Pin
5+
from time import sleep_us
6+
7+
TM1640_CMD1 = const(64) # 0x40 data command
8+
TM1640_CMD2 = const(192) # 0xC0 address command
9+
TM1640_CMD3 = const(128) # 0x80 display control command
10+
TM1640_DSP_ON = const(8) # 0x08 display on
11+
TM1640_DELAY = const(10) # 10us delay between clk/dio pulses
12+
13+
class TM1640(object):
14+
"""Library for LED matrix display modules based on the TM1640 LED driver."""
15+
def __init__(self, clk, dio, brightness=7):
16+
self.clk = clk
17+
self.dio = dio
18+
19+
if not 0 <= brightness <= 7:
20+
raise ValueError("Brightness out of range")
21+
self._brightness = brightness
22+
23+
self.clk.init(Pin.OUT, value=0)
24+
self.dio.init(Pin.OUT, value=0)
25+
sleep_us(TM1640_DELAY)
26+
27+
self._write_data_cmd()
28+
self._write_dsp_ctrl()
29+
30+
def _start(self):
31+
self.dio(0)
32+
sleep_us(TM1640_DELAY)
33+
self.clk(0)
34+
sleep_us(TM1640_DELAY)
35+
36+
def _stop(self):
37+
self.dio(0)
38+
sleep_us(TM1640_DELAY)
39+
self.clk(1)
40+
sleep_us(TM1640_DELAY)
41+
self.dio(1)
42+
43+
def _write_data_cmd(self):
44+
# automatic address increment, normal mode
45+
self._start()
46+
self._write_byte(TM1640_CMD1)
47+
self._stop()
48+
49+
def _write_dsp_ctrl(self):
50+
# display on, set brightness
51+
self._start()
52+
self._write_byte(TM1640_CMD3 | TM1640_DSP_ON | self._brightness)
53+
self._stop()
54+
55+
def _write_byte(self, b):
56+
for i in range(8):
57+
self.dio((b >> i) & 1)
58+
sleep_us(TM1640_DELAY)
59+
self.clk(1)
60+
sleep_us(TM1640_DELAY)
61+
self.clk(0)
62+
sleep_us(TM1640_DELAY)
63+
64+
def brightness(self, val=None):
65+
"""Set the display brightness 0-7."""
66+
# brightness 0 = 1/16th pulse width
67+
# brightness 7 = 14/16th pulse width
68+
if val is None:
69+
return self._brightness
70+
if not 0 <= val <= 7:
71+
raise ValueError("Brightness out of range")
72+
73+
self._brightness = val
74+
self._write_data_cmd()
75+
self._write_dsp_ctrl()
76+
77+
def write(self, rows, pos=0):
78+
if not 0 <= pos <= 7:
79+
raise ValueError("Position out of range")
80+
81+
self._write_data_cmd()
82+
self._start()
83+
84+
self._write_byte(TM1640_CMD2 | pos)
85+
for row in rows:
86+
self._write_byte(row)
87+
88+
self._stop()
89+
self._write_dsp_ctrl()
90+
91+
def write_int(self, int, pos=0, len=8):
92+
self.write(int.to_bytes(len, 'big'), pos)
93+
94+
def write_hmsb(self, buf, pos=0):
95+
self._write_data_cmd()
96+
self._start()
97+
98+
self._write_byte(TM1640_CMD2 | pos)
99+
for i in range(7-pos, -1, -1):
100+
self._write_byte(buf[i])
101+
102+
self._stop()
103+
self._write_dsp_ctrl()

0 commit comments

Comments
 (0)