Skip to content

Commit

Permalink
Add scene management functionality with color application and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samclane committed Dec 16, 2024
1 parent cb175ef commit 4cc80ea
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Mantle is a desktop application for controlling LIFX lights, born from the ashes

You can download the latest release [here](https://github.com/samclane/mantle/releases).

**Note:** Right now I'm only building for Windows, but it should build on Linux and MacOS as well. Still getting GitHub Actions set up for that.

## Screenshots

![Mantle](res/screenshot.png)
Expand Down
7 changes: 7 additions & 0 deletions src/device_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ impl DeviceInfo {
DeviceInfo::Group(g) => Some(g.label.to_string()),
}
}

pub fn color(&self) -> Option<&HSBK> {
match self {
DeviceInfo::Bulb(b) => b.get_color(),
DeviceInfo::Group(_) => None, // TODO: Implement group color
}
}
}

impl BulbInfo {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod device_manager;
pub mod listener;
pub mod products;
pub mod refreshable_data;
pub mod scenes;
pub mod screencap;
pub mod serializers;
pub mod settings;
Expand Down
87 changes: 87 additions & 0 deletions src/scenes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use lifx_core::HSBK;

use crate::{color::default_hsbk, device_info::DeviceInfo, LifxManager};

pub struct Scene {
pub device_color_pairs: Vec<(DeviceInfo, HSBK)>,
}

impl Scene {
/// A scene defines a set of devices and their colors so that they can be applied all at once.
/// This is useful for setting up a specific lighting configuration that you want to be able to
/// apply quickly.
pub fn new(device_color_pairs: Vec<(DeviceInfo, HSBK)>) -> Self {
Self { device_color_pairs }
}

pub fn apply(&self, lifx_manager: &mut LifxManager) {
for (device, color) in &self.device_color_pairs {
match device {
DeviceInfo::Bulb(bulb) => {
lifx_manager.set_color(&&**bulb, *color, None).unwrap();
}
DeviceInfo::Group(group) => {
lifx_manager
.set_group_color(group, *color, &lifx_manager.bulbs.lock().unwrap(), None)
.unwrap();
}
}
}
}
}

impl From<Vec<DeviceInfo>> for Scene {
fn from(devices: Vec<DeviceInfo>) -> Self {
let device_color_pairs = devices
.into_iter()
.map(|device| (device.clone(), *device.color().unwrap_or(&default_hsbk())))
.collect();

Self::new(device_color_pairs)
}
}

#[cfg(test)]
mod test {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use super::*;
use crate::device_info::BulbInfo;

#[test]
fn test_scene_from_vec() {
let source = 1234;
let target = 5678;
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 56700);
let mut bulb = BulbInfo::new(source, target, addr);
bulb.update(addr);
let scene = Scene::from(vec![DeviceInfo::Bulb(Box::new(bulb.clone()))]);

assert_eq!(scene.device_color_pairs.len(), 1);
assert_eq!(
scene.device_color_pairs[0].0,
DeviceInfo::Bulb(Box::new(bulb))
);
}

#[test]
fn test_create_scene() {
let source = 1234;
let target = 5678;
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 56700);
let mut bulb = BulbInfo::new(source, target, addr);
bulb.update(addr);
let scene = Scene::new(vec![(
DeviceInfo::Bulb(Box::new(bulb.clone())),
default_hsbk(),
)]);

assert_eq!(scene.device_color_pairs.len(), 1);
assert_eq!(
scene.device_color_pairs[0].0,
DeviceInfo::Bulb(Box::new(bulb))
);
assert_eq!(scene.device_color_pairs[0].1, default_hsbk());
}
}

0 comments on commit 4cc80ea

Please sign in to comment.