Add Runtime, Token implementations, Registers
parent
6c6de07631
commit
d7c689f7d2
@ -0,0 +1,5 @@
|
||||
use std::io;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::net::TcpStream;
|
||||
|
||||
const STATE_COMMAND_PREFIX: u8 = 0x71;
|
||||
const PROGRAM_COMMAND_PREFIX: u8 = 0x61;
|
||||
|
||||
pub enum StateStripCommand {
|
||||
On = 0x23,
|
||||
Off = 0x24,
|
||||
}
|
||||
|
||||
pub enum ProgramStripCommand {
|
||||
SevenCrossFade = 0x25,
|
||||
RedGradual = 0x26,
|
||||
GreenGradual = 0x27,
|
||||
BlueGradual = 0x28,
|
||||
WhiteGradual = 0x2c,
|
||||
RedGreenCross = 0x2d,
|
||||
RedBlueCross = 0x2e,
|
||||
GreenBlueCross = 0x2f,
|
||||
SevenStrobe = 0x30,
|
||||
RedStrobe = 0x31,
|
||||
GreenStrobe = 0x32,
|
||||
BlueStrobe = 0x33,
|
||||
WhiteStrobe = 0x37,
|
||||
SevenJumping = 0x38,
|
||||
}
|
||||
|
||||
pub struct LedStripController {
|
||||
stream: TcpStream,
|
||||
pub r: u8,
|
||||
pub g: u8,
|
||||
pub b: u8,
|
||||
}
|
||||
|
||||
impl LedStripController {
|
||||
pub fn new(ip: &str, port: usize) -> io::Result<Self> {
|
||||
let stream = TcpStream::connect(format!("{}:{}", ip, port))?;
|
||||
|
||||
Ok(Self {
|
||||
stream,
|
||||
r: 0,
|
||||
g: 0,
|
||||
b: 0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Send an rgb color to the led strip
|
||||
pub fn send_rgb_color(&mut self, r: u8, g: u8, b: u8) -> io::Result<()> {
|
||||
self.r = r;
|
||||
self.g = g;
|
||||
self.b = b;
|
||||
let message = create_message(&[0x31, r, g, b, 0xf0]);
|
||||
self.stream.write(&message)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the state of the strip to either on or off
|
||||
pub fn set_state(&mut self, cmd: StateStripCommand) -> io::Result<()> {
|
||||
self.stream
|
||||
.write(&create_message(&[STATE_COMMAND_PREFIX, cmd as u8]))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// Sends a strip command with a specified speed
|
||||
/// that is one of 0x01 0x06, 0x10, 0x1c
|
||||
pub fn send_command(&mut self, cmd: ProgramStripCommand, speed: u8) -> io::Result<()> {
|
||||
self.stream
|
||||
.write(&create_message(&[PROGRAM_COMMAND_PREFIX, cmd as u8, speed]))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a message for the led strip
|
||||
fn create_message(data: &[u8]) -> Vec<u8> {
|
||||
let mut data = data.clone().to_vec();
|
||||
data.append(&mut vec![0x0f]);
|
||||
data.push(data.iter().sum::<u8>() & 255);
|
||||
|
||||
data
|
||||
}
|
@ -1,2 +1,4 @@
|
||||
pub mod asm_tokens;
|
||||
pub mod ledstrip_controller;
|
||||
pub mod registers;
|
||||
pub mod runtime;
|
||||
pub mod tokens;
|
||||
|
@ -0,0 +1,46 @@
|
||||
use crate::ledstrip_controller::LedStripController;
|
||||
use crate::registers::{Rcb, Rcg, Rcr, Rcs, Rgd, Rgi, Rgl, Rgo, Rgp};
|
||||
use crate::tokens::Token;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub struct Runtime {
|
||||
pub rcs: Rcs,
|
||||
pub rcr: Rcr,
|
||||
pub rcg: Rcg,
|
||||
pub rcb: Rcb,
|
||||
pub rgd: Rgd,
|
||||
pub rgp: Rgp,
|
||||
pub rgi: Rgi,
|
||||
pub rgo: Rgo,
|
||||
pub rgl: Rgl,
|
||||
pub memory: HashMap<u32, u32>,
|
||||
text: Vec<Box<dyn Token>>,
|
||||
labels: HashMap<u32, u128>,
|
||||
strip_controller: Rc<RefCell<LedStripController>>,
|
||||
}
|
||||
|
||||
impl Runtime {
|
||||
pub fn new(ip: &str, port: usize) -> Self {
|
||||
let controller = LedStripController::new(ip, port)
|
||||
.expect("failed to establish a connection to the led strip");
|
||||
let mut controller = Rc::new(RefCell::new(controller));
|
||||
|
||||
Self {
|
||||
rcs: Rcs::new(controller.clone()),
|
||||
rcr: Rcr::new(controller.clone()),
|
||||
rcg: Rcg::new(controller.clone()),
|
||||
rcb: Rcb::new(controller.clone()),
|
||||
rgd: Rgd::new(),
|
||||
rgp: Rgp::new(),
|
||||
rgi: Rgi::new(),
|
||||
rgo: Rgo::new(),
|
||||
rgl: Rgl::new(),
|
||||
memory: HashMap::new(),
|
||||
text: Vec::new(),
|
||||
labels: HashMap::new(),
|
||||
strip_controller: controller,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue