Skip to content

Commit

Permalink
rusty_hangulclock: use wps
Browse files Browse the repository at this point in the history
  • Loading branch information
suapapa committed Jan 15, 2025
1 parent 64e2336 commit 97f7047
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 44 deletions.
10 changes: 2 additions & 8 deletions rusty_hangulclock/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// use esp_idf_svc::wifi::{AsyncWifi, EspWifi};
use lazy_static::lazy_static;
use std::sync::{
mpsc::{self, Receiver, Sender},
// mpsc::{self, Receiver, Sender},
Mutex,
};

lazy_static! {
pub static ref WIFI_CONFIGURED: Mutex<bool> = Mutex::new(false);
pub static ref TIME_SYNCED: Mutex<bool> = Mutex::new(false);
// pub static ref WIFI: Mutex<Arc<AsyncWifi<EspWifi<'static>>>> = Mutex::new(None);

// mpsc 채널을 Mutex로 감싸서 전역으로 선언
pub static ref CHAN_NET: (Mutex<Sender<String>>, Mutex<Receiver<String>>) = {
let (tx, rx) = mpsc::channel();
(Mutex::new(tx), Mutex::new(rx))
};
pub static ref CMD_NET: Mutex<String> = Mutex::new(String::new());
}
20 changes: 18 additions & 2 deletions rusty_hangulclock/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use esp_idf_svc::hal::spi::{
config::Config as SpiConfig, config::DriverConfig as SpiDriverConfig, SpiBusDriver, SpiDriver,
};
use esp_idf_svc::hal::task;
use esp_idf_svc::sys::time;
use esp_idf_svc::timer::EspTaskTimerService;
use esp_idf_svc::wifi::{AsyncWifi, EspWifi};
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition};
Expand Down Expand Up @@ -101,12 +102,13 @@ fn main() -> anyhow::Result<()> {
)?;

// task::block_on(time_sync_loop(&mut wifi))?;
let time_sync_task = net::net_loop(&mut wifi, wifi_led);
let net_task = net::net_loop(&mut wifi, wifi_led);
let show_time_task = show_time_loop(&mut sleds);
let menu_task = menu::menu_loop(&mut disp, menu_sel);
let time_sync_task = time_sync_loop();

task::block_on(async {
match futures::try_join!(time_sync_task, show_time_task, menu_task) {
match futures::try_join!(net_task, show_time_task, menu_task, time_sync_task) {
Ok(_) => info!("All tasks completed"),
Err(e) => info!("Error in task: {:?}", e),
}
Expand All @@ -115,6 +117,17 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

async fn time_sync_loop() -> anyhow::Result<()> {
loop {
Timer::after(Duration::from_secs(60 * 60 * 24)).await; // 1 day
{
let mut cmd_net = global::CMD_NET.lock().unwrap();
*cmd_net = "NTP".to_string();
info!("NTP cmd sent");
}
}
}

async fn show_time_loop<SPI>(sleds: &mut Ws2812<SPI>) -> anyhow::Result<()>
where
SPI: embedded_hal::spi::SpiBus,
Expand All @@ -127,6 +140,9 @@ where
let time_synced = global::TIME_SYNCED.lock().unwrap();
if !*time_synced {
warn!("Time not synced yet");
let mut cmd_net = global::CMD_NET.lock().unwrap();
*cmd_net = "NTP".to_string();
info!("NTP cmd sent");
skip_loop = true;
}
}
Expand Down
20 changes: 11 additions & 9 deletions rusty_hangulclock/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ pub async fn menu_loop(
p_sel.wait_for_high().await.unwrap();
let ts_high = get_ts();
if ts_high - ts_low < 500 {
info!("short press");
sel_pressed = true;
menu = (menu + 1) % menu_max;
info!("sel press: {}", menu);
// sel_pressed = true;
decide_pressed = false;
} else {
info!("long press");
info!("enter press: {}", menu);
decide_pressed = true;
// sel_pressed = false;
}

disp.clear();
Expand All @@ -60,16 +63,15 @@ pub async fn menu_loop(
.draw(disp)?;
disp.flush().unwrap();

if sel_pressed {
menu = (menu + 1) % menu_max;
}

if decide_pressed {
match menu {
0 => {
info!("WPS selected");
let tx = global::CHAN_NET.0.lock().unwrap().clone();
tx.send("WPS".to_string()).unwrap();
// let tx = global::CHAN_NET.0.lock().unwrap().clone();
// tx.send("WPS".to_string()).unwrap();
let mut cmd_net = global::CMD_NET.lock().unwrap();
*cmd_net = "WPS".to_string();
info!("WPS cmd sent");
}
1 => {
info!("Menu 2 selected");
Expand Down
46 changes: 29 additions & 17 deletions rusty_hangulclock/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const WPS_CONFIG: WpsConfig = WpsConfig {
};

pub async fn connect_wps(wifi: &mut AsyncWifi<EspWifi<'static>>) -> anyhow::Result<()> {
let mut wifi_configured = global::WIFI_CONFIGURED.lock().unwrap();
// let mut wifi_configured = global::WIFI_CONFIGURED.lock().unwrap();

wifi.start().await?;
info!("Wifi started");
Expand Down Expand Up @@ -61,10 +61,13 @@ pub async fn connect_wps(wifi: &mut AsyncWifi<EspWifi<'static>>) -> anyhow::Resu
wifi.wait_netif_up().await?;
info!("Wifi netif up");

sync_time().await;
info!("Time synced");

wifi.stop().await?;
info!("Wifi stopped");

*wifi_configured = true;
// *wifi_configured = true;

Ok(())
}
Expand All @@ -84,21 +87,27 @@ pub async fn net_loop(

// wifi.set_configuration(&wifi_configuration)?;
debug_led.set_high().unwrap();
let rx = global::CHAN_NET.1.lock().unwrap();
for cmd in rx.iter() {
match cmd.as_str() {
// let rx = global::CHAN_NET.1.lock().unwrap();

loop {
let cmd_net: String;
{
cmd_net = global::CMD_NET.lock().unwrap().to_string();
}

match cmd_net.as_str() {
"WPS" => {
info!("Received WPS command");
connect_wps(wifi).await?;
}
"NTP" => {
info!("Received NTP command");
let wifi_configured = { global::WIFI_CONFIGURED.lock().unwrap() };
if !*wifi_configured {
info!("Connecting to wifi using WPS");
Timer::after(Duration::from_secs(3)).await;
continue;
}
// let wifi_configured = { global::WIFI_CONFIGURED.lock().unwrap() };
// if !*wifi_configured {
// info!("Connecting to wifi using WPS");
// Timer::after(Duration::from_secs(3)).await;
// continue;
// }

{
info!("Resetting time_synced");
Expand All @@ -120,26 +129,29 @@ pub async fn net_loop(
warn!("Failed to sync time");
}

sync_time().await;

wifi.stop().await?;
info!("Wifi stopped");
{
info!("Setting time_synced");
let mut time_synced = global::TIME_SYNCED.lock().unwrap();
*time_synced = sync_time_result;
}
sync_time().await;
}
_ => {
warn!("Unknown command: {}", cmd);
// warn!("Unknown command: \"{}\"", cmd_net);
}
}

if cmd_net.as_str() != "" {
let mut cmd_net = { global::CMD_NET.lock().unwrap() };
*cmd_net = "".to_string();
}

debug_led.set_low().unwrap();
// Timer::after(Duration::from_secs(60 * 60 * 24)).await;
// Timer::after(Duration::from_secs(30)).await;
Timer::after(Duration::from_secs(1)).await;
}

Ok(())
}

async fn sync_time() -> bool {
Expand Down
16 changes: 8 additions & 8 deletions rusty_hangulclock/src/panel_ws2812.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ws2812_spi::Ws2812;
use embedded_hal;
use smart_leds::{gamma, hsv::hsv2rgb, hsv::Hsv, SmartLedsWrite, RGB8};
use ws2812_spi::Ws2812;

const LED_NUM: usize = 25;
const DEFAULT_BRIGHTNESS: u8 = 100;
// const DEFAULT_BRIGHTNESS: u8 = 100;

pub fn welcome<SPI>(sleds: &mut Ws2812<SPI>)
where
Expand Down Expand Up @@ -124,17 +124,17 @@ where
}
if m10 + m1 != 0 {
match m10 {
1 => leds.extend(vec![5]), // 십
2 => leds.extend(vec![7, 2]), // 이십
3 => leds.extend(vec![6, 5]), // 삼십
4 => leds.extend(vec![0, 2]), // 사십
5 => leds.extend(vec![1, 2]), // 오십
1 => leds.extend(vec![5]), // 십
2 => leds.extend(vec![7, 2]), // 이십
3 => leds.extend(vec![6, 5]), // 삼십
4 => leds.extend(vec![0, 2]), // 사십
5 => leds.extend(vec![1, 2]), // 오십
_ => (),
}
if m1 == 5 {
leds.extend(vec![3, 4]); // 오분
} else {
leds.extend(vec![4]); // 분
leds.extend(vec![4]); // 분
}
}
show_leds(panel, leds);
Expand Down
Binary file removed sch/.DS_Store
Binary file not shown.

0 comments on commit 97f7047

Please sign in to comment.