Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ui-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,23 @@ pub enum Button {
B,
}

#[derive(Copy, Clone, Debug, PartialEq, Format)]
pub enum FromNet {
Pong,
}

#[derive(Copy, Clone, Debug, PartialEq, Format)]
pub enum ToNet {
Ping,
}

#[derive(Copy, Clone, Debug, PartialEq, Format)]
pub enum Event {
ButtonDown(Button),
ButtonUp(Button),
KeyDown(Key, KeyValue),
KeyUp(Key),
FromNet(FromNet),
}

#[allow(dead_code)]
Expand Down Expand Up @@ -131,9 +142,14 @@ pub trait Screen {
fn draw(&mut self, left: usize, right: usize, top: usize, bottom: usize, data: &[u16]);
}

pub trait NetTx {
fn write(&mut self, to_net: &ToNet);
}

pub trait Outputs {
fn status_led(&mut self) -> &mut impl Led;
fn screen(&mut self) -> &mut impl Screen;
fn net_tx(&mut self) -> &mut impl NetTx;
fn log(&mut self, message: &str);
}

Expand Down Expand Up @@ -204,18 +220,23 @@ impl App {
match event {
Event::ButtonDown(button) => match button {
Button::A => {
out.log("button a down");
self.a_down = true;
out.net_tx().write(&ToNet::Ping);
}
Button::B => {
out.log("button b down");
self.b_down = true;
}
},

Event::ButtonUp(button) => match button {
Button::A => {
out.log("button a up");
self.a_down = false;
}
Button::B => {
out.log("button b up");
self.b_down = false;
}
},
Expand All @@ -227,6 +248,12 @@ impl App {
Event::KeyUp(_key) => {
out.log("key up");
}

Event::FromNet(from_net) => match from_net {
FromNet::Pong => {
out.log("pong");
}
},
}

out.status_led().set(false, self.a_down, self.b_down);
Expand Down
26 changes: 26 additions & 0 deletions ui-app/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,22 @@ impl Screen for MockScreen {
}
}

#[derive(Default)]
struct MockNetTx {
last_to_net: Option<ToNet>,
}

impl NetTx for MockNetTx {
fn write(&mut self, to_net: &ToNet) {
self.last_to_net = Some(*to_net);
}
}

#[derive(Default)]
struct MockOutputs {
status_led: MockLed,
screen: MockScreen,
net_tx: MockNetTx,
last_message: String,
}

Expand All @@ -49,6 +61,10 @@ impl Outputs for MockOutputs {
&mut self.screen
}

fn net_tx(&mut self) -> &mut impl NetTx {
&mut self.net_tx
}

fn log(&mut self, message: &str) {
self.last_message = message.into();
}
Expand Down Expand Up @@ -130,3 +146,13 @@ fn key_logging() {
app.handle(Event::KeyUp(Key::A), &mut outputs);
assert_eq!(outputs.last_message, "key up");
}

#[test]
fn button_a_sends_ping() {
let mut outputs = MockOutputs::default();
let mut app = App::new();
app.start(&mut outputs);

app.handle(Event::ButtonDown(Button::A), &mut outputs);
assert_eq!(outputs.net_tx.last_to_net, Some(ToNet::Ping));
}
10 changes: 8 additions & 2 deletions ui-stm32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ embassy-executor = { version = "0.8.0", features = ["arch-cortex-m", "executor-t
embassy-futures = { version = "0.1.1", features = ["defmt"] }
embassy-stm32 = { version = "0.3.0", features = ["defmt", "stm32f405rg", "memory-x", "time-driver-tim1", "exti", "chrono"] }
embassy-time = { version = "0.4.0", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }

heapless = { version = "0.8", default-features = false }
panic-probe = { version = "1.0.0", features = ["print-defmt"] }
embassy-sync = { version = "0.7.1", features = ["defmt"] }
num_enum = { version = "0.7.4", default-features = false }
hex = { version = "0.4.3", default-features = false }
heapless = { version = "0.9.1", features = ["defmt"] }
embedded-io-async = "0.6.1"
embedded-io = { version = "0.6.1", default-features = false }

[profile.release]
debug = 2

[profile.dev]
debug = 2
opt-level = "s"
46 changes: 37 additions & 9 deletions ui-stm32/src/board/ev12.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
use super::{Button, Keyboard, Screen, StatusLed};
use super::{Button, Keyboard, NetTx, Screen, StatusLed};
use embassy_stm32::{
bind_interrupts,
exti::ExtiInput,
gpio::{Input, Level, Output, Pull, Speed},
mode::Async,
peripherals,
spi::Spi,
usart::{self, UartRx, UartTx},
};
use ui_app::{Led, Outputs};

pub struct Board {
status_led: StatusLed,
screen: Screen,
pub ptt_button: Option<Button>,
pub ai_button: Option<Button>,
net_tx: NetTx<UartTx<'static, Async>>,
pub button_a: Option<Button>,
pub button_b: Option<Button>,
pub keyboard: Option<Keyboard>,
pub net_rx: Option<UartRx<'static, Async>>,
}

bind_interrupts!(struct Irqs {
USART2 => usart::InterruptHandler<peripherals::USART2>;
});

impl Board {
pub async fn new() -> Self {
let config = {
Expand Down Expand Up @@ -55,8 +65,8 @@ impl Board {
let status_led = StatusLed { r, g, b };

// Buttons
let ai_button = ExtiInput::new(p.PC0, p.EXTI0, Pull::Up);
let ptt_button = ExtiInput::new(p.PC1, p.EXTI1, Pull::Up);
let button_a = ExtiInput::new(p.PC1, p.EXTI1, Pull::Up);
let button_b = ExtiInput::new(p.PC0, p.EXTI0, Pull::Up);

// Keyboard
let cols = [
Expand Down Expand Up @@ -94,15 +104,29 @@ impl Board {
let spi1 = Spi::new_blocking_txonly(p.SPI1, p.PA5, p.PA7, config);
let screen = Screen::new(chip_select, data_command, reset, backlight, spi1).await;

// TODO(RLB): NET UART
// TODO(RLB): MGMT UART
// NET UART
let net_uart = {
use embassy_stm32::usart::*;
let mut config = Config::default();
config.baudrate = 460800;
config.data_bits = DataBits::DataBits8;
config.stop_bits = StopBits::STOP2;
config.parity = Parity::ParityNone;

Uart::new(p.USART2, p.PA3, p.PA2, Irqs, p.DMA1_CH6, p.DMA1_CH5, config).unwrap()
};

let (net_tx, net_rx) = net_uart.split();
let net_tx = NetTx::new(net_tx);

Self {
status_led,
screen,
ptt_button: Some(ptt_button),
ai_button: Some(ai_button),
net_tx,
button_a: Some(button_a),
button_b: Some(button_b),
keyboard: Some(keyboard),
net_rx: Some(net_rx),
}
}
}
Expand All @@ -116,6 +140,10 @@ impl Outputs for Board {
&mut self.screen
}

fn net_tx(&mut self) -> &mut impl ui_app::NetTx {
&mut self.net_tx
}

fn log(&mut self, message: &str) {
defmt::info!("{}", message);
}
Expand Down
78 changes: 68 additions & 10 deletions ui-stm32/src/board/ev13.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
use super::{Button, Keyboard, Screen, StatusLed};
use super::{Button, Keyboard, NetTx, Screen, StatusLed};
use embassy_stm32::{
bind_interrupts,
exti::ExtiInput,
gpio::{Input, Level, Output, Pull, Speed},
mode::Async,
peripherals,
spi::Spi,
usart::{self, UartRx, UartTx},
};
use ui_app::{Led, Outputs};

pub struct Board {
status_led: StatusLed,
screen: Screen,
pub ptt_button: Option<Button>,
pub ai_button: Option<Button>,
net_tx: NetTx<UartTx<'static, Async>>,
pub button_a: Option<Button>,
pub button_b: Option<Button>,
pub keyboard: Option<Keyboard>,
pub net_rx: Option<UartRx<'static, Async>>,
}

bind_interrupts!(struct Irqs {
USART2 => usart::InterruptHandler<peripherals::USART2>;
});

impl Board {
pub async fn new() -> Self {
let p = embassy_stm32::init(Default::default());
let config = {
use embassy_stm32::{rcc::*, time::Hertz};

let mut config = embassy_stm32::Config::default();

config.rcc.hse = Some(Hse {
freq: Hertz(6_000_000),
mode: HseMode::Bypass,
});
config.rcc.sys = Sysclk::PLL1_P;
config.rcc.pll_src = PllSource::HSE;
config.rcc.pll = Some(Pll {
prediv: PllPreDiv::DIV3,
mul: PllMul::MUL168,
divp: Some(PllPDiv::DIV2),
divq: Some(PllQDiv::DIV7),
divr: None,
});

config.rcc.ahb_pre = AHBPrescaler::DIV1;
config.rcc.apb1_pre = APBPrescaler::DIV4;
config.rcc.apb2_pre = APBPrescaler::DIV2;
config.rcc.ls = LsConfig {
rtc: RtcClockSource::LSI,
lsi: true,
lse: None,
};

config
};
let p = embassy_stm32::init(config);

// Status LED
let r = Output::new(p.PA4, Level::Low, Speed::Low);
Expand All @@ -25,8 +65,8 @@ impl Board {
let status_led = StatusLed { r, g, b };

// Buttons
let ai_button = ExtiInput::new(p.PC0, p.EXTI0, Pull::Up);
let ptt_button = ExtiInput::new(p.PC1, p.EXTI1, Pull::Up);
let button_a = ExtiInput::new(p.PC1, p.EXTI1, Pull::Up);
let button_b = ExtiInput::new(p.PC0, p.EXTI0, Pull::Up);

// Keyboard
let cols = [
Expand Down Expand Up @@ -64,15 +104,29 @@ impl Board {
let spi1 = Spi::new_blocking_txonly(p.SPI1, p.PA5, p.PA7, config);
let screen = Screen::new(chip_select, data_command, reset, backlight, spi1).await;

// TODO(RLB): NET UART
// TODO(RLB): MGMT UART
// NET UART
let net_uart = {
use embassy_stm32::usart::*;
let mut config = Config::default();
config.baudrate = 460800;
config.data_bits = DataBits::DataBits8;
config.stop_bits = StopBits::STOP2;
config.parity = Parity::ParityNone;

Uart::new(p.USART2, p.PA3, p.PA2, Irqs, p.DMA1_CH6, p.DMA1_CH5, config).unwrap()
};

let (net_tx, net_rx) = net_uart.split();
let net_tx = NetTx::new(net_tx);

Self {
status_led,
screen,
ptt_button: Some(ptt_button),
ai_button: Some(ai_button),
net_tx,
button_a: Some(button_a),
button_b: Some(button_b),
keyboard: Some(keyboard),
net_rx: Some(net_rx),
}
}
}
Expand All @@ -86,6 +140,10 @@ impl Outputs for Board {
&mut self.screen
}

fn net_tx(&mut self) -> &mut impl ui_app::NetTx {
&mut self.net_tx
}

fn log(&mut self, message: &str) {
defmt::info!("{}", message);
}
Expand Down
3 changes: 3 additions & 0 deletions ui-stm32/src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub use keyboard::Keyboard;
mod screen;
pub use screen::Screen;

mod net;
pub use net::{NetRx, NetTx};

struct StatusLed {
r: Output<'static>,
g: Output<'static>,
Expand Down
Loading