Last active
September 11, 2023 05:12
-
-
Save angusbarnes/0c705343cae2331d95b8805ba09802b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
#GPIO Mode (BOARD / BCM) | |
GPIO.setmode(GPIO.BCM) | |
# Define the update rate of this program | |
# Avoid large values as this will needlessly cycle and heat | |
# the BCM chip on the board. In theory this value could be as | |
# low as 0.5 - 1 Hz given our usecase, but 50 is used for responsivity | |
CLOCK_RATE_HZ = 50 | |
# Input Pins: We recieve signals here | |
READ_STATE_C = 5 | |
READ_STATE_D = 6 | |
READ_STATE_E = 13 | |
READ_STATE_F = 19 | |
# Ouptut Pins: We send signals here | |
OUT_INPUT_A = 23 | |
OUT_INPUT_B = 24 | |
OUT_CLOCK = 14 | |
#set GPIO direction (IN / OUT) | |
GPIO.setup( | |
[READ_STATE_C, READ_STATE_D, READ_STATE_E, READ_STATE_F], | |
GPIO.IN, pull_up_down=GPIO.PUD_DOWN | |
) | |
GPIO.setup(OUT_INPUT_A, GPIO.OUT) | |
GPIO.setup(OUT_INPUT_B, GPIO.OUT) | |
GPIO.setup(OUT_CLOCK, GPIO.OUT) | |
STATE_LIST = { | |
'0000' : 'HW_INIT', | |
'0001' : 'BOOT_SUCCESS', | |
'0010' : 'NET_CHECK', | |
'0100' : 'MS_OFFLINE', | |
'0101' : 'OFFLINE_EMPTY', | |
'0110' : 'OFFLINE_TAKEN', | |
'1000' : 'MS_ONLINE', | |
'1001' : 'ONLINE_FREE', | |
'1010' : 'ONLINE_TAKEN', | |
'1011' : 'ONLINE_RESERVED', | |
'1111' : 'HW_DISABLED' | |
} | |
FAULT_STATE_UNKOWN = 'FAULT_STATE_UNKNOWN' | |
SIG_NO_OP = (0, 0) | |
SIG_ALPHA = (0, 1) | |
SIG_BETA = (1, 0) | |
SIG_GAMMA = (1, 1) | |
def send_clock(): | |
GPIO.output(OUT_CLOCK, True) | |
time.sleep(0.1) | |
GPIO.output(OUT_CLOCK, False) | |
def send_signal(signal): | |
GPIO.output(OUT_INPUT_A, signal[0]) | |
GPIO.output(OUT_INPUT_B, signal[1]) | |
def read_state(): | |
Qc = GPIO.input(READ_STATE_C) | |
Qd = GPIO.input(READ_STATE_D) | |
Qe = GPIO.input(READ_STATE_E) | |
Qf = GPIO.input(READ_STATE_F) | |
code = f"{Qc}{Qd}{Qe}{Qf}" | |
if code in STATE_LIST: | |
return STATE_LIST[code] | |
return FAULT_STATE_UNKNOWN | |
# This calls a system command to measure CPU temp | |
def get_cpu_temp(): | |
temp_result = subprocess.run(['vcgencmd', 'measure_temp'], stdout = subprocess.PIPE) | |
# Chop out the temperature value from the stdout | |
return temp_result.stdout.decode('utf-8')[5:-3] | |
import subprocess | |
def main_loop(): | |
time_sample = time.time() | |
temperature = 38.0 | |
while txt := input('SIGNAL SELECT :> '): | |
if txt == 'alpha': | |
send_signal(SIG_ALPHA) | |
elif txt == 'beta': | |
send_signal(SIG_BETA) | |
elif txt == 'gamma': | |
send_signal(SIG_GAMMA) | |
else: | |
send_signal(SIG_NO_OP) | |
send_clock() | |
if (t := time.time()) - time_sample > 1: | |
time_sample = t | |
temperature = get_cpu_temp() | |
state = read_state() | |
print(f"STATE CHANGE: {state: < 20} Temp={temperature}C ") | |
time.sleep(0.1) | |
if __name__ == '__main__': | |
try: | |
main_loop() | |
# Reset by pressing CTRL + C | |
except KeyboardInterrupt: | |
print("Measurement stopped by User") | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment