Skip to content

Commit 46953d5

Browse files
committed
init LEDs.set_portion_high
1 parent 73c0b58 commit 46953d5

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ esp-println = { version = "0.14.0", features = [
5353
"log-04",
5454
] }
5555
log = "0.4.27"
56+
micromath = "2.1.0"
5657
panic-rtt-target = { version = "0.2.0", features = ["defmt"] }
5758
postcard = { version = "1.1.1", features = ["use-defmt"] }
5859
rotary-encoder-hal = { version = "0.6.0", features = [

src/bin/meeting_sign.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@
88

99
use desk_control_panel::meeting_instruction::{self, MeetingSignInstruction};
1010
use embassy_executor::Spawner;
11+
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
1112
use embassy_sync::mutex::Mutex;
1213
use embassy_time::{Duration, Timer};
13-
// use esp_backtrace as _;
14-
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
1514
use 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
};
2221
use log::{debug, error, info, trace, warn, LevelFilter};
22+
use micromath::F32Ext;
2323
use 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+
2528
extern 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>
2932
esp_bootloader_esp_idf::esp_app_desc!();
3033

31-
const NUM_LEDS: usize = 9;
32-
3334
// Global static for the panic pin
3435
static 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
198226
unsafe 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
219239
unsafe 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

Comments
 (0)