From 556e920981548785106fd6e66c739631cd97f92f Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 5 Jan 2026 08:57:24 +0800 Subject: [PATCH] add a demo of captouch + ws2812 composed BIO apps --- apps-dabao/dabao-console/src/cmds/ws2812.rs | 78 ++++++++++++++++++++- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/apps-dabao/dabao-console/src/cmds/ws2812.rs b/apps-dabao/dabao-console/src/cmds/ws2812.rs index 31875deea..cdde036cd 100644 --- a/apps-dabao/dabao-console/src/cmds/ws2812.rs +++ b/apps-dabao/dabao-console/src/cmds/ws2812.rs @@ -1,18 +1,27 @@ use std::time::{Duration, Instant}; +use bao1x_api::bio::IoConfigMode; use bio_lib::ws2812::rgb_to_u32; use crate::cmds::ShellCmdApi; const LED_BIO_PIN: u8 = 5; const INIT_STRIP_LENGTH: usize = 6; +const CAPTOUCH_PIN: u8 = 1; pub struct Ws2812 { len: usize, pin: arbitrary_int::u5, + captouch_pin: arbitrary_int::u5, } impl Ws2812 { - pub fn new() -> Self { Ws2812 { len: INIT_STRIP_LENGTH, pin: arbitrary_int::u5::new(LED_BIO_PIN) } } + pub fn new() -> Self { + Ws2812 { + len: INIT_STRIP_LENGTH, + pin: arbitrary_int::u5::new(LED_BIO_PIN), + captouch_pin: arbitrary_int::u5::new(CAPTOUCH_PIN), + } + } } impl<'a> ShellCmdApi<'a> for Ws2812 { @@ -34,9 +43,16 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { self.pin = arbitrary_int::u5::new(pin); } } + "captouch-pin" => { + if let Some(pin_str) = tokens.next() { + let pin = u8::from_str_radix(pin_str, 10).unwrap_or(LED_BIO_PIN); + log::info!("Setting input pin to BIO pin {}", pin); + self.captouch_pin = arbitrary_int::u5::new(pin); + } + } "rainbow" => { let mut ws2812 = - bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin).unwrap(); + bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin, None).unwrap(); let duration = Duration::from_secs(if let Some(duration) = tokens.next() { u64::from_str_radix(duration, 10).unwrap_or(u64::MAX) @@ -68,6 +84,61 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { env.ticktimer.sleep_ms(10).ok(); } } + "touch" => { + let mut captouch = + bio_lib::captouch::Captouch::new(self.captouch_pin, Some(IoConfigMode::Overwrite)) + .unwrap(); + let cal = captouch.calibrate(None); + log::info!("touch cal: {:?}", cal); + + let duration = Duration::from_secs(if let Some(duration) = tokens.next() { + u64::from_str_radix(duration, 10).unwrap_or(u64::MAX) + } else { + u64::MAX + }); + + let mut hues: Vec = Vec::new(); + for i in 0..self.len { + hues.push((i as f32 * 30.0) % 360.0); + } + let mut strip = vec![0u32; self.len]; + + let mut saturation = 0.8; + let mut touch_state = false; + + let mut ws2812 = bio_lib::ws2812::Ws2812::new( + bio_lib::ws2812::LedVariant::B, + self.pin, + Some(IoConfigMode::SetOnly), + ) + .unwrap(); + + let start_time = Instant::now(); + while Instant::now().duration_since(start_time).lt(&duration) { + // convert and update values + for (i, led) in hues.iter_mut().enumerate() { + let (r, g, b) = hsv_to_rgb(*led, saturation, 0.05); + // Pack into 24-bit RGB value + let rgb_value: u32 = rgb_to_u32(r, g, b); + strip[i] = rgb_value; + *led += 1.0; + if *led >= 360.0 { + *led = 0.0; + } + } + // send + ws2812.send(&strip); + + env.ticktimer.sleep_ms(20).ok(); + if !touch_state & captouch.is_touched() { + saturation = saturation + 0.2; + if saturation > 1.0 { + saturation = 0.0; + } + } + touch_state = captouch.is_touched(); + } + } "length" => { self.len = if let Some(len_str) = tokens.next() { usize::from_str_radix(len_str, 10).unwrap_or(INIT_STRIP_LENGTH) @@ -79,7 +150,8 @@ impl<'a> ShellCmdApi<'a> for Ws2812 { "hexcolor" => { if let Some(hexcolor) = tokens.next() { let mut ws2812 = - bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin).unwrap(); + bio_lib::ws2812::Ws2812::new(bio_lib::ws2812::LedVariant::B, self.pin, None) + .unwrap(); if hexcolor.len() != 7 || &hexcolor[0..1] != "#" { write!(ret, "Usage: 'hexcolor #rrggbb'; the # is mandatory").unwrap(); return Ok(Some(ret));