Skip to content

Instantly share code, notes, and snippets.

@CamDavidsonPilon
Last active November 4, 2021 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamDavidsonPilon/e9d861c185f62d0c880d3d1b31ef93cb to your computer and use it in GitHub Desktop.
Save CamDavidsonPilon/e9d861c185f62d0c880d3d1b31ef93cb to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import annotations
import signal
from pioreactor.automations import LEDAutomationContrib
from pioreactor.automations import events
from pioreactor.actions.led_intensity import LED_Channel
__plugin_summary__ = "An LED automation for ON/OFF cycles"
__plugin_version__ = "0.0.2"
__plugin_name__ = "Light Dark Cycle LED automation"
class LightDarkCycle(LEDAutomationContrib):
"""
Follows as h light / h dark cycle. Starts with light on.
"""
key: str = "light_dark_cycle"
published_settings: dict[str, dict] = {
"duration": {"datatype": "float", "settable": False, "unit": "min"}, # doesn't make sense to change duration.
"light_intensity": {"datatype": "float", "settable": True, "unit": "%"},
"light_duration_hours": {"datatype": "int", "settable": True, "unit": "h"},
"dark_duration_hours": {"datatype": "int", "settable": True, "unit": "h"},
}
def __init__(self, light_intensity: float, light_duration_hours: int, dark_duration_hours: int, **kwargs):
super().__init__(**kwargs)
self.hours_online: int = -1
self.light_active: bool = False
self.channels: list[LED_Channel] = [LED_Channel("B"), LED_Channel("C")]
self.set_light_intensity(light_intensity)
self.light_duration_hours = float(light_duration_hours)
self.dark_duration_hours = float(dark_duration_hours)
def trigger_leds(self, hours: int) -> events.Event:
cycle_duration: int = self.light_duration_hours + self.dark_duration_hours
if ((hours % cycle_duration) < self.light_duration_hours) and (not self.light_active):
self.light_active = True
for channel in self.channels:
self.set_led_intensity(channel, self.light_intensity)
return events.ChangedLedIntensity(f"{hours}h: turned on LEDs")
elif ((hours % cycle_duration) >= self.light_duration_hours) and (self.light_active):
self.light_active = False
for channel in self.channels:
self.set_led_intensity(channel, 0)
return events.ChangedLedIntensity(f"{hours}h: turned off LEDs")
else:
return events.NoEvent(f"{hours}h: no change")
def set_light_intensity(self, intensity):
self.light_intensity = float(intensity)
if self.light_active:
# update now!
for channel in self.channels:
self.set_led_intensity(channel, self.light_intensity)
def execute(self) -> events.Event:
self.hours_online += 1
event = self.trigger_leds(self.hours_online)
return event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment