class Encoder: def __init__(self): self.steps = 0 self.gpioconfig = GPIOConfig() self.gpiopin = GPIOPin self.pin = Pin self.encoder_a = False self.encoder_b = False self.lockRotary = threading.Lock() self.dt = None self.clk = None def __event_detect__(self): if config.Config.getInt("count_steps_simple", "encoder_step") == 0: self.gpioconfig.add_event_detect_both( self.dt, callback=self.__count_steps__) self.gpioconfig.add_event_detect_both( self.clk, callback=self.__count_steps__) def __remove_event_detect__(self): self.gpioconfig.remove_event_detect(self.dt) self.gpioconfig.remove_event_detect(self.clk) def __count_steps__(self, dt_or_clk): self.encoder_a = (self.gpioconfig.status_enc(self.dt)) self.encoder_b = (self.gpioconfig.status_enc(self.clk)) print("encoder_a", self.encoder_a) print("encoder_b", self.encoder_b) #GPIOPin.CLK_W = GPIO.input(18) #GPIOPin.DT_W = GPIO.input(22) #GPIOPin.CLK_E = GPIO.input(12) #GPIOPin.DT_E = GPIO.input(16) clkLast = (self.clk) print(clkLast) counter = 0 if self.encoder_a and self.encoder_b: self.lockRotary.acquire() try: while True: clk = self.clk dt = self.dt if clk != clkLast: if dt != clk: counter += 1 else: counter -= 1 print("counter", counter) clkLast = clk sleep(0.01) finally: GPIO.cleanup() self.lockRotary.release()
class RoofControl(metaclass=Singleton): def __init__(self): self.gpioconfig = GPIOConfig() def open(self): self.gpioconfig.turn_on(GPIOPin.SWITCH_ROOF) return self.gpioconfig.wait_for_off(GPIOPin.VERIFY_OPEN) def close(self): self.gpioconfig.turn_off(GPIOPin.SWITCH_ROOF) return self.gpioconfig.wait_for_off(GPIOPin.VERIFY_CLOSED) def read(self): is_roof_closed = self.gpioconfig.status(GPIOPin.VERIFY_CLOSED) is_roof_open = self.gpioconfig.status(GPIOPin.VERIFY_OPEN) is_switched_on = self.gpioconfig.status(GPIOPin.SWITCH_ROOF) if is_roof_closed and is_roof_open: return Status.ERROR elif is_roof_closed and not is_switched_on: return Status.CLOSED elif is_roof_open and is_switched_on: return Status.OPEN elif is_switched_on: return Status.OPENING else: return Status.CLOSING
def __init__(self): self.steps = 0 self.gpioconfig = GPIOConfig() self.gpiopin = GPIOPin self.pin = Pin self.encoder_a = False self.encoder_b = False self.lockRotary = threading.Lock() self.dt = None self.clk = None
class ButtonControl(): def __init__(self, pin): self.pin = pin self.gpioconfig = GPIOConfig() def on(self): self.gpioconfig.turn_on(self.pin) def off(self): self.gpioconfig.turn_off(self.pin) def read(self): if self.gpioconfig.status(self.pin): return ButtonStatus.ON else: return ButtonStatus.OFF
class GPIOConfigTest(unittest.TestCase): def setUp(self): Singleton._instances = {} self.gpioConfig = GPIOConfig() def test_is_instance(self): self.assertTrue(isinstance(self.gpioConfig, GPIOConfig)) def test_generic_exec(self): GPIOConfig.status = MagicMock( side_effect=lambda value: value != GPIOPin.SWITCH_ROOF) GPIOConfig.wait_for_off = MagicMock(return_value=True) GPIOConfig.wait_for_on = MagicMock(return_value=True) self.gpioConfig.turn_on(GPIOPin.SWITCH_ROOF) self.gpioConfig.turn_off(GPIOPin.SWITCH_ROOF) self.assertTrue(self.gpioConfig.wait_for_on(GPIOPin.SWITCH_ROOF)) self.assertTrue(self.gpioConfig.wait_for_off(GPIOPin.SWITCH_ROOF)) self.assertFalse(self.gpioConfig.status(GPIOPin.SWITCH_ROOF)) def test_is_singleton(self): self.assertEqual(GPIOConfig(), self.gpioConfig) def test_timeout_value(self): cparser = configparser.ConfigParser() cparser.read('config.ini') self.assertEqual( config.Config.getInt("wait_for_timeout", "roof_board"), cparser["roof_board"].getint("wait_for_timeout")) self.assertEqual( config.Config.getInt("event_bouncetime", "roof_board"), cparser["roof_board"].getint("event_bouncetime"))
def exit_program(self, n: int = 0) -> None: """ Shutdown the server """ Logger.getLogger().info("Uscita dall'applicazione con codice %s", n) self.telescope.close_connection() if not self.mock: Logger.getLogger().debug("Mock: %s", self.mock) from gpio_config import GPIOConfig GPIOConfig().cleanup(n)
def threaded_event_simulation(pin, pin_status, edge, callback, bouncetime=100): new_pin_status = GPIOConfig().status(pin) if pin_status != new_pin_status: if (edge == "BOTH" or (edge == "FALLING" and new_pin_status is False) or (edge == "RISING" and new_pin_status is True)): callback(pin) pin_status = new_pin_status if bouncetime > 0: time.sleep(bouncetime / 1000) return new_pin_status
def setUp(self): Singleton._instances = {} self.gpioConfig = GPIOConfig()
def test_is_singleton(self): self.assertEqual(GPIOConfig(), self.gpioConfig)
import socket from status import Orientation import config from logger import Logger from gpio_config import GPIOConfig from gpio_pin import GPIOPin from components.curtains.factory_curtain import FactoryCurtain # Standard loopback interface address (localhost) HOST: str = config.Config.getValue("loopback_ip", "server") # Port to listen on (non-privileged ports are > 1023) PORT: str = config.Config.getInt("port", "server") error_level: int = 0 gpioConfig = GPIOConfig() curtain_east = FactoryCurtain.curtain(orientation=Orientation.EAST) curtain_west = FactoryCurtain.curtain(orientation=Orientation.WEST) def convert_steps(steps): return f'{steps:03}' try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() Logger.getLogger().info("Server avviato") while True: conn, _ = s.accept() with conn:
def __init__(self): self.gpioconfig = GPIOConfig()
def __init__(self, pin): self.pin = pin self.gpioconfig = GPIOConfig()