Skip to content

Commit

Permalink
what is actually listened to here?
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Nov 11, 2023
1 parent b3d1161 commit e5e18ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion pioreactor/background_jobs/leader/mqtt_to_db_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def callback(message: pt.MQTTMessage) -> None:
try:
new_rows = parser(message.topic, message.payload)
except Exception as e:
self.logger.error(e)
self.logger.error(f"Encountered error in saving to DB: {e}. See logs.")
self.logger.debug(
f"Error in {parser.__name__}. message.payload that caused error: `{message.payload.decode()}`",
exc_info=True,
Expand Down Expand Up @@ -145,6 +145,7 @@ def callback(message: pt.MQTTMessage) -> None:

def initialize_callbacks(self, topics_and_callbacks: list[TopicToCallback]) -> None:
for topic_and_callback in topics_and_callbacks:
print(str(topic_and_callback.topic))
self.subscribe_and_callback(
topic_and_callback.callback,
str(topic_and_callback.topic),
Expand Down
29 changes: 16 additions & 13 deletions pioreactor/utils/pwm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import lgpio

from pioreactor import types as pt
from pioreactor.exc import PWMError
from pioreactor.logging import create_logger
from pioreactor.logging import Logger
Expand All @@ -36,7 +37,9 @@
class HardwarePWMOutputDevice(HardwarePWM):
HARDWARE_PWM_CHANNELS: dict[GpioPin, int] = {12: 0, 13: 1}

def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100):
def __init__(
self, pin: GpioPin, initial_dc: pt.FloatBetween0and100 = 0.0, frequency: float = 100
) -> None:
if (
pin not in self.HARDWARE_PWM_CHANNELS
): # Only GPIO pins 18 and 19 are supported for hardware PWM
Expand All @@ -48,28 +51,28 @@ def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100):
super().__init__(pwm_channel, hz=frequency)
self._dc = initial_dc

def start(self):
def start(self) -> None:
super().start(self.dc)

def off(self):
def off(self) -> None:
self.dc = 0.0

@property
def dc(self) -> float:
def dc(self) -> pt.FloatBetween0and100:
return self._dc

@dc.setter
def dc(self, dc: float) -> None:
def dc(self, dc: pt.FloatBetween0and100) -> None:
dc = clamp(0.0, dc, 100.0)
self.change_duty_cycle(dc)
self._dc = dc

def close(self):
def close(self) -> None:
pass


class SoftwarePWMOutputDevice:
def __init__(self, pin: GpioPin, initial_dc: float = 0.0, frequency=100):
def __init__(self, pin: GpioPin, initial_dc: pt.FloatBetween0and100 = 0.0, frequency=100):
self.pin = pin
self._dc = initial_dc
self.frequency = frequency
Expand All @@ -91,11 +94,11 @@ def off(self):
pass

@property
def dc(self) -> float:
def dc(self) -> pt.FloatBetween0and100:
return self._dc

@dc.setter
def dc(self, dc: float) -> None:
def dc(self, dc: pt.FloatBetween0and100) -> None:
dc = clamp(0.0, dc, 100.0)
self._dc = dc
if self._started:
Expand Down Expand Up @@ -239,18 +242,18 @@ def _serialize(self):
f"pioreactor/{self.unit}/{self.experiment}/pwms/dc", dumps(current_values), retain=True
)

def start(self, initial_duty_cycle: float) -> None:
if not (0.0 <= initial_duty_cycle <= 100.0):
def start(self, duty_cycle: pt.FloatBetween0and100) -> None:
if not (0.0 <= duty_cycle <= 100.0):
raise PWMError("duty_cycle should be between 0 and 100, inclusive.")

self.change_duty_cycle(initial_duty_cycle)
self.change_duty_cycle(duty_cycle)
self._pwm.start()

def stop(self) -> None:
self._pwm.off()
self.change_duty_cycle(0.0)

def change_duty_cycle(self, duty_cycle: float) -> None:
def change_duty_cycle(self, duty_cycle: pt.FloatBetween0and100) -> None:
if not (0.0 <= duty_cycle <= 100.0):
raise PWMError("duty_cycle should be between 0 and 100, inclusive.")

Expand Down

0 comments on commit e5e18ce

Please sign in to comment.