cleaned up code, changed on/off functionality to boolean

on/off functionality was used by u32. once this u32 reaches
a certain number, it is then an overflow error. booleans doesn't
have this problem.
This commit is contained in:
Wyatt J. Miller 2020-04-03 23:39:59 +01:00
parent a3ddfc2734
commit 7112dadf29
6 changed files with 309 additions and 428 deletions

601
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -18,3 +18,4 @@ smart-leds = "0.3.0"
ws2812-spi = "0.2.0"
embedded-hal = "0.2.3"
linux-embedded-hal = "*"
failure = "0.1.7"

View File

@ -1,4 +1,5 @@
// enum used when a certain TV app is being used
#[warn(dead_code)]
pub enum ActiveApp {
Roku,
Netflix,
@ -14,6 +15,7 @@ pub enum ActiveApp {
// enum used to select what kind of TV you are using
// not used atm
#[warn(dead_code)]
pub enum TV {
Roku,
Android,

View File

@ -2,16 +2,13 @@ extern crate serde;
extern crate serde_derive;
extern crate serde_json;
use std::fs::{write, File};
use std::io::{BufReader, Read, BufWriter, Write};
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::future::Future;
use reqwest::Client;
use select::document::Document;
use select::predicate::Name;
use serde::{Serialize, Deserialize};
use serde_json::Result;
// config structure
#[derive(Serialize, Deserialize, Debug)]
@ -67,10 +64,6 @@ impl Configuration {
next.text().to_string()
}
pub fn change_color(num_leds: usize, num_one: &u8, num_two: &u8, num_three: &u8) {
// code goes here
}
}
// grab the config

View File

@ -1,105 +0,0 @@
extern crate rppal;
use rppal::spi::{Bus, Mode, Spi, SlaveSelect};
/// Struct for storing grb values
pub struct Led {
buffer: Vec<u8>,
spi: Spi,
num_leds: u32,
}
/// Struct for storing rgb values
pub struct ColorRGB (pub u8, pub u8, pub u8);
impl Led {
/// create a Led instance
pub fn new(num_leds: u32) -> Led {
// initlaize all the things
// like the buffer, the bus, the mode that we want the SPI in,
// the slave and the clock speed
let buffer = Vec::new();
let bus = Bus::Spi0;
let mode = Mode::Mode0;
let slave = SlaveSelect::Ss0;
let clock = 3 * 1000 * 1000;
// actually create a new instance of this thing and return it
Led {
buffer,
spi: Spi::new(bus, slave, clock, mode)
.unwrap(),
num_leds
}
}
// Write out to the leds to the controller
fn write(&mut self) {
// collects all the bytes to write
let output = self.buffer
.drain(..)
.flat_map(|val| byte_to_spi_bytes(val).to_vec())
.collect::<Vec<u8>>();
self.spi.write(&output).unwrap();
}
/// Set the LEDs so that they could be written to the conntroller
/// This method is written for converting RGB to GRB
pub fn set_leds(&mut self, hex_codes: &[ColorRGB]) {
hex_codes
.iter()
.for_each(|hex_code| {
// swapping here from RGB to the GRB expected
self.buffer.extend_from_slice(&[hex_code.1, hex_code.0, hex_code.2]);
});
self.write();
}
/// Turns all LEDs off and clears buffer
/// Useful for when the TV is off
pub fn clear_all_leds(&mut self) {
self.buffer.clear();
let mut clear_codes = vec![0; (self.num_leds * 3) as usize];
self.buffer.append(&mut clear_codes);
self.write();
}
}
/// Function to convert bytes to SPI bytes
pub fn byte_to_spi_bytes(input: u8) -> [u8; 3] {
// first convert the u8 to 24 bits
let mut bool_array = [false; 24];
for bit_index in 0..8 {
let bit = input & (1 << bit_index) != 0;
let out_index = bit_index * 3;
// first bit is always 0
// this could be omitted because the array is initialized to false
// last bit is always 1
bool_array[out_index] = false;
bool_array[out_index + 1] = bit;
bool_array[out_index + 2] = true;
}
// then convert the 24 bits to three u8
[
bool_slice_to_u8(&bool_array[0..8]),
bool_slice_to_u8(&bool_array[8..16]),
bool_slice_to_u8(&bool_array[16..24]),
]
}
/// Function that checks if there are eight booleans
pub fn bool_slice_to_u8(input: &[bool]) -> u8 {
if input.len() != 8 { panic!("bool to u8 conversion requires exactly 8 booleans") }
let mut out = 0b0000_0000u8;
for (carry_bit, flag) in input.iter().enumerate() {
if *flag { out += 0b0000_0001u8 << carry_bit }
}
out
}

View File

@ -17,7 +17,6 @@ use ws2812_spi::Ws2812;
mod app;
mod config;
mod generate;
mod led;
const NUM_LEDS: usize = 150;
@ -25,7 +24,7 @@ fn main() {
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_000_000, Mode::Mode0).unwrap();
let mut ws = Ws2812::new(spi);
let mut configuration = config::init_config();
let mut headless_count: u32 = 0;
let mut is_headless: bool = false;
loop {
println!("{:?}", configuration);
@ -37,9 +36,8 @@ fn main() {
match tvpower {
app::TVPower::Off => {
headless_count += headless_count + 1;
if headless_count <= 2 {
if is_headless == false {
is_headless = true;
let color = RGB8::new(0, 0, 0);
let mut data = [RGB8::default(); NUM_LEDS];
@ -52,7 +50,7 @@ fn main() {
},
app::TVPower::On => {
if false == configuration.is_change_app() {
headless_count = 0;
is_headless = false;
let app_text = configuration.get_app_status();
configuration.change_active_app(&app_text);
@ -116,3 +114,8 @@ fn main() {
thread::sleep(sec);
}
}
#[warn(unreachable_patterns)]
pub fn change_color(_num_leds: usize, _num_one: &u8, _num_two: &u8, _num_three: &u8) {
// code goes here
}