Compare commits

...

5 Commits

Author SHA1 Message Date
Philipp_EndevourOS
fc7bd8b089 still some errors with LED an levelshifter... 2025-08-13 02:36:25 +02:00
Philipp_EndevourOS
3117c55b1c LED is working and implement in feedback 2025-08-13 02:05:49 +02:00
Philipp_EndevourOS
593d98df74 test LED Array works.. 2025-08-13 01:51:00 +02:00
Philipp_EndevourOS
fa6d1f024c test LED Array works.. 2025-08-13 01:36:13 +02:00
Philipp_EndevourOS
36dc52f464 try to control LED with SmartLED and RMT 2025-08-13 00:55:25 +02:00
5 changed files with 167 additions and 25 deletions

54
Cargo.lock generated
View File

@@ -578,6 +578,19 @@ dependencies = [
"embedded-nal",
]
[[package]]
name = "embedded-sdmmc-dev"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ceec3a654b00a4acf9e2788d72b66fc3ba86d91b834586a4ed576324e351e38b"
dependencies = [
"byteorder",
"embedded-hal 1.0.0",
"embedded-io",
"heapless",
"log",
]
[[package]]
name = "embedded-storage"
version = "0.3.1"
@@ -761,6 +774,16 @@ dependencies = [
"termcolor",
]
[[package]]
name = "esp-hal-smartled"
version = "0.16.0"
source = "git+https://github.com/esp-rs/esp-hal-community.git?branch=main#bbe8484a013267132cd3fb4693606de2c8317a53"
dependencies = [
"document-features",
"esp-hal",
"smart-leds-trait",
]
[[package]]
name = "esp-metadata"
version = "0.8.0"
@@ -1025,17 +1048,21 @@ dependencies = [
"embassy-net",
"embassy-sync 0.7.0",
"embassy-time",
"embedded-hal 1.0.0",
"embedded-io",
"embedded-io-async",
"embedded-sdmmc-dev",
"esp-alloc",
"esp-bootloader-esp-idf",
"esp-hal",
"esp-hal-embassy",
"esp-hal-smartled",
"esp-println",
"esp-wifi",
"heapless",
"log",
"picoserve",
"smart-leds",
"smoltcp",
"static_cell",
]
@@ -1521,6 +1548,15 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
[[package]]
name = "rgb"
version = "0.8.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6a884d2998352bb4daf0183589aec883f16a6da1f4dde84d8e2e9a5409a1ce"
dependencies = [
"bytemuck",
]
[[package]]
name = "riscv"
version = "0.12.1"
@@ -1642,6 +1678,24 @@ version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "smart-leds"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66df34e571fa9993fa6f99131a374d58ca3d694b75f9baac93458fe0d6057bf0"
dependencies = [
"smart-leds-trait",
]
[[package]]
name = "smart-leds-trait"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edeb89c73244414bb0568611690dd095b2358b3fda5bae65ad784806cca00157"
dependencies = [
"rgb",
]
[[package]]
name = "smoltcp"
version = "0.12.0"

View File

@@ -18,6 +18,7 @@ embassy-net = { version = "0.7.0", features = [
"tcp",
"udp",
] }
embedded-hal = "=1.0.0"
embedded-io = "0.6.1"
embedded-io-async = "0.6.1"
esp-alloc = "0.8.0"
@@ -63,6 +64,11 @@ embassy-sync = { version = "0.7.0", features = ["log"] }
ds3231 = { version = "0.3.0", features = ["async", "temperature_f32"] }
chrono = { version = "0.4.41", default-features = false }
dir-embed = "0.3.0"
embedded-sdmmc-dev = "0.8.2"
esp-hal-smartled = { git = "https://github.com/esp-rs/esp-hal-community.git", package = "esp-hal-smartled", branch = "main", features = ["esp32c6"]}
smart-leds = "0.4.0"
[profile.dev]
# Rust debug is too slow.

View File

@@ -1,6 +1,11 @@
use embassy_time::{Delay, Duration, Timer};
use esp_hal::{delay, gpio::Output, peripherals};
use esp_hal::{delay, gpio::Output, peripherals, rmt::ConstChannelAccess};
use esp_hal_smartled::SmartLedsAdapterAsync;
use log::{debug, error, info};
use init::hardware;
use smart_leds::colors::{BLACK, GREEN, RED, YELLOW};
use smart_leds::{brightness, colors::BLUE};
use smart_leds::SmartLedsWriteAsync;
use crate::{FEEDBACK_STATE, init};
@@ -10,23 +15,28 @@ pub enum FeedbackState {
Nak,
Error,
Startup,
WIFI,
Idle,
}
const LED_LEVEL: u8 = 255;
#[embassy_executor::task]
pub async fn feedback_task(buzzer: peripherals::GPIO21<'static>) {
pub async fn feedback_task(mut led: SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, { init::hardware::LED_BUFFER_SIZE }>, buzzer: peripherals::GPIO21<'static>) {
debug!("Starting feedback task");
let mut buzzer = init::hardware::setup_buzzer(buzzer);
loop {
let feedback_state = FEEDBACK_STATE.wait().await;
match feedback_state {
FeedbackState::Ack => {
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
buzzer.set_high();
Timer::after(Duration::from_millis(100)).await;
buzzer.set_low();
Timer::after(Duration::from_millis(50)).await;
}
FeedbackState::Nak => {
led.write(brightness([YELLOW; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
buzzer.set_high();
Timer::after(Duration::from_millis(100)).await;
buzzer.set_low();
@@ -34,8 +44,10 @@ pub async fn feedback_task(buzzer: peripherals::GPIO21<'static>) {
buzzer.set_high();
Timer::after(Duration::from_millis(100)).await;
buzzer.set_low();
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
}
FeedbackState::Error => {
led.write(brightness([RED; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
buzzer.set_high();
Timer::after(Duration::from_millis(500)).await;
buzzer.set_low();
@@ -45,6 +57,7 @@ pub async fn feedback_task(buzzer: peripherals::GPIO21<'static>) {
buzzer.set_low();
}
FeedbackState::Startup => {
led.write(brightness([GREEN; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
buzzer.set_high();
Timer::after(Duration::from_millis(10)).await;
buzzer.set_low();
@@ -56,6 +69,10 @@ pub async fn feedback_task(buzzer: peripherals::GPIO21<'static>) {
buzzer.set_high();
Timer::after(Duration::from_millis(100)).await;
buzzer.set_low();
led.write(brightness([BLACK; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
}
FeedbackState::WIFI => {
led.write(brightness([BLUE; init::hardware::NUM_LEDS].into_iter(), LED_LEVEL)).await.unwrap();
}
FeedbackState::Idle => {
// Do nothing
@@ -65,6 +82,7 @@ pub async fn feedback_task(buzzer: peripherals::GPIO21<'static>) {
}
}
// async fn beep_ack() {
// buzzer.set_high();
// buzzer.set_low();

View File

@@ -1,24 +1,37 @@
use embassy_executor::Spawner;
use embassy_net::{Stack, driver};
use embassy_sync::blocking_mutex::Mutex;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use esp_hal::gpio::{Input, Pull};
use embassy_net::Stack;
use embassy_time::{Duration, Timer};
use embedded_sdmmc_dev::SdCard;
use esp_hal::i2c::master::Config;
use esp_hal::peripherals::{
self, GPIO0, GPIO1, GPIO2, GPIO21, GPIO22, GPIO23, GPIO16, GPIO17, GPIO20, GPIO10, GPIO19, GPIO6, GPIO7, I2C0,
UART1,
self, GPIO0, GPIO1, GPIO2, GPIO10, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22,
GPIO23, I2C0, RMT, SPI2, UART1,
};
use esp_hal::spi::master::Spi;
use esp_hal::spi::master::Config as Spi_config;
use esp_hal::rmt::{ConstChannelAccess, Rmt, Tx};
use esp_hal::spi::{
Mode,
master::{Config as Spi_config, Spi},
};
use esp_hal::time::Rate;
use esp_hal::timer::timg::TimerGroup;
use esp_hal::{
Async,
clock::CpuClock,
gpio::{Output, OutputConfig},
i2c::master::I2c,
timer::{systimer::SystemTimer, timg::TimerGroup},
timer::systimer::SystemTimer,
uart::Uart,
};
use esp_hal_smartled::{SmartLedsAdapterAsync, buffer_size_async};
use smart_leds::colors::{BLUE, GREEN, RED};
use smart_leds::{
RGB8, SmartLedsWriteAsync, brightness, gamma,
hsv::{Hsv, hsv2rgb},
};
use esp_println::logger::init_logger;
use log::{debug, error};
@@ -42,6 +55,9 @@ use crate::init::wifi;
*
*************************************************/
pub const NUM_LEDS: usize = 66;
pub const LED_BUFFER_SIZE: usize = NUM_LEDS * 25;
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
loop {
@@ -57,6 +73,7 @@ pub async fn hardware_init(
Uart<'static, Async>,
Stack<'static>,
I2c<'static, Async>,
SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE>,
GPIO21<'static>,
) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
@@ -77,33 +94,38 @@ pub async fn hardware_init(
let interfaces = wifi::setup_wifi(timer1.timer0, rng, peripherals.WIFI, spawner);
let stack = network::setup_network(network_seed, interfaces.ap, spawner);
Timer::after(Duration::from_millis(1)).await;
init_lvl_shifter(peripherals.GPIO0);
let uart_device = setup_uart(peripherals.UART1, peripherals.GPIO16, peripherals.GPIO17);
let i2c_device = setup_i2c(peripherals.I2C0, peripherals.GPIO22, peripherals.GPIO23);
let spi_device = setup_spi(
peripherals.SPI2,
peripherals.GPIO19,
peripherals.GPIO20,
peripherals.GPIO18,
peripherals.GPIO2,
);
let sd_card = setup_sdcard(spi_device);
let buzzer_gpio = peripherals.GPIO21;
let spi = match Spi::new(peripherals.SPI2 , Spi_config::default()) {
Ok(spi) => spi.with_mosi(peripherals.GPIO18).with_miso(peripherals.GPIO20),
Err(e) => {
error!("Failed to initialize I2C: {:?}", e);
panic!(); //TODO panic!
}
};
Timer::after(Duration::from_millis(500)).await;
let led = setup_led(peripherals.RMT, peripherals.GPIO1);
debug!("hardware init done");
(uart_device, stack, i2c_device, buzzer_gpio)
(uart_device, stack, i2c_device, led, buzzer_gpio)
}
// Initialize the level shifter for the NFC reader and LED (output-enable (OE) input is low, all outputs are placed in the high-impedance (Hi-Z) state)
fn init_lvl_shifter(oe_pin: GPIO0<'static>) {
let mut oe_lvl_shifter =
Output::new(oe_pin, esp_hal::gpio::Level::Low, OutputConfig::default());
Output::new(oe_pin, esp_hal::gpio::Level::Low, OutputConfig::default().with_drive_mode(esp_hal::gpio::DriveMode::PushPull).with_drive_strength(esp_hal::gpio::DriveStrength::_10mA));
oe_lvl_shifter.set_high();
}
@@ -140,6 +162,29 @@ fn setup_i2c(
i2c
}
fn setup_spi(
spi2: SPI2<'static>,
sck: GPIO19<'static>,
miso: GPIO20<'static>,
mosi: GPIO18<'static>,
cs: GPIO2<'static>,
) -> Spi<'static, Async> {
let spi = match Spi::new(spi2, Spi_config::default()) {
Ok(spi) => spi
.with_sck(sck)
.with_miso(miso)
.with_mosi(mosi)
.with_cs(cs)
.into_async(),
Err(e) => panic!("Failed to initialize SPI: {:?}", e),
};
spi
}
fn setup_sdcard(spi_device: Spi<'static, Async>) {
//let sdcard = SdCard::new(spi_device as embedded_hal::spi::SpiDevice(), delayer)
}
pub fn setup_buzzer(buzzer_gpio: GPIO21<'static>) -> Output<'static> {
let config = esp_hal::gpio::OutputConfig::default()
.with_drive_strength(esp_hal::gpio::DriveStrength::_40mA);
@@ -148,4 +193,23 @@ pub fn setup_buzzer(buzzer_gpio: GPIO21<'static>) -> Output<'static> {
buzzer
}
fn setup_spi_led() {}
fn setup_led(
rmt: RMT<'static>,
led_gpio: GPIO1<'static>,
) -> SmartLedsAdapterAsync<ConstChannelAccess<esp_hal::rmt::Tx, 0>, LED_BUFFER_SIZE> {
debug!("setup led");
let rmt: Rmt<'_, esp_hal::Async> = {
let frequency: Rate = Rate::from_mhz(80);
Rmt::new(rmt, frequency)
}
.expect("Failed to initialize RMT")
.into_async();
let rmt_channel = rmt.channel0;
let rmt_buffer = [0_u32; buffer_size_async(NUM_LEDS)];
let led: SmartLedsAdapterAsync<_, LED_BUFFER_SIZE> =
SmartLedsAdapterAsync::new(rmt_channel, led_gpio, rmt_buffer);
led
}

View File

@@ -30,7 +30,7 @@ type TallyPublisher = Publisher<'static, NoopRawMutex, TallyID, 8, 2, 1>;
#[esp_hal_embassy::main]
async fn main(mut spawner: Spawner) {
let (uart_device, stack, _i2c, buzzer_gpio) =
let (uart_device, stack, _i2c, _led, buzzer_gpio) =
init::hardware::hardware_init(&mut spawner).await;
wait_for_stack_up(stack).await;
@@ -53,7 +53,7 @@ async fn main(mut spawner: Spawner) {
));
debug!("spawing feedback task..");
spawner.must_spawn(feedback::feedback_task(buzzer_gpio));
spawner.must_spawn(feedback::feedback_task(_led, buzzer_gpio));
/******************************************************************************/
let mut sub = chan.subscriber().unwrap();