def __init__(self, path, log_sensors): self.lcd = CharLCDPlate() self.lcd.begin(16, 1) self.port = None self.sensor_list = [] destination_file = path + ( datetime.now().strftime('%d%b-%H_%M_%S')) + ".csv" self.log_csv = open(destination_file, "w", 128) self.log_csv.write("Time,RPM,MPH,Throttle-Position,Calculated-Load," "Coolant-Temp,Air-Temp,Intake-Manifold-Pressure," "Air-Flow-Rate,Engine-Time,MPG\n") for sensor in log_sensors: self.add_log_sensor(sensor) self.config_path = "/home/pi/PiMyRide/config.ini" self.Config = ConfigParser.ConfigParser() if not os.path.isfile(self.config_path): open(self.config_path, "w") self.Config.add_section("Settings") self.Config.set("Settings", "logging_enabled", True) self.Config.set("Settings", "mpg_enabled", True) self.Config.set("Settings", "current_screen", 0) self.Config.write(self.config_path) self.config_path.close() self.Config.read(self.config_path) self.logging_enabled = self.Config.getboolean("Settings", "logging_enabled") self.mpg_enabled = self.Config.getboolean("Settings", "mpg_enabled") self.current_screen = self.Config.get("Settings", "current_screen") self.display_enabled = True self.display_in_use = False
def __init__(self, path, log_sensors): self.port = None self.lcd = CharLCDPlate() self.lcd.begin(16, 1) self.sensor_list = [] destination_file = path + ( datetime.now().strftime('%d%b-%H:%M:%S')) + ".csv" self.log_csv = open(destination_file, "w", 128) self.log_csv.write( "Time,RPM,MPH,Throttle-Position,Calculated-Load," "Coolant-Temp,Air-Temp,Intake-Manifold-Pressure,Air-Flow-Rate,MPG\n" ) for sensor in log_sensors: self.add_log_sensor(sensor)
def __init__(self, path, log_sensors): self.port = None self.lcd = CharLCDPlate() self.lcd.begin(16, 1) #self.lcd.backlight(lcd.ON) self.sensorlist = [] dest_file = path + (datetime.now().strftime('%d%b-%H:%M:%S')) + ".csv" self.log_csv = open(dest_file, "w", 128) self.log_csv.write( "Time,RPM,MPH,Throttle-Position,Calculated-Load,Coolant-Temp,Air-Temp,Intake-Manifold-Pressure,Air-Flow-Rate,MPG\n"); for sensor in log_sensors: self.add_log_sensor(sensor)
class PiMyRide_Logger(): """THE class""" def __init__(self, path, log_sensors): self.lcd = CharLCDPlate() self.lcd.begin(16, 1) self.port = None self.sensor_list = [] destination_file = path + ( datetime.now().strftime('%d%b-%H_%M_%S')) + ".csv" self.log_csv = open(destination_file, "w", 128) self.log_csv.write("Time,RPM,MPH,Throttle-Position,Calculated-Load," "Coolant-Temp,Air-Temp,Intake-Manifold-Pressure," "Air-Flow-Rate,Engine-Time,MPG\n") for sensor in log_sensors: self.add_log_sensor(sensor) self.config_path = "/home/pi/PiMyRide/config.ini" self.Config = ConfigParser.ConfigParser() if not os.path.isfile(self.config_path): open(self.config_path, "w") self.Config.add_section("Settings") self.Config.set("Settings", "logging_enabled", True) self.Config.set("Settings", "mpg_enabled", True) self.Config.set("Settings", "current_screen", 0) self.Config.write(self.config_path) self.config_path.close() self.Config.read(self.config_path) self.logging_enabled = self.Config.getboolean("Settings", "logging_enabled") self.mpg_enabled = self.Config.getboolean("Settings", "mpg_enabled") self.current_screen = self.Config.get("Settings", "current_screen") self.display_enabled = True self.display_in_use = False def exit(self, shutdown): self.Config.set("Settings", "current_screen", self.current_screen) self.Config.set("Settings", "logging_enabled", self.logging_enabled) self.Config.set("Settings", "mpg_enabled", self.mpg_enabled) self.Config.write(self.config_path) print("Configuration saved.") self.lcd.clear() self.lcd.noDisplay() if shutdown: os.command("sudo shutdown now") sys.exit() elif not shutdown: sys.exit() def display_alert(self, message, alert_duration): """Take control of the LCD screen for displaying an alert""" self.display_in_use = True self.lcd.clear() self.lcd.home() self.lcd.message(message) sleep(alert_duration) self.display_in_use = False def lcd_update(self, message, is_alert): """ Update the LCD screen. Allows for rapid updating of the screen, while not stopping the program if an alert needs to be displayed. Sensor data will not be shown while an alert is being displayed """ if self.display_enabled and not self.display_in_use: if not is_alert: # Rapidly update sensor data readout self.lcd.clear() self.lcd.home() self.lcd.message(message) elif is_alert: # Display alert using a new thread as to not # intturpt the sensor logging. alert_duration = 1.5 thread.start_new_thread(self.display_alert, ( message, alert_duration)) def connection_error(self, reason): print("Connection error, disconnecting") if not self.display_enabled: self.lcd.display() self.lcd_update("Connection error\n" + reason, False) sleep(2) while 1: self.lcd_update("OK to reconnect\nDOWN to shutdown", False) if self.lcd.buttonPressed(self.lcd.SELECT): print("Select buttpn pressed, \ attempting to recconect...") self.lcd_update("Reconnecting...", False) self.start() elif self.lcd.buttonPressed(self.lcd.DOWN): print("Down buttpn pressed, shutting down...") self.lcd_update("Shutting Down...", False) sleep(1) self.lcd.noDisplay() self.exit(True) elif self.lcd.buttonPressed(self.lcd.DOWN): print("Up buttpn pressed, exiting...") self.lcd_update("Exiting\nProgram...", False) sleep(1) self.lcd.clear() self.lcd.noDisplay() self.exit(False) sleep(0.10) def connect(self): """ Check all serial ports, print availible ports, close on no open ports. """ port_names = scanSerial() print(port_names) for port in port_names: self.port = obd_io.OBDPort(port, None, 2, 2) if self.port.State == 0: self.port.close() self.port = None # No open ports close else: break # Break with connection if self.port: print("Connected") self.lcd_update("Connected", False) else: print("Not Connected") self.connection_error("Not connected") def is_connected(self): return self.port def add_log_sensor(self, sensor): """ Add the sensors to read from from the list. These sensors are in obd_sensors.py. """ for index, e in enumerate(obd_sensors.SENSORS): if sensor == e.shortname: self.sensor_list.append(index) print("Logging Sensor: " + e.name) break def get_mpg(self, MPH, MAF): """Calculate MPG for petrol enignes, not for diesel""" Instant_MPG = (14.7 * 7.273744 * 4.54 * MPH) / (3600 * MAF / 100) return Instant_MPG def record_data(self): """Actually start recording the data and doing stuff with it""" if self.port is None: return None data_screens = [ ["rpm", "speed"], ["throttle_pos", "load"], ["temp", "intake_air_temp"], ["manifold_pressure", "maf"], ["engine_time", "timing_advance"], ] sensor_names = { "rpm": "RPM", "speed": "Speed", "throttle_pos": "Throttle", "load": "Load", "temp": "Temp", "intake_air_temp": "Intake Temp", "manifold_pressure": "Pressure", "maf": "MAF", "engine_time": "Time", "timing_advance": "Timing Advance", "fuel_status": "Fuel Status", "fuel_rate": "Fuel Rate", "fuel_pressure": "Fuel Pressure" } print("Logging started") self.lcd_update("Logging started", True) while 1: if self.display_enabled: if self.lcd.buttonPressed(self.lcd.RIGHT): # Go to next screen; setting is persistent total_screens = len(self.data_screens) - 1 if self.current_screen < total_screens: self.current_screen = self.current_screen + 1 elif self.current_screen == total_screens: self.current_screen = 0 assert self.current_screen <= total_screens if self.lcd.buttonPressed(self.lcd.LEFT): # Go to previous screen; setting is persistent total_screens = len(self.data_screens) - 1 if self.current_screen > 0: self.current_screen = self.current_screen - 1 elif self.current_screen == 0: self.current_screen = total_screens if self.lcd.buttonPressed(self.lcd.UP): # Enable/disable calculating MPG; persistent if self.mpg_enabled: self.mpg_enabled = False print("MPG disabled") self.lcd_update("MPG disabled", True) elif not self.mpg_enabled: self.mpg_enabled = True print("MPG enabled") self.lcd_update("MPG enabled", True) if self.lcd.buttonPressed(self.lcd.DOWN): # Enable/disable logging; persistent if self.logging_enabled: self.logging_enabled = False print("Logging disabled") self.lcd_update("Logging disabled", True) elif not self.logging_enabled: self.logging_enabled = True print("Logging enabled") self.lcd_update("Logging enabled", True) if self.lcd.buttonPressed(self.lcd.SELECT): # Turn off/on LCD; not persistent if self.display_enabled: self.display_enabled = False self.lcd.clear() self.lcd.home() self.lcd.noDisplay() print("Display disabled") elif not self.display_enabled: self.display_enabled = True self.lcd.display() print("Display enabled") self.lcd_update("Display enabled", True) log_time = datetime.now().strftime('%d%b-%H:%M:%S.%f') log_data = log_time # Start of the logging string result_set = {} # Log all of sensor data from sensor_list for index in self.sensor_list: (name, value, unit) = self.port.sensor(index) # print(self.port.sensor(index)) # Print the data log_data = log_data + "," + str(value) result_set[obd_sensors.SENSORS[index].shortname] = value # Exit the program on "NODATA"/dropped connection if (result_set["rpm"] == "NODATA") or ( result_set["speed"] == "NODATA") or ( result_set["temp"] == "NODATA"): self.connection_error("NODATA") break if self.mpg_enabled: Instant_MPG = self.get_mpg(result_set["speed"], result_set["maf"]) # Add mpg to log string log_data = log_data + "," + str(Instant_MPG) if self.logging_enabled: self.log_csv.write(log_data + "\n") if self.display_enabled: # Show data on lcd msg_line_1 = sensor_names[data_screens[self.current_screen][0]] +\ " " + str(round(result_set[data_screens[self.current_screen][0]], 2)) msg_line_2 = sensor_names[data_screens[self.current_screen][1]] +\ " " + str(round(result_set[data_screens[self.current_screen][1]], 2)) lcd_message = str(msg_line_1) + "\n" + str(msg_line_2) self.lcd_update(lcd_message, False) def start(self): self.connect() if not self.is_connected(): self.connection_error("Not connected") self.record_data()
class PiMyRide_Logger(): def __init__(self, path, log_sensors): self.port = None self.lcd = CharLCDPlate() self.lcd.begin(16, 1) self.sensor_list = [] destination_file = path + ( datetime.now().strftime('%d%b-%H:%M:%S')) + ".csv" self.log_csv = open(destination_file, "w", 128) self.log_csv.write( "Time,RPM,MPH,Throttle-Position,Calculated-Load," "Coolant-Temp,Air-Temp,Intake-Manifold-Pressure,Air-Flow-Rate,MPG\n" ) for sensor in log_sensors: self.add_log_sensor(sensor) def connect(self): # Check all serial ports. port_names = scanSerial() # print available ports print port_names for port in port_names: self.port = obd_io.OBDPort(port, None, 2, 2) if self.port.State == 0: self.port.close() self.port = None # no open ports close else: break # break with connection if self.port: self.lcd.clear() self.lcd.message("Connected") print "Connected" def is_connected(self): # check if connected return self.port # add the sensors to read from from the list below. this sensors are in obd_sensors.py def add_log_sensor(self, sensor): for index, e in enumerate(obd_sensors.SENSORS): if sensor == e.shortname: self.sensor_list.append(index) print "Logging Sensor: " + e.name # logging this sensor break def get_mpg(self, MPH, MAF): # Instant_MPG = (14.7 * 8.637571 * 4.54 * MPH) / (3600 * (MAF * 7.5599) / 100) #Diesel Inaccurate formula Instant_MPG = (14.7 * 7.273744 * 4.54 * MPH) / ( 3600 * MAF / 100) # Petrol Should accurate return Instant_MPG def record_data(self): if self.port is None: return None self.lcd.clear() self.lcd.message("Logging started") print "Logging started" while 1: log_time = datetime.now().strftime( '%d%b-%H:%M:%S.%f') # today's date and time log_data = log_time # start of the logging string result_set = {} for index in self.sensor_list: # log all of our sensors data from sensor_list (name, value, unit) = self.port.sensor(index) print self.port.sensor( index) # print the data provides feedback to user log_data = log_data + "," + str(value) # add to log string result_set[ obd_sensors.SENSORS[index]. shortname] = value # add data to a result set for more manipulation # we don't want to log "NODATA" if the car drops the OBDII connection rather exit the program if (result_set["rpm"] == "NODATA") or (result_set["speed"] == "NODATA") or ( result_set["throttle_pos"] == "NODATA") or (result_set["load"] == "NODATA") or ( result_set["temp"] == "NODATA" ) or (result_set["intake_air_temp"] == "NODATA") or ( result_set["manifold_pressure"] == "NODATA") or (result_set["maf"] == "NODATA"): self.lcd.clear() self.lcd.message("Connection Error\n Disconnecting") sleep(3) # show the message self.lcd.message(" Disconnected") sys.exit() # exit the program Instant_MPG = self.get_mpg(result_set["speed"], result_set["maf"]) # calculate mpg self.lcd.clear() self.lcd.message('MPG ' + '%.2f' % Instant_MPG + '\n') self.lcd.message('RPM ' + str(result_set["rpm"])) log_data = log_data + "," + str( Instant_MPG) # add mpg to result string self.log_csv.write(log_data + "\n") # write to csv
log_data = log_data + "," + str( Instant_MPG) # add mpg to result string self.log_csv.write(log_data + "\n") # write to csv def ensure_dir( f ): # Make a new directory for each day will make managing the logs easier d = os.path.dirname(f) if not os.path.exists( d): # check if directory exists if it does not create it os.makedirs(d) dir_date = datetime.now() path = datetime.now().strftime('%d-%b-%Y') ensure_dir("/home/pi/logs/" + path + "/") # ensure the dir is available log_sensors = [ "rpm", "speed", "throttle_pos", "load", "temp", "intake_air_temp", "manifold_pressure", "maf" ] logger = PiMyRide_Logger("/home/pi/logs/" + path + "/", log_sensors) logger.connect() if not logger.is_connected(): error = CharLCDPlate() error.begin(16, 1) error.message(" Not connected") logger.record_data()
import socket import fcntl import struct from CharLCDPlate import CharLCDPlate SIOCGIFADDR = 0x8915 def get_ip_addr(iface): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(), SIOCGIFADDR, struct.pack('256s', iface[:15]))[20:24]) if __name__ == "__main__": lcd = CharLCDPlate() ip = get_ip_addr("wlan0") lcd.message("IP-Addresse:\n%s" % ip)
class PiMyRide_Logger(): def __init__(self, path, log_sensors): self.port = None self.lcd = CharLCDPlate() self.lcd.begin(16, 1) #self.lcd.backlight(lcd.ON) self.sensorlist = [] dest_file = path + (datetime.now().strftime('%d%b-%H:%M:%S')) + ".csv" self.log_csv = open(dest_file, "w", 128) self.log_csv.write( "Time,RPM,MPH,Throttle-Position,Calculated-Load,Coolant-Temp,Air-Temp,Intake-Manifold-Pressure,Air-Flow-Rate,MPG\n"); for sensor in log_sensors: self.add_log_sensor(sensor) def connect(self):# Check all serial ports. portnames = scanSerial()# print available ports print portnames for port in portnames: self.port = obd_io.OBDPort(port, None, 2, 2) if (self.port.State == 0): self.port.close() self.port = None # no open ports close else: break # break with connection if (self.port): self.lcd.clear() self.lcd.message("Connected") print "Connected" def is_connected(self): # check if connected return self.port def add_log_sensor(self, sensor): # add the sensors to read from from the list below. this sensors are in obd_sensors.py for index, e in enumerate(obd_sensors.SENSORS): if (sensor == e.shortname): self.sensorlist.append(index) print "Logging Sensor: " + e.name # logging this sensor break def get_mpg(self, MPH, MAF): # Instant_MPG = (14.7 * 8.637571 * 4.54 * MPH) / (3600 * (MAF * 7.5599) / 100) #Diesel Inaccurate formula Instant_MPG = (14.7 * 7.273744 * 4.54 * MPH) / (3600 * MAF / 100) # Petrol Should be highly accurate return Instant_MPG def record_data(self): if (self.port is None): return None self.lcd.clear() self.lcd.message("Logging started") print "Logging started" while 1: log_time = datetime.now().strftime('%d%b-%H:%M:%S.%f') # todays date and time log_data = log_time # start of the logging string result_set = {} for index in self.sensorlist:# log all of our sensors data from sensorlist (name, value, unit) = self.port.sensor(index) print self.port.sensor(index) # print the data provides feedback to user log_data = log_data + "," + str(value) # add to log string result_set[ obd_sensors.SENSORS[index].shortname] = value; # add data to a result set for more manipulation # we dont want to log "NODATA" if the car drops the OBDII connetion rather exit the program if (result_set["rpm"] == "NODATA") or (result_set["speed"] == "NODATA") or ( result_set["throttle_pos"] == "NODATA") or (result_set["load"] == "NODATA") or ( result_set["temp"] == "NODATA") or (result_set["intake_air_temp"] == "NODATA") or ( result_set["manifold_pressure"] == "NODATA") or (result_set["maf"] == "NODATA"): self.lcd.clear() self.lcd.message("Connection Error\n Disconnecting") sleep(3) # show the message self.lcd.message(" Disconnected") sys.exit() # exit the program Instant_MPG = self.get_mpg(result_set["speed"], result_set["maf"]) # calculate mpg self.lcd.clear() self.lcd.message('MPG ' + '%.2f' % Instant_MPG + '\n') self.lcd.message('RPM ' + str(result_set["rpm"])) log_data = log_data + "," + str(Instant_MPG)# add mpg to result string self.log_csv.write(log_data + "\n") # write to csv
self.lcd.clear() self.lcd.message('MPG ' + '%.2f' % Instant_MPG + '\n') self.lcd.message('RPM ' + str(result_set["rpm"])) log_data = log_data + "," + str(Instant_MPG)# add mpg to result string self.log_csv.write(log_data + "\n") # write to csv def ensure_dir(f): # Make a new directory for each day will make managing the logs easier d = os.path.dirname(f) if not os.path.exists(d): # check if directory exists if it does not create it os.makedirs(d) dir_date = datetime.now() path = datetime.now().strftime('%d-%b-%Y') ensure_dir("/home/pi/logs/" + path + "/") # ensure the dir is available log_sensors = ["rpm", "speed", "throttle_pos", "load", "temp", "intake_air_temp", "manifold_pressure", "maf"] logger = PiMyRide_Logger("/home/pi/logs/" + path + "/", log_sensors) logger.connect() if not logger.is_connected(): error = CharLCDPlate() error.begin(16, 1) error.message(" Not connected") logger.record_data()
@staticmethod def format_time(seconds): return "%d:%02d" % (floor(seconds / 60), seconds % 60) @staticmethod def parse_invoice_barcode(barcode): if len(barcode) != 13: return barcode invoice = barcode[1:-1] return invoice.strip("0") if __name__ == "__main__": lcd = CharLCDPlate() lcd.begin(16, 2) def on_exit(tracker): def signal_handler(signum, frame): print("Caught signal %d" % signum) tracker.add_event((tracker.EVENT_QUIT, signum)) return signal_handler try: settings = Settings() tracker = PackingTimeTracker(settings, lcd) import signal signal.signal(signal.SIGTERM, on_exit(tracker))
#!/usr/bin/python # coding=utf-8 from CharLCDPlate import NumberSelector, CharLCDPlate from gaia.webservice.Webservice import Webservice from gaia.webservice.Settings import Settings from time import sleep from barcode_scanner import BarcodeScanner # Configuration settings = Settings() lcd = CharLCDPlate() sel = NumberSelector(lcd) scanner = BarcodeScanner.find_scanner() # Create webservice and set last token from configuration ws = Webservice(settings.get("url")) ws.token = settings.get("token") ws.username = settings.get("username") ws.password = settings.get("password") def packer_selected_callback(ws): def callback(sel, number): result = ws.call("Packaging", "getPackerInfo", {"packerId": number}) print(result) lcd.clear() if result["_success"]: lcd.message("Hallo,\n%s!" % result["name"].encode("ascii", "replace"))
class PiMyRide_Logger(): """THE class""" def __init__(self, path, log_sensors): self.lcd = CharLCDPlate() self.lcd.begin(16, 1) self.port = None self.sensor_list = [] destination_file = path + ( datetime.now().strftime('%d%b-%H_%M_%S')) + ".csv" self.log_csv = open(destination_file, "w", 128) self.log_csv.write("Time,RPM,MPH,Throttle-Position,Calculated-Load," "Coolant-Temp,Air-Temp,Intake-Manifold-Pressure," "Air-Flow-Rate,Engine-Time,MPG\n") for sensor in log_sensors: self.add_log_sensor(sensor) self.config_path = "/home/pi/PiMyRide/config.ini" self.Config = ConfigParser.ConfigParser() if not os.path.isfile(self.config_path): open(self.config_path, "w") self.Config.add_section("Settings") self.Config.set("Settings", "logging_enabled", True) self.Config.set("Settings", "mpg_enabled", True) self.Config.set("Settings", "current_screen", 0) self.Config.write(self.config_path) self.config_path.close() self.Config.read(self.config_path) self.logging_enabled = self.Config.getboolean("Settings", "logging_enabled") self.mpg_enabled = self.Config.getboolean("Settings", "mpg_enabled") self.current_screen = self.Config.get("Settings", "current_screen") self.display_enabled = True self.display_in_use = False def exit(self, shutdown): self.Config.set("Settings", "current_screen", self.current_screen) self.Config.set("Settings", "logging_enabled", self.logging_enabled) self.Config.set("Settings", "mpg_enabled", self.mpg_enabled) self.Config.write(self.config_path) print("Configuration saved.") self.lcd.clear() self.lcd.noDisplay() if shutdown: os.command("sudo shutdown now") sys.exit() elif not shutdown: sys.exit() def display_alert(self, message, alert_duration): """Take control of the LCD screen for displaying an alert""" self.display_in_use = True self.lcd.clear() self.lcd.home() self.lcd.message(message) sleep(alert_duration) self.display_in_use = False def lcd_update(self, message, is_alert): """ Update the LCD screen. Allows for rapid updating of the screen, while not stopping the program if an alert needs to be displayed. Sensor data will not be shown while an alert is being displayed """ if self.display_enabled and not self.display_in_use: if not is_alert: # Rapidly update sensor data readout self.lcd.clear() self.lcd.home() self.lcd.message(message) elif is_alert: # Display alert using a new thread as to not # intturpt the sensor logging. alert_duration = 1.5 thread.start_new_thread(self.display_alert, (message, alert_duration)) def connection_error(self, reason): print("Connection error, disconnecting") if not self.display_enabled: self.lcd.display() self.lcd_update("Connection error\n" + reason, False) sleep(2) while 1: self.lcd_update("OK to reconnect\nDOWN to shutdown", False) if self.lcd.buttonPressed(self.lcd.SELECT): print("Select buttpn pressed, \ attempting to recconect...") self.lcd_update("Reconnecting...", False) self.start() elif self.lcd.buttonPressed(self.lcd.DOWN): print("Down buttpn pressed, shutting down...") self.lcd_update("Shutting Down...", False) sleep(1) self.lcd.noDisplay() self.exit(True) elif self.lcd.buttonPressed(self.lcd.DOWN): print("Up buttpn pressed, exiting...") self.lcd_update("Exiting\nProgram...", False) sleep(1) self.lcd.clear() self.lcd.noDisplay() self.exit(False) sleep(0.10) def connect(self): """ Check all serial ports, print availible ports, close on no open ports. """ port_names = scanSerial() print(port_names) for port in port_names: self.port = obd_io.OBDPort(port, None, 2, 2) if self.port.State == 0: self.port.close() self.port = None # No open ports close else: break # Break with connection if self.port: print("Connected") self.lcd_update("Connected", False) else: print("Not Connected") self.connection_error("Not connected") def is_connected(self): return self.port def add_log_sensor(self, sensor): """ Add the sensors to read from from the list. These sensors are in obd_sensors.py. """ for index, e in enumerate(obd_sensors.SENSORS): if sensor == e.shortname: self.sensor_list.append(index) print("Logging Sensor: " + e.name) break def get_mpg(self, MPH, MAF): """Calculate MPG for petrol enignes, not for diesel""" Instant_MPG = (14.7 * 7.273744 * 4.54 * MPH) / (3600 * MAF / 100) return Instant_MPG def record_data(self): """Actually start recording the data and doing stuff with it""" if self.port is None: return None data_screens = [ ["rpm", "speed"], ["throttle_pos", "load"], ["temp", "intake_air_temp"], ["manifold_pressure", "maf"], ["engine_time", "timing_advance"], ] sensor_names = { "rpm": "RPM", "speed": "Speed", "throttle_pos": "Throttle", "load": "Load", "temp": "Temp", "intake_air_temp": "Intake Temp", "manifold_pressure": "Pressure", "maf": "MAF", "engine_time": "Time", "timing_advance": "Timing Advance", "fuel_status": "Fuel Status", "fuel_rate": "Fuel Rate", "fuel_pressure": "Fuel Pressure" } print("Logging started") self.lcd_update("Logging started", True) while 1: if self.display_enabled: if self.lcd.buttonPressed(self.lcd.RIGHT): # Go to next screen; setting is persistent total_screens = len(self.data_screens) - 1 if self.current_screen < total_screens: self.current_screen = self.current_screen + 1 elif self.current_screen == total_screens: self.current_screen = 0 assert self.current_screen <= total_screens if self.lcd.buttonPressed(self.lcd.LEFT): # Go to previous screen; setting is persistent total_screens = len(self.data_screens) - 1 if self.current_screen > 0: self.current_screen = self.current_screen - 1 elif self.current_screen == 0: self.current_screen = total_screens if self.lcd.buttonPressed(self.lcd.UP): # Enable/disable calculating MPG; persistent if self.mpg_enabled: self.mpg_enabled = False print("MPG disabled") self.lcd_update("MPG disabled", True) elif not self.mpg_enabled: self.mpg_enabled = True print("MPG enabled") self.lcd_update("MPG enabled", True) if self.lcd.buttonPressed(self.lcd.DOWN): # Enable/disable logging; persistent if self.logging_enabled: self.logging_enabled = False print("Logging disabled") self.lcd_update("Logging disabled", True) elif not self.logging_enabled: self.logging_enabled = True print("Logging enabled") self.lcd_update("Logging enabled", True) if self.lcd.buttonPressed(self.lcd.SELECT): # Turn off/on LCD; not persistent if self.display_enabled: self.display_enabled = False self.lcd.clear() self.lcd.home() self.lcd.noDisplay() print("Display disabled") elif not self.display_enabled: self.display_enabled = True self.lcd.display() print("Display enabled") self.lcd_update("Display enabled", True) log_time = datetime.now().strftime('%d%b-%H:%M:%S.%f') log_data = log_time # Start of the logging string result_set = {} # Log all of sensor data from sensor_list for index in self.sensor_list: (name, value, unit) = self.port.sensor(index) # print(self.port.sensor(index)) # Print the data log_data = log_data + "," + str(value) result_set[obd_sensors.SENSORS[index].shortname] = value # Exit the program on "NODATA"/dropped connection if (result_set["rpm"] == "NODATA") or (result_set["speed"] == "NODATA") or (result_set["temp"] == "NODATA"): self.connection_error("NODATA") break if self.mpg_enabled: Instant_MPG = self.get_mpg(result_set["speed"], result_set["maf"]) # Add mpg to log string log_data = log_data + "," + str(Instant_MPG) if self.logging_enabled: self.log_csv.write(log_data + "\n") if self.display_enabled: # Show data on lcd msg_line_1 = sensor_names[data_screens[self.current_screen][0]] +\ " " + str(round(result_set[data_screens[self.current_screen][0]], 2)) msg_line_2 = sensor_names[data_screens[self.current_screen][1]] +\ " " + str(round(result_set[data_screens[self.current_screen][1]], 2)) lcd_message = str(msg_line_1) + "\n" + str(msg_line_2) self.lcd_update(lcd_message, False) def start(self): self.connect() if not self.is_connected(): self.connection_error("Not connected") self.record_data()