From b8e13e0bc3c50a361f935c6753df4553e997e360 Mon Sep 17 00:00:00 2001 From: Alex Eden Date: Wed, 5 Feb 2025 20:02:02 -0600 Subject: [PATCH] Update neorotary4 --- ...ary_encoder_test.rs => neorotary4_test.rs} | 43 ++++++++++--------- src/devices/neorotary4.rs | 4 +- 2 files changed, 26 insertions(+), 21 deletions(-) rename examples/{quad_rotary_encoder_test.rs => neorotary4_test.rs} (73%) diff --git a/examples/quad_rotary_encoder_test.rs b/examples/neorotary4_test.rs similarity index 73% rename from examples/quad_rotary_encoder_test.rs rename to examples/neorotary4_test.rs index 77a34d3..461137a 100644 --- a/examples/quad_rotary_encoder_test.rs +++ b/examples/neorotary4_test.rs @@ -2,7 +2,11 @@ #![no_main] #![allow(incomplete_features)] #![feature(generic_const_exprs)] -use adafruit_seesaw::{devices::NeoRotary4, prelude::*, SeesawRefCell}; +use adafruit_seesaw::{ + devices::{NeoRotary4, NeoRotary4Color}, + 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}; @@ -37,14 +41,13 @@ fn main() -> ! { rprintln!("Position {} changed to {}", i, position); } let c = color_wheel(((position & 0xFF) as u8).wrapping_mul(3)); - let Color(r, g, b) = c.set_brightness(255); encoder - .set_nth_neopixel_color(i, r, g, b) + .set_nth_neopixel_color(i, c) .expect("Failed to set neopixel"); if let Ok(true) = encoder.button(i) { - rprintln!("Button {} pressed", i); + rprintln!("Button {} pressed; color is {:?}", i, c); encoder.set_position(i, 0).expect("Failed to set position"); } } @@ -68,22 +71,22 @@ fn handle_panic(info: &core::panic::PanicInfo) -> ! { loop {} } -fn color_wheel(byte: u8) -> Color { +fn color_wheel(byte: u8) -> NeoRotary4Color { 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 => NeoRotary4Color { + r: 255 - byte * 3, + g: 0, + b: byte * 3, + }, + 85..=169 => NeoRotary4Color { + r: 0, + g: (byte - 85) * 3, + b: 255 - (byte - 85) * 3, + }, + _ => NeoRotary4Color { + r: (byte - 170) * 3, + g: 255 - (byte - 170) * 3, + b: 0, + }, } } diff --git a/src/devices/neorotary4.rs b/src/devices/neorotary4.rs index 2de18d9..25eb53d 100644 --- a/src/devices/neorotary4.rs +++ b/src/devices/neorotary4.rs @@ -16,10 +16,12 @@ seesaw_device! { modules: [ EncoderModule { num_encoders: 4, encoder_btn_pins: [12, 14, 17, 9] }, GpioModule, - NeopixelModule> { num_leds: 4, pin: 18 } + NeopixelModule { num_leds: 4, pin: 18 } ] } +pub type NeoRotary4Color = rgb::Grb; + impl SeesawDeviceInit for NeoRotary4 { fn init(mut self) -> Result { self.reset_and_verify_seesaw()