88
99use desk_control_panel:: meeting_instruction:: { self , MeetingSignInstruction } ;
1010use embassy_executor:: Spawner ;
11+ use embassy_sync:: blocking_mutex:: raw:: CriticalSectionRawMutex ;
1112use embassy_sync:: mutex:: Mutex ;
1213use embassy_time:: { Duration , Timer } ;
13- // use esp_backtrace as _;
14- use embassy_sync:: blocking_mutex:: raw:: CriticalSectionRawMutex ;
1514use esp_hal:: {
1615 clock:: CpuClock ,
17- gpio:: { AnyPin , Input , InputConfig , Level , Output , OutputConfig } ,
16+ gpio:: { AnyPin , Level , Output , OutputConfig } ,
1817 timer:: systimer:: SystemTimer ,
1918 uart:: { Config , RxConfig , Uart } ,
2019 Async ,
2120} ;
2221use log:: { debug, error, info, trace, warn, LevelFilter } ;
22+ use micromath:: F32Ext ;
2323use static_cell:: StaticCell ;
2424
25+ const NUM_LEDS : usize = 9 ;
26+ const LED_PINS : [ u8 ; NUM_LEDS ] = [ 5 , 6 , 7 , 8 , 9 , 10 , 20 , 21 , 0 ] ;
27+
2528extern crate alloc;
2629
2730// This creates a default app-descriptor required by the esp-idf bootloader.
2831// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
2932esp_bootloader_esp_idf:: esp_app_desc!( ) ;
3033
31- const NUM_LEDS : usize = 9 ;
32-
3334// Global static for the panic pin
3435static mut PANIC_PIN : Option < Output < ' static > > = None ;
3536
@@ -52,7 +53,27 @@ impl<'a> LEDs<'a> {
5253 led. toggle ( ) ;
5354 }
5455 }
55- // pub fn set_portion_high()
56+
57+ pub fn set_portion_high ( & mut self , numerator : f32 , denominator : f32 ) {
58+ let num_on_leds = if numerator <= 0.0 || denominator <= 0.0 {
59+ warn ! (
60+ "Invalid portion values: numerator {}, denominator {}" ,
61+ numerator, denominator
62+ ) ;
63+ 0 // This will turn off all LEDs
64+ } else {
65+ ( NUM_LEDS as f32 * numerator / denominator) . round ( ) as usize
66+ } ;
67+
68+ for ( led_idx, led) in self . led_outs . iter_mut ( ) . enumerate ( ) {
69+ // if led_idx + 1 <= num_on_leds {
70+ if led_idx < num_on_leds {
71+ led. set_high ( ) ;
72+ } else {
73+ led. set_low ( ) ;
74+ }
75+ }
76+ }
5677}
5778
5879#[ esp_hal_embassy:: main]
@@ -194,16 +215,15 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
194215 }
195216}
196217
218+ // ESP32-C3 GPIO register addresses (from ESP32-C3 TRM Table 3.3-3 and Section 5.14.1)
219+ // GPIO base address: 0x60004000, offsets from Section 5.14.1:
220+ // https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf
221+ const GPIO_BASE_REG : u32 = 0x60004000 ;
222+ const GPIO_OUT_REG : * mut u32 = ( GPIO_BASE_REG + 0x0004 ) as * mut u32 ; // GPIO output register
223+ const GPIO_ENABLE_REG : * mut u32 = ( GPIO_BASE_REG + 0x0020 ) as * mut u32 ; // GPIO output enable register
224+
197225/// Emergency function to set GPIO pin low using direct register access
198226unsafe fn emergency_gpio3_low ( ) {
199- // ESP32-C3 GPIO register addresses (from ESP32-C3 TRM Table 3.3-3 and Section 5.14.1)
200- // GPIO base address: 0x60004000, offsets from Section 5.14.1:
201- // https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf
202-
203- const GPIO_BASE : u32 = 0x60004000 ;
204- const GPIO_OUT_REG : * mut u32 = ( GPIO_BASE + 0x0004 ) as * mut u32 ; // GPIO output register
205- const GPIO_ENABLE_REG : * mut u32 = ( GPIO_BASE + 0x0020 ) as * mut u32 ; // GPIO output enable register
206-
207227 const GPIO_PIN_NUMBER : u32 = 3 ;
208228
209229 // Enable GPIO pin as output
@@ -217,16 +237,6 @@ unsafe fn emergency_gpio3_low() {
217237
218238/// Panic function to set panic pattern on LEDs via GPIO pins
219239unsafe fn set_leds_panic_pattern ( ) {
220- // ESP32-C3 GPIO register addresses (from ESP32-C3 TRM Table 3.3-3 and Section 5.14.1)
221- // GPIO base address: 0x60004000, offsets from Section 5.14.1:
222- // https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf
223-
224- const GPIO_BASE : u32 = 0x60004000 ;
225- const GPIO_OUT_REG : * mut u32 = ( GPIO_BASE + 0x0004 ) as * mut u32 ; // GPIO output register
226- const GPIO_ENABLE_REG : * mut u32 = ( GPIO_BASE + 0x0020 ) as * mut u32 ; // GPIO output enable register
227-
228- const LED_PINS : [ u8 ; NUM_LEDS ] = [ 5 , 6 , 7 , 8 , 9 , 10 , 20 , 21 , 0 ] ;
229-
230240 for ( pin_number, turn_on) in LED_PINS . iter ( ) . zip ( [ true , false ] . iter ( ) . cycle ( ) ) {
231241 // Enable GPIO pin as output
232242 let enable_val = core:: ptr:: read_volatile ( GPIO_ENABLE_REG ) ;
0 commit comments