def _verify_device_state(expected_state): """INTERNAL. Verify that that current device state matches that expected""" current_state = _read_device_state() if expected_state == current_state: return True else: PTLogger.warning( "Error: Device write verification failed. Expected: " + _get_bit_string(expected_state) + " Received: " + _get_bit_string(current_state)) return False
def _get_timeout_from_file(self): if path.exists(self.CONFIG_FILE): try: file = open(self.CONFIG_FILE, 'r+') fileContents = file.read().strip() file.close() self._idle_timeout_s = int(fileContents) PTLogger.info("Idletime retrieved from config: " + str(self._idle_timeout_s)) except: PTLogger.warning( "Idletime could not be retrieved from config. Using default: " + str(self._idle_timeout_s))
def _read_device_state(): """INTERNAL. Read from the I2C bus to get the current state of the pulse. Caller should handle exceptions""" try: PTLogger.debug("Connecting to bus...") i2c_bus = SMBus(_bus_id) current_state = i2c_bus.read_byte(_device_addr) & 0x0F return int(current_state) except: PTLogger.warning("Error: There was a problem reading from the device") # Best to re-raise as we can't recover from this raise
def wait_for_device_identification(self): PTLogger.debug("Waiting for device id to be established...") time_waited = 0 while (time_waited < 5): device_id = self.get_device_id() if (device_id == DeviceID.unknown): sleep(0.25) time_waited += 0.25 else: PTLogger.debug("Got device id (" + str(device_id) + "). Waited " + str(time_waited) + " seconds") return PTLogger.warning("Timed out waiting for device identification.")
def get_connected_device_addresses(): addresses_arr = [] # Switch on I2C if it's not enabled if I2C.get_state() is False: PTLogger.warning( "I2C is not initialised - attempting to initialise") I2C.set_state(True) # Error if still false if I2C.get_state() is False: PTLogger.error( "Unable to initialise I2C - unable to get connected device addresses" ) else: addresses_arr = _SystemCalls.get_connected_i2c_device_addresses() return addresses_arr
def _update_device_state_bit(bit, value): """INTERNAL. Set a particular device state bit to enable or disable a particular function""" # Bits: 0x0000 # Index: 3210 if bit not in [0, 1, 2, 3]: PTLogger.warning("Error: Not a valid state bit") return False try: current_state = _read_device_state() PTLogger.debug("Current device state: " + _get_bit_string(current_state)) except: PTLogger.warning( "Error: There was a problem getting the current device state") return False # Get the bit mask for the new state new_state = _get_addr_for_bit(bit) if value == 0: new_state = ~new_state # Check if there is anything to do if (value == 1 and (new_state & current_state) != 0) or ( value == 0 and (~new_state & ~current_state) != 0): PTLogger.debug("Warning: Mode already set, nothing to send") return True if value == 0: new_state = new_state & current_state else: new_state = new_state | current_state # Combine the old with the new and send return _write_device_state(new_state)
def connect_to_hub(self): # Attempt to connect to a v2 hub first. This is because we can # positively identify v2 on i2c. We can also positively identify # a v1 pi-top, however we cannot do this for a CEED. Hence this # is the fall-through case. PTLogger.info("Attempting to find pi-topHUB v2...") try: self._module_hub_v2 = self._import_module("pthub2.pthub2") if (self._module_hub_v2.initialise() is True): self._active_hub_module = self._module_hub_v2 PTLogger.info("Connected to pi-topHUB v2") self._register_client() return True else: PTLogger.warning("Could not initialise v2 hub") except Exception as e: PTLogger.warning("Failed to connect to a v2 hub. " + str(e)) PTLogger.info(traceback.format_exc()) PTLogger.info("Attempting to find pi-topHUB v1...") try: self._module_hub_v1 = self._import_module("pthub.pthub") if (self._module_hub_v1.initialise() is True): self._active_hub_module = self._module_hub_v1 PTLogger.info("Connected to pi-topHUB v1") self._register_client() return True else: PTLogger.warning("Could not initialise v1 hub") except Exception as e: PTLogger.warning("Failed to connect to a v1 hub. " + str(e)) PTLogger.info(traceback.format_exc()) PTLogger.error("Could not connect to a hub!") return False
def boot_config_correctly_configured(expected_clock_val=None, expected_baud_val=None, expected_enabled_val=None): if expected_clock_val is not None: if isinstance(expected_clock_val, int): clock_string = _BootConfig.get_value( property_name="init_uart_clock", value_is_number=True) clock_val_okay = (clock_string == str(expected_clock_val)) else: PTLogger.warning( "Invalid init_uart_clock value to check for in /boot/config.txt - must be an integer" ) else: clock_val_okay = True if expected_baud_val is not None: if isinstance(expected_baud_val, int): baud_string = _BootConfig.get_value( property_name="init_uart_baud", value_is_number=True) baud_val_okay = (baud_string == str(expected_baud_val)) else: PTLogger.warning( "Invalid init_uart_baud value to check for in /boot/config.txt - must be an integer" ) else: baud_val_okay = True if expected_enabled_val is not None: if expected_enabled_val == 1 or expected_enabled_val == 0: enabled_string = _BootConfig.get_value( property_name="enable_uart", value_is_number=True) enabled_val_okay = ( enabled_string == str(expected_enabled_val)) else: PTLogger.warning( "Invalid enable_uart value to check for in /boot/config.txt - must be 0 or 1" ) else: enabled_val_okay = True return (clock_val_okay and baud_val_okay and enabled_val_okay)
def configure_in_boot_config(init_uart_clock=None, init_uart_baud=None, enable_uart=None): if isinstance(init_uart_clock, int): _BootConfig.set_value("init_uart_clock", init_uart_clock) else: PTLogger.warning( "Unable to set init_uart_clock in /boot/config.txt to non-integer value" ) if isinstance(init_uart_baud, int): _BootConfig.set_value("init_uart_baud", init_uart_baud) else: PTLogger.warning( "Unable to set init_uart_baud in /boot/config.txt to non-integer value" ) if enable_uart == 1 or enable_uart == 0: _BootConfig.set_value("enable_uart", enable_uart) else: PTLogger.warning( "Unable to set enable_uart in /boot/config.txt to value other than 0 or 1" )
def start(self): if (self._hub_connected()): self._active_hub_module.start() else: PTLogger.warning( "Attempted to call start when there was no active hub")
def _stop_i2c(): if pthub_i2c._run_main_thread: pthub_i2c.stop() else: PTLogger.warning("Unable to stop I2C - not currently running")
def _stop_spi(): if pthub_spi._run_main_thread: pthub_spi.stop() else: PTLogger.warning("Unable to stop SPI - not currently running")
def _start_i2c(): if pthub_i2c.is_initialised(): pthub_i2c.start() else: PTLogger.warning("I2C is not available")
def disable_hdmi_to_i2s_audio(): PTLogger.warning("V1 hub called to disable HDMI to I2S audio - this hub does not support this")