diff --git a/devtimer.py b/devtimer.py index 6041217..24d6f73 100755 --- a/devtimer.py +++ b/devtimer.py @@ -1,118 +1,20 @@ #!/usr/bin/env python3 +# devtimer.py - Temperature Controlled Photographic Darkroom Timer +# Targeted for RaspberryPi +# Copyright (c) 2018 TundraWare Inc. +# Permission Hereby Granted For Unrestricted Personal Or Commercial Use -import subprocess -from time import time, sleep, localtime -from wiringpi import wiringPiSetupGpio, pinMode, digitalRead, digitalWrite, GPIO +from time import time, sleep +from tm1637 import * +from wiringpi import wiringPiSetupGpio + wiringPiSetupGpio() +BRIGHTNESS = 0x09 CLK = 21 DIO = 20 -""" - A - --- - F | | B - -G- - E | | C - --- - D - -""" - - -class TM1637: - I2C_COMM1 = 0x40 - I2C_COMM2 = 0xC0 - I2C_COMM3 = 0x80 - digit_to_segment = [ - 0b0111111, # 0 - 0b0000110, # 1 - 0b1011011, # 2 - 0b1001111, # 3 - 0b1100110, # 4 - 0b1101101, # 5 - 0b1111101, # 6 - 0b0000111, # 7 - 0b1111111, # 8 - 0b1101111, # 9 - 0b1110111, # A - 0b1111100, # b - 0b0111001, # C - 0b1011110, # d - 0b1111001, # E - 0b1110001 # F - ] - - def __init__(self, clk, dio): - self.clk = clk - self.dio = dio - self.brightness = 0x09 # 0x0f - - pinMode(self.clk, GPIO.INPUT) - pinMode(self.dio, GPIO.INPUT) - digitalWrite(self.clk, GPIO.LOW) - digitalWrite(self.dio, GPIO.LOW) - - def bit_delay(self): - sleep(0.001) - return - - def set_segments(self, segments, pos=0): - # Write COMM1 - self.start() - self.write_byte(self.I2C_COMM1) - self.stop() - - # Write COMM2 + first digit address - self.start() - self.write_byte(self.I2C_COMM2 + pos) - - for seg in segments: - self.write_byte(seg) - self.stop() - - # Write COMM3 + brightness - self.start() - self.write_byte(self.I2C_COMM3 + self.brightness) - self.stop() - - def start(self): - pinMode(self.dio, GPIO.OUTPUT) - self.bit_delay() - - def stop(self): - pinMode(self.dio, GPIO.OUTPUT) - self.bit_delay() - pinMode(self.clk, GPIO.INPUT) - self.bit_delay() - pinMode(self.dio, GPIO.INPUT) - self.bit_delay() - - def write_byte(self, b): - # 8 Data Bits - for i in range(8): - - # CLK low - pinMode(self.clk, GPIO.OUTPUT) - self.bit_delay() - - pinMode(self.dio, GPIO.INPUT if b & 1 else GPIO.OUTPUT) - - self.bit_delay() - - pinMode(self.clk, GPIO.INPUT) - self.bit_delay() - b >>= 1 - - pinMode(self.clk, GPIO.OUTPUT) - self.bit_delay() - pinMode(self.clk, GPIO.INPUT) - self.bit_delay() - pinMode(self.clk, GPIO.OUTPUT) - self.bit_delay() - - return def show_elapsed(tm, elapsed): @@ -124,33 +26,18 @@ d3 = tm.digit_to_segment[sec % 10] tm.set_segments([d0, 0x80 + d1, d2, d3]) + + if __name__ == "__main__": - tm = TM1637(CLK, DIO) + tm = TM1637(CLK, DIO, BRIGHTNESS) start_time = int(time()) show_elapsed(tm, 0) while True: + sleep(.05) show_elapsed(tm, int(time())-start_time) if int(time())-start_time == 5999: start_time = int(time()) - - - -# tm.set_segments([0, 0, 0, 0]) -# value = 0 -# digits = [] -# th = value // 1000 -# hu = (value % 1000) // 100 -# tn = (value % 100) // 10 -# on = value % 10 -# digits = [tm.digit_to_segment[th], tm.digit_to_segment[hu], tm.digit_to_segment[tn], tm.digit_to_segment[on]] -# tm.set_segments(digits) -# sleep(.05) - -# if value == 9999: -# value = 0 -# else: -# value += 1 diff --git a/tm1637.py b/tm1637.py new file mode 100644 index 0000000..e417b5a --- /dev/null +++ b/tm1637.py @@ -0,0 +1,113 @@ +# Tip of the hat to https://github.com/johnlr/raspberrypi-tm1637 for +# this code. I has been very slightly modified, but it mostly his +# work. + +from time import sleep +from wiringpi import pinMode, digitalRead, digitalWrite, GPIO + +""" + A + --- + F | | B + -G- + E | | C + --- + D + +""" + + +class TM1637: + I2C_COMM1 = 0x40 + I2C_COMM2 = 0xC0 + I2C_COMM3 = 0x80 + digit_to_segment = [ + 0b0111111, # 0 + 0b0000110, # 1 + 0b1011011, # 2 + 0b1001111, # 3 + 0b1100110, # 4 + 0b1101101, # 5 + 0b1111101, # 6 + 0b0000111, # 7 + 0b1111111, # 8 + 0b1101111, # 9 + 0b1110111, # A + 0b1111100, # b + 0b0111001, # C + 0b1011110, # d + 0b1111001, # E + 0b1110001 # F + ] + + def __init__(self, clk, dio, brightness): + self.clk = clk + self.dio = dio + self.brightness = brightness + + pinMode(self.clk, GPIO.INPUT) + pinMode(self.dio, GPIO.INPUT) + digitalWrite(self.clk, GPIO.LOW) + digitalWrite(self.dio, GPIO.LOW) + + def bit_delay(self): + sleep(0.001) + return + + def set_segments(self, segments, pos=0): + # Write COMM1 + self.start() + self.write_byte(self.I2C_COMM1) + self.stop() + + # Write COMM2 + first digit address + self.start() + self.write_byte(self.I2C_COMM2 + pos) + + for seg in segments: + self.write_byte(seg) + self.stop() + + # Write COMM3 + brightness + self.start() + self.write_byte(self.I2C_COMM3 + self.brightness) + self.stop() + + def start(self): + pinMode(self.dio, GPIO.OUTPUT) + self.bit_delay() + + def stop(self): + pinMode(self.dio, GPIO.OUTPUT) + self.bit_delay() + pinMode(self.clk, GPIO.INPUT) + self.bit_delay() + pinMode(self.dio, GPIO.INPUT) + self.bit_delay() + + def write_byte(self, b): + # 8 Data Bits + for i in range(8): + + # CLK low + pinMode(self.clk, GPIO.OUTPUT) + self.bit_delay() + + pinMode(self.dio, GPIO.INPUT if b & 1 else GPIO.OUTPUT) + + self.bit_delay() + + pinMode(self.clk, GPIO.INPUT) + self.bit_delay() + b >>= 1 + + pinMode(self.clk, GPIO.OUTPUT) + self.bit_delay() + pinMode(self.clk, GPIO.INPUT) + self.bit_delay() + pinMode(self.clk, GPIO.OUTPUT) + self.bit_delay() + + return + +# End of class 'TM1637'