@@ -35,6 +35,8 @@ use crate::games::snake;
3535use crate :: matrix:: * ;
3636#[ cfg( feature = "ledmatrix" ) ]
3737use crate :: patterns:: * ;
38+ #[ cfg( feature = "ledmatrix" ) ]
39+ use is31fl3741:: PwmFreq ;
3840
3941#[ cfg( feature = "c1minimal" ) ]
4042use smart_leds:: { SmartLedsWrite , RGB8 } ;
@@ -66,6 +68,7 @@ pub enum CommandVals {
6668 SetFps = 0x1A ,
6769 SetPowerMode = 0x1B ,
6870 AnimationPeriod = 0x1C ,
71+ PwmFreq = 0x1E ,
6972 Version = 0x20 ,
7073}
7174
@@ -125,6 +128,30 @@ pub enum DisplayMode {
125128 Hpm = 0x01 ,
126129}
127130
131+ #[ cfg( feature = "ledmatrix" ) ]
132+ #[ derive( Copy , Clone , num_derive:: FromPrimitive ) ]
133+ pub enum PwmFreqArg {
134+ /// 29kHz
135+ P29k = 0x00 ,
136+ /// 3.6kHz
137+ P3k6 = 0x01 ,
138+ /// 1.8kHz
139+ P1k8 = 0x02 ,
140+ /// 900Hz
141+ P900 = 0x03 ,
142+ }
143+ #[ cfg( feature = "ledmatrix" ) ]
144+ impl From < PwmFreqArg > for PwmFreq {
145+ fn from ( val : PwmFreqArg ) -> Self {
146+ match val {
147+ PwmFreqArg :: P29k => PwmFreq :: P29k ,
148+ PwmFreqArg :: P3k6 => PwmFreq :: P3k6 ,
149+ PwmFreqArg :: P1k8 => PwmFreq :: P1k8 ,
150+ PwmFreqArg :: P900 => PwmFreq :: P900 ,
151+ }
152+ }
153+ }
154+
128155// TODO: Reduce size for modules that don't require other commands
129156pub enum Command {
130157 /// Get current brightness scaling
@@ -175,6 +202,9 @@ pub enum Command {
175202 GetPowerMode ,
176203 SetAnimationPeriod ( u16 ) ,
177204 GetAnimationPeriod ,
205+ #[ cfg( feature = "ledmatrix" ) ]
206+ SetPwmFreq ( PwmFreqArg ) ,
207+ GetPwmFreq ,
178208 _Unknown,
179209}
180210
@@ -348,6 +378,13 @@ pub fn parse_module_command(count: usize, buf: &[u8]) -> Option<Command> {
348378 Some ( Command :: GetAnimationPeriod )
349379 }
350380 }
381+ Some ( CommandVals :: PwmFreq ) => {
382+ if let Some ( freq) = arg {
383+ FromPrimitive :: from_u8 ( freq) . map ( Command :: SetPwmFreq )
384+ } else {
385+ Some ( Command :: GetPwmFreq )
386+ }
387+ }
351388 _ => None ,
352389 }
353390 } else {
@@ -567,6 +604,16 @@ pub fn handle_command(
567604 response[ 0 ..2 ] . copy_from_slice ( & ( period_ms as u16 ) . to_le_bytes ( ) ) ;
568605 Some ( response)
569606 }
607+ Command :: SetPwmFreq ( arg) => {
608+ state. pwm_freq = * arg;
609+ matrix. device . set_pwm_freq ( state. pwm_freq . into ( ) ) . unwrap ( ) ;
610+ None
611+ }
612+ Command :: GetPwmFreq => {
613+ let mut response: [ u8 ; 32 ] = [ 0 ; 32 ] ;
614+ response[ 0 ] = state. pwm_freq as u8 ;
615+ Some ( response)
616+ }
570617 _ => handle_generic_command ( command) ,
571618 }
572619}
0 commit comments