pass in peripherals to led control loop, no synclazy

This commit is contained in:
Evan Feenstra
2022-06-28 17:56:32 -07:00
parent 4355d610aa
commit 5a8d14770d
4 changed files with 34 additions and 24 deletions

View File

@@ -4,7 +4,8 @@ mod core;
mod periph;
use crate::core::{events::*, config::*};
use crate::periph::led::{Led, led_control_loop};
use crate::periph::led::led_control_loop;
use crate::periph::sd::sd_card;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use std::thread;
@@ -15,7 +16,7 @@ use anyhow::Result;
use esp_idf_svc::nvs::*;
use esp_idf_svc::nvs_storage::EspNvsStorage;
use embedded_svc::storage::Storage;
use embedded_svc::wifi::Wifi;
use esp_idf_hal::peripherals::Peripherals;
use sphinx_key_signer::lightning_signer::bitcoin::Network;
@@ -51,9 +52,15 @@ fn main() -> Result<()> {
thread::sleep(Duration::from_secs(1));
log::info!("Network set to {:?}", network);
let peripherals = Peripherals::take().unwrap();
let pins = peripherals.pins;
let (led_tx, led_rx) = mpsc::channel();
// LED control thread
led_control_loop(led_rx);
led_control_loop(pins.gpio8, peripherals.rmt.channel0, led_rx);
// sd card
sd_card(peripherals.spi2);
let default_nvs = Arc::new(EspDefaultNvs::new()?);
let mut store = EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage");
@@ -62,7 +69,7 @@ fn main() -> Result<()> {
println!("=============> START CLIENT NOW <============== {:?}", exist);
// store.remove("config").expect("couldnt remove config");
led_tx.send(Status::ConnectingToWifi).unwrap();
let wifi = start_wifi_client(default_nvs.clone(), &exist)?;
let _wifi = start_wifi_client(default_nvs.clone(), &exist)?;
let (tx, rx) = mpsc::channel();

View File

@@ -1,25 +1,14 @@
use crate::core::events::Status;
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::{gpio, rmt};
use esp_idf_hal::rmt::config::TransmitConfig;
use esp_idf_hal::rmt::{FixedLengthSignal, PinState, Pulse, Transmit};
use std::sync::mpsc;
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;
use std::collections::BTreeMap;
use std::sync::{LazyLock, Mutex};
static TX: LazyLock<Mutex<Transmit<Gpio8<esp_idf_hal::gpio::Output>, esp_idf_hal::rmt::CHANNEL0>>> = LazyLock::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())
});
type Color = u32;
type Time = u32;
@@ -40,7 +29,14 @@ fn states() -> BTreeMap<Status, (Color, Time)> {
s
}
pub fn led_control_loop(rx: mpsc::Receiver<Status>) {
pub fn led_control_loop(
gpio8: gpio::Gpio8<gpio::Unknown>,
channel0: rmt::CHANNEL0,
rx: mpsc::Receiver<Status>
) {
let led = gpio8.into_output().unwrap();
let config = TransmitConfig::new().clock_divider(1);
let transmit = Arc::new(Mutex::new(Transmit::new(led, channel0, &config).unwrap()));
thread::spawn(move || {
let mut led = Led::new(0x000001, 100);
let states = states();
@@ -51,7 +47,7 @@ pub fn led_control_loop(rx: mpsc::Receiver<Status>) {
led.set(s.0, s.1);
}
}
led.blink();
led.blink(transmit.clone());
thread::sleep(Duration::from_millis(400));
}
});
@@ -70,10 +66,10 @@ impl Led {
self.blink_length = blink_length;
}
pub fn blink(&mut self) {
let mut tx = TX.lock().unwrap();
pub fn blink(&mut self, transmit: Arc<Mutex<Transmit<gpio::Gpio8<gpio::Output>, rmt::CHANNEL0>>>) {
// Prepare signal
let ticks_hz = (*tx).counter_clock().unwrap();
let mut tx = transmit.lock().unwrap();
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();
@@ -86,7 +82,7 @@ impl Led {
signal.set(i as usize, &(high_pulse, low_pulse)).unwrap();
}
// Set high and wait
(*tx).start_blocking(&signal).unwrap();
tx.start_blocking(&signal).unwrap();
Ets.delay_ms(self.blink_length).unwrap();
// Set low
let mut signal = FixedLengthSignal::<24>::new();
@@ -95,7 +91,7 @@ impl Led {
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();
tx.start_blocking(&signal).unwrap();
}
}

View File

@@ -1 +1,2 @@
pub mod led;
pub mod sd;

View File

@@ -0,0 +1,6 @@
use esp_idf_hal::spi;
pub fn sd_card(_spi: spi::SPI2) {
log::info!("sd_card");
}