From d29015560feafdb91746173de224a5fe692f7b6b Mon Sep 17 00:00:00 2001 From: Russ Handorf Date: Thu, 13 Jan 2022 21:52:33 -0500 Subject: [PATCH] more backups --- python/lcd-clear.py | 13 ++++++ python/lcd-test.py | 45 ++++++++++++++++++ python/py-gaugette | 1 + python/rotor-test.py | 29 ++++++++++++ python/rotor-test2.py | 35 ++++++++++++++ python/rotor-test3.py | 76 ++++++++++++++++++++++++++++++ python/sw.py | 11 +++++ python/unit-test.py | 106 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 316 insertions(+) create mode 100644 python/lcd-clear.py create mode 100644 python/lcd-test.py create mode 160000 python/py-gaugette create mode 100644 python/rotor-test.py create mode 100644 python/rotor-test2.py create mode 100644 python/rotor-test3.py create mode 100644 python/sw.py create mode 100644 python/unit-test.py diff --git a/python/lcd-clear.py b/python/lcd-clear.py new file mode 100644 index 0000000..2914f25 --- /dev/null +++ b/python/lcd-clear.py @@ -0,0 +1,13 @@ +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 + +lcd = CharLCD(cols=40, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD) +lcd.clear() +lcd.clear() +lcd.close() diff --git a/python/lcd-test.py b/python/lcd-test.py new file mode 100644 index 0000000..9dcad18 --- /dev/null +++ b/python/lcd-test.py @@ -0,0 +1,45 @@ +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 + +lcd = CharLCD(cols=20, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], numbering_mode=GPIO.BOARD) +lcd.clear() + +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]) + +if __name__ == '__main__': + signal(SIGINT, handler) + while True: + for i in range(0,6): + 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")) + time.sleep(1) + lcd.clear() + lcd.write_string("IP Address:") + lcd.cursor_pos = (1, 0) + lcd.write_string(get_ip_address('eth0')) + time.sleep(5) + +#lcd.clear() +#lcd.write_string(u'Hello world!') +#lcd.crlf() +#lcd.write_string(u'Hello world!') +lcd.close() diff --git a/python/py-gaugette b/python/py-gaugette new file mode 160000 index 0000000..14732a7 --- /dev/null +++ b/python/py-gaugette @@ -0,0 +1 @@ +Subproject commit 14732a78fc953cfc920f548b04d69d5b6219ba54 diff --git a/python/rotor-test.py b/python/rotor-test.py new file mode 100644 index 0000000..5dcebd7 --- /dev/null +++ b/python/rotor-test.py @@ -0,0 +1,29 @@ +import time +import gaugette.rotary_encoder +import gaugette.gpio +import gaugette.switch + +A_PIN = 5 +B_PIN = 4 +SW_PIN = 2 + +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() + +while True: + delta = encoder.get_cycles() + state = sw.get_state() + if state != last_state: + print "switch %d" % state + last_state = state + + if delta!=0: + print "rotate %d" % delta + else: + time.sleep(0.1) + diff --git a/python/rotor-test2.py b/python/rotor-test2.py new file mode 100644 index 0000000..2fdc836 --- /dev/null +++ b/python/rotor-test2.py @@ -0,0 +1,35 @@ +from RPi import GPIO +from time import sleep + +clk = 23 +dt = 24 +sw = 27 + +GPIO.setmode(GPIO.BCM) +GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) +GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) +GPIO.setup(sw, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) + +counter = 0 +clkLastState = GPIO.input(clk) +swLastState = GPIO.input(sw) + +try: + + while True: + clkState = GPIO.input(clk) + dtState = GPIO.input(dt) + swState = GPIO.input(sw) + if clkState != clkLastState: + if dtState != clkState: + counter += 1 + else: + counter -= 1 + print counter + clkLastState = clkState + if swState != swLastState: + print "Pushed" + swLastState = swState + sleep(0.01) +finally: + GPIO.cleanup() diff --git a/python/rotor-test3.py b/python/rotor-test3.py new file mode 100644 index 0000000..14968b4 --- /dev/null +++ b/python/rotor-test3.py @@ -0,0 +1,76 @@ +# Sample code for both the RotaryEncoder class and the Switch class. +# The common pin for the encoder should be wired to ground. +# The sw_pin should be shorted to ground by the switch. + +# Output looks like this: +# +# A B STATE SEQ DELTA SWITCH +# 1 1 3 2 1 0 +# 0 1 2 3 1 0 +# 0 0 0 0 1 0 +# 1 0 1 1 1 0 +# 1 1 3 2 1 0 +# 0 1 2 3 1 0 + +import gaugette.rotary_encoder +import gaugette.switch +import math + +A_PIN = 5 +B_PIN = 4 +SW_PIN = 2 + +encoder = gaugette.rotary_encoder.RotaryEncoder(A_PIN, B_PIN) +switch = gaugette.switch.Switch(SW_PIN) + +last_state = None +last_switch_state = None +last_delta = 0 +last_sequence = encoder.rotation_sequence() +last_heading = 0 + +# NOTE: the library includes individual calls to get +# the rotation_state, rotation_sequence and delta values. +# However this demo only reads the rotation_state and locally +# derives the rotation_sequence and delta. This ensures that +# the derived values are based on the same two input bits A and B. +# If we used the library calls, there is a very real chance that +# the inputs would change while we were sampling, giving us +# inconsistent values in the output table. + +while True: + + state = encoder.rotation_state() + switch_state = switch.get_state() + + if (state != last_state or switch_state != last_switch_state): + last_switch_state = switch_state + last_state = state + + # print a heading every 20 lines + if last_heading % 20 == 0: + print "A B STATE SEQ DELTA SWITCH" + last_heading += 1 + + # extract individual signal bits for A and B + a_state = state & 0x01 + b_state = (state & 0x02) >> 1 + + # compute sequence number: + # This is the same as the value returned by encoder.rotation_sequence() + sequence = (a_state ^ b_state) | b_state << 1 + + # compute delta: + # This is the same as the value returned by encoder.get_delta() + delta = (sequence - last_sequence) % 4 + if delta == 3: + delta = -1 + elif delta==2: + # this is an attempt to make sense out of a missed step: + # assume that we have moved two steps in the same direction + # that we were previously moving. + delta = int(math.copysign(delta, last_delta)) + last_delta = delta + last_sequence = sequence + + print '%1d %1d %3d %4d %4d %4d' % (a_state, b_state, state, sequence, delta, switch_state) diff --git a/python/sw.py b/python/sw.py new file mode 100644 index 0000000..6c8d9a0 --- /dev/null +++ b/python/sw.py @@ -0,0 +1,11 @@ +import gaugette.gpio +import gaugette.switch +SW_PIN = 13 +gpio = gaugette.gpio.GPIO() +sw = gaugette.switch.Switch(gpio, SW_PIN) +last_state = sw.get_state() +while True: + state = sw.get_state() + if state != last_state: + print "switch %d" % state + last_state = state 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() -- 2.17.1