def __input_monitor(self): """ Thread payload, that listens to state change of the input pin. :return: None """ if self._state == 0: new_state = 1 else: new_state = 0 while self._mode == 0: if live_mode_is_on(): new_state = wiringpi.digitalRead(self._pin) if self._state != new_state: self.log( LogLevel.INFO, "Input state change detected. Was {0}, now {1}".format( self._state, new_state)) self._state = new_state if new_state == 0: self._input_control = Thread(target=self._low_callback[0], args=self._low_callback[1]) elif new_state == 1: self._input_control = Thread(target=self._high_callback[0], args=self._high_callback[1]) self._input_control.setDaemon(True) self._input_control.start() sleep(0.05)
def __init__(self): super().__init__(logger_name=__class__.__name__, logging_level=GENERAL["logging_level"]) self.log(LogLevel.INFO, "Initializing GPIO Controller") if live_mode_is_on(): import wiringpi wiringpi.wiringPiSetup() self._pins_config = None
def set_output(self, state): """ Sets the OUTPUT pin value to either LOW (0) or HIGH (1). Works ONLY in OUTPUT mode :param state: State ID (1 or 0) :return: None """ if not self._locked: if self._mode == 1: self.log(LogLevel.INFO, "Setting output to {0}".format(state)) if live_mode_is_on(): wiringpi.digitalWrite(self._pin, state) self._state = state else: self.log( LogLevel.WARNING, "Set Output is only available for PIN working in OUTPUT mode" ) else: self.__lock_access_warning() return self
def __init__(self, pin, mode): """ :param pin: wPi pin number :param mode: operational mode (ranges from 0 to 6, see wiringpi modes) """ super().__init__(logger_name="GPIO-PIN-{0}".format(pin), logging_level=GENERAL["logging_level"]) self._pin = pin self._mode = mode self.log( LogLevel.INFO, "Initializing {0} GPIO Pin controller with {1} mode".format( pin, mode)) if live_mode_is_on(): wiringpi.pinMode(pin, mode) self._state = wiringpi.digitalRead(pin) else: self._state = 0 self.set_low_callback(default_callback, self) self.set_high_callback(default_callback, self) self._reset_callbacks = False self._locked = False
def update_values(self): """ Reads current system values (or randomizes the values, if client is in demo mode) and updates the value conts. :return: None """ if live_mode_is_on(): try: # Orange PI devices running armbian have the temperature available from "iio_hwmon" kernel library. self.cpu_temperature = psutil.sensors_temperatures( )["iio_hwmon"][0].current except KeyError: # If that key is not present - we assume that temp sensors are not available. self.cpu_temperature = 0 self.cpu_load_percentage = psutil.cpu_percent(interval=5) memory_readings = psutil.virtual_memory() self.total_ram = round(memory_readings.total / 1000) self.ram_used = round(memory_readings.used / 1000) self.used_ram_percentage = memory_readings.percent else: # If we're in demo mode, we simulate the board by invoking pseudo-random values generation. # To provide values consistency, we check if it is 0, and if not - we increment/decrement the previous value self.cpu_temperature = randint(35, 57) \ if self.cpu_temperature == 0 \ else self.__get_altered_value(self.cpu_temperature, 35, 57, randint(1, 4)) self.cpu_load_percentage = randint(5, 30) \ if self.cpu_load_percentage == 0 \ else self.__get_altered_value(self.cpu_load_percentage, 5, 30, randint(1, 3)) self.total_ram = 512000 self.ram_used = randint(56000, 92000) \ if self.ram_used == 0 \ else self.__get_altered_value(self.ram_used, 56000, 92000, randint(64, 4068)) self.used_ram_percentage = round( (self.ram_used / (self.total_ram / 100)), 2)
def set_mode(self, mode): """ Switches/sets pin mode to selected value. See main controller for Pin modes :param mode: Pin mode ID :return: None """ if not self._locked: self._mode = mode if live_mode_is_on(): wiringpi.pinMode(self._pin, mode) if mode == 0: self.__start_input_monitor() self.lock_pin() else: if self._reset_callbacks: self.perform_callbacks_reset() self.log(LogLevel.INFO, "Mode set to {0}".format(mode)) else: self.log( LogLevel.WARNING, "Attempt to change mode of a locked pin. Unlock it first") return self
import types from threading import Thread from time import sleep import logging from client.config import GENERAL from client.gpio.utils import live_mode_is_on from common.class_base import ClassBase from common.logger import LogLevel if live_mode_is_on(): import wiringpi def default_callback(pin_instance): """ Default callback, used for both HIGH and LOW input states if no other callbacks specified :return: None """ logger = logging.getLogger("GPIO_PIN_{0}".format(pin_instance.get_pin())) logger.setLevel(logging.DEBUG) logger.debug("Default callback triggered") class Pin(ClassBase): """ Operational interface for GPIO pins, represents a given PIN on the plate. Uses simple lockable API to prevent same pin being used by multiple objects/threads at the same time """ def __init__(self, pin, mode):