main.rs 6.18 KB
Newer Older
Andre Julius's avatar
Andre Julius committed
1
2
3
extern crate clap;

use clap::*;
Andre Julius's avatar
Andre Julius committed
4
5
use std::net::UdpSocket;

Andre Julius's avatar
Andre Julius committed
6
7
fn send(sock: &UdpSocket, address: &String, seq: u8, hid: u8, ofs:u8, len: u16,
        r: u8, g: u8, b: u8, w: u8) -> std::io::Result<usize> {
Andre Julius's avatar
Andre Julius committed
8
    if len == 0 {
9
        return Ok(0);
Andre Julius's avatar
Andre Julius committed
10
    }
11
12
13
14
15
16
17
    let header_len = 4;
    let mut packet = vec![0 as u8; (header_len + len*4) as usize];
    // VER|SEQ|HID|OFS|G,R,B,W,G,...
    packet[0] = 2; // version
    packet[1] = seq; // sequence
    packet[2] = hid; // host id
    packet[3] = ofs; // offset
18
    for i in 0..len {
19
20
21
22
        packet[(header_len + (i*4) + 0) as usize] = g;
        packet[(header_len + (i*4) + 1) as usize] = r;
        packet[(header_len + (i*4) + 2) as usize] = b;
        packet[(header_len + (i*4) + 3) as usize] = w;
Andre Julius's avatar
Andre Julius committed
23
    }
24
25

    return sock.send_to(&packet, &address);
Andre Julius's avatar
Andre Julius committed
26
27
}

Andre Julius's avatar
init  
Andre Julius committed
28
29

fn main() {
Andre Julius's avatar
Andre Julius committed
30
    let socket = UdpSocket::bind("0.0.0.0:10400").expect(
31
        "Could not setup socket");
32
    drop(socket.set_broadcast(true));
Andre Julius's avatar
Andre Julius committed
33
    let address: String;
Andre Julius's avatar
Andre Julius committed
34
    let sequence_number: u8;
35
36
37
    let hostid: u8;
    let offset: u8;
    let number_of_leds: u16;
Andre Julius's avatar
Andre Julius committed
38
39
40
41
    let red: u8;
    let green: u8;
    let blue: u8;
    let white: u8;
42

Andre Julius's avatar
Andre Julius committed
43
    let matches = App::new("simple-led-control")
44
                            .version(env!("CARGO_PKG_VERSION"))
Andre Julius's avatar
Andre Julius committed
45
46
47
48
49
50
                            .author("Andre Julius <noromoron@gmail.com>")
                            .about("Control led strips")
                            .arg(Arg::with_name("verbose")
                                .short("v")
                                .long("verbose")
                                .multiple(true)
51
                                .help("Show some debugging prints. Maximum level is 2."))
Andre Julius's avatar
Andre Julius committed
52
53
54
                            .arg(Arg::with_name("sequence")
                                .short("s")
                                .long("sequence-number")
55
                                .help("Sets the sequence number. Ranges from 0 to 255.")
Andre Julius's avatar
Andre Julius committed
56
                                .takes_value(true))
57
58
59
                            .arg(Arg::with_name("hostid")
                                .short("h")
                                .long("hostid")
60
                                .help("Sets the hostid. Ranges from 0 to 255. At a value of 0 the flag is ignored.")
61
62
63
64
                                .takes_value(true))
                            .arg(Arg::with_name("offset")
                                .short("o")
                                .long("offset")
65
                                .help("Sets the offset. Ranges from 0 to 255.")
66
67
68
69
                                .takes_value(true))
                            .arg(Arg::with_name("number")
                                .short("n")
                                .long("number-of-leds")
70
                                .help("Sets the number of leds to fill. No range limit.")
71
                                .takes_value(true))
Andre Julius's avatar
Andre Julius committed
72
73
74
                            .arg(Arg::with_name("red")
                                .short("r")
                                .long("color-red")
75
                                .help("Sets the red value. Ranges from 0 to 255. All values greater 255 are set to 0.")
Andre Julius's avatar
Andre Julius committed
76
77
78
79
                                .takes_value(true))
                            .arg(Arg::with_name("green")
                                .short("g")
                                .long("color-green")
80
                                .help("Sets the green value. Ranges from 0 to 255. All values greater 255 are set to 0.")
Andre Julius's avatar
Andre Julius committed
81
82
83
84
                                .takes_value(true))
                            .arg(Arg::with_name("blue")
                                .short("b")
                                .long("color-blue")
85
                                .help("Sets the blue value. Ranges from 0 to 255. All values greater 255 are set to 0.")
Andre Julius's avatar
Andre Julius committed
86
87
88
89
                                .takes_value(true))
                            .arg(Arg::with_name("white")
                                .short("w")
                                .long("color-white")
90
                                .help("Sets the white value. Ranges from 0 to 255. All values greater 255 are set to 0.")
Andre Julius's avatar
Andre Julius committed
91
92
                                .takes_value(true))
                            .arg(Arg::with_name("address")
93
                                .help("Set the address of the led strip. hostname:port")
Andre Julius's avatar
Andre Julius committed
94
95
96
                                .required(true)
                                .index(1))
                            .get_matches();
Andre Julius's avatar
Andre Julius committed
97

Andre Julius's avatar
Andre Julius committed
98
99
    address = value_t!(matches.value_of("address"), String).unwrap();

Andre Julius's avatar
Andre Julius committed
100
    sequence_number = value_t!(matches.value_of("sequence"), u8).unwrap_or(0);
101
102
103
    hostid = value_t!(matches.value_of("hostid"), u8).unwrap_or(0);
    offset = value_t!(matches.value_of("offset"), u8).unwrap_or(0);
    number_of_leds = value_t!(matches.value_of("number"), u16).unwrap_or(0);
Andre Julius's avatar
Andre Julius committed
104

Andre Julius's avatar
Andre Julius committed
105
    // Get all red, green and blue values
Andre Julius's avatar
Andre Julius committed
106
107
108
109
    red = value_t!(matches.value_of("red"),   u8).unwrap_or(0);
    green = value_t!(matches.value_of("green"), u8).unwrap_or(0);
    blue = value_t!(matches.value_of("blue"),  u8).unwrap_or(0);
    white = value_t!(matches.value_of("white"), u8).unwrap_or(0);
Andre Julius's avatar
Andre Julius committed
110

111
    match matches.occurrences_of("verbose") {
Andre Julius's avatar
Andre Julius committed
112
113
        0 => {},
        1 => {
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
            println!("Address:    {}", address);
            println!("Red:        {}", red);
            println!("Green:      {}", green);
            println!("Blue:       {}", blue);
            println!("White:      {}", white);
        },
        2 | _ => {
            println!("Address:    {}", address);
            println!("Red:        {}", red);
            println!("Green:      {}", green);
            println!("Blue:       {}", blue);
            println!("White:      {}", white);
            println!("Sequence:   {}", sequence_number);
            println!("Offset:     {}", offset);
            println!("Hostid:     {}", hostid);
            println!("Led count:  {}", number_of_leds);
Andre Julius's avatar
Andre Julius committed
130
131
132
        },
    }

Andre Julius's avatar
Andre Julius committed
133
134
    match send(&socket, &String::from(address), sequence_number, hostid,
        offset, number_of_leds, red, green, blue, white) {
135
136
137
138
139
140
141
142
        Err(e) => {
            match e.get_ref() {
                Some(inner_e) => println!("Error: {}", inner_e.description()),
                None => println!("{:?}", e),
            }
        },
        Ok(u) => println!("Bytes sent: {}", u),
    };
Andre Julius's avatar
Andre Julius committed
143
}