door/inside/src/lib.rs
2024-12-04 22:19:51 -05:00

69 lines
2.3 KiB
Rust

#![no_std]
#![no_main]
#![feature(const_trait_impl)]
use core::sync::atomic::AtomicU32;
use blocking_network_stack::Socket;
use embedded_hal::pwm::SetDutyCycle;
use embedded_io::Write;
use esp_hal::{delay::Delay, gpio::GpioPin, ledc::{channel::Channel, LowSpeed}, };
use esp_println::println;
use esp_wifi::wifi::{WifiDevice, WifiStaDevice};
//pub fn handle<T,U,const V: u8>(slice: &[u8], motor: PwmPin<T,U, V,true>) {
pub fn handle(slice: &[u8], motor: &mut Channel<LowSpeed, GpioPin<15>>, socket: &mut Socket<WifiDevice<WifiStaDevice>>) {
println!("dat: {:?}", slice);
let mut command = slice.trim_ascii().split(|c| *c == b' ');
// hate hate hate
static LAST_BADGE_LOW: AtomicU32 = AtomicU32::new(0);
static LAST_BADGE_HIG: AtomicU32 = AtomicU32::new(0);
match char::from_u32(command.next().unwrap()[0] as u32).unwrap() {
'D' => {
let hex = command.next().unwrap();
println!("{hex:#?}");
let buf: [u8;2] = hex::FromHex::from_hex(hex).unwrap();
let power: u16 = u16::from_be_bytes(buf);
println!("{power}");
motor.set_duty_cycle(power).unwrap();
},
'O' => {
open_door(motor);
}
'B' => {
let buf: [u8;8] = hex::FromHex::from_hex(command.next().unwrap()).unwrap();
let card: u64 = u64::from_ne_bytes(buf);
println!("card {card:#16x}");
LAST_BADGE_LOW.store(card as u32, core::sync::atomic::Ordering::SeqCst);
LAST_BADGE_HIG.store((card >> 32) as u32, core::sync::atomic::Ordering::SeqCst);
open_door(motor);
},
'G' => {
let mut last_badge: u64 = LAST_BADGE_LOW.load(core::sync::atomic::Ordering::SeqCst) as u64;
last_badge += (LAST_BADGE_HIG.load(core::sync::atomic::Ordering::SeqCst) as u64) << 32;
let _ = socket.write_fmt(
format_args!("B{last_badge}\r\n")
);
},
_ => {}
}
}
pub fn open_door(motor: &mut Channel<LowSpeed, GpioPin<15>>) {
let delay = Delay::new();
let _ = motor.set_duty_cycle(0x533);
delay.delay_millis(3000);
//Timer::after_millis(3000).await;
let _ = motor.set_duty_cycle(0x4c8);
delay.delay_millis(750);
let _ = motor.set_duty_cycle(0x4CD);
}