properties: rewrite properties (whoops)
This commit is contained in:
103
src/inputs/circadian.rs
Normal file
103
src/inputs/circadian.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use chrono::{DateTime, Timelike, Utc};
|
||||
|
||||
use crate::{events::{Event, EventBus}, lib8::interpolate::lerp8by8, prop_id, properties::{PropertyID, Variant}, render::props::Output as OutputNS, task::Task, time::Periodically};
|
||||
use crate::events::System as SystemNS;
|
||||
use paste::paste;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ScheduleEntry {
|
||||
hour: u8,
|
||||
brightness: u8
|
||||
}
|
||||
|
||||
pub struct CircadianRhythm {
|
||||
time_check: Periodically,
|
||||
schedule: [ScheduleEntry;10]
|
||||
}
|
||||
|
||||
impl CircadianRhythm {
|
||||
pub fn new() -> Self {
|
||||
CircadianRhythm {
|
||||
time_check: Periodically::new_every_n_seconds(60),
|
||||
schedule: [
|
||||
ScheduleEntry { hour: 0, brightness: 0 },
|
||||
ScheduleEntry { hour: 5, brightness: 0 },
|
||||
ScheduleEntry { hour: 6, brightness: 10 },
|
||||
ScheduleEntry { hour: 7, brightness: 20 },
|
||||
ScheduleEntry { hour: 8, brightness: 80 },
|
||||
ScheduleEntry { hour: 11, brightness: 120 },
|
||||
ScheduleEntry { hour: 18, brightness: 200 },
|
||||
ScheduleEntry { hour: 19, brightness: 255 },
|
||||
ScheduleEntry { hour: 22, brightness: 120 },
|
||||
ScheduleEntry { hour: 23, brightness: 5 }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn update_brightness(&self, bus: &mut EventBus) {
|
||||
let now: DateTime<Utc> = std::time::SystemTime::now().into();
|
||||
let next_brightness = self.brightness_for_time(now.hour() as u8, now.minute() as u8);
|
||||
bus.set_property(OutputNS::Brightness, next_brightness);
|
||||
}
|
||||
|
||||
fn brightness_for_time(&self, hour: u8, minute: u8) -> u8 {
|
||||
let mut start = self.schedule.last().unwrap();
|
||||
let mut end = self.schedule.first().unwrap();
|
||||
for cur in self.schedule.iter() {
|
||||
if cur.hour <= hour {
|
||||
start = cur;
|
||||
} else {
|
||||
end = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("hour={:?} minute={:?} start={:?} end={:?}", hour, minute, start, end);
|
||||
|
||||
let mut adjusted_end = end.clone();
|
||||
if start.hour > end.hour {
|
||||
adjusted_end.hour += 24;
|
||||
}
|
||||
|
||||
let start_time = (start.hour as u16).wrapping_mul(60);
|
||||
let end_time = (end.hour as u16).wrapping_mul(60);
|
||||
let now_time = (hour as u16).wrapping_mul(60).wrapping_add(minute as u16);
|
||||
|
||||
let duration = end_time - start_time;
|
||||
let cur_duration = now_time - start_time;
|
||||
|
||||
let frac = map_range(cur_duration.into(), 0, duration.into(), 0, 255) as u8;
|
||||
|
||||
lerp8by8(start.brightness, end.brightness, frac)
|
||||
}
|
||||
}
|
||||
|
||||
fn map_range(x: u16, in_min: u16, in_max: u16, out_min: u16, out_max: u16) -> u16 {
|
||||
let run = in_max - in_min;
|
||||
if run == 0 {
|
||||
return 0;
|
||||
}
|
||||
let rise = out_max - out_min;
|
||||
let delta = x - in_min;
|
||||
return (delta.wrapping_mul(rise)) / run + out_min;
|
||||
}
|
||||
|
||||
|
||||
impl Task for CircadianRhythm {
|
||||
fn on_ready(&mut self, bus: &mut EventBus) {
|
||||
self.update_brightness(bus);
|
||||
}
|
||||
|
||||
fn on_property_change(&mut self, key: PropertyID, value: &Variant, bus: &mut EventBus) {
|
||||
match (key, value) {
|
||||
(prop_id!(SystemNS::TimeSync), Variant::Boolean(true)) => self.update_brightness(bus),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_tick(&mut self, bus: &mut EventBus) {
|
||||
if self.time_check.tick() {
|
||||
self.update_brightness(bus);
|
||||
}
|
||||
}
|
||||
}
|
1
src/inputs/mod.rs
Normal file
1
src/inputs/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod circadian;
|
Reference in New Issue
Block a user