Skip to content

Commit 8555d4c

Browse files
committed
Clean up
1 parent bbb8e81 commit 8555d4c

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ I started the project using the Espressif IDF (a powerful tool indeed) but switc
1919
ecosystem.
2020
- I chose the `#[no_std]` (i.e. no Espressif IDF) option, mostly to feel out the ecosystem in a pure Rust world. This
2121
is an art piece after all.
22+
- Async has some significant advantages, particularly with stack management and efficient concurrency so I have
23+
made some effort to use async interfaces wherever possible.
2224

2325
## Hardware Requirements
2426
This project is based around the ESP32 family of embedded microprocessors with some basic requirements and a standard
@@ -58,6 +60,13 @@ some of the builds. Running `just --list` will show all the available tasks.
5860

5961

6062

63+
## Useful links
64+
65+
- [ESP32-C6 esp_hal documention](https://docs.esp-rs.org/esp-hal/esp-hal/0.23.1/esp32c6/esp_hal/)
66+
- [Rust on ESP book](https://docs.esp-rs.org/book/)
67+
-
68+
69+
6170
# TODO
6271

6372
- [ ] Set up hardware targets for the ESP32-S3 and ESP32-H2 targets

src/display_task.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
use embassy_futures::select::{select, Either};
1+
use crate::led_driver::LedDriver;
2+
use embassy_futures::select::{Either, select};
23
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
3-
use smart_leds::RGB8;
44
use embassy_sync::channel::Channel;
55
use embassy_time::{Duration, Ticker};
66
use log::info;
7-
use crate::led_driver::LedDriver;
7+
use smart_leds::RGB8;
88

99
/// Manage the display state by sending it messages of this type. If anyone asks why I like Rust,
1010
/// this is one of the many reasons
1111
pub enum DisplayState {
1212
/// Suspends animation update
13-
Stop,
13+
Stop,
1414
/// Restart animation update
1515
Start,
1616
/// Set the pixel colour. It is always the 1st pixel. Boring but....
1717
Colour(RGB8),
1818
/// Switch of all the LEDs
1919
Off,
2020
/// Sets the led to torch mode. This disables the animation
21-
Torch(u8)
21+
Torch(u8),
2222
}
2323

2424
/// The channel type detail
2525
pub type DisplayControlChannel = Channel<CriticalSectionRawMutex, DisplayState, 3>;
2626

27-
2827
/// Display driver main task.
2928
/// The display is fully managed from this task. It contains the state and responds to messages
3029
/// sent to it via the channel.
3130
#[embassy_executor::task]
3231
pub async fn display_task(channel: &'static DisplayControlChannel, led: &'static mut LedDriver) {
3332
let mut ticker = Ticker::every(Duration::from_millis(100));
34-
let mut running = false;
33+
let mut running = true;
3534
info!("DISPLAY_TASK: Task started. Waiting for messages...");
36-
led.update_string();
3735
loop {
3836
match select(ticker.next(), channel.receive()).await {
39-
Either::First(_) => { // The ticker woke us up
37+
Either::First(_) => {
38+
// The ticker woke us up
4039
if running {
4140
led.rotate_left();
4241
led.update_string();
4342
}
4443
}
45-
Either::Second(message) => { // We received a message
44+
Either::Second(message) => {
45+
// We received a message
4646
use DisplayState::*;
4747
match message {
4848
Stop => running = false,
@@ -51,21 +51,19 @@ pub async fn display_task(channel: &'static DisplayControlChannel, led: &'static
5151
led.all_off();
5252
led.buffer[0] = c;
5353
led.update_string();
54-
},
54+
}
5555
Off => {
5656
led.all_off();
5757
led.update_string();
5858
running = false;
59-
},
59+
}
6060
Torch(c) => {
6161
led.torch(c);
6262
led.update_string();
6363
running = false;
6464
}
65-
66-
6765
}
6866
}
6967
};
7068
}
71-
}
69+
}

src/led_driver.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,20 @@ impl LedDriver {
5353
pub fn rotate_right(&mut self) {
5454
self.buffer.rotate_left(1)
5555
}
56-
56+
5757
/// Switches all the LEDS off
5858
#[allow(unused)]
5959
pub fn all_off(&mut self) {
6060
self.buffer.fill(RGB8 { r: 0, g: 0, b: 0 });
6161
}
62-
63-
/// Switches all the LEDS to white at the specified brightness.
64-
pub fn torch(&mut self, brightness: u8) {
65-
self.buffer.fill(RGB8 { r: brightness, g: brightness, b: brightness });
62+
63+
/// Switches all the LEDS to white at the specified brightness.
64+
#[allow(unused)]
65+
pub fn torch(&mut self, brightness: u8) {
66+
self.buffer.fill(RGB8 {
67+
r: brightness,
68+
g: brightness,
69+
b: brightness,
70+
});
6671
}
6772
}

src/main.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
holding buffers for the duration of a data transfer."
77
)]
88

9-
extern crate alloc;
10-
mod led_driver;
119
mod display_task;
10+
mod led_driver;
1211

12+
use crate::display_task::DisplayState::*;
13+
use crate::display_task::{DisplayControlChannel, display_task};
1314
use crate::led_driver::LedDriver;
1415
use bt_hci::controller::ExternalController;
1516
use embassy_executor::Spawner;
@@ -18,14 +19,13 @@ use embassy_time::{Duration, Timer};
1819
use esp_hal::clock::CpuClock;
1920
use esp_hal::timer::systimer::SystemTimer;
2021
use esp_hal::timer::timg::TimerGroup;
22+
use esp_hal::{interrupt, peripherals};
2123
use esp_wifi::ble::controller::BleConnector;
2224
use log::info;
2325
#[cfg(feature = "log-rtt")]
2426
use rtt_target::rtt_init_log;
2527
use smart_leds::RGB8;
2628
use static_cell::StaticCell;
27-
use crate::display_task::{display_task, DisplayControlChannel};
28-
use crate::display_task::DisplayState::*;
2929

3030
/// Communicate with the display task using this channel and the DisplayState enum
3131
static DISPLAY_CHANNEL: StaticCell<DisplayControlChannel> = StaticCell::new();
@@ -62,34 +62,43 @@ async fn main(spawner: Spawner) {
6262
let timer0 = SystemTimer::new(peripherals.SYSTIMER);
6363
esp_hal_embassy::init(timer0.alarm0);
6464

65+
info!("MAIN: Enabling RMT ISR");
66+
interrupt::enable(peripherals::Interrupt::RMT, interrupt::Priority::Priority1).unwrap();
67+
6568
info!("MAIN: Embassy initialized!");
6669

6770
let rng = esp_hal::rng::Rng::new(peripherals.RNG);
6871
let timer1 = TimerGroup::new(peripherals.TIMG0);
6972
let wifi_init = esp_wifi::init(timer1.timer0, rng, peripherals.RADIO_CLK)
7073
.expect("MAIN: Failed to initialize WIFI/BLE controller");
71-
74+
7275
// find more examples https://github.com/embassy-rs/trouble/tree/main/examples/esp32
7376
let transport = BleConnector::new(&wifi_init, peripherals.BT);
7477
let _ble_controller = ExternalController::<_, 20>::new(transport);
7578

7679
info!("MAIN: Setting up LED driver controller");
7780
let display_channel = DISPLAY_CHANNEL.init(Channel::new());
78-
let led_driver: &'static mut LedDriver = LED_DRIVER.init(LedDriver::new(peripherals.RMT, peripherals.GPIO6));
79-
spawner.spawn(display_task(display_channel, led_driver)).expect("Failed to spawn display task");
80-
81+
let led_driver: &'static mut LedDriver =
82+
LED_DRIVER.init(LedDriver::new(peripherals.RMT, peripherals.GPIO6));
83+
84+
spawner
85+
.spawn(display_task(display_channel, led_driver))
86+
.expect("Failed to spawn display task");
87+
Timer::after(Duration::from_secs(1)).await;
88+
8189
// Simple example that exercises the display task
8290
loop {
83-
display_channel.send(Off).await;
84-
display_channel.send(Colour(RGB8::new(0,10,10))).await;
91+
info!("MAIN: Loop cycling");
92+
display_channel.send(Colour(RGB8::new(0, 10, 0))).await;
8593
display_channel.send(Start).await;
94+
info!("MAIN: Sent start message");
8695

8796
Timer::after(Duration::from_secs(10)).await;
8897
display_channel.send(Stop).await;
8998

9099
Timer::after(Duration::from_secs(1)).await;
91100
display_channel.send(Start).await;
92-
101+
93102
Timer::after(Duration::from_secs(1)).await;
94103
display_channel.send(Torch(10)).await;
95104

@@ -98,6 +107,4 @@ async fn main(spawner: Spawner) {
98107

99108
Timer::after(Duration::from_secs(1)).await;
100109
}
101-
102-
// for inspiration, have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-beta.1/examples/src/bin
103110
}

0 commit comments

Comments
 (0)