+41
-60
src/main.rs
+41
-60
src/main.rs
···
1
#![no_std]
2
#![no_main]
3
4
use badge_display::display_image::DisplayImage;
5
use badge_display::{
6
CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, FORCE_SCREEN_REFRESH, RECENT_WIFI_NETWORKS,
···
45
use serde::Deserialize;
46
use static_cell::StaticCell;
47
use temp_sensor::run_the_temp_sensor;
48
use {defmt_rtt as _, panic_probe as _};
49
50
mod badge_display;
···
122
let btn_a = Input::new(p.PIN_12, Pull::Down);
123
let btn_b = Input::new(p.PIN_13, Pull::Down);
124
let btn_c = Input::new(p.PIN_14, Pull::Down);
125
126
let spi = Spi::new(
127
p.SPI0,
···
156
);
157
158
//rtc setup
159
-
let mut rtc = embassy_rp::rtc::Rtc::new(p.RTC);
160
161
spawner.must_spawn(net_task(runner));
162
//Attempt to connect to wifi to get RTC time loop for 2 minutes
···
193
let i2c_dev = I2cDevice::new(i2c_bus);
194
let mut rtc_device = PCF85063::new(i2c_dev);
195
196
-
let mut time_was_set = false;
197
if connected_to_wifi {
198
info!("waiting for DHCP...");
199
while !stack.is_config_up() {
···
291
second: second as u8,
292
};
293
294
-
// // prepare date and time to be set
295
-
// let now = DateTime {
296
-
// year: 21, // 2021
297
-
// month: 4, // April
298
-
// weekday: 0, // Sunday
299
-
// day: 4,
300
-
// day_of_week: DayOfWeek::Sunday,
301
-
// hour,
302
-
// minute,
303
-
// hours: 16,
304
-
// minutes: 52,
305
-
// seconds: 00,
306
-
// second: 0,
307
-
// };
308
-
309
rtc_device
310
.set_datetime(&rtc_time)
311
.expect("TODO: panic message");
312
-
// rtc.set_datetime(rtc_time).unwrap();
313
-
time_was_set = true;
314
-
let _ = control.leave().await;
315
}
316
Err(_e) => {
317
error!("Failed to parse response body");
···
324
// return; // handle the error
325
}
326
};
327
}
328
329
//Set up saving
330
let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH3);
331
-
//TODO baaaaaad
332
let mut save =
333
read_postcard_from_flash(ADDR_OFFSET, &mut flash, SAVE_OFFSET).unwrap_or_else(|err| {
334
error!("Error getting the save from the flash: {:?}", err);
···
344
let cycle = Duration::from_millis(100);
345
let mut current_cycle = 0;
346
let mut time_to_scan = true;
347
-
//5 minutes(ish) idk it's late and my math is so bad rn
348
-
let reset_cycle = 3_000;
349
//Turn off led to signify that the badge is ready
350
user_led.set_low();
351
352
-
loop {
353
-
//TODO take out
354
-
Timer::after(Duration::from_millis(5_000)).await;
355
-
356
-
let time = rtc_device.get_datetime().unwrap();
357
-
info!("Hour: {:?}", time.hour());
358
-
info!("Minute: {:?}", time.minute());
359
-
info!("Second: {:?}", time.second());
360
361
-
// match rtc_device.get_datetime() {
362
-
// Ok(time) => {
363
-
// info!("RTC time: {:?}", time.as_utc().time().minute());
364
-
// }
365
-
// Err(err) => {
366
-
// error!("Error reading rtc");
367
-
// }
368
-
// }
369
-
370
//Change Image Button
371
if btn_c.is_high() {
372
info!("Button C pressed");
···
408
409
if btn_b.is_high() {
410
info!("Button B pressed");
411
-
412
SCREEN_TO_SHOW.lock(|screen| {
413
if *screen.borrow() == Screen::Badge {
414
//IF on badge screen and b pressed reset wifi count
···
455
continue;
456
}
457
458
-
if time_was_set {
459
-
let now = rtc.now();
460
-
match now {
461
-
Ok(time) => set_display_time(time),
462
-
Err(_) => {
463
-
info!("Error getting time");
464
-
}
465
}
466
-
} else {
467
-
RTC_TIME_STRING.lock(|rtc_time_string| {
468
-
rtc_time_string.borrow_mut().clear();
469
-
rtc_time_string.borrow_mut().push_str("No Wifi").unwrap();
470
-
});
471
-
}
472
if time_to_scan {
473
info!("Scanning for wifi networks");
474
time_to_scan = false;
···
489
}
490
}
491
492
-
fn set_display_time(time: DateTime) {
493
let mut am = true;
494
-
let twelve_hour = if time.hour > 12 {
495
am = false;
496
-
time.hour - 12
497
-
} else if time.hour == 0 {
498
12
499
} else {
500
-
time.hour
501
};
502
503
let am_pm = if am { "AM" } else { "PM" };
504
505
let formatted_time = easy_format::<8>(format_args!(
506
"{:02}:{:02} {}",
507
-
twelve_hour, time.minute, am_pm
508
));
509
510
RTC_TIME_STRING.lock(|rtc_time_string| {
···
1
#![no_std]
2
#![no_main]
3
4
+
use crate::pcf85063a::Control;
5
use badge_display::display_image::DisplayImage;
6
use badge_display::{
7
CHANGE_IMAGE, CURRENT_IMAGE, DISPLAY_CHANGED, FORCE_SCREEN_REFRESH, RECENT_WIFI_NETWORKS,
···
46
use serde::Deserialize;
47
use static_cell::StaticCell;
48
use temp_sensor::run_the_temp_sensor;
49
+
use time::PrimitiveDateTime;
50
use {defmt_rtt as _, panic_probe as _};
51
52
mod badge_display;
···
124
let btn_a = Input::new(p.PIN_12, Pull::Down);
125
let btn_b = Input::new(p.PIN_13, Pull::Down);
126
let btn_c = Input::new(p.PIN_14, Pull::Down);
127
+
let rtc_alarm = Input::new(p.PIN_8, Pull::Down);
128
129
let spi = Spi::new(
130
p.SPI0,
···
159
);
160
161
//rtc setup
162
+
// let mut rtc = embassy_rp::rtc::Rtc::new(p.RTC);
163
164
spawner.must_spawn(net_task(runner));
165
//Attempt to connect to wifi to get RTC time loop for 2 minutes
···
196
let i2c_dev = I2cDevice::new(i2c_bus);
197
let mut rtc_device = PCF85063::new(i2c_dev);
198
199
if connected_to_wifi {
200
info!("waiting for DHCP...");
201
while !stack.is_config_up() {
···
293
second: second as u8,
294
};
295
296
rtc_device
297
.set_datetime(&rtc_time)
298
.expect("TODO: panic message");
299
}
300
Err(_e) => {
301
error!("Failed to parse response body");
···
308
// return; // handle the error
309
}
310
};
311
+
//leave the wifi no longer needed
312
+
let _ = control.leave().await;
313
}
314
315
//Set up saving
316
let mut flash = embassy_rp::flash::Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH3);
317
let mut save =
318
read_postcard_from_flash(ADDR_OFFSET, &mut flash, SAVE_OFFSET).unwrap_or_else(|err| {
319
error!("Error getting the save from the flash: {:?}", err);
···
329
let cycle = Duration::from_millis(100);
330
let mut current_cycle = 0;
331
let mut time_to_scan = true;
332
+
//15 minutes(ish) idk it's late and my math is so bad rn
333
+
let reset_cycle = 9_000;
334
+
335
//Turn off led to signify that the badge is ready
336
user_led.set_low();
337
338
+
//RTC alarm stuff
339
+
// info!("going to sleep");
340
+
// Timer::after(Duration::from_millis(5_000)).await;
341
+
// //Set the rtc and sleep for 15 minutes
342
+
// //goes to sleep for 15 mins
343
+
// _ = rtc_device.clear_alarm_flag();
344
+
// _ = rtc_device.set_alarm_minutes(5);
345
+
// _ = rtc_device.control_alarm_minutes(Control::On);
346
+
// _ = rtc_device.control_alarm_interrupt(Control::On);
347
+
// power.set_low();
348
349
+
loop {
350
//Change Image Button
351
if btn_c.is_high() {
352
info!("Button C pressed");
···
388
389
if btn_b.is_high() {
390
info!("Button B pressed");
391
SCREEN_TO_SHOW.lock(|screen| {
392
if *screen.borrow() == Screen::Badge {
393
//IF on badge screen and b pressed reset wifi count
···
434
continue;
435
}
436
437
+
match rtc_device.get_datetime() {
438
+
Ok(now) => set_display_time(now),
439
+
Err(_err) => {
440
+
error!("Error getting time");
441
+
RTC_TIME_STRING.lock(|rtc_time_string| {
442
+
rtc_time_string.borrow_mut().clear();
443
+
rtc_time_string.borrow_mut().push_str("Error").unwrap();
444
+
});
445
}
446
+
};
447
+
448
if time_to_scan {
449
info!("Scanning for wifi networks");
450
time_to_scan = false;
···
465
}
466
}
467
468
+
fn set_display_time(time: PrimitiveDateTime) {
469
let mut am = true;
470
+
let twelve_hour = if time.hour() == 0 {
471
+
12
472
+
} else if time.hour() == 12 {
473
am = false;
474
12
475
+
} else if time.hour() > 12 {
476
+
am = false;
477
+
time.hour() - 12
478
} else {
479
+
time.hour()
480
};
481
482
let am_pm = if am { "AM" } else { "PM" };
483
484
let formatted_time = easy_format::<8>(format_args!(
485
"{:02}:{:02} {}",
486
+
twelve_hour,
487
+
time.minute(),
488
+
am_pm
489
));
490
491
RTC_TIME_STRING.lock(|rtc_time_string| {