Commit 051ca096 authored by Astro's avatar Astro
Browse files

revise leds api

parent 02d06b8f
Pipeline #1380 passed with stage
in 7 minutes
...@@ -7,24 +7,45 @@ extern { ...@@ -7,24 +7,45 @@ extern {
fn leds_set_hsv(led: u8, h: f32, s: f32, v: f32); fn leds_set_hsv(led: u8, h: f32, s: f32, v: f32);
} }
pub const LEDS: u8 = 15; pub struct Leds;
pub fn init() { impl Leds {
unsafe { leds_init(); } pub fn new() -> Self {
} unsafe { leds_init(); }
Leds
}
pub fn update() { pub fn len(&self) -> usize {
unsafe { leds_update(); } 15
} }
pub fn led(&self, idx: usize) -> Led {
Led { idx }
}
pub fn set_dim(led: u8,dim: u8) { pub fn iter(&self) -> impl Iterator<Item=Led> {
unsafe { leds_set_dim(led, dim); } (0..self.len()).map(|idx| Led { idx })
}
pub fn update(&self) {
unsafe { leds_update(); }
}
} }
pub fn set(led: u8,r: u8,g: u8,b: u8) { pub struct Led {
unsafe { leds_set(led, r, g, b); } idx: usize,
} }
pub fn set_hsv(led: u8, h: f32, s: f32, v: f32) { impl Led {
unsafe { leds_set_hsv(led, h, s, v); } pub fn set_dim(&self, dim: u8) {
unsafe { leds_set_dim(self.idx as u8, dim); }
}
pub fn set(&self, r: u8, g: u8, b: u8) {
unsafe { leds_set(self.idx as u8, r, g, b); }
}
pub fn set_hsv(&self, h: f32, s: f32, v: f32) {
unsafe { leds_set_hsv(self.idx as u8, h, s, v); }
}
} }
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
#![no_main] #![no_main]
use panic_abort as _; use panic_abort as _;
use card10::{entry, lcd, leds}; use card10::{entry, lcd, leds::Leds};
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
card10::init(); card10::init();
leds::init(); let leds = Leds::new();
lcd::set_backlight(1000); lcd::set_backlight(1000);
let mut t = 0; let mut t = 0;
...@@ -24,10 +24,10 @@ fn main() -> ! { ...@@ -24,10 +24,10 @@ fn main() -> ! {
} }
lcd::update(); lcd::update();
for led in 0..leds::LEDS { for led in leds.iter() {
leds::set(led, ((t << 2) & 0xFF) as u8, ((t << 1) & 0xFF) as u8, ((t >> 2) & 0xFF) as u8); led.set(((t << 2) & 0xFF) as u8, ((t << 1) & 0xFF) as u8, ((t >> 2) & 0xFF) as u8);
} }
leds::update(); leds.update();
t += 1; t += 1;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment