-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
337 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export type ControllerType = "Wled" | "Dmx"; | ||
export type ControllerType = "WLED" | "DMX"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface DmxControllerConfiguration { port: string, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface DmxControllerData { data: Array<number>, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
import type { DmxControllerConfiguration } from "./DmxControllerConfiguration"; | ||
import type { DmxControllerData } from "./DmxControllerData"; | ||
|
||
export type DmxControllerMessage = { type: "UpdateConfiguration" } & DmxControllerConfiguration | { type: "Data" } & DmxControllerData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
|
||
export interface WledControllerData { data: Array<number>, } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. | ||
import type { WledControllerConfiguration } from "./WledControllerConfiguration"; | ||
import type { WledControllerData } from "./WledControllerData"; | ||
|
||
export type WledControllerMessage = { UpdateConfiguration: WledControllerConfiguration } | { Data: Array<number> }; | ||
export type WledControllerMessage = { type: "UpdateConfiguration" } & WledControllerConfiguration | { type: "Data" } & WledControllerData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serialport::SerialPort; | ||
use std::sync::mpsc::{Receiver, Sender}; | ||
use ts_rs::TS; | ||
|
||
use super::controller::{BridgeToControllerMessage, Controller, ControllerToBridgeMessage}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, TS)] | ||
#[ts(export)] | ||
pub struct DmxControllerConfiguration { | ||
pub port: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, TS)] | ||
#[ts(export)] | ||
pub struct DmxControllerData { | ||
pub data: Vec<u8>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, TS)] | ||
#[serde(tag = "type")] | ||
#[ts(export)] | ||
pub enum DmxControllerMessage { | ||
UpdateConfiguration(DmxControllerConfiguration), | ||
Data(DmxControllerData), | ||
} | ||
|
||
pub struct DmxController { | ||
tx: Sender<ControllerToBridgeMessage>, | ||
rx: Receiver<BridgeToControllerMessage>, | ||
serialport: Option<Box<dyn SerialPort>>, | ||
} | ||
|
||
impl DmxController { | ||
fn handle_message(self: &mut Self, msg: DmxControllerMessage) { | ||
match msg { | ||
DmxControllerMessage::UpdateConfiguration(configuration) => { | ||
println!("Updating configuration: {:?}", configuration); | ||
|
||
let mut port = serialport::new(&configuration.port, 115200) | ||
.parity(serialport::Parity::None) | ||
.data_bits(serialport::DataBits::Eight) | ||
.stop_bits(serialport::StopBits::One) | ||
.open(); | ||
|
||
match port { | ||
Ok(ref mut p) => { | ||
let _ = p.as_mut().write(&[99]); | ||
} | ||
Err(ref err) => { | ||
println!("Failed to open port {}: {}", configuration.port, err); | ||
} | ||
} | ||
} | ||
DmxControllerMessage::Data(data) => { | ||
println!("Sending data: {:?}", data); | ||
if let Some(ref mut port) = self.serialport { | ||
let _ = port.write(&data.data[..]); | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Controller for DmxController { | ||
fn new(tx: Sender<ControllerToBridgeMessage>, rx: Receiver<BridgeToControllerMessage>) -> Self | ||
where | ||
Self: Sized, | ||
{ | ||
return Self { | ||
tx, | ||
rx, | ||
serialport: None, | ||
}; | ||
} | ||
|
||
fn run(&mut self) -> () { | ||
loop { | ||
match self.rx.recv() { | ||
Ok(msg) => match msg { | ||
BridgeToControllerMessage::Message(str_message) => { | ||
println!("Received message: {}", str_message); | ||
let parsed_message: DmxControllerMessage = | ||
serde_json::from_str(&str_message).unwrap(); | ||
self.handle_message(parsed_message); | ||
} | ||
BridgeToControllerMessage::Dispose => { | ||
break; | ||
} | ||
}, | ||
Err(err) => { | ||
println!("Error receiving message: {}", err); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod controller; | ||
pub mod dmx_controller; | ||
pub mod wled_controller; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.