Skip to content

Commit

Permalink
Merge pull request #2 from depklyon/port-to-python
Browse files Browse the repository at this point in the history
Port to python 3
  • Loading branch information
depklyon authored Jul 27, 2017
2 parents 97446a9 + 005200d commit 6d6f307
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 52 deletions.
28 changes: 28 additions & 0 deletions clock.py
Original file line number Diff line number Diff line change
@@ -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)

20 changes: 10 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
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',
],
)
)
72 changes: 39 additions & 33 deletions tm1637.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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."""
Expand Down
17 changes: 8 additions & 9 deletions tm1637_test.py
Original file line number Diff line number Diff line change
@@ -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])
Expand Down Expand Up @@ -150,4 +149,4 @@
tm.numbers(12,59)

# show temperature
tm.temperature(24) '24*C'
tm.temperature(24) '24*C'

0 comments on commit 6d6f307

Please sign in to comment.