Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Works randomly , nearly not work #34

Open
gaochuntie opened this issue Feb 29, 2024 · 0 comments
Open

Works randomly , nearly not work #34

gaochuntie opened this issue Feb 29, 2024 · 0 comments

Comments

@gaochuntie
Copy link

The on and off for the 30 led strings just work some time,for most of the time,it didn't work and gives no error .
In other words,it only works if the leds is just connect to the rpi.
fn main() {
let mut controller = init_controller();
warm_light_off(&mut controller);
let listener = TcpListener::bind("127.0.0.1:8891").unwrap();

loop {
    println!("Waiting for client to connect");
    match listener.accept() {
        Ok((stream, _)) => {
            handle_client(stream, &mut controller);
        }
        Err(e) => {
            eprintln!("Unable to connect: {}", e);
        }
    }

}

}
fn handle_client(mut stream: TcpStream, controller: &mut Controller) {
println!("Client connected");
let mut buffer = [0; 512];
match stream.read(&mut buffer) {
Ok(size) => {
let msg = String::from_utf8_lossy(&buffer[..size]);
let msg = msg.trim_end_matches(|c| c == '\n' || c == '\r');
println!("Received: {}", msg);
match msg {
"on" => warm_light_on(controller),
"off" => warm_light_off(controller),
_ => {
eprintln!("Unknown command: [{}]", msg);
}
}
}
Err(e) => {
eprintln!("Failed to receive data: {}", e);
}
}
}

fn init_controller() -> Controller {
ControllerBuilder::new()
.freq(800_000)
.dma(10)
.channel(
0, // Channel Index
ChannelBuilder::new()
.pin(10) // GPIO 10 = SPI0 MOSI
.count(30) // Number of LEDs
.strip_type(StripType::Ws2812)
.brightness(255) // default: 255
.build(),
)
.build()
.unwrap()
}

fn warm_light_on(controller: &mut Controller) {
let leds = controller.leds_mut(0);

for led in leds {
    //LBRB
    //(255, 230, 150)
    *led = [255, 230, 150, 0];
}
if let Err(e) = controller.render() {
    eprintln!("Failed to render: {}", e);
}
controller.wait().unwrap();
println!("Warm light on");

}

fn warm_light_off(controller: &mut Controller) {
let leds = controller.leds_mut(0);
for led in leds {
*led = [0, 0, 0, 0];
}
if let Err(e) = controller.render() {
eprintln!("Failed to render: {}", e);
}
controller.wait().unwrap();
println!("Warm light off");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant