netcode
This commit is contained in:
parent
763db5f983
commit
1e8208b1e8
3 changed files with 56 additions and 48 deletions
|
@ -58,6 +58,19 @@ fn panic( info: &PanicInfo) -> ! {
|
||||||
loop { }
|
loop { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_motor(pwm_c: &mut pwm::Config, pwm: &mut Pwm, pulse: u16) {
|
||||||
|
pwm_c.compare_b = pulse;
|
||||||
|
pwm.set_config(&pwm_c);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn open_door(mut pwm_c: &mut pwm::Config, mut pwm: &mut Pwm<'_>) {
|
||||||
|
run_motor(&mut pwm_c, &mut pwm, 0x13DF); // up
|
||||||
|
Timer::after_millis(3000).await;
|
||||||
|
run_motor(&mut pwm_c, &mut pwm, 0x123C); // down
|
||||||
|
Timer::after_millis(750).await;
|
||||||
|
run_motor(&mut pwm_c, &mut pwm, 4687); // stop
|
||||||
|
}
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(spawner: Spawner) {
|
async fn main(spawner: Spawner) {
|
||||||
|
|
||||||
|
@ -172,6 +185,14 @@ async fn main(spawner: Spawner) {
|
||||||
info!("power {pwr:?}");
|
info!("power {pwr:?}");
|
||||||
pwm.set_config(&c);
|
pwm.set_config(&c);
|
||||||
},
|
},
|
||||||
|
'B' => {
|
||||||
|
let mut buf: [u8;8] = [0;8];
|
||||||
|
buf.copy_from_slice(segs.next().unwrap());
|
||||||
|
let card = u64::from_ne_bytes(buf);
|
||||||
|
info!("card {card:#16x}");
|
||||||
|
|
||||||
|
open_door(&mut c, &mut pwm).await;
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,8 @@ license = "MIT OR Apache-2.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
embassy-executor = { version = "0.6.0", git="https://github.com/embassy-rs/embassy", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
|
embassy-executor = { version = "0.6.0", git="https://github.com/embassy-rs/embassy", features = ["task-arena-size-98304", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
|
||||||
embassy-time = { version = "0.3.2", git="https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
|
embassy-time = { version = "0.3.2", git="https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
|
||||||
embassy-rp = { version = "0.2.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] }
|
embassy-sync = { version = "0.6.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||||
|
embassy-rp = { version = "0.2.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040", "rt"] }
|
||||||
embassy-usb = { version = "0.3.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
embassy-usb = { version = "0.3.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||||
embassy-net = { version = "0.4.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns"] }
|
embassy-net = { version = "0.4.0", git="https://github.com/embassy-rs/embassy", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns"] }
|
||||||
embassy-net-wiznet = { version = "0.1.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
embassy-net-wiznet = { version = "0.1.0", git="https://github.com/embassy-rs/embassy", features = ["defmt"] }
|
||||||
|
|
|
@ -16,18 +16,21 @@ use core::sync::atomic::{AtomicU8};
|
||||||
|
|
||||||
use bt_hci::cmd::info;
|
use bt_hci::cmd::info;
|
||||||
use critical_section::Mutex;
|
use critical_section::Mutex;
|
||||||
|
use cyw43::JoinOptions;
|
||||||
use cyw43_pio::PioSpi;
|
use cyw43_pio::PioSpi;
|
||||||
use embassy_futures::join::join;
|
use embassy_futures::join::join;
|
||||||
use embassy_futures::yield_now;
|
use embassy_futures::yield_now;
|
||||||
use embassy_rp::interrupt::typelevel::{Handler, Interrupt, IO_IRQ_BANK0};
|
use embassy_rp::interrupt::typelevel::{Handler, Interrupt, IO_IRQ_BANK0};
|
||||||
use embassy_rp::multicore::{spawn_core1, Stack};
|
use embassy_rp::multicore::{spawn_core1, Stack};
|
||||||
use embassy_rp::pwm::{self, Pwm};
|
use embassy_rp::pwm::{self, Pwm};
|
||||||
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||||
|
use embassy_sync::channel::Channel;
|
||||||
use fixed::FixedU16;
|
use fixed::FixedU16;
|
||||||
use log::*;
|
use log::*;
|
||||||
//use embassy_rp::i2c::InterruptHandler;
|
//use embassy_rp::i2c::InterruptHandler;
|
||||||
use embassy_executor::{InterruptExecutor, Spawner};
|
use embassy_executor::{InterruptExecutor, Spawner};
|
||||||
use embassy_net::tcp::TcpSocket;
|
use embassy_net::tcp::TcpSocket;
|
||||||
use embassy_net::{Config, StackResources};
|
use embassy_net::{Config, IpEndpoint, Ipv4Address, StackResources};
|
||||||
use embassy_rp::{bind_interrupts, interrupt};
|
use embassy_rp::{bind_interrupts, interrupt};
|
||||||
use embassy_rp::clocks::RoscRng;
|
use embassy_rp::clocks::RoscRng;
|
||||||
use embassy_rp::gpio::{AnyPin, Input, InterruptTrigger, Level, Output};
|
use embassy_rp::gpio::{AnyPin, Input, InterruptTrigger, Level, Output};
|
||||||
|
@ -51,6 +54,8 @@ static READ_CARD: Mutex<RefCell<u64>> =
|
||||||
Mutex::new(RefCell::new(0));
|
Mutex::new(RefCell::new(0));
|
||||||
static BIT: AtomicU8 = AtomicU8::new(0);
|
static BIT: AtomicU8 = AtomicU8::new(0);
|
||||||
|
|
||||||
|
static CHANNEL: Channel<CriticalSectionRawMutex, u64, 1> = Channel::new();
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn logger_task(driver: Driver<'static, USB>) {
|
async fn logger_task(driver: Driver<'static, USB>) {
|
||||||
embassy_usb_logger::run!(1024, log::LevelFilter::Debug, driver);
|
embassy_usb_logger::run!(1024, log::LevelFilter::Debug, driver);
|
||||||
|
@ -80,6 +85,7 @@ async fn data_extractor() -> ! {
|
||||||
if bit == last_bit {
|
if bit == last_bit {
|
||||||
if card !=0 {
|
if card !=0 {
|
||||||
info!("read a card: {card:#16x}");
|
info!("read a card: {card:#16x}");
|
||||||
|
CHANNEL.send(card).await;
|
||||||
}
|
}
|
||||||
critical_section::with(|cs| {
|
critical_section::with(|cs| {
|
||||||
READ_CARD.replace(cs, 0);
|
READ_CARD.replace(cs, 0);
|
||||||
|
@ -134,7 +140,7 @@ async fn main(spawner: Spawner) {
|
||||||
|
|
||||||
// Use a link-local address for communication without DHCP server
|
// Use a link-local address for communication without DHCP server
|
||||||
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
|
let config = Config::ipv4_static(embassy_net::StaticConfigV4 {
|
||||||
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 1, 1), 16),
|
address: embassy_net::Ipv4Cidr::new(embassy_net::Ipv4Address::new(169, 254, 2, 1), 16),
|
||||||
dns_servers: heapless::Vec::new(),
|
dns_servers: heapless::Vec::new(),
|
||||||
gateway: None,
|
gateway: None,
|
||||||
});
|
});
|
||||||
|
@ -148,8 +154,20 @@ async fn main(spawner: Spawner) {
|
||||||
|
|
||||||
defmt::unwrap!(spawner.spawn(net_task(runner)));
|
defmt::unwrap!(spawner.spawn(net_task(runner)));
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match control
|
||||||
|
.join("door409", JoinOptions::new("babelite".as_bytes()))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => break,
|
||||||
|
Err(err) => {
|
||||||
|
info!("join failed with status={}", err.status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//control.start_ap_open("door409", 5).await;
|
//control.start_ap_open("door409", 5).await;
|
||||||
control.start_ap_wpa2("door409-outside", "outerbabes", 5).await;
|
//control.start_ap_wpa2("door409-outside", "outerbabes", 5).await;
|
||||||
|
|
||||||
let mut rx_buffer = [0; 4096];
|
let mut rx_buffer = [0; 4096];
|
||||||
let mut tx_buffer = [0; 4096];
|
let mut tx_buffer = [0; 4096];
|
||||||
|
@ -193,57 +211,25 @@ async fn main(spawner: Spawner) {
|
||||||
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let card = CHANNEL.receive().await;
|
||||||
socket.set_timeout(Some(Duration::from_secs(3)));
|
|
||||||
|
|
||||||
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||||
|
info!("attempting conn to inside");
|
||||||
control.gpio_set(0, false).await;
|
control.gpio_set(0, false).await;
|
||||||
info!("Listening on TCP:1234...");
|
if let Err(e) = socket.connect(IpEndpoint::new(Ipv4Address::new(169, 254, 1, 1).into_address(), 1234)).await {
|
||||||
if let Err(e) = socket.accept(1234).await {
|
warn!("connect error: {:?}", e);
|
||||||
warn!("accept error: {:?}", e);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("Received connection from {:?}", socket.remote_endpoint());
|
info!("Connected to {:?}", socket.remote_endpoint());
|
||||||
control.gpio_set(0, true).await;
|
control.gpio_set(0, true).await;
|
||||||
|
|
||||||
loop {
|
socket.write(b"C ").await.unwrap();
|
||||||
let mut n = match socket.read(&mut buf).await {
|
socket.write(&card.to_ne_bytes()).await.unwrap();
|
||||||
Ok(0) => {
|
info!("wrote to {:?}", socket.remote_endpoint());
|
||||||
warn!("read EOF");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Ok(n) => n,
|
|
||||||
Err(e) => {
|
|
||||||
warn!("read error: {:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
socket.close();
|
||||||
info!("rxd {}", from_utf8(&buf[..n]).unwrap());
|
control.gpio_set(0, false).await;
|
||||||
Timer::after_millis(30).await;
|
|
||||||
|
|
||||||
let mut segs = buf[..n].trim_ascii().split(|c| *c == ' ' as u8);
|
|
||||||
|
|
||||||
match char::from_u32(segs.next().unwrap()[0] as u32).unwrap() {
|
|
||||||
'D' => {
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//let mut response: [u8;2] = [0;2];
|
|
||||||
|
|
||||||
//let _ = bus.read_async(0xC0u16, &mut response).await;
|
|
||||||
//let _ = hex::encode_to_slice(response, &mut buf);
|
|
||||||
|
|
||||||
match socket.write_all(&buf[..n]).await {
|
|
||||||
Ok(()) => {}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("write error: {:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue