blink blue when online, blink green when receive message

This commit is contained in:
Jules Comte
2022-05-30 12:05:08 -04:00
parent 95d6f3105c
commit 6da291545f
6 changed files with 82 additions and 48 deletions

View File

@@ -36,7 +36,3 @@ anyhow = "1"
[[bin]]
name = "clear"
path = "src/clear.rs"
[[bin]]
name = "led"
path = "src/led.rs"

View File

@@ -1,4 +1,5 @@
use crate::conn::mqtt::RETURN_TOPIC;
use crate::periph::led::Led;
use esp_idf_svc::eventloop::*;
use embedded_svc::httpd::Result;
@@ -75,10 +76,12 @@ pub fn make_eventloop(client: Arc<Mutex<EspMqttClient<ConnState<MessageImpl, Esp
.. Default::default()
},
)?;
let mut green = Led::new(0x000100, 10);
info!("About to subscribe to the background event loop");
let subscription = eventloop.subscribe(move |message: &Message| {
info!("!!! Got message from the event loop"); //: {:?}", message.0);
green.blink();
let msg_str = message.read_string();
// let msg_str = String::from_utf8_lossy(&msg[..]);
match client.lock() {
@@ -97,4 +100,4 @@ pub fn make_eventloop(client: Arc<Mutex<EspMqttClient<ConnState<MessageImpl, Esp
// let subscription = eventloop.subscribe(cb)?;
Ok((eventloop, subscription))
}
}

View File

@@ -1,40 +0,0 @@
use core::time::Duration;
use embedded_hal::delay::blocking::DelayUs;
use esp_idf_hal::delay::Ets;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::rmt::config::TransmitConfig;
use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, Transmit};
fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
let peripherals = Peripherals::take().unwrap();
let led = peripherals.pins.gpio8.into_output()?;
let channel = peripherals.rmt.channel0;
let config = TransmitConfig::new().clock_divider(1);
let mut tx = Transmit::new(led, channel, &config)?;
let rgbs = [0x000000, 0x010001]; // grb?
loop {
for rgb in rgbs {
let ticks_hz = tx.counter_clock()?;
let t0h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(350))?;
let t0l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(800))?;
let t1h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(700))?;
let t1l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(600))?;
let mut signal = FixedLengthSignal::<24>::new();
for i in 0..24 {
let bit = 2_u32.pow(i) & rgb != 0;
let (high_pulse, low_pulse) = if bit { (t1h, t1l) } else { (t0h, t0l) };
signal.set(i as usize, &(high_pulse, low_pulse))?;
}
tx.start_blocking(&signal)?;
Ets.delay_ms(200)?;
}
}
}
fn ns(nanos: u64) -> Duration {
Duration::from_nanos(nanos)
}

View File

@@ -1,8 +1,10 @@
#![feature(once_cell)]
mod conn;
mod core;
mod periph;
use crate::core::{events::*, config::*};
use crate::periph::led::Led;
use sphinx_key_signer;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
@@ -42,10 +44,12 @@ fn main() -> Result<()> {
// the sub needs to publish back to mqtt???
let (eventloop, _sub) = make_eventloop(mqtt.clone())?;
let _mqtt_client = conn::mqtt::start_listening(mqtt, mqtt_and_conn.1, eventloop)?;
let mut blue = Led::new(0x000001, 100);
println!("{:?}", wifi.get_status());
for s in 0..60 {
log::info!("Shutting down in {} secs", 60 - s);
loop {
log::info!("Listening...");
blue.blink();
thread::sleep(Duration::from_secs(1));
}
drop(wifi);

View File

@@ -0,0 +1,70 @@
use core::time::Duration;
use embedded_hal::delay::blocking::DelayUs;
use esp_idf_hal::delay::Ets;
use esp_idf_hal::gpio::Gpio8;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::rmt::config::TransmitConfig;
use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, Transmit};
use std::lazy::SyncLazy;
use std::sync::Mutex;
static TX: SyncLazy<Mutex<Transmit<Gpio8<esp_idf_hal::gpio::Output>, esp_idf_hal::rmt::CHANNEL0>>> = SyncLazy::new(|| {
let peripherals = Peripherals::take().unwrap();
let led = peripherals.pins.gpio8.into_output().unwrap();
let channel = peripherals.rmt.channel0;
let config = TransmitConfig::new().clock_divider(1);
Mutex::new(Transmit::new(led, channel, &config).unwrap())
});
pub struct Led {
brg: u32,
blink_length: u32,
}
impl Led {
pub fn new(rgb: u32, blink_length: u32) -> Led {
Led {
brg: rotate_rgb(rgb),
blink_length,
}
}
pub fn blink(&mut self) {
let mut tx = TX.lock().unwrap();
// Prepare signal
let ticks_hz = (*tx).counter_clock().unwrap();
let t0h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(350)).unwrap();
let t0l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(800)).unwrap();
let t1h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(700)).unwrap();
let t1l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(600)).unwrap();
// Set led color
let mut signal = FixedLengthSignal::<24>::new();
for i in 0..24 {
let bit = 2_u32.pow(i) & self.brg != 0;
let (high_pulse, low_pulse) = if bit { (t1h, t1l) } else { (t0h, t0l) };
signal.set(i as usize, &(high_pulse, low_pulse)).unwrap();
}
// Set high and wait
(*tx).start_blocking(&signal).unwrap();
Ets.delay_ms(self.blink_length).unwrap();
// Set low
let mut signal = FixedLengthSignal::<24>::new();
for i in 0..24 {
let bit = 2_u32.pow(i) & 0x000000 != 0;
let (high_pulse, low_pulse) = if bit { (t1h, t1l) } else { (t0h, t0l) };
signal.set(i as usize, &(high_pulse, low_pulse)).unwrap();
}
(*tx).start_blocking(&signal).unwrap();
}
}
fn ns(nanos: u64) -> Duration {
Duration::from_nanos(nanos)
}
fn rotate_rgb(rgb: u32) -> u32 {
let b_mask: u32 = 0xff;
let blue = (rgb & b_mask) << 16;
blue | (rgb >> 8)
}

View File

@@ -0,0 +1 @@
pub mod led;