Skip to content

Commit

Permalink
Update encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeden committed Feb 6, 2025
1 parent 255b46b commit 123e5a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
45 changes: 24 additions & 21 deletions examples/rotary_encoder_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");

Expand All @@ -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,
},
}
}
4 changes: 3 additions & 1 deletion src/devices/rotary_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ seesaw_device! {
modules: [
EncoderModule { num_encoders: 1, encoder_btn_pins: [24] },
GpioModule,
NeopixelModule<color_type = rgb::Rgb<u8>> { num_leds: 1, pin: 6 },
NeopixelModule<color_type = RotaryEncoderColor> { num_leds: 1, pin: 6 },
]
}

pub type RotaryEncoderColor = rgb::Grb<u8>;

impl<D: Driver> SeesawDeviceInit<D> for RotaryEncoder<D> {
fn init(mut self) -> Result<Self, Self::Error> {
self.reset_and_verify_seesaw()
Expand Down

0 comments on commit 123e5a4

Please sign in to comment.