# read the values from Arduino:
import serial # import serial library to read the values from Arduino Uno via serial (usb) connection
import os # library to use bash commands
from friction_label import friction_label
from future_relics import future_relics
from print_output import print_output
function_index = {
" FL " : friction_label ,
" FR " : future_relics
}
module_index = {
" FL " : " friction_label " ,
" FR " : " future_relics "
}
if __name__ == ' __main__ ' :
ser = serial . Serial ( ' /dev/ttyS0 ' , 9600 , timeout = 1 ) # establish a serial connection using Serial Pi Zero (S0), using the RX $
ser . reset_input_buffer ( )
while True :
if ser . in_waiting > 0 :
line = ser . readline ( ) # reads bytes
try :
line = line . decode ( " utf-8 " , ' ignore ' ) . rstrip ( ) # converts bytes to string
line = line . replace ( " \r \n " , " " ) # replaces "\r\n" (end of line) with nothing
line = line . replace ( " \r " , " " ) # replaces "\r" (between two modules) with nothing
line = line . split ( " # " ) # splits the line into a list of lists
# after this split the list begins with an empty list, so delete it:
if line [ 0 ] == " " : #if first list in lists is empty:
del line [ 0 ] #delete the first element
# print(line)
modules = [ ]
for module in line :
values = module . split ( " , " ) # splits each module into values
modules . append ( values )
print ( " These are the values of each module inside a list of modules " )
print ( modules )
previous_module = " "
for module in modules : #for each module inside the list of modules
if module [ 0 ] not in module_index : #this if break is added to avoid the script from breaking at the start (which happens when serial is not yet receiving all values, but only fragments)
# print("break")
break # break and start the for loop again
function_index [ module [ 0 ] ] ( previous_module , module ) #use function_index to run functions for each module
previous_module = module_index [ module [ 0 ] ] #use module_index to replace abbr. with full name
if module == modules [ - 1 ] : #if module is the last module in list of modules
final_module = module_index [ module [ 0 ] ] #use name of the last module to define final_module
# print(final_module)
print_output ( final_module ) #call print_output function with last_module as variable
except OSError as e :
print ( " Error: could not decode bytes " )
print ( e )