X-Git-Url: http://russells-world.com/code/?p=rb-clock.git;a=blobdiff_plain;f=python%2Funit-test.py;fp=python%2Funit-test.py;h=bf72cff720c4825c1270ec0a00e61889be8bd7b5;hp=0000000000000000000000000000000000000000;hb=d29015560feafdb91746173de224a5fe692f7b6b;hpb=f2580fcc9470d1ac688ca9b6edd87666784eaf6b diff --git a/python/unit-test.py b/python/unit-test.py new file mode 100644 index 0000000..bf72cff --- /dev/null +++ b/python/unit-test.py @@ -0,0 +1,106 @@ +import RPi.GPIO as GPIO +from RPLCD.gpio import CharLCD +import socket +import fcntl +import struct +import time +from signal import signal, SIGINT +from sys import exit +import gaugette.rotary_encoder +import gaugette.gpio +import gaugette.switch +import os + +A_PIN = 5 +B_PIN = 4 +SW_PIN = 2 + +menusize=3 +menupos=0 +timeout=time.localtime() + +lcd = CharLCD(cols=40, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD) +lcd.clear() + +gpio = gaugette.gpio.GPIO() +encoder = gaugette.rotary_encoder.RotaryEncoder(gpio, A_PIN, B_PIN) +encoder.start() +switch = gaugette.switch.Switch(gpio, SW_PIN) +last_state = None +sw = gaugette.switch.Switch(gpio, SW_PIN) +last_state = sw.get_state() + +def handler(signal_received, frame): + lcd.close() + print('SIGINT or CTRL-C detected. Exiting gracefully') + exit(0) + +def get_ip_address(ifname): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + return socket.inet_ntoa(fcntl.ioctl( + s.fileno(), + 0x8915, + struct.pack('256s', ifname[:15]) + )[20:24]) + +def display_ip(lcd): + lcd.clear() + lcd.write_string("IP Address:") + lcd.cursor_pos = (1, 0) + lcd.write_string(get_ip_address('eth0')) + +def display_time(lcd): + lcd.clear() + lcd.write_string("Time: %s" %time.strftime("%H:%M:%S")) + lcd.cursor_pos = (1, 0) + lcd.write_string("Date: %s" %time.strftime("%m/%d/%Y")) + +def display_reboot(lcd): + lcd.clear() + lcd.write_string("Reboot?") + +def display_blank(lcd): + lcd.clear() + lcd.write_string("Dynamic screen status") + +if __name__ == '__main__': + signal(SIGINT, handler) + while True: + delta = encoder.get_cycles() + state = sw.get_state() + if state != last_state: + print "switch %d" % state + last_state = state + if menupos==1: + lcd.clear() + lcd.write_string("Rebooting!!!") + time.sleep(3) + os.system("sudo reboot") + + if delta!=0: + if delta>0: + menupos+=1 + if delta<0: + menupos-=1 + if menupos<0: + menupos=menusize + if menupos>menusize: + menupos=0 + + if menupos==0: + display_blank(lcd) + if menupos==1: + display_reboot(lcd) + if menupos==2: + display_time(lcd) + if menupos==3: + display_ip(lcd) + #print "rotate %d" % delta + else: + time.sleep(0.1) + if menupos==2: + if timeout!=time.localtime(): + display_time(lcd) + timeout=time.localtime() + +lcd.close()