Beispiel #1
0
    def cleanup(self) -> None:
        self.stop()
        self.unlock()

        with local_intermittent_storage("pwm_dc") as cache:
            if str(self.pin) in cache:
                del cache[str(self.pin)]

        with local_intermittent_storage("pwm_hz") as cache:
            if str(self.pin) in cache:
                del cache[str(self.pin)]

        gpio_helpers.set_gpio_availability(
            self.pin, gpio_helpers.GPIO_states.GPIO_AVAILABLE)

        if self.using_hardware:
            # `stop` handles cleanup.
            pass
        else:

            import RPi.GPIO as GPIO

            GPIO.setmode(GPIO.BCM)
            GPIO.setup(self.pin, GPIO.OUT, initial=GPIO.LOW)
            GPIO.cleanup(self.pin)

        self.logger.debug(f"Cleaned up PWM-{self.pin}.")
Beispiel #2
0
    def setup_GPIO(self) -> None:
        set_gpio_availability(BUTTON_PIN, GPIO_states.GPIO_UNAVAILABLE)
        set_gpio_availability(LED_PIN, GPIO_states.GPIO_UNAVAILABLE)

        import RPi.GPIO as GPIO  # type: ignore

        # I am hiding all the slow imports, but in this case, I need GPIO module
        # in many functions.
        self.GPIO = GPIO

        self.GPIO.setmode(GPIO.BCM)
        self.GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=self.GPIO.PUD_DOWN)
        self.GPIO.setup(LED_PIN, GPIO.OUT)
        self.GPIO.add_event_detect(BUTTON_PIN,
                                   self.GPIO.RISING,
                                   callback=self.button_down_and_up)
Beispiel #3
0
    def __init__(self) -> None:
        set_gpio_availability(self.hall_sensor_pin,
                              GPIO_states.GPIO_UNAVAILABLE)

        import RPi.GPIO as GPIO  # type: ignore

        self.GPIO = GPIO
        self.GPIO.setmode(self.GPIO.BCM)
        self.GPIO.setup(self.hall_sensor_pin,
                        self.GPIO.IN,
                        pull_up_down=self.GPIO.PUD_UP)

        # ignore any changes that occur within 15ms - at 1000rpm (very fast), the
        # delta between changes is ~60ms, so 15ms is good enough.
        self.GPIO.add_event_detect(self.hall_sensor_pin,
                                   self.GPIO.RISING,
                                   callback=self.callback,
                                   bouncetime=15)
        self.turn_off_collection()
Beispiel #4
0
    def __init__(self,
                 pin: GpioPin,
                 hz: float,
                 always_use_software: bool = False) -> None:
        self.logger = create_logger("PWM")
        self.pin = pin
        self.hz = hz

        if self.is_locked():
            self.logger.debug(
                f"GPIO-{self.pin} is currently locked but a task is overwriting it. Either too many jobs are trying to access this pin, or a job didn't clean up properly."
            )

        gpio_helpers.set_gpio_availability(
            self.pin, gpio_helpers.GPIO_states.GPIO_UNAVAILABLE)

        if (not always_use_software) and (pin in self.HARDWARE_PWM_CHANNELS):

            self.pwm = HardwarePWM(self.HARDWARE_PWM_CHANNELS[self.pin],
                                   self.hz)

        else:

            import RPi.GPIO as GPIO  # type: ignore

            GPIO.setmode(GPIO.BCM)
            GPIO.setup(self.pin, GPIO.OUT, initial=GPIO.LOW)

            if self.hz > 2000:
                self.logger.warning(
                    "Setting a PWM to a very high frequency with software. Did you mean to use a hardware PWM?"
                )

            self.pwm = GPIO.PWM(self.pin, self.hz)

        with local_intermittent_storage("pwm_hz") as cache:
            cache[str(self.pin)] = str(self.hz)

        self.logger.debug(
            f"Initialized PWM-{self.pin} with {'hardware' if self.using_hardware else 'software'}, initial frequency is {self.hz}hz."
        )
Beispiel #5
0
 def cleanup(self) -> None:
     self.GPIO.cleanup(self.hall_sensor_pin)
     set_gpio_availability(self.hall_sensor_pin, GPIO_states.GPIO_AVAILABLE)
Beispiel #6
0
 def on_disconnected(self) -> None:
     self.GPIO.cleanup(LED_PIN)
     self.GPIO.cleanup(BUTTON_PIN)
     set_gpio_availability(BUTTON_PIN, GPIO_states.GPIO_AVAILABLE)
     set_gpio_availability(LED_PIN, GPIO_states.GPIO_AVAILABLE)