1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{router::Router, Browser, Inputs, B};
use aimc_comms::Communications;
use aimc_fps_counter::FpsCounter;
use aimc_hal::System;
use aimc_motion::Motion;
use anpp::Packet;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Debug)]
pub struct App {
inputs: Inputs,
fps: FpsCounter,
comms: Communications,
motion: Motion,
}
impl App {
pub fn new(inputs: Inputs) -> Self {
let fps = FpsCounter::default();
let comms = Communications::new();
let motion = Motion::default();
App {
inputs,
fps,
comms,
motion,
}
}
pub fn poll(&mut self, browser: &Browser) {
self.inputs.begin_tick();
let mut browser = B(browser);
self.handle_comms(&mut browser);
self.fps.poll(&self.inputs, &mut browser);
self.inputs.end_tick();
}
fn handle_comms(&mut self, browser: &mut B) {
let mut router = Router {
fps: &mut self.fps,
motion: &mut self.motion,
};
let mut outputs = aimc_comms::Outputs::new(browser, &mut router);
self.comms.poll(&self.inputs, &mut outputs);
}
}
#[wasm_bindgen]
impl App {
pub fn on_data_received(&mut self, data: &[u8]) {
self.inputs.on_data_received(data);
}
pub fn echo(&mut self, text: &str) -> Result<(), JsValue> {
let pkt = Packet::with_data(42, text.as_bytes())
.map_err(|_| "The input text is too long")?;
let mut buffer = [0; Packet::MAX_PACKET_SIZE + 5];
let bytes_written = pkt
.write_to_buffer(&mut buffer)
.map_err(|_| "Unable to write the packet to a buffer")?;
self.on_data_received(&buffer[..bytes_written]);
Ok(())
}
}