attempt to fix
This commit is contained in:
parent
37936d121d
commit
b2c4becbec
5 changed files with 75 additions and 47 deletions
|
|
@ -9,13 +9,18 @@ const socket = Bun.listen({
|
|||
hostname: "0.0.0.0",
|
||||
socket: {
|
||||
data(socket, buf) {
|
||||
const str = new TextDecoder().decode(buf);
|
||||
console.log("Data from", socket.remoteAddress, socket.remotePort)
|
||||
const str = new TextDecoder().decode(buf);
|
||||
console.log(str);
|
||||
},
|
||||
open(socket) {
|
||||
sockets.add(socket);
|
||||
open(socket) {
|
||||
console.log("Connection from", socket.remoteAddress, socket.remotePort)
|
||||
sockets.add(socket);
|
||||
socket.setKeepAlive(true);
|
||||
if (lastData) socket.write(lastData);
|
||||
},
|
||||
close(socket) {
|
||||
close(socket) {
|
||||
console.log("Connection closed", socket.remoteAddress, socket.remotePort)
|
||||
sockets.delete(socket);
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use core::{convert::Infallible, ops::Deref};
|
||||
use core::{convert::Infallible, fmt::Display, ops::Deref};
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use embassy_futures::yield_now;
|
||||
|
|
@ -6,6 +6,12 @@ use embassy_net::Stack;
|
|||
|
||||
pub struct WriteBuffer<const CAP: usize>(ArrayVec<u8, CAP>);
|
||||
|
||||
impl<const CAP: usize> Display for WriteBuffer<CAP> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.write_str(str::from_utf8(self).map_err(|_| core::fmt::Error)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const CAP: usize> Deref for WriteBuffer<CAP> {
|
||||
type Target = [u8];
|
||||
|
||||
|
|
|
|||
|
|
@ -8,19 +8,22 @@ use embassy_rp::{
|
|||
};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel, mutex::Mutex};
|
||||
use embassy_time::{Duration, Timer};
|
||||
use ufmt::uwrite;
|
||||
|
||||
use crate::{Irqs, WHEEL_VALUE, unwrap};
|
||||
use crate::{Irqs, WHEEL_VALUE, screen::SCREEN_BUFFER, unwrap};
|
||||
|
||||
/// Button input notification channel
|
||||
pub static INPUT: Channel<CriticalSectionRawMutex, u8, 64> = Channel::new();
|
||||
|
||||
#[embassy_executor::task]
|
||||
#[embassy_executor::task(pool_size = 4)]
|
||||
/// Polls a single pin for falling edge (assumes buttons are pulled up and shorted when pressed)
|
||||
/// Sends [INPUT] with given id
|
||||
pub async fn button_poll_task(mut button: Input<'static>, id: u8) {
|
||||
loop {
|
||||
button.wait_for_falling_edge().await;
|
||||
INPUT.send(id).await;
|
||||
// uwrite!(SCREEN_BUFFER.lock().await.line1(), "btn {}", id);
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -36,10 +39,12 @@ pub async fn rotation_read_task(
|
|||
let mut as5600 = As5600::new(i2c);
|
||||
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
let angle = as5600.angle().unwrap_or(0);
|
||||
let mut locked = ANGLE.lock().await;
|
||||
let old = *locked as i32;
|
||||
*locked = angle;
|
||||
drop(locked);
|
||||
let left_diff = old - angle as i32;
|
||||
let right_diff = angle as i32 - old;
|
||||
let diff = if left_diff.abs() < right_diff.abs() {
|
||||
|
|
@ -57,7 +62,7 @@ pub async fn rotation_read_task(
|
|||
wheel.value = wheel.max;
|
||||
}
|
||||
}
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
drop(wheel);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_sync::signal::Signal;
|
||||
use embedded_io::Write;
|
||||
use log::info;
|
||||
use owned_str::OwnedStr;
|
||||
use ufmt::uwrite;
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ use crate::owned_str_writer::{OwnedStrWriter, center_str};
|
|||
use crate::screen::{lcd_display_task, overwrite_lcd};
|
||||
use ag_lcd::{Blink, Cursor, Display, LcdDisplay};
|
||||
use defmt::*;
|
||||
use embassy_executor::Executor;
|
||||
use embassy_executor::{Executor, Spawner};
|
||||
use embassy_rp::gpio::{Level, Output};
|
||||
use embassy_rp::i2c::{self};
|
||||
use embassy_rp::peripherals::{DMA_CH0, I2C0, PIO0};
|
||||
|
|
@ -89,7 +90,7 @@ pub async fn tcp_read_loop(mut read: TcpReader<'_>) {
|
|||
|
||||
while let Ok(len) = read.read(&mut buf).await {
|
||||
if len == 0 {
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
let Ok(str) = str::from_utf8(&buf[..len]) else {
|
||||
continue;
|
||||
|
|
@ -177,8 +178,10 @@ pub async fn tcp_write_loop(mut write: TcpWriter<'_>) {
|
|||
let mut buffer = WriteBuffer::<256>::new();
|
||||
loop {
|
||||
let data = INPUT.receive().await;
|
||||
info!("button={}", data);
|
||||
let angle = *ANGLE.lock().await;
|
||||
core::writeln!(buffer, "button={} angle={}", data, angle).ok();
|
||||
info!("write: {}", &buffer);
|
||||
write.write(&buffer).await.ok();
|
||||
buffer.clear();
|
||||
}
|
||||
|
|
@ -192,6 +195,7 @@ const DOT: char = char::from_u32(0b1010_0101).unwrap();
|
|||
pub async fn main_loop() {
|
||||
let mut last_gen = 0;
|
||||
let mut title_offset = 0;
|
||||
info!("Main loop started");
|
||||
loop {
|
||||
Timer::after_millis(50).await;
|
||||
let wheel = *WHEEL_VALUE.lock().await;
|
||||
|
|
@ -213,49 +217,52 @@ pub async fn main_loop() {
|
|||
let number_str: OwnedStr<16> = match question.q_type {
|
||||
QuestionType::Choice => {
|
||||
let mut writer = OwnedStrWriter::new();
|
||||
writer.push(DOT);
|
||||
writer.push(' ');
|
||||
writer.push(DOT).unwrap();
|
||||
writer.push(' ').unwrap();
|
||||
uwrite!(writer, "{}", question.points).unwrap();
|
||||
writer.into()
|
||||
}
|
||||
QuestionType::Numeric { min, max } => {
|
||||
let mut writer = OwnedStrWriter::new();
|
||||
if wheel.value > min {
|
||||
writer.push(ARROW_LEFT);
|
||||
writer.push(' ');
|
||||
writer.push(ARROW_LEFT).unwrap();
|
||||
writer.push(' ').unwrap();
|
||||
}
|
||||
uwrite!(writer, "{}", wheel.value);
|
||||
uwrite!(writer, "{}", wheel.value).unwrap();
|
||||
if wheel.value < max {
|
||||
writer.push(ARROW_RIGHT);
|
||||
writer.push(' ');
|
||||
writer.push(ARROW_RIGHT).unwrap();
|
||||
writer.push(' ').unwrap();
|
||||
}
|
||||
|
||||
writer.into()
|
||||
}
|
||||
};
|
||||
let second_line = center_str::<16>(&number_str, 16).unwrap();
|
||||
overwrite_lcd(title_line, &second_line);
|
||||
info!("lcd: {} {}", title_line, second_line);
|
||||
overwrite_lcd(title_line, &second_line).await;
|
||||
}
|
||||
}
|
||||
|
||||
static mut CORE1_STACK: Stack<4096> = Stack::new();
|
||||
static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
|
||||
// static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
|
||||
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
|
||||
|
||||
#[cortex_m_rt::entry]
|
||||
fn main() -> ! {
|
||||
// #[cortex_m_rt::entry]
|
||||
|
||||
#[embassy_executor::main]
|
||||
async fn main(spawner: Spawner) -> () {
|
||||
let p = embassy_rp::init(Default::default());
|
||||
|
||||
let lcd: LcdDisplay<_, _> = LcdDisplay::new(
|
||||
Output::new(p.PIN_15, Level::Low),
|
||||
Output::new(p.PIN_14, Level::Low),
|
||||
Output::new(p.PIN_10, Level::Low),
|
||||
Output::new(p.PIN_11, Level::Low),
|
||||
Delay,
|
||||
)
|
||||
.with_half_bus(
|
||||
Output::new(p.PIN_13, Level::Low),
|
||||
Output::new(p.PIN_12, Level::Low),
|
||||
Output::new(p.PIN_11, Level::Low),
|
||||
Output::new(p.PIN_10, Level::Low),
|
||||
Output::new(p.PIN_13, Level::Low),
|
||||
Output::new(p.PIN_14, Level::Low),
|
||||
Output::new(p.PIN_15, Level::Low),
|
||||
)
|
||||
.with_autoscroll(ag_lcd::AutoScroll::Off)
|
||||
.with_display(Display::On)
|
||||
|
|
@ -277,25 +284,29 @@ fn main() -> ! {
|
|||
},
|
||||
);
|
||||
|
||||
let executor0 = EXECUTOR0.init(Executor::new());
|
||||
executor0.run(|spawner| {
|
||||
spawner.spawn(unwrap!(logger_task(p.USB)));
|
||||
spawner.spawn(unwrap!(network_setup_task(
|
||||
p.PIN_23, p.PIN_25, p.PIO0, p.PIN_24, p.PIN_29, p.DMA_CH0, spawner
|
||||
)));
|
||||
crate::input::setup(
|
||||
spawner,
|
||||
InputConfig {
|
||||
i2c0: p.I2C0,
|
||||
scl: p.PIN_5,
|
||||
sda: p.PIN_4,
|
||||
button_pins: [
|
||||
(p.PIN_18.into(), 1),
|
||||
(p.PIN_19.into(), 2),
|
||||
(p.PIN_20.into(), 3),
|
||||
(p.PIN_21.into(), 4),
|
||||
],
|
||||
},
|
||||
)
|
||||
});
|
||||
// let executor0 = EXECUTOR0.init(Executor::new());
|
||||
// executor0.run(|spawner| {
|
||||
spawner.spawn(unwrap!(logger_task(p.USB)));
|
||||
spawner.spawn(unwrap!(network_setup_task(
|
||||
p.PIN_23, p.PIN_25, p.PIO0, p.PIN_24, p.PIN_29, p.DMA_CH0, spawner
|
||||
)));
|
||||
// let mut lcd = lcd.build();
|
||||
// lcd.set_blink(Blink::Off);
|
||||
// lcd.set_cursor(Cursor::Off);
|
||||
// spawner.spawn(unwrap!(lcd_display_task(lcd)));
|
||||
crate::input::setup(
|
||||
spawner,
|
||||
InputConfig {
|
||||
i2c0: p.I2C0,
|
||||
scl: p.PIN_5,
|
||||
sda: p.PIN_4,
|
||||
button_pins: [
|
||||
(p.PIN_18.into(), 1),
|
||||
(p.PIN_19.into(), 2),
|
||||
(p.PIN_20.into(), 3),
|
||||
(p.PIN_21.into(), 4),
|
||||
],
|
||||
},
|
||||
);
|
||||
// });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,6 +120,7 @@ pub async fn network_setup_task(
|
|||
let mut tx_buffer = [0; 4096];
|
||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||
socket.set_keep_alive(Some(Duration::from_secs(1)));
|
||||
if let Err(e) = socket
|
||||
.connect(SocketAddrV4::new(host_addr, TARGET_PORT))
|
||||
.await
|
||||
|
|
|
|||
Loading…
Reference in a new issue