mutex
This commit is contained in:
parent
30e8e476b9
commit
6333245304
2 changed files with 17 additions and 11 deletions
12
src/main.rs
12
src/main.rs
|
@ -3,7 +3,7 @@ pub mod util;
|
||||||
use util::lights::*;
|
use util::lights::*;
|
||||||
use util::pattern::*;
|
use util::pattern::*;
|
||||||
use util::webserver::*;
|
use util::webserver::*;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock, Mutex};
|
||||||
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
|
use rs_ws281x::{RawColor, Controller, ControllerBuilder, ChannelBuilder, StripType};
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ pub const LOOP_WAIT: u64 = 250;
|
||||||
fn main() {
|
fn main() {
|
||||||
let p: RawColor = [0, 0, 0, 0];
|
let p: RawColor = [0, 0, 0, 0];
|
||||||
let lock = Arc::new(RwLock::new([p; LED_SIZE]));
|
let lock = Arc::new(RwLock::new([p; LED_SIZE]));
|
||||||
let mut pattern: Vec<Box<dyn Pattern>> = Vec::new();
|
let mut pattern: Arc<Mutex<Vec<Box<dyn Pattern>>>> = Arc::new(Mutex::new(Vec::new()));
|
||||||
let read = Arc::clone(&lock);
|
let read = Arc::clone(&lock);
|
||||||
//light management
|
//light management
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
@ -34,14 +34,18 @@ fn main() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
websocket(&mut pattern);
|
let pat_clone = Arc::clone(&pattern);
|
||||||
|
thread::spawn(move || loop {
|
||||||
|
websocket(&pat_clone);
|
||||||
|
});
|
||||||
|
|
||||||
//pattern management
|
//pattern management
|
||||||
loop {
|
loop {
|
||||||
thread::sleep(time::Duration::from_millis(LOOP_WAIT));
|
thread::sleep(time::Duration::from_millis(LOOP_WAIT));
|
||||||
let c_lock = Arc::clone(&lock);
|
let c_lock = Arc::clone(&lock);
|
||||||
let mut lights = c_lock.write().unwrap();
|
let mut lights = c_lock.write().unwrap();
|
||||||
for p in &pattern {
|
let mut data = pattern.lock().unwrap();
|
||||||
|
for p in *data {
|
||||||
//let c_lock = Arc::clone(&lock);
|
//let c_lock = Arc::clone(&lock);
|
||||||
//let mut lights = c_lock.write().unwrap();
|
//let mut lights = c_lock.write().unwrap();
|
||||||
p.execute(&mut lights);
|
p.execute(&mut lights);
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use std::thread;
|
use std::{thread, sync::{Arc, Mutex}};
|
||||||
use tungstenite::accept;
|
use tungstenite::accept;
|
||||||
use crate::util::pattern;
|
use crate::util::pattern::*;
|
||||||
|
|
||||||
pub fn websocket(pattern: &mut Vec<Box<dyn pattern::Pattern>>) {
|
pub fn websocket(pattern: &Arc<Mutex<Vec<Box<dyn Pattern>>>>) {
|
||||||
let server = TcpListener::bind("0.0.0.0:29999").unwrap();
|
let server = TcpListener::bind("0.0.0.0:29999").unwrap();
|
||||||
for stream in server.incoming() {
|
for stream in server.incoming() {
|
||||||
|
let clone = Arc::clone(pattern);
|
||||||
thread::spawn (move || {
|
thread::spawn (move || {
|
||||||
let mut websocket = accept(stream.unwrap()).unwrap();
|
let mut websocket = accept(stream.unwrap()).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let input = websocket.read_message().unwrap();
|
let input = websocket.read_message().unwrap();
|
||||||
if input.is_text() {
|
if input.is_text() {
|
||||||
let string = input.to_text().unwrap_or("unit");
|
let string = input.to_text().unwrap_or("unit");
|
||||||
let mul_string = pattern::format_multiline(string);
|
let mul_string = format_multiline(string);
|
||||||
match mul_string.iter().cloned().map(pattern::parse_line).collect() {
|
match mul_string.iter().cloned().map(parse_line).collect() {
|
||||||
Ok(patterns) => {
|
Ok(patterns) => {
|
||||||
*pattern = patterns;
|
let mut data = clone.lock().unwrap();
|
||||||
|
*data = patterns;
|
||||||
let text = format!("Ok!\n\n{}", string);
|
let text = format!("Ok!\n\n{}", string);
|
||||||
let msg = tungstenite::Message::from(text);
|
let msg = tungstenite::Message::from(text);
|
||||||
websocket.write_message(msg).unwrap();
|
websocket.write_message(msg).unwrap();
|
||||||
|
|
Loading…
Reference in a new issue