def run(self): global sensors #self.flask_input = open('read_pipe','w') #self.flask_output = open('write_pipe','r') self.mmap_file = open('mmap_ipc','a+b') self.mmap_buf = mmap.mmap(self.mmap_file.fileno(),0,access=mmap.ACCESS_WRITE) self.mmap_altitude_file = open('/home/pi/.sensorian/mmap_altitude','a+b') self.mmap_altitude = mmap.mmap(self.mmap_altitude_file.fileno(),0,access=mmap.ACCESS_WRITE) self.mmap_pressure_file = open('/home/pi/.sensorian/mmap_pressure','a+b') self.mmap_pressure = mmap.mmap(self.mmap_pressure_file.fileno(),0,access=mmap.ACCESS_WRITE) self.mmap_temperature_file = open('/home/pi/.sensorian/mmap_temperature','a+b') self.mmap_temperature = mmap.mmap(self.mmap_temperature_file.fileno(),0,access=mmap.ACCESS_WRITE) self.mmap_ambientlight_file = open('/home/pi/.sensorian/mmap_ambientlight','a+b') self.mmap_ambientlight = mmap.mmap(self.mmap_ambientlight_file.fileno(),0,access=mmap.ACCESS_WRITE) SensorsInterface.setupSensorian() #Setup a dummy alarm for now SensorsInterface.setRTCCtime(2016,3,4,5,33,0) SensorsInterface.setRTCCalarm(2018,1,1,1,40,20,1) #MINUTES_MATCH mode prev_touchpad = SensorsInterface.getTouchpad() #prev_magnetometer = SensorsInterface.getMagnetometer() prev_magnetometer = mk_bin_tuple(SensorsInterface.getMagnetometer()) prev_accelerometer = mk_bin_tuple(SensorsInterface.getAccelerometer()) prev_alarmtrigger = SensorsInterface.pollRTCCalarm() while self.alive: sensors_lock.acquire() sensors = {} touchpad = SensorsInterface.getTouchpad() if touchpad != prev_touchpad: print "Touchpad: " + str(touchpad) #sensors_lock.acquire() sensors['touchpad'] = touchpad #sensors_lock.release() prev_touchpad = touchpad #magnetometer = SensorsInterface.getMagnetometer() magnetometer = mk_bin_tuple(SensorsInterface.getMagnetometer()) if magnetometer != prev_magnetometer: print "Magnetometer: " + str(magnetometer) #sensors_lock.acquire() sensors['magnetometer'] = magnetometer #sensors_lock.release() prev_magnetometer = magnetometer accelerometer = mk_bin_tuple(SensorsInterface.getAccelerometer()) if accelerometer != prev_accelerometer: print "Accelerometer: " + str(accelerometer) sensors['accelerometer'] = accelerometer prev_accelerometer = accelerometer alarmtrigger = SensorsInterface.pollRTCCalarm() if alarmtrigger != prev_alarmtrigger: print "Alarm Trigger: " + str(alarmtrigger) sensors['alarmtrigger'] = alarmtrigger prev_alarmtrigger = alarmtrigger #Poll flask_rtcc_interface.py for new updates to RTCC #print "polling flask_rtcc_interface.py" #self.flask_input.write("reqUpdate\n") #self.flask_input.flush() if self.mmap_buf[0] == '1': #Update the time new_time_tuple = get_time_from_mmap(self.mmap_buf,2) SensorsInterface.setRTCCtime(new_time_tuple[0],new_time_tuple[1],new_time_tuple[2],new_time_tuple[3],new_time_tuple[4],new_time_tuple[5]) print "done setting the time" self.mmap_buf[0] = '0' #Clear NEW_TIME if self.mmap_buf[1] == '1': #Update the alarm new_alarm_tuple = get_alarm_from_mmap(self.mmap_buf,16) SensorsInterface.setRTCCalarm(new_alarm_tuple[0],new_alarm_tuple[1],new_alarm_tuple[2],new_alarm_tuple[3],new_alarm_tuple[4],new_alarm_tuple[5],new_alarm_tuple[6]) print "done setting the alarm" self.mmap_buf[1] = '0' #Clear NEW_ALARM #Altitude if self.mmap_altitude[0] == '1': #Send altitude data... but first set the BUSY byte self.mmap_altitude[1] = '1' #Get the Altitude data print "Getting altitude data" sensors['altitude'] = str(SensorsInterface.getAltitude()) self.mmap_altitude[0] = '0' #The request has been completed self.mmap_altitude[1] = '0' #...and we are no longer busy #Pressure if self.mmap_pressure[0] == '1': #Send pressure data... but first set the BUSY byte self.mmap_pressure[1] = '1' #Get the Pressure data print "Getting pressure data" sensors['pressure'] = str(SensorsInterface.getBarometricPressure()) self.mmap_pressure[0] = '0' #The request has been completed self.mmap_pressure[1] = '0' #...and we are no longer busy #Temperature if self.mmap_temperature[0] == '1': #Send temperature data... but first set the BUSY byte self.mmap_temperature[1] = '1' #Get the Temperature data print "Getting temperature data" sensors['temperature'] = str(SensorsInterface.getTemperature()) self.mmap_temperature[0] = '0' #The request has been completed self.mmap_temperature[1] = '0' #...and we are no longer busy #AmbientLight if self.mmap_ambientlight[0] == '1': #Send temperature data... but first set the BUSY byte self.mmap_ambientlight[1] = '1' #Get the Ambient Light data print "Getting ambient light data" sensors['ambientlight'] = str(SensorsInterface.getAmbientLight()) self.mmap_ambientlight[0] = '0' #The request has been completed self.mmap_ambientlight[1] = '0' #...and we are no longer busy sensors_lock.release() """If there is new data stored in sensors --> sensors != {}, then we should make this thread wait until it is sent to the socket. """ if sensors != {}: data_ready_event.clear() data_ready_event.wait()
def run(self): global sensors #self.flask_input = open('read_pipe','w') #self.flask_output = open('write_pipe','r') self.mmap_file = open('mmap_ipc', 'a+b') self.mmap_buf = mmap.mmap(self.mmap_file.fileno(), 0, access=mmap.ACCESS_WRITE) self.mmap_altitude_file = open('/home/pi/.sensorian/mmap_altitude', 'a+b') self.mmap_altitude = mmap.mmap(self.mmap_altitude_file.fileno(), 0, access=mmap.ACCESS_WRITE) self.mmap_pressure_file = open('/home/pi/.sensorian/mmap_pressure', 'a+b') self.mmap_pressure = mmap.mmap(self.mmap_pressure_file.fileno(), 0, access=mmap.ACCESS_WRITE) self.mmap_temperature_file = open( '/home/pi/.sensorian/mmap_temperature', 'a+b') self.mmap_temperature = mmap.mmap(self.mmap_temperature_file.fileno(), 0, access=mmap.ACCESS_WRITE) self.mmap_ambientlight_file = open( '/home/pi/.sensorian/mmap_ambientlight', 'a+b') self.mmap_ambientlight = mmap.mmap( self.mmap_ambientlight_file.fileno(), 0, access=mmap.ACCESS_WRITE) SensorsInterface.setupSensorian() #Setup a dummy alarm for now SensorsInterface.setRTCCtime(2016, 3, 4, 5, 33, 0) SensorsInterface.setRTCCalarm(2018, 1, 1, 1, 40, 20, 1) #MINUTES_MATCH mode prev_touchpad = SensorsInterface.getTouchpad() #prev_magnetometer = SensorsInterface.getMagnetometer() prev_magnetometer = mk_bin_tuple(SensorsInterface.getMagnetometer()) prev_accelerometer = mk_bin_tuple(SensorsInterface.getAccelerometer()) prev_alarmtrigger = SensorsInterface.pollRTCCalarm() while self.alive: sensors_lock.acquire() sensors = {} touchpad = SensorsInterface.getTouchpad() if touchpad != prev_touchpad: print "Touchpad: " + str(touchpad) #sensors_lock.acquire() sensors['touchpad'] = touchpad #sensors_lock.release() prev_touchpad = touchpad #magnetometer = SensorsInterface.getMagnetometer() magnetometer = mk_bin_tuple(SensorsInterface.getMagnetometer()) if magnetometer != prev_magnetometer: print "Magnetometer: " + str(magnetometer) #sensors_lock.acquire() sensors['magnetometer'] = magnetometer #sensors_lock.release() prev_magnetometer = magnetometer accelerometer = mk_bin_tuple(SensorsInterface.getAccelerometer()) if accelerometer != prev_accelerometer: print "Accelerometer: " + str(accelerometer) sensors['accelerometer'] = accelerometer prev_accelerometer = accelerometer alarmtrigger = SensorsInterface.pollRTCCalarm() if alarmtrigger != prev_alarmtrigger: print "Alarm Trigger: " + str(alarmtrigger) sensors['alarmtrigger'] = alarmtrigger prev_alarmtrigger = alarmtrigger #Poll flask_rtcc_interface.py for new updates to RTCC #print "polling flask_rtcc_interface.py" #self.flask_input.write("reqUpdate\n") #self.flask_input.flush() if self.mmap_buf[0] == '1': #Update the time new_time_tuple = get_time_from_mmap(self.mmap_buf, 2) SensorsInterface.setRTCCtime( new_time_tuple[0], new_time_tuple[1], new_time_tuple[2], new_time_tuple[3], new_time_tuple[4], new_time_tuple[5]) print "done setting the time" self.mmap_buf[0] = '0' #Clear NEW_TIME if self.mmap_buf[1] == '1': #Update the alarm new_alarm_tuple = get_alarm_from_mmap(self.mmap_buf, 16) SensorsInterface.setRTCCalarm( new_alarm_tuple[0], new_alarm_tuple[1], new_alarm_tuple[2], new_alarm_tuple[3], new_alarm_tuple[4], new_alarm_tuple[5], new_alarm_tuple[6]) print "done setting the alarm" self.mmap_buf[1] = '0' #Clear NEW_ALARM #Altitude if self.mmap_altitude[0] == '1': #Send altitude data... but first set the BUSY byte self.mmap_altitude[1] = '1' #Get the Altitude data print "Getting altitude data" sensors['altitude'] = str(SensorsInterface.getAltitude()) self.mmap_altitude[0] = '0' #The request has been completed self.mmap_altitude[1] = '0' #...and we are no longer busy #Pressure if self.mmap_pressure[0] == '1': #Send pressure data... but first set the BUSY byte self.mmap_pressure[1] = '1' #Get the Pressure data print "Getting pressure data" sensors['pressure'] = str( SensorsInterface.getBarometricPressure()) self.mmap_pressure[0] = '0' #The request has been completed self.mmap_pressure[1] = '0' #...and we are no longer busy #Temperature if self.mmap_temperature[0] == '1': #Send temperature data... but first set the BUSY byte self.mmap_temperature[1] = '1' #Get the Temperature data print "Getting temperature data" sensors['temperature'] = str(SensorsInterface.getTemperature()) self.mmap_temperature[0] = '0' #The request has been completed self.mmap_temperature[1] = '0' #...and we are no longer busy #AmbientLight if self.mmap_ambientlight[0] == '1': #Send temperature data... but first set the BUSY byte self.mmap_ambientlight[1] = '1' #Get the Ambient Light data print "Getting ambient light data" sensors['ambientlight'] = str( SensorsInterface.getAmbientLight()) self.mmap_ambientlight[ 0] = '0' #The request has been completed self.mmap_ambientlight[1] = '0' #...and we are no longer busy sensors_lock.release() """If there is new data stored in sensors --> sensors != {}, then we should make this thread wait until it is sent to the socket. """ if sensors != {}: data_ready_event.clear() data_ready_event.wait()
tft_printer.screen_print_rotated("This is a longer string to demonstrate the wrapping and text background", 1, colour=(0, 0, 0, 0), background=(255, 255, 255)) time.sleep(2) # Wait for 2 seconds before continuing. sensor_string = "" # Prepare an empty string to be printed to the screen sensor_string += " Light: " + str(SensorsInterface.getAmbientLight()) # Add the light value to the string sensor_string += " Temp: " + str(SensorsInterface.getTemperature()) # Add the temperature to the string sensor_string += " Alt: " + str(SensorsInterface.getAltitude()) # Add the altitude to the string sensor_string += " Press: " + str(SensorsInterface.getBarometricPressure()) # Add the pressure to the string sensor_string += " Button: " + str(SensorsInterface.getTouchpad()) # Add the last button press to the string sensor_string += " Accel: " + str(SensorsInterface.getAccelerometer()) # Add the accelerometer value to the string sensor_string += " Magnet: " + str(SensorsInterface.getMagnetometer()) # Add the magnetometer value to the string sensor_string += " Time: " + str(SensorsInterface.getRTCCtime()) # Add the date and time to the string tft_printer.screen_print_rotated(sensor_string, 1) # Print the completed string to the landscape display time.sleep(2) # Wait for 2 seconds before proceeding tools_string = "" # Prepare an empty string to be printed to the screen tools_string += " Serial: " + PiTools.get_cpu_serial() # Add the RPi's CPU Serial number to the string
def run(self): self.alive = True while self.alive: self.sensors_lock.acquire() ######################### Touchpad #################### cap_button = SensorsInterface.getTouchpad() try: if cap_button != self.sensors[0]["da"]: # self.sensors[0]["da"] = cap_button # self.sensors[0]["dr"] = True self.sensors[0] = {"da": cap_button, "dr": True} print "Updating touchpad" except: # self.sensors[0]["da"] = cap_button # self.sensors[0]["dr"] = True self.sensors[0] = {"da": cap_button, "dr": True} print "Updating touchpad" #################### Ambient Light ##################### ambient_light = SensorsInterface.getAmbientLight() try: if ambient_light != self.sensors[1]["da"]: self.sensors[1] = {"da": ambient_light, "dr": True} except: self.sensors[1] = {"da": ambient_light, "dr": True} ##################### Barometric Pressure ################ pressure = SensorsInterface.getBarometricPressure() try: if pressure != self.sensors[2]["da"]: self.sensors[2] = {"da": pressure, "dr": True} except: self.sensors[2] = {"da": pressure, "dr": True} ##################### Altitude ############################ altitude = SensorsInterface.getAltitude() try: if altitude != self.sensors[3]["da"]: self.sensors[3] = {"da": altitude, "dr": True} except: self.sensors[3] = {"da": altitude, "dr": True} ##################### Temperature ######################### temperature = SensorsInterface.getTemperature() try: if temperature != self.sensors[4]["da"]: self.sensors[4] = {"da": temperature, "dr": True} except: self.sensors[4] = {"da": temperature, "dr": True} ##################### Accelerometer ####################### accel = SensorsInterface.getAccelerometer() # -------- X component --------- accel_x = accel[0] try: if accel_x != self.sensors[5]["da"]: self.sensors[5] = {"da": accel_x, "dr": True} except: self.sensors[5] = {"da": accel_x, "dr": True} # ---------- Y component -------- accel_y = accel[1] try: if accel_y != self.sensors[6]["da"]: self.sensors[6] = {"da": accel_y, "dr": True} except: self.sensors[6] = {"da": accel_y, "dr": True} # ---------- Z component ---------- accel_z = accel[2] try: if accel_z != self.sensors[7]["da"]: self.sensors[7] = {"da": accel_z, "dr": True} except: self.sensors[7] = {"da": accel_z, "dr": True} ##################### Magnetometer ####################### magneto = SensorsInterface.getMagnetometer() # -------- X component --------- magneto_x = magneto[0] try: if magneto_x != self.sensors[8]["da"]: self.sensors[8] = {"da": magneto_x, "dr": True} except: self.sensors[8] = {"da": magneto_x, "dr": True} # ---------- Y component -------- magneto_y = magneto[1] try: if magneto_y != self.sensors[9]["da"]: self.sensors[9] = {"da": magneto_y, "dr": True} except: self.sensors[9] = {"da": magneto_y, "dr": True} # ---------- Z component ---------- magneto_z = magneto[2] try: if magneto_z != self.sensors[10]["da"]: self.sensors[10] = {"da": magneto_z, "dr": True} except: self.sensors[10] = {"da": magneto_z, "dr": True} ###################### RTCC ############################## rtcc = SensorsInterface.getRTCCtime() # ----------- year rtcc_year = rtcc[0] try: if rtcc_year != self.sensors[11]["da"]: self.sensors[11] = {"da": rtcc_year, "dr": True} except: self.sensors[11] = {"da": rtcc_year, "dr": True} # ---------- month rtcc_month = rtcc[1] try: if rtcc_month != self.sensors[12]["da"]: self.sensors[12] = {"da": rtcc_month, "dr": True} except: self.sensors[12] = {"da": rtcc_month, "dr": True} # ---------- date rtcc_date = rtcc[2] try: if rtcc_date != self.sensors[13]["da"]: self.sensors[13] = {"da": rtcc_date, "dr": True} except: self.sensors[13] = {"da": rtcc_date, "dr": True} # ---------- hour rtcc_hour = rtcc[3] try: if rtcc_hour != self.sensors[14]["da"]: self.sensors[14] = {"da": rtcc_hour, "dr": True} except: self.sensors[14] = {"da": rtcc_hour, "dr": True} # ---------- minute rtcc_minute = rtcc[4] try: if rtcc_minute != self.sensors[15]["da"]: self.sensors[15] = {"da": rtcc_minute, "dr": True} except: self.sensors[15] = {"da": rtcc_minute, "dr": True} # ---------- second rtcc_second = rtcc[5] try: if rtcc_second != self.sensors[16]["da"]: self.sensors[16] = {"da": rtcc_second, "dr": True} except: self.sensors[16] = {"da": rtcc_second, "dr": True} # ------------- alarm trigger alarm_trigger = SensorsInterface.pollRTCCalarm() try: if alarm_trigger != self.sensors[17]["da"]: self.sensors[17] = {"da": alarm_trigger, "dr": True} except: self.sensors[17] = {"da": alarm_trigger, "dr": True} self.sensors_lock.release() for s in self.exported_sensors: if self.sensors[s]["dr"] == True: # Wait here until data has been sent to socket self.has_data_event.clear() self.has_data_event.wait() break
sensor_string = "" # Prepare an empty string to be printed to the screen sensor_string += " Light: " + str( SensorsInterface.getAmbientLight()) # Add the light value to the string sensor_string += " Temp: " + str( SensorsInterface.getTemperature()) # Add the temperature to the string sensor_string += " Alt: " + str( SensorsInterface.getAltitude()) # Add the altitude to the string sensor_string += " Press: " + str( SensorsInterface.getBarometricPressure()) # Add the pressure to the string sensor_string += " Button: " + str( SensorsInterface.getTouchpad()) # Add the last button press to the string sensor_string += " Accel: " + str(SensorsInterface.getAccelerometer( )) # Add the accelerometer value to the string sensor_string += " Magnet: " + str(SensorsInterface.getMagnetometer( )) # Add the magnetometer value to the string sensor_string += " Time: " + str( SensorsInterface.getRTCCtime()) # Add the date and time to the string tft_printer.screen_print_rotated( sensor_string, 1) # Print the completed string to the landscape display time.sleep(2) # Wait for 2 seconds before proceeding
def run(self): self.alive = True while self.alive: self.sensors_lock.acquire() ######################### Touchpad #################### cap_button = SensorsInterface.getTouchpad() try: if cap_button != self.sensors[0]["da"]: #self.sensors[0]["da"] = cap_button #self.sensors[0]["dr"] = True self.sensors[0] = {"da": cap_button, "dr": True} print "Updating touchpad" except: #self.sensors[0]["da"] = cap_button #self.sensors[0]["dr"] = True self.sensors[0] = {"da": cap_button, "dr": True} print "Updating touchpad" #################### Ambient Light ##################### ambient_light = SensorsInterface.getAmbientLight() try: if ambient_light != self.sensors[1]["da"]: self.sensors[1] = {"da": ambient_light, "dr": True} except: self.sensors[1] = {"da": ambient_light, "dr": True} ##################### Barometric Pressure ################ pressure = SensorsInterface.getBarometricPressure() try: if pressure != self.sensors[2]["da"]: self.sensors[2] = {"da": pressure, "dr": True} except: self.sensors[2] = {"da": pressure, "dr": True} ##################### Altitude ############################ altitude = SensorsInterface.getAltitude() try: if altitude != self.sensors[3]["da"]: self.sensors[3] = {"da": altitude, "dr": True} except: self.sensors[3] = {"da": altitude, "dr": True} ##################### Temperature ######################### temperature = SensorsInterface.getTemperature() try: if temperature != self.sensors[4]["da"]: self.sensors[4] = {"da": temperature, "dr": True} except: self.sensors[4] = {"da": temperature, "dr": True} ##################### Accelerometer ####################### accel = SensorsInterface.getAccelerometer() #-------- X component --------- accel_x = accel[0] try: if accel_x != self.sensors[5]["da"]: self.sensors[5] = {"da": accel_x, "dr": True} except: self.sensors[5] = {"da": accel_x, "dr": True} #---------- Y component -------- accel_y = accel[1] try: if accel_y != self.sensors[6]["da"]: self.sensors[6] = {"da": accel_y, "dr": True} except: self.sensors[6] = {"da": accel_y, "dr": True} #---------- Z component ---------- accel_z = accel[2] try: if accel_z != self.sensors[7]["da"]: self.sensors[7] = {"da": accel_z, "dr": True} except: self.sensors[7] = {"da": accel_z, "dr": True} ##################### Magnetometer ####################### magneto = SensorsInterface.getMagnetometer() #-------- X component --------- magneto_x = magneto[0] try: if magneto_x != self.sensors[8]["da"]: self.sensors[8] = {"da": magneto_x, "dr": True} except: self.sensors[8] = {"da": magneto_x, "dr": True} #---------- Y component -------- magneto_y = magneto[1] try: if magneto_y != self.sensors[9]["da"]: self.sensors[9] = {"da": magneto_y, "dr": True} except: self.sensors[9] = {"da": magneto_y, "dr": True} #---------- Z component ---------- magneto_z = magneto[2] try: if magneto_z != self.sensors[10]["da"]: self.sensors[10] = {"da": magneto_z, "dr": True} except: self.sensors[10] = {"da": magneto_z, "dr": True} ###################### RTCC ############################## rtcc = SensorsInterface.getRTCCtime() #----------- year rtcc_year = rtcc[0] try: if rtcc_year != self.sensors[11]["da"]: self.sensors[11] = {"da": rtcc_year, "dr": True} except: self.sensors[11] = {"da": rtcc_year, "dr": True} #---------- month rtcc_month = rtcc[1] try: if rtcc_month != self.sensors[12]["da"]: self.sensors[12] = {"da": rtcc_month, "dr": True} except: self.sensors[12] = {"da": rtcc_month, "dr": True} #---------- date rtcc_date = rtcc[2] try: if rtcc_date != self.sensors[13]["da"]: self.sensors[13] = {"da": rtcc_date, "dr": True} except: self.sensors[13] = {"da": rtcc_date, "dr": True} #---------- hour rtcc_hour = rtcc[3] try: if rtcc_hour != self.sensors[14]["da"]: self.sensors[14] = {"da": rtcc_hour, "dr": True} except: self.sensors[14] = {"da": rtcc_hour, "dr": True} #---------- minute rtcc_minute = rtcc[4] try: if rtcc_minute != self.sensors[15]["da"]: self.sensors[15] = {"da": rtcc_minute, "dr": True} except: self.sensors[15] = {"da": rtcc_minute, "dr": True} #---------- second rtcc_second = rtcc[5] try: if rtcc_second != self.sensors[16]["da"]: self.sensors[16] = {"da": rtcc_second, "dr": True} except: self.sensors[16] = {"da": rtcc_second, "dr": True} #------------- alarm trigger alarm_trigger = SensorsInterface.pollRTCCalarm() try: if alarm_trigger != self.sensors[17]["da"]: self.sensors[17] = {"da": alarm_trigger, "dr": True} except: self.sensors[17] = {"da": alarm_trigger, "dr": True} self.sensors_lock.release() for s in self.exported_sensors: if self.sensors[s]["dr"] == True: #Wait here until data has been sent to socket self.has_data_event.clear() self.has_data_event.wait() break