added funtionality to turn off lights when TV is off

Added some functionality when the TV is turned off, the lights turn off.
This commit is contained in:
Wyatt J. Miller 2020-01-24 22:08:10 +00:00
parent fa6b56e675
commit 023434cfcc
4 changed files with 97 additions and 38 deletions

View File

@ -37,6 +37,7 @@ pub fn match_to_app(text: String) -> ActiveApp {
Some("Plex") => ActiveApp::Plex, Some("Plex") => ActiveApp::Plex,
Some("Pandora") => ActiveApp::Pandora, Some("Pandora") => ActiveApp::Pandora,
Some("Spotify") => ActiveApp::Spotify, Some("Spotify") => ActiveApp::Spotify,
Some("Plex") => ActiveApp::Plex,
_ => ActiveApp::Roku, _ => ActiveApp::Roku,
} }
} }

View File

@ -2,36 +2,45 @@ extern crate serde;
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
use std::fs::File; use std::fs::{write, File};
use std::io::{BufReader, Read}; use std::io::{BufReader, Read, BufWriter, Write};
use std::path::Path; use std::path::Path;
use std::future::Future; use std::future::Future;
use reqwest::Client; use reqwest::Client;
use select::document::Document; use select::document::Document;
use select::predicate::Name; use select::predicate::Name;
use serde::Deserialize; use serde::{Serialize, Deserialize};
use serde_json::Result; use serde_json::Result;
// config structure // config structure
#[derive(Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Configuration { pub struct Configuration {
pub ipaddr: String, pub ipaddr: String,
pub port: i32, pub port: i32,
pub tv: String, pub tv: String,
pub active_app: String, pub active_app: String,
pub power_status: String,
} }
impl Configuration { impl Configuration {
pub fn is_change_app(&self) -> bool { pub fn is_change_app(&self) -> bool {
true self.active_app == self.get_app_status()
} }
pub fn change_active_app(&mut self) { pub fn is_change_power(&self) -> bool {
// stuff happens here 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!( let request = format!(
"http://{ipaddr}:{port}/query/active-app", "http://{ipaddr}:{port}/query/active-app",
ipaddr = self.ipaddr, ipaddr = self.ipaddr,

6
src/led.py Normal file
View File

@ -0,0 +1,6 @@
import board
import neopixel
pixels = neopixel.NeoPixel(board.D5, 30)
def theaterChaseRainbow():
pass

View File

@ -13,9 +13,8 @@ use std::error::Error;
use rppal::spi::{Bus, Mode, SlaveSelect, Spi}; use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
use rppal::gpio::Gpio; use rppal::gpio::Gpio;
use rppal::hal::{Delay, Timer}; 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 ws2812_spi::Ws2812;
use embedded_hal;
// local files that need to be imported // local files that need to be imported
mod app; mod app;
@ -30,44 +29,88 @@ fn main() {
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_000_000, Mode::Mode0).unwrap(); let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_000_000, Mode::Mode0).unwrap();
let mut ws = Ws2812::new(spi); let mut ws = Ws2812::new(spi);
let delay = Delay::new(); let delay = Delay::new();
let configuration = config::init_config(); let mut configuration = config::init_config();
println!("{:?}", configuration);
// let gil = Python::acquire_gil();
// let py = gil.python();
// let m = PyModule::import(py, "led.py").unwrap();
loop { loop {
let app_text = configuration.get_tv_status(); println!("{:?}", configuration);
let power_text = configuration.get_power_status();
let activeapp = app::match_to_app(app_text); if false == configuration.is_change_app() {
let tvpower = app::match_to_power_status(power_text); let app_text = configuration.get_app_status();
configuration.change_active_app(&app_text);
let activeapp = app::match_to_app(app_text);
match activeapp { match activeapp {
app::ActiveApp::Roku => println!("The lights are light purple!"), app::ActiveApp::Roku => {
app::ActiveApp::Netflix => { let color = RGB8::new(255, 0, 255);
let green = RGB8::new(255, 0, 0); let mut data = [RGB8::default(); NUM_LEDS];
let mut data = [RGB8::default(); NUM_LEDS];
for j in 0..(256 * 5) {
for i in 0..NUM_LEDS { 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(); ws.write(brightness(data.iter().cloned(), 32)).unwrap();
} },
}, app::ActiveApp::Hulu => {
app::ActiveApp::Hulu => println!("The lights are green!"), let green = RGB8::new(51, 255, 85);
app::ActiveApp::AmazonPrime => println!("The light are light blue!"), let mut data = [RGB8::default(); NUM_LEDS];
app::ActiveApp::Spotify => println!("The lights are light green!"),
_ => println!("Oops!"), 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 { if false == configuration.is_change_power() {
app::TVPower::On => println!("TV is on!"), let power_text = configuration.get_power_status();
app::TVPower::Off => println!("TV is off"), configuration.change_power(&power_text);
_ => println!("We don't know what the power status of the TV is..."), 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); let sec = time::Duration::from_secs(3);