Skip to content

Commit 234bcd1

Browse files
committed
more re-organizing
1 parent 96db036 commit 234bcd1

File tree

2 files changed

+104
-98
lines changed

2 files changed

+104
-98
lines changed

src/main.rs

+13-98
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,33 @@
55
#![no_std]
66
#![no_main]
77

8-
use core::fmt::Write;
8+
mod ui;
9+
910
use embassy_executor::Spawner;
1011
use embassy_rp::bind_interrupts;
11-
use embassy_rp::i2c::{self, Blocking, Config, I2c};
12-
use embassy_rp::peripherals::I2C1;
12+
use embassy_rp::i2c::{self, Config};
1313
use embassy_rp::peripherals::USB;
1414
use embassy_rp::usb::{Driver, InterruptHandler as USBInterruptHandler};
1515
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
1616
use embassy_sync::mutex::Mutex;
17-
use embedded_graphics::image::{Image, ImageRaw};
18-
use embedded_graphics::mono_font::ascii::FONT_6X12;
19-
use embedded_graphics::mono_font::MonoTextStyle;
20-
use embedded_graphics::text::Text;
21-
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
22-
use ssd1306::mode::BufferedGraphicsMode;
23-
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
2417
use {defmt_rtt as _, panic_probe as _};
2518

19+
use self::ui::{draw_ui_task, UiScreen, UiState};
20+
2621
bind_interrupts!(struct Irqs {
2722
USBCTRL_IRQ => USBInterruptHandler<USB>;
2823
});
2924

30-
const DISPLAY_FRAME_TIME: u64 = 1000 / 4;
31-
32-
const RUST_LOGO_BYTES: &[u8] = include_bytes!("../assets/rust.raw");
33-
34-
type Display = Ssd1306<
35-
I2CInterface<I2c<'static, I2C1, Blocking>>,
36-
DisplaySize128x64,
37-
BufferedGraphicsMode<DisplaySize128x64>,
38-
>;
39-
4025
type Resource<R> = Mutex<ThreadModeRawMutex, Option<R>>;
4126

42-
enum UiScreen {
43-
HelloWorld,
44-
RustLogo,
45-
}
46-
47-
impl UiScreen {
48-
fn draw(&self, display: &mut Display) {
49-
display.clear_buffer();
50-
51-
match self {
52-
UiScreen::HelloWorld => {
53-
display.clear_buffer();
54-
55-
Text::new(
56-
"Hello, World!",
57-
Point::new(4, 12),
58-
MonoTextStyle::new(&FONT_6X12, BinaryColor::On),
59-
)
60-
.draw(display)
61-
.unwrap();
62-
}
63-
UiScreen::RustLogo => {
64-
display.clear_buffer();
65-
66-
let raw: ImageRaw<BinaryColor> = ImageRaw::new(RUST_LOGO_BYTES, 64);
67-
let im = Image::new(&raw, Point::new(32, 0));
68-
69-
im.draw(display).unwrap();
70-
}
71-
}
72-
73-
display.flush().unwrap();
74-
}
75-
}
76-
77-
struct UiState {
78-
display: Display,
79-
screen: UiScreen,
80-
}
81-
82-
impl UiState {
83-
fn new(i2c: I2c<'static, I2C1, Blocking>) -> Self {
84-
let interface = I2CDisplayInterface::new(i2c);
85-
let mut display =
86-
Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
87-
.into_buffered_graphics_mode();
88-
display.init().unwrap();
89-
90-
Self {
91-
display,
92-
screen: UiScreen::HelloWorld,
93-
}
94-
}
95-
96-
fn draw(&mut self) {
97-
self.screen.draw(&mut self.display);
98-
}
99-
}
100-
27+
const DISPLAY_FRAME_TIME: u64 = 1000 / 4;
10128
static UI_STATE: Resource<UiState> = Resource::new(None);
10229

10330
#[embassy_executor::task]
10431
async fn logger_task(driver: Driver<'static, USB>) {
10532
embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver);
10633
}
10734

108-
#[embassy_executor::task]
109-
async fn draw_ui_task(ui: &'static Resource<UiState>) {
110-
let mut frame_ticker = embassy_time::Ticker::every(
111-
embassy_time::Duration::from_millis(DISPLAY_FRAME_TIME),
112-
);
113-
114-
loop {
115-
{
116-
let mut ui = ui.lock().await;
117-
ui.as_mut().unwrap().draw();
118-
}
119-
120-
frame_ticker.next().await;
121-
}
122-
}
123-
12435
#[embassy_executor::main]
12536
async fn main(spawner: Spawner) {
12637
let p = embassy_rp::init(Default::default());
@@ -131,11 +42,15 @@ async fn main(spawner: Spawner) {
13142
let sda = p.PIN_14;
13243
let scl = p.PIN_15;
13344

134-
let i2c = i2c::I2c::new_blocking(p.I2C1, scl, sda, Config::default());
45+
// build the UI state
13546
{
136-
*(UI_STATE.lock().await) = Some(UiState::new(i2c));
47+
*(UI_STATE.lock().await) = Some(UiState::new(i2c::I2c::new_blocking(
48+
p.I2C1,
49+
scl,
50+
sda,
51+
Config::default(),
52+
)));
13753
}
138-
13954
spawner.spawn(draw_ui_task(&UI_STATE)).unwrap();
14055

14156
// switch between screens every 2 seconds

src/ui.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use embassy_rp::i2c::{Blocking, I2c};
2+
use embassy_rp::peripherals::I2C1;
3+
use embedded_graphics::image::{Image, ImageRaw};
4+
use embedded_graphics::mono_font::ascii::FONT_6X12;
5+
use embedded_graphics::mono_font::MonoTextStyle;
6+
use embedded_graphics::pixelcolor::BinaryColor;
7+
use embedded_graphics::prelude::*;
8+
use embedded_graphics::text::Text;
9+
use ssd1306::mode::BufferedGraphicsMode;
10+
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
11+
12+
const RUST_LOGO_BYTES: &[u8] = include_bytes!("../assets/rust.raw");
13+
14+
type Display = Ssd1306<
15+
I2CInterface<I2c<'static, I2C1, Blocking>>,
16+
DisplaySize128x64,
17+
BufferedGraphicsMode<DisplaySize128x64>,
18+
>;
19+
20+
pub enum UiScreen {
21+
HelloWorld,
22+
RustLogo,
23+
}
24+
25+
impl UiScreen {
26+
pub fn draw(&self, display: &mut Display) {
27+
match self {
28+
UiScreen::HelloWorld => {
29+
display.clear_buffer();
30+
31+
Text::new(
32+
"Hello, World!",
33+
Point::new(4, 12),
34+
MonoTextStyle::new(&FONT_6X12, BinaryColor::On),
35+
)
36+
.draw(display)
37+
.unwrap();
38+
}
39+
UiScreen::RustLogo => {
40+
display.clear_buffer();
41+
42+
let raw: ImageRaw<BinaryColor> = ImageRaw::new(RUST_LOGO_BYTES, 64);
43+
let im = Image::new(&raw, Point::new(32, 0));
44+
45+
im.draw(display).unwrap();
46+
}
47+
}
48+
49+
display.flush().unwrap();
50+
}
51+
}
52+
53+
pub struct UiState {
54+
display: Display,
55+
pub screen: UiScreen,
56+
}
57+
58+
impl UiState {
59+
pub fn new(i2c: I2c<'static, I2C1, Blocking>) -> Self {
60+
let interface = I2CDisplayInterface::new(i2c);
61+
let mut display =
62+
Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
63+
.into_buffered_graphics_mode();
64+
display.init().unwrap();
65+
66+
Self {
67+
display,
68+
screen: UiScreen::HelloWorld,
69+
}
70+
}
71+
72+
pub fn draw(&mut self) {
73+
self.screen.draw(&mut self.display);
74+
}
75+
}
76+
77+
#[embassy_executor::task]
78+
pub async fn draw_ui_task(ui: &'static Resource<UiState>) {
79+
let mut frame_ticker = embassy_time::Ticker::every(
80+
embassy_time::Duration::from_millis(DISPLAY_FRAME_TIME),
81+
);
82+
83+
loop {
84+
{
85+
let mut ui = ui.lock().await;
86+
ui.as_mut().unwrap().draw();
87+
}
88+
89+
frame_ticker.next().await;
90+
}
91+
}

0 commit comments

Comments
 (0)