diff --git a/config.py b/config.py index c0ee048cce3410946cea8ccfe7007c5d3c0e0623..f92ef7e6481f4abeda78b5b81a7e4d06b3257a09 100644 --- a/config.py +++ b/config.py @@ -15,7 +15,8 @@ OHMPI_CONFIG = { 'version': 2, 'max_elec': 64, 'board_address': {'A': 0x70, 'B': 0x71, 'M': 0x72, 'N': 0x73}, # def. {'A': 0x76, 'B': 0x71, 'M': 0x74, 'N': 0x70} -} + 'settings': 'ohmpi_settings.json' +} # TODO: add a dictionary with INA models and associated gain values CONTROL_CONFIG = { 'tcp_port': 5555, diff --git a/ohmpi.py b/ohmpi.py index 3db2c402f749d3dfe93a9c77749942c31c7b989e..6aa95ac827ef4e07971768f23906a0f6390c29b6 100644 --- a/ohmpi.py +++ b/ohmpi.py @@ -20,7 +20,7 @@ from datetime import datetime from termcolor import colored import threading from logging_setup import setup_loggers -from config import CONTROL_CONFIG +from config import CONTROL_CONFIG, OHMPI_CONFIG from subprocess import Popen # finish import (done only when class is instantiated as some libs are only available on arm64 platform) @@ -118,6 +118,7 @@ class OhmPi(object): self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=0x49) # Starts the command processing thread + self.cmd_listen = True self.cmd_thread = threading.Thread(target=self.process_commands) self.cmd_thread.start() @@ -650,7 +651,7 @@ class OhmPi(object): print(colored(f'Listening to commands on tcp port {tcp_port}.' f' Make sure your client interface is running and bound to this port...', 'blue')) self.exec_logger.debug(f'Start listening for commands on port {tcp_port}') - while True: + while self.cmd_listen: message = socket.recv() self.exec_logger.debug(f'Received command: {message}') e = None @@ -780,6 +781,15 @@ class OhmPi(object): if self.thread is not None: self.thread.join() self.exec_logger.debug(f'Status: {self.status}') + + def quit(self): + """Quit OhmPi. + """ + self.cmd_listen = False + if self.cmd_thread is not None: + self.cmd_thread.join() + self.exec_logger.debug(f'Stopped listening to tcp port.') + exit() @@ -812,7 +822,7 @@ if __name__ == "__main__": # start interface #Popen(['python', CONTROL_CONFIG['interface']]) - ohmpi = OhmPi(settings='ohmpi_settings.json') + ohmpi = OhmPi(settings=OHMPI_CONFIG['settings'])