wip-3
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
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::EventBus, lib8::interpolate::lerp8by8, properties::{PropertyID, Variant}, render::props::Output as OutputNS, task::{Environment, Task}, time::Periodically};
|
||||
use crate::events::System as SystemNS;
|
||||
use paste::paste;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ScheduleEntry {
|
||||
@@ -34,10 +33,10 @@ impl CircadianRhythm {
|
||||
}
|
||||
}
|
||||
|
||||
fn update_brightness(&self, bus: &mut EventBus) {
|
||||
fn update_brightness(&self, env: &mut Environment) {
|
||||
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);
|
||||
env.set_property(OutputNS::Brightness, next_brightness);
|
||||
}
|
||||
|
||||
fn brightness_for_time(&self, hour: u8, minute: u8) -> u8 {
|
||||
@@ -84,20 +83,20 @@ fn map_range(x: u16, in_min: u16, in_max: u16, out_min: u16, out_max: u16) -> u1
|
||||
|
||||
|
||||
impl Task for CircadianRhythm {
|
||||
fn on_ready(&mut self, bus: &mut EventBus) {
|
||||
self.update_brightness(bus);
|
||||
fn on_ready(&mut self, env: &mut Environment) {
|
||||
self.update_brightness(env);
|
||||
}
|
||||
|
||||
fn on_property_change(&mut self, key: PropertyID, value: &Variant, bus: &mut EventBus) {
|
||||
fn on_property_change(&mut self, key: PropertyID, value: &Variant, env: &mut Environment) {
|
||||
match (key, value) {
|
||||
(prop_id!(SystemNS::TimeSync), Variant::Boolean(true)) => self.update_brightness(bus),
|
||||
(SystemNS::TimeSync, Variant::Boolean(true)) => self.update_brightness(env),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
fn on_tick(&mut self, bus: &mut EventBus) {
|
||||
fn on_tick(&mut self, env: &mut Environment) {
|
||||
if self.time_check.tick() {
|
||||
self.update_brightness(bus);
|
||||
self.update_brightness(env);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
38
src/inputs/idle.rs
Normal file
38
src/inputs/idle.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::task::Environment;
|
||||
use crate::{task::Task, time::Periodically};
|
||||
use crate::events::System;
|
||||
|
||||
pub struct IdleTimer {
|
||||
timeout_start: Instant,
|
||||
timeout_duration: Duration,
|
||||
timeout_check: Periodically
|
||||
}
|
||||
|
||||
impl IdleTimer {
|
||||
pub fn new() -> Self {
|
||||
IdleTimer {
|
||||
timeout_start: Instant::now(),
|
||||
timeout_duration: Duration::from_secs(60),
|
||||
timeout_check: Periodically::new_every_n_seconds(5)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Task for IdleTimer {
|
||||
fn start(&mut self, env: &mut Environment) {
|
||||
env.set_property(System::Idle, true);
|
||||
}
|
||||
|
||||
fn on_tick(&mut self, env: &mut Environment) {
|
||||
if self.timeout_check.tick() {
|
||||
let is_idle: bool = env.get_property(System::Idle).unwrap();
|
||||
if !is_idle && self.timeout_start.elapsed() >= self.timeout_duration {
|
||||
env.set_property(System::Idle, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod circadian;
|
||||
pub mod circadian;
|
||||
pub mod idle;
|
||||
Reference in New Issue
Block a user