From 023434cfcc1fd5558ab48bd2ba2c3232a121638a Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Fri, 24 Jan 2020 22:08:10 +0000 Subject: [PATCH] added funtionality to turn off lights when TV is off Added some functionality when the TV is turned off, the lights turn off. --- src/app.rs | 1 + src/config.rs | 25 ++++++++---- src/led.py | 6 +++ src/main.rs | 103 +++++++++++++++++++++++++++++++++++--------------- 4 files changed, 97 insertions(+), 38 deletions(-) create mode 100644 src/led.py diff --git a/src/app.rs b/src/app.rs index 5c6aa3f..c885b40 100644 --- a/src/app.rs +++ b/src/app.rs @@ -37,6 +37,7 @@ pub fn match_to_app(text: String) -> ActiveApp { Some("Plex") => ActiveApp::Plex, Some("Pandora") => ActiveApp::Pandora, Some("Spotify") => ActiveApp::Spotify, + Some("Plex") => ActiveApp::Plex, _ => ActiveApp::Roku, } } diff --git a/src/config.rs b/src/config.rs index 0986799..77e566b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,36 +2,45 @@ extern crate serde; extern crate serde_derive; extern crate serde_json; -use std::fs::File; -use std::io::{BufReader, Read}; +use std::fs::{write, File}; +use std::io::{BufReader, Read, BufWriter, Write}; use std::path::Path; use std::future::Future; use reqwest::Client; use select::document::Document; use select::predicate::Name; -use serde::Deserialize; +use serde::{Serialize, Deserialize}; use serde_json::Result; // config structure -#[derive(Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct Configuration { pub ipaddr: String, pub port: i32, pub tv: String, pub active_app: String, + pub power_status: String, } impl Configuration { pub fn is_change_app(&self) -> bool { - true + self.active_app == self.get_app_status() } - pub fn change_active_app(&mut self) { - // stuff happens here + pub fn is_change_power(&self) -> bool { + self.power_status == self.get_power_status() } - pub fn get_tv_status(&self) -> String { + pub fn change_active_app(&mut self, app_text: &String) { + self.active_app = app_text.to_string(); + } + + pub fn change_power(&mut self, power_text: &String) { + self.power_status = power_text.to_string(); + } + + pub fn get_app_status(&self) -> String { let request = format!( "http://{ipaddr}:{port}/query/active-app", ipaddr = self.ipaddr, diff --git a/src/led.py b/src/led.py new file mode 100644 index 0000000..fc846e9 --- /dev/null +++ b/src/led.py @@ -0,0 +1,6 @@ +import board +import neopixel +pixels = neopixel.NeoPixel(board.D5, 30) + +def theaterChaseRainbow(): + pass \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 77e82d9..37948d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,9 +13,8 @@ use std::error::Error; use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use rppal::gpio::Gpio; use rppal::hal::{Delay, Timer}; -use smart_leds::{RGB8, SmartLedsWrite, brightness}; +use smart_leds::{colors, hsv::{Hsv, hsv2rgb}, RGB8, SmartLedsWrite, brightness}; use ws2812_spi::Ws2812; -use embedded_hal; // local files that need to be imported mod app; @@ -30,44 +29,88 @@ fn main() { let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_000_000, Mode::Mode0).unwrap(); let mut ws = Ws2812::new(spi); let delay = Delay::new(); - let configuration = config::init_config(); - println!("{:?}", configuration); - - - // let gil = Python::acquire_gil(); - // let py = gil.python(); - // let m = PyModule::import(py, "led.py").unwrap(); + let mut configuration = config::init_config(); loop { - let app_text = configuration.get_tv_status(); - let power_text = configuration.get_power_status(); + println!("{:?}", configuration); - let activeapp = app::match_to_app(app_text); - let tvpower = app::match_to_power_status(power_text); + if false == configuration.is_change_app() { + let app_text = configuration.get_app_status(); + configuration.change_active_app(&app_text); + let activeapp = app::match_to_app(app_text); - match activeapp { - app::ActiveApp::Roku => println!("The lights are light purple!"), - app::ActiveApp::Netflix => { - let green = RGB8::new(255, 0, 0); - let mut data = [RGB8::default(); NUM_LEDS]; + match activeapp { + app::ActiveApp::Roku => { + let color = RGB8::new(255, 0, 255); + let mut data = [RGB8::default(); NUM_LEDS]; - for j in 0..(256 * 5) { for i in 0..NUM_LEDS { - data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8); + data[i] = color; } ws.write(brightness(data.iter().cloned(), 32)).unwrap(); - } - }, - app::ActiveApp::Hulu => println!("The lights are green!"), - app::ActiveApp::AmazonPrime => println!("The light are light blue!"), - app::ActiveApp::Spotify => println!("The lights are light green!"), - _ => println!("Oops!"), + }, + app::ActiveApp::Hulu => { + let green = RGB8::new(51, 255, 85); + let mut data = [RGB8::default(); NUM_LEDS]; + + for i in 0..NUM_LEDS { + data[i] = green; + } + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); + }, + app::ActiveApp::Netflix => { + let red = RGB8::new(255, 77, 77); + let mut data = [RGB8::default(); NUM_LEDS]; + + for i in 0..NUM_LEDS { + data[i] = red; + } + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); + }, + app::ActiveApp::AmazonPrime => println!("The light are light blue!"), + app::ActiveApp::Spotify => { + let green = RGB8::new(51, 255, 85); + let mut data = [RGB8::default(); NUM_LEDS]; + + for i in 0..NUM_LEDS { + data[i] = green; + } + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); + }, + app::ActiveApp::Plex => { + let orange = RGB8::new(255, 187, 51); + let mut data = [RGB8::default(); NUM_LEDS]; + + for i in 0..NUM_LEDS { + data[i] = orange; + } + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); + }, + _ => println!("Oops!"), + } } - match tvpower { - app::TVPower::On => println!("TV is on!"), - app::TVPower::Off => println!("TV is off"), - _ => println!("We don't know what the power status of the TV is..."), + if false == configuration.is_change_power() { + let power_text = configuration.get_power_status(); + configuration.change_power(&power_text); + let tvpower = app::match_to_power_status(power_text); + + match tvpower { + app::TVPower::On => { + continue + }, + app::TVPower::Off => { + let color = RGB8::new(0, 0, 0); + let mut data = [RGB8::default(); NUM_LEDS]; + + for i in 0..NUM_LEDS { + data[i] = color; + } + + ws.write(brightness(data.iter().cloned(), 32)).unwrap(); + }, + _ => println!("We don't know what the power status of the TV is..."), + } } let sec = time::Duration::from_secs(3);