1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use anyhow::{anyhow, Context, Result};
use gpio_cdev::*;

const DEFAULT_BRIGHTNESS: u8 = 7;
const RED_INDEX: usize = 0;
const GREEN_INDEX: usize = 1;
const BLUE_INDEX: usize = 2;
const BRIGHTNESS_INDEX: usize = 3;
const DATA_LINE: u32 = 23;
const CLOCK_LINE: u32 = 24;

#[derive(Debug, Copy, Clone)]
struct Pixel {
    values: [u8; 4],
}

impl Pixel {
    pub fn rgb(&self) -> (u8, u8, u8) {
        (
            self.values[RED_INDEX],
            self.values[GREEN_INDEX],
            self.values[BLUE_INDEX],
        )
    }

    pub fn rgbb(&self) -> (u8, u8, u8, f32) {
        (
            self.values[RED_INDEX],
            self.values[GREEN_INDEX],
            self.values[BLUE_INDEX],
            f32::from(0b0001_1111 & self.values[BRIGHTNESS_INDEX]) / 31.0,
        )
    }

    pub fn set_brightness(&mut self, brightness: f32) {
        self.values[BRIGHTNESS_INDEX] = 0b1110_0000 | ((31.0 * brightness.max(0.0).min(1.0)) as u8);
    }

    pub fn set_rgb(&mut self, red: u8, green: u8, blue: u8) {
        self.values[RED_INDEX] = red;
        self.values[GREEN_INDEX] = green;
        self.values[BLUE_INDEX] = blue;
    }

    pub fn set_rgbb(&mut self, red: u8, green: u8, blue: u8, brightness: f32) {
        self.set_rgb(red, green, blue);
        self.set_brightness(brightness);
    }

    pub(crate) fn data(&self) -> &[u8] {
        &self.values
    }

    pub fn clear(&mut self) {
        self.set_rgb(0, 0, 0);
    }
}

impl Default for Pixel {
    fn default() -> Pixel {
        Pixel {
            values: [0, 0, 0, 0b1110_0000 | DEFAULT_BRIGHTNESS],
        }
    }
}

pub struct Blinkt {
    data_output_handle: LineHandle,
    clock_output_handle: LineHandle,
    clear_on_drop: bool,
    pixels: Vec<Pixel>,
}

impl Blinkt {
    pub fn new() -> Result<Blinkt> {
        let mut chip = Chip::new("/dev/gpiochip0").context("Failed to get gpio chip")?;

        let data_output = chip
            .get_line(DATA_LINE)
            .context("Failed to get data line")?;
        let data_output_handle = data_output
            .request(LineRequestFlags::OUTPUT, 0, "data-output")
            .context("Failed to get data output handle")?;

        let clock_output = chip
            .get_line(CLOCK_LINE)
            .context("Failed to get clock line")?;
        let clock_output_handle = clock_output
            .request(LineRequestFlags::OUTPUT, 0, "clock-output")
            .context("Failed to get clock output handle")?;
        Ok(Blinkt {
            data_output_handle,
            clock_output_handle,
            clear_on_drop: true,
            pixels: vec![Pixel::default(); 8],
        })
    }

    fn write_sof(&self) -> Result<()> {
        self.data_output_handle
            .set_value(0)
            .context("Failed to write start of sof")?;
        for _ in 0..32 {
            self.clock_output_handle
                .set_value(1)
                .context("Failed to write sof")?;

            self.clock_output_handle
                .set_value(0)
                .context("Failed to write sof")?;
        }
        Ok(())
    }

    fn write_eof(&self) -> Result<()> {
        self.data_output_handle
            .set_value(0)
            .context("Failed to write start of eof")?;
        for _ in 0..36 {
            self.clock_output_handle
                .set_value(1)
                .context("Failed to write sof")?;

            self.clock_output_handle
                .set_value(0)
                .context("Failed to write sof")?;
        }
        Ok(())
    }

    fn write_byte(&self, mut byte: u8) -> Result<()> {
        for x in 0..8 {
            self.data_output_handle
                .set_value(byte & 0b1000_0000)
                .with_context(|| format!("Failed to write bit \"{:?}\" of byte", x))?;
            self.clock_output_handle
                .set_value(1)
                .context("Failed to start clock pulse of writing bit")?;

            byte <<= 1;
            self.clock_output_handle
                .set_value(0)
                .context("Failed to end clock pulse of writing bit")?;
        }
        Ok(())
    }

    pub fn show(&self) -> Result<()> {
        self.write_sof()?;
        for pixel in &self.pixels {
            let data = pixel.data();
            // write brightness
            self.write_byte(0b1110_0000 | data[3])
                .context("Failed to write brightness byte")?;
            //write blue
            self.write_byte(data[2])
                .context("Failed to write blue byte")?;
            // write green
            self.write_byte(data[1])
                .context("Failed to write green byte")?;
            // write red
            self.write_byte(data[0])
                .context("Failed to write red byte")?;
        }
        self.write_eof()?;
        Ok(())
    }

    pub fn get_pixel(&mut self, pixel: usize) -> Result<(u8, u8, u8, f32)> {
        if let Some(pixel) = self.pixels.get_mut(pixel) {
            return Ok(pixel.rgbb());
        }
        Err(anyhow!("Failed to get data for pixel \"{:?}\"", pixel))
    }

    pub fn set_pixel(&mut self, pixel: usize, red: u8, green: u8, blue: u8, brightness: f32) {
        if let Some(pixel) = self.pixels.get_mut(pixel) {
            pixel.set_rgbb(red, green, blue, brightness);
        }
    }

    pub fn set_all_pixels(&mut self, red: u8, green: u8, blue: u8, brightness: f32) {
        for pixel in 0..8 {
            self.set_pixel(pixel, red, green, blue, brightness);
        }
    }

    pub fn clear(&mut self) {
        self.set_all_pixels(0, 0, 0, 0.2);
    }
}

impl Drop for Blinkt {
    fn drop(&mut self) {
        if self.clear_on_drop {
            self.clear();
            let _ = self.show();
        }
    }
}