Skip to content

Works randomly , nearly not work #34

@gaochuntie

Description

@gaochuntie

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");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions