From 005200d80fb2eb783cc27651f19f2d34db118e36 Mon Sep 17 00:00:00 2001 From: Patrick Palma Date: Wed, 26 Jul 2017 23:11:01 -0300 Subject: [PATCH] Port to python 3 --- clock.py | 28 ++++++++++++++++++++ setup.py | 20 +++++++------- tm1637.py | 72 +++++++++++++++++++++++++++----------------------- tm1637_test.py | 17 ++++++------ 4 files changed, 85 insertions(+), 52 deletions(-) create mode 100644 clock.py diff --git a/clock.py b/clock.py new file mode 100644 index 0000000..81c039c --- /dev/null +++ b/clock.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +from tm1637 import TM1637 +from time import time, sleep, localtime + +DIO=0 +CLK=1 + +def show_clock(tm): + t = localtime() + sleep(1 - time() % 1) + + tm.numbers(t.tm_hour, t.tm_min, True) + sleep(.5) + tm.numbers(t.tm_hour, t.tm_min, False) + +if __name__ == "__main__": + print("\n") + print("============================") + print(" Starting clock application") + print("============================") + + tm = TM1637(CLK, DIO) + tm.brightness(1) + + while True: + show_clock(tm) + diff --git a/setup.py b/setup.py index 86a94dd..557306c 100644 --- a/setup.py +++ b/setup.py @@ -5,21 +5,21 @@ from setuptools import setup setup( - name='micropython-tm1637', + name='raspberrypi-python-tm1637', py_modules=['tm1637'], - version='1.1.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', - url='https://github.com/mcauser/micropython-tm1637', + version='1.0.0', + description='Raspberry Pi Python port from 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 with Raspberry PI.', + keywords='tm1637 raspberry pi seven segment led python', + url='https://github.com/depklyon/raspberrypi-python-tm1637', author='Mike Causer', author_email='mcauser@gmail.com', - maintainer='Mike Causer', - maintainer_email='mcauser@gmail.com', + maintainer='Patrick Palma', + maintainer_email='patrick.depalma@gmail.com', license='MIT', classifiers = [ 'Development Status :: 5 - Production/Stable', - 'Programming Language :: Python :: Implementation :: MicroPython', + 'Programming Language :: Python :: Implementation :: Python', 'License :: OSI Approved :: MIT License', ], -) \ No newline at end of file +) diff --git a/tm1637.py b/tm1637.py index 51314d6..5e1b13e 100644 --- a/tm1637.py +++ b/tm1637.py @@ -1,18 +1,24 @@ -# MicroPython TM1637 quad 7-segment LED display driver +import subprocess +from time import time, sleep, localtime +from wiringpi import wiringPiSetupGpio, pinMode, digitalRead, digitalWrite, GPIO -from machine import Pin -from time import sleep_us +wiringPiSetupGpio() -_CMD_SET1 = const(64) # 0x40 data set -_CMD_SET2 = const(192) # 0xC0 address command set -_CMD_SET3 = const(128) # 0x80 data control command set +_CMD_SET1 = 0x40 # 0x40 data set +_CMD_SET2 = 0xc0 # 0xC0 address command set +_CMD_SET3 = 0x80 # 0x80 data control command set # 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] +_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 the quad 7-segment LED display modules based on the TM1637 + LED driver. + """ + def __init__(self, clk, dio, brightness=7): self.clk = clk self.dio = dio @@ -21,22 +27,22 @@ 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) + pinMode(self.clk, GPIO.INPUT) + pinMode(self.dio, GPIO.INPUT) + digitalWrite(self.clk, 0) + digitalWrite(self.dio, 0) def _start(self): - self.dio.init(Pin.OUT) - sleep_us(50) + pinMode(self.dio, GPIO.OUTPUT) + sleep(0.00050) 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) + pinMode(self.dio, GPIO.OUTPUT) + sleep(0.00050) + pinMode(self.clk, GPIO.INPUT) + sleep(0.00050) + pinMode(self.dio, GPIO.INPUT) + sleep(0.00050) def _write_comm1(self): self._start() @@ -51,19 +57,19 @@ def _write_comm3(self): 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) + pinMode(self.clk, GPIO.OUTPUT) + sleep(0.00050) + pinMode(self.dio, GPIO.INPUT if b & 1 else GPIO.OUTPUT) + sleep(0.00050) + pinMode(self.clk, GPIO.INPUT) + sleep(0.00050) 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) + pinMode(self.clk, GPIO.OUTPUT) + sleep(0.00050) + pinMode(self.clk, GPIO.INPUT) + sleep(0.00050) + pinMode(self.clk, GPIO.OUTPUT) + sleep(0.00050) def brightness(self, val=None): """Set the display brightness 0-7.""" diff --git a/tm1637_test.py b/tm1637_test.py index c2bc959..8294571 100644 --- a/tm1637_test.py +++ b/tm1637_test.py @@ -1,14 +1,13 @@ -# MicroPython TM1637 quad 7-segment LED display driver examples +#!/usr/bin/env python3 -# WeMos D1 Mini -- 4 Digit Display -# D1 (GPIO5) ----- CLK -# D2 (GPIO4) ----- DIO -# 3V3 ------------ VCC -# G -------------- GND +# Raspberry Pi Python 3 TM1637 quad 7-segment LED display driver examples import tm1637 -from machine import Pin -tm = tm1637.TM1637(clk=Pin(5), dio=Pin(4)) + +CLK=1 +DIO=0 + +tm = tm1637.TM1637(clk=CLK, dio=DIO) # all LEDS on "88:88" tm.write([127, 255, 127, 127]) @@ -150,4 +149,4 @@ tm.numbers(12,59) # show temperature -tm.temperature(24) '24*C' \ No newline at end of file +tm.temperature(24) '24*C'