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:
parent
fa6b56e675
commit
023434cfcc
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
6
src/led.py
Normal file
6
src/led.py
Normal file
@ -0,0 +1,6 @@
|
||||
import board
|
||||
import neopixel
|
||||
pixels = neopixel.NeoPixel(board.D5, 30)
|
||||
|
||||
def theaterChaseRainbow():
|
||||
pass
|
87
src/main.rs
87
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,45 +29,89 @@ 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);
|
||||
|
||||
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);
|
||||
let tvpower = app::match_to_power_status(power_text);
|
||||
|
||||
match activeapp {
|
||||
app::ActiveApp::Roku => println!("The lights are light purple!"),
|
||||
app::ActiveApp::Netflix => {
|
||||
let green = RGB8::new(255, 0, 0);
|
||||
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 => {
|
||||
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::Hulu => println!("The lights are green!"),
|
||||
app::ActiveApp::AmazonPrime => println!("The light are light blue!"),
|
||||
app::ActiveApp::Spotify => println!("The lights are light green!"),
|
||||
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!"),
|
||||
}
|
||||
}
|
||||
|
||||
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 => println!("TV is on!"),
|
||||
app::TVPower::Off => println!("TV is off"),
|
||||
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);
|
||||
thread::sleep(sec);
|
||||
|
Loading…
Reference in New Issue
Block a user