Compare commits

..

No commits in common. "93861bb7ce90990a4e0e40b15760a9e763b989bb" and "ea1f1a335890d4803edc508d84e03637c1fda3fd" have entirely different histories.

7 changed files with 18 additions and 84 deletions

View file

@ -1,17 +0,0 @@
{
"lsp": {
"rust-analyzer": {
"initialization_options": {
"linkedProjects": [
"device-state/Cargo.toml",
"esp32/Cargo.toml",
"pico/Cargo.toml",
"simulator/Cargo.toml"
],
"check": {
"workspace": false
}
}
}
}
}

View file

@ -17,7 +17,7 @@ type DeviceSocketMessage =
type DeviceProxyEvent =
| PartySocketEvent
| { type: "device_connect_required" }
| { type: "device_connected"; username: string };
| { type: "device_connected" };
type DeviceQuizResponsePayload = {
QuizResponse: number;
@ -125,24 +125,12 @@ async function syncDeviceConnectionStatus(deviceId: string) {
return;
}
const userRecord = await db.query.user.findFirst({
where: { id: device.userId },
});
const username = userRecord?.name ?? device.userId;
console.log(
"[device-socket] device claimed",
deviceId,
device.userId,
username,
);
console.log("[device-socket] device claimed", deviceId, device.userId);
await db
.update(deviceConnection)
.set({ lastSeen: new Date() })
.where(eq(deviceConnection.id, deviceId));
sendDeviceEvent(deviceId, {
type: "device_connected",
username,
});
sendDeviceEvent(deviceId, { type: "device_connected" });
}
export async function claimDeviceForUser(deviceId: string, userId: string) {
@ -308,10 +296,7 @@ export const deviceClaimApp = new Elysia()
"/:deviceId/connect",
async ({ user, params }) => {
await claimDeviceForUser(params.deviceId, user.id);
sendDeviceEvent(params.deviceId, {
type: "device_connected",
username: user.name,
});
sendDeviceEvent(params.deviceId, { type: "device_connected" });
return { ok: true, deviceId: params.deviceId, userId: user.id };
},
{ auth: true },

View file

@ -66,7 +66,7 @@ type ErrorEvent = {
type DeviceLifecycleEvent =
| { type: "device_connect_required" }
| { type: "device_connected"; username: string };
| { type: "device_connected" };
type PartySocketEvent =
| PartyStatusEvent
@ -262,7 +262,7 @@ function handleApiMessage(e: MessageEvent) {
if (event.type === "device_connected") {
console.log("Writing waiting-for-party", message.deviceId);
writeProxyOutput(socket, { WaitingForParty: event.username });
writeProxyOutput(socket, "WaitingForParty");
return;
}

View file

@ -48,7 +48,7 @@ pub struct QuestionDataNet<'a> {
#[derive(Deserialize)]
pub enum ProxyOutput<'a> {
ConnectPrompt(&'a str),
WaitingForParty(&'a str),
WaitingForParty,
Question(QuestionDataNet<'a>),
Results,
Error(&'a str),
@ -87,7 +87,6 @@ impl WheelData {
pub struct DeviceState {
view: ViewState,
device_id: Option<OwnedStr<64>>,
connected_user_name: Option<OwnedStr<64>>,
question: Option<QuestionData>,
wheel: WheelData,
last_index: usize,
@ -99,7 +98,6 @@ impl DeviceState {
Self {
view: ViewState::Loading,
device_id: None,
connected_user_name: None,
question: None,
wheel: WheelData::empty(),
last_index: 0,
@ -109,7 +107,6 @@ impl DeviceState {
pub fn reset(&mut self) {
self.view = ViewState::Loading;
self.connected_user_name = None;
self.question = None;
self.wheel = WheelData::empty();
self.last_index = 0;
@ -117,7 +114,6 @@ impl DeviceState {
}
pub fn reconnecting(&mut self) {
self.connected_user_name = None;
self.question = None;
self.wheel = WheelData::empty();
self.view = ViewState::Reconnecting;
@ -148,14 +144,7 @@ impl DeviceState {
self.question = None;
self.view = ViewState::ConnectPrompt;
}
ProxyOutput::WaitingForParty(user_name) => {
let mut owned_user_name = OwnedStr::new();
for char in user_name.chars() {
if owned_user_name.try_push(char).is_err() {
break;
}
}
self.connected_user_name = Some(owned_user_name);
ProxyOutput::WaitingForParty => {
self.question = None;
self.view = ViewState::WaitingForParty;
}
@ -226,16 +215,9 @@ impl DeviceState {
}
if self.view == ViewState::WaitingForParty {
let user_name = self.connected_user_name.as_ref()?;
let mut display_name: OwnedStr<16> = OwnedStr::new();
for char in user_name.as_str().chars() {
if display_name.try_push(char).is_err() {
break;
}
}
return Some((
display_name,
OwnedStr::from_str("Awaiting party").unwrap(),
OwnedStr::from_str("Connected").unwrap(),
OwnedStr::from_str("Waiting party").unwrap(),
));
}
@ -245,10 +227,8 @@ impl DeviceState {
let question = self.question.as_ref()?;
let title_line = if question.text.len() > 16 {
// overscroll, should show spaces after the end
self.title_offset %= question.text.len() - 31;
let end = usize::min(self.title_offset + 16, question.text.len());
OwnedStr::from_str(&question.text[self.title_offset..end]).unwrap()
self.title_offset %= question.text.len() - 16;
OwnedStr::from_str(&question.text[self.title_offset..self.title_offset + 16]).unwrap()
} else {
OwnedStr::from_str(&question.text).unwrap()
};
@ -427,15 +407,15 @@ mod tests {
#[test]
fn parses_and_renders_waiting_for_party() {
let data = parse_proxy_output(r#"{"WaitingForParty":"User"}"#).unwrap();
let data = parse_proxy_output(r#""WaitingForParty""#).unwrap();
let mut state = DeviceState::new();
state.apply_proxy_output(data);
assert_eq!(state.view_state(), ViewState::WaitingForParty);
let (line1, line2) = state.render_lines().unwrap();
assert_eq!(line1.as_str(), "User");
assert_eq!(line2.as_str(), "Awaiting party");
assert_eq!(line1.as_str(), "Connected");
assert_eq!(line2.as_str(), "Waiting party");
}
#[test]

View file

@ -3,8 +3,6 @@
extern crate alloc;
use alloc::borrow::ToOwned;
use alloc::string::String;
use device_state::DeviceState;
use embassy_executor::Spawner;
use embassy_futures::select::{Either, select};
@ -135,9 +133,8 @@ pub async fn tcp_write_loop(
#[embassy_executor::task]
pub async fn main_loop() {
println!("Main loop started");
let mut last = (String::new(), String::new());
loop {
embassy_time::Timer::after_millis(100).await;
embassy_time::Timer::after_millis(50).await;
let mut state = STATE.lock().await;
state.tick();
let lines = state.render_lines();
@ -146,15 +143,7 @@ pub async fn main_loop() {
let Some((title_line, second_line)) = lines else {
continue;
};
let rendered = (
title_line.as_str().to_owned(),
second_line.as_str().to_owned(),
);
if rendered == last {
continue;
}
last = rendered;
println!("lcd1: {}\nlcd2: {}", title_line, second_line);
println!("lcd: {} {}", title_line, second_line);
screen::overwrite_lcd(&title_line, &second_line).await;
}
}

View file

@ -14,7 +14,7 @@ use esp_radio::wifi::sta::StationConfig;
use esp_radio::wifi::{Config, ControllerConfig, scan::ScanConfig};
use crate::screen::overwrite_lcd;
use crate::{WIFI_NETWORK, WIFI_PASSWORD, main_loop};
use crate::{WIFI_NETWORK, WIFI_PASSWORD};
use crate::{buffer::wait_for_config, tcp_read_loop, tcp_write_loop};
use crate::{reconnecting_state, reset_state};
@ -68,7 +68,6 @@ pub async fn network_setup_task(
seed,
);
spawner.spawn(net_task(runner).expect("spawn net task"));
spawner.spawn(main_loop().expect("spawn main loop"));
loop {
loop {

View file

@ -89,9 +89,7 @@ pub async fn lcd_display_task(config: LcdConfig) {
for byte in &buffer.line1 {
lcd.write(*byte);
}
Timer::after_micros(500).await;
lcd.set_position(0, 1);
Timer::after_micros(500).await;
for byte in &buffer.line2 {
lcd.write(*byte);
}