From 123e5a4ff67063eccc91eb3780ee0ac1dfd006ef Mon Sep 17 00:00:00 2001 From: Alex Eden Date: Wed, 5 Feb 2025 20:12:41 -0600 Subject: [PATCH] Update encoder --- examples/rotary_encoder_test.rs | 45 ++++++++++++++++++--------------- src/devices/rotary_encoder.rs | 4 ++- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/examples/rotary_encoder_test.rs b/examples/rotary_encoder_test.rs index ea688b9..b60787a 100644 --- a/examples/rotary_encoder_test.rs +++ b/examples/rotary_encoder_test.rs @@ -2,7 +2,11 @@ #![no_main] #![allow(incomplete_features)] #![feature(generic_const_exprs)] -use adafruit_seesaw::{devices::RotaryEncoder, prelude::*, SeesawRefCell}; +use adafruit_seesaw::{ + devices::{RotaryEncoder, RotaryEncoderColor}, + prelude::*, + SeesawRefCell, +}; use cortex_m_rt::entry; use rtt_target::{rprintln, rtt_init_print}; use stm32f4xx_hal::{gpio::GpioExt, i2c::I2c, pac, prelude::*, rcc::RccExt}; @@ -34,15 +38,14 @@ fn main() -> ! { let mut prev_position = 0; loop { let position = encoder.position(0).expect("Failed to get position"); + let c = color_wheel(((position & 0xFF) as u8).wrapping_mul(3)); if position != prev_position { prev_position = position; - rprintln!("Position changed to {}", position); + rprintln!("Position changed to {}, new color is {:?}", position, c); } - let c = color_wheel(((position & 0xFF) as u8).wrapping_mul(3)); - let Color(r, g, b) = c.set_brightness(255); encoder - .set_neopixel_color(r, g, b) + .set_neopixel_color(c) .and_then(|_| encoder.sync_neopixel()) .expect("Failed to set neopixel"); @@ -68,22 +71,22 @@ fn handle_panic(info: &core::panic::PanicInfo) -> ! { loop {} } -fn color_wheel(byte: u8) -> Color { +fn color_wheel(byte: u8) -> RotaryEncoderColor { match byte { - 0..=84 => Color(255 - byte * 3, 0, byte * 3), - 85..=169 => Color(0, (byte - 85) * 3, 255 - (byte - 85) * 3), - _ => Color((byte - 170) * 3, 255 - (byte - 170) * 3, 0), - } -} - -struct Color(pub u8, pub u8, pub u8); - -impl Color { - pub fn set_brightness(self, brightness: u8) -> Self { - Self( - ((self.0 as u16 * brightness as u16) >> 8) as u8, - ((self.1 as u16 * brightness as u16) >> 8) as u8, - ((self.2 as u16 * brightness as u16) >> 8) as u8, - ) + 0..=84 => RotaryEncoderColor { + r: 255 - byte * 3, + g: 0, + b: byte * 3, + }, + 85..=169 => RotaryEncoderColor { + r: 0, + g: (byte - 85) * 3, + b: 255 - (byte - 85) * 3, + }, + _ => RotaryEncoderColor { + r: (byte - 170) * 3, + g: 255 - (byte - 170) * 3, + b: 0, + }, } } diff --git a/src/devices/rotary_encoder.rs b/src/devices/rotary_encoder.rs index e1cf8c0..e344f93 100644 --- a/src/devices/rotary_encoder.rs +++ b/src/devices/rotary_encoder.rs @@ -12,10 +12,12 @@ seesaw_device! { modules: [ EncoderModule { num_encoders: 1, encoder_btn_pins: [24] }, GpioModule, - NeopixelModule> { num_leds: 1, pin: 6 }, + NeopixelModule { num_leds: 1, pin: 6 }, ] } +pub type RotaryEncoderColor = rgb::Grb; + impl SeesawDeviceInit for RotaryEncoder { fn init(mut self) -> Result { self.reset_and_verify_seesaw()