├── LICENSE ├── README.md └── tm1637.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 John La Rooy 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raspberrypi-tm1637 2 | Proof of concept TM1637 driver for raspberry pi for Python3 3 | 4 | 5 | -------------------------------------------------------------------------------- /tm1637.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import subprocess 4 | from time import time, sleep, localtime 5 | 6 | from wiringpi2 import wiringPiSetupGpio, pinMode, digitalRead, digitalWrite, GPIO 7 | wiringPiSetupGpio() 8 | 9 | CLK = 21 10 | DIO = 20 11 | 12 | """ 13 | A 14 | --- 15 | F | | B 16 | -G- 17 | E | | C 18 | --- 19 | D 20 | 21 | """ 22 | 23 | 24 | class TM1637: 25 | I2C_COMM1 = 0x40 26 | I2C_COMM2 = 0xC0 27 | I2C_COMM3 = 0x80 28 | digit_to_segment = [ 29 | 0b0111111, # 0 30 | 0b0000110, # 1 31 | 0b1011011, # 2 32 | 0b1001111, # 3 33 | 0b1100110, # 4 34 | 0b1101101, # 5 35 | 0b1111101, # 6 36 | 0b0000111, # 7 37 | 0b1111111, # 8 38 | 0b1101111, # 9 39 | 0b1110111, # A 40 | 0b1111100, # b 41 | 0b0111001, # C 42 | 0b1011110, # d 43 | 0b1111001, # E 44 | 0b1110001 # F 45 | ] 46 | 47 | def __init__(self, clk, dio): 48 | self.clk = clk 49 | self.dio = dio 50 | self.brightness = 0x0f 51 | 52 | pinMode(self.clk, GPIO.INPUT) 53 | pinMode(self.dio, GPIO.INPUT) 54 | digitalWrite(self.clk, GPIO.LOW) 55 | digitalWrite(self.dio, GPIO.LOW) 56 | 57 | def bit_delay(self): 58 | sleep(0.001) 59 | return 60 | 61 | def set_segments(self, segments, pos=0): 62 | # Write COMM1 63 | self.start() 64 | self.write_byte(self.I2C_COMM1) 65 | self.stop() 66 | 67 | # Write COMM2 + first digit address 68 | self.start() 69 | self.write_byte(self.I2C_COMM2 + pos) 70 | 71 | for seg in segments: 72 | self.write_byte(seg) 73 | self.stop() 74 | 75 | # Write COMM3 + brightness 76 | self.start() 77 | self.write_byte(self.I2C_COMM3 + self.brightness) 78 | self.stop() 79 | 80 | def start(self): 81 | pinMode(self.dio, GPIO.OUTPUT) 82 | self.bit_delay() 83 | 84 | def stop(self): 85 | pinMode(self.dio, GPIO.OUTPUT) 86 | self.bit_delay() 87 | pinMode(self.clk, GPIO.INPUT) 88 | self.bit_delay() 89 | pinMode(self.dio, GPIO.INPUT) 90 | self.bit_delay() 91 | 92 | def write_byte(self, b): 93 | # 8 Data Bits 94 | for i in range(8): 95 | 96 | # CLK low 97 | pinMode(self.clk, GPIO.OUTPUT) 98 | self.bit_delay() 99 | 100 | pinMode(self.dio, GPIO.INPUT if b & 1 else GPIO.OUTPUT) 101 | 102 | self.bit_delay() 103 | 104 | pinMode(self.clk, GPIO.INPUT) 105 | self.bit_delay() 106 | b >>= 1 107 | 108 | pinMode(self.clk, GPIO.OUTPUT) 109 | self.bit_delay() 110 | pinMode(self.clk, GPIO.INPUT) 111 | self.bit_delay() 112 | pinMode(self.clk, GPIO.OUTPUT) 113 | self.bit_delay() 114 | 115 | return 116 | 117 | 118 | def show_ip_address(tm): 119 | ipaddr = subprocess.check_output("hostname -I", shell=True, timeout=1).strip().split(b".") 120 | for octet in ipaddr: 121 | tm.set_segments([0, 0, 0, 0]) 122 | sleep(0.1) 123 | tm.set_segments([tm.digit_to_segment[int(x) & 0xf] for x in octet]) 124 | sleep(0.9) 125 | 126 | 127 | def show_clock(tm): 128 | t = localtime() 129 | sleep(1 - time() % 1) 130 | d0 = tm.digit_to_segment[t.tm_hour // 10] if t.tm_hour // 10 else 0 131 | d1 = tm.digit_to_segment[t.tm_hour % 10] 132 | d2 = tm.digit_to_segment[t.tm_min // 10] 133 | d3 = tm.digit_to_segment[t.tm_min % 10] 134 | tm.set_segments([d0, 0x80 + d1, d2, d3]) 135 | sleep(.5) 136 | tm.set_segments([d0, d1, d2, d3]) 137 | 138 | 139 | if __name__ == "__main__": 140 | tm = TM1637(CLK, DIO) 141 | 142 | show_ip_address(tm) 143 | 144 | while True: 145 | show_clock(tm) 146 | 147 | 148 | 149 | --------------------------------------------------------------------------------