Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
392 lines (349 loc) · 13.3 KB
/
Copy pathmain.rs
File metadata and controls
392 lines (349 loc) · 13.3 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//! LED Matrix Module
#![no_std]
#![no_main]
#![allow(clippy::needless_range_loop)]
use bsp::entry;
use cortex_m::delay::Delay;
//use defmt::*;
use defmt_rtt as _;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use rp2040_hal::gpio::{Output, Pin, PushPull};
//#[cfg(debug_assertions)]
//use panic_probe as _;
use rp2040_panic_usb_boot as _;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::*;
use embedded_hal::blocking::spi;
use st7306::{FpsConfig, HpmFps, LpmFps, PowerMode, ST7306};
// Provide an alias for our BSP so we can switch targets quickly.
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
use fl16_inputmodules::lcd_hal as bsp;
//use rp_pico as bsp;
// use sparkfun_pro_micro_rp2040 as bsp;
use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
gpio, pac,
sio::Sio,
usb,
watchdog::Watchdog,
Timer,
};
use fugit::RateExtU32;
// USB Device support
use usb_device::{class_prelude::*, prelude::*};
// USB Communications Class Device support
use usbd_serial::{SerialPort, USB_CLASS_CDC};
// Used to demonstrate writing formatted strings
use core::fmt::Debug;
use core::fmt::Write;
use heapless::String;
use fl16_inputmodules::control::*;
use fl16_inputmodules::graphics::*;
use fl16_inputmodules::serialnum::{device_release, get_serialnum};
// FRA - Framwork
// KDE - C1 LED Matrix
// AM - Atemitech
// 00 - Default Configuration
// 00000000 - Device Identifier
const DEFAULT_SERIAL: &str = "FRAKDEAM0000000000";
type B1ST7306 = ST7306<
rp2040_hal::Spi<rp2040_hal::spi::Enabled, pac::SPI0, 8>,
Pin<gpio::bank0::Gpio20, Output<PushPull>>,
Pin<gpio::bank0::Gpio17, Output<PushPull>>,
Pin<gpio::bank0::Gpio21, Output<PushPull>>,
25,
200,
>;
const DEBUG: bool = false;
const SCRNS_DELTA: i32 = 5;
const WIDTH: i32 = 300;
const HEIGHT: i32 = 400;
const SIZE: Size = Size::new(WIDTH as u32, HEIGHT as u32);
#[entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let sio = Sio::new(pac.SIO);
let clocks = init_clocks_and_plls(
bsp::XOSC_CRYSTAL_FREQ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let pins = bsp::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
// Set up the USB driver
let usb_bus = UsbBusAllocator::new(usb::UsbBus::new(
pac.USBCTRL_REGS,
pac.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut pac.RESETS,
));
// Set up the USB Communications Class Device driver
let mut serial = SerialPort::new(&usb_bus);
let serialnum = if let Some(serialnum) = get_serialnum() {
serialnum.serialnum
} else {
DEFAULT_SERIAL
};
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x32ac, 0x0021))
.manufacturer("Framework Computer Inc")
.product("B1 Display")
.serial_number(serialnum)
.max_power(500) // TODO: Check how much
.device_release(device_release()) // TODO: Assign dynamically based on crate version
.device_class(USB_CLASS_CDC)
.build();
// Display SPI pins
let _spi_sclk = pins.scl.into_mode::<bsp::hal::gpio::FunctionSpi>();
let _spi_mosi = pins.sda.into_mode::<bsp::hal::gpio::FunctionSpi>();
let _spi_miso = pins.miso.into_mode::<bsp::hal::gpio::FunctionSpi>();
let spi = bsp::hal::Spi::<_, _, 8>::new(pac.SPI0);
// Display control pins
let dc = pins.dc.into_push_pull_output();
let mut cs = pins.cs.into_push_pull_output();
cs.set_low().unwrap();
let rst = pins.rstb.into_push_pull_output();
let spi = spi.init(
&mut pac.RESETS,
clocks.peripheral_clock.freq(),
16_000_000u32.Hz(),
&embedded_hal::spi::MODE_0,
);
let mut state = B1DIsplayState {
sleeping: SimpleSleepState::Awake,
screen_inverted: false,
screen_on: true,
screensaver: Some(ScreenSaverState::default()),
power_mode: PowerMode::Lpm,
fps_config: FpsConfig {
hpm: HpmFps::ThirtyTwo,
lpm: LpmFps::Two,
},
animation_period: 1_000_000, // 1000ms = 1Hz
};
const INVERTED: bool = false;
const AUTO_PWRDOWN: bool = true;
const TE_ENABLE: bool = true;
const COL_START: u16 = 0x12;
const ROW_START: u16 = 0x00;
let mut disp: B1ST7306 = ST7306::new(
spi,
dc,
cs,
rst,
INVERTED,
AUTO_PWRDOWN,
TE_ENABLE,
state.fps_config,
WIDTH as u16,
HEIGHT as u16,
COL_START,
ROW_START,
);
disp.init(&mut delay).unwrap();
// Clear display, might have garbage in display memory
// TODO: Seems broken
//disp.clear(Rgb565::WHITE).unwrap();
Rectangle::new(Point::new(0, 0), SIZE)
.into_styled(PrimitiveStyle::with_fill(Rgb565::WHITE))
.draw(&mut disp)
.unwrap();
let logo_rect = draw_logo(&mut disp, Point::new(LOGO_OFFSET_X, LOGO_OFFSET_Y)).unwrap();
if DEBUG {
Rectangle::new(Point::new(10, 10), Size::new(10, 10))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut disp)
.unwrap();
Rectangle::new(Point::new(20, 20), Size::new(10, 10))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut disp)
.unwrap();
Rectangle::new(Point::new(30, 30), Size::new(10, 10))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut disp)
.unwrap();
Rectangle::new(Point::new(40, 40), Size::new(10, 10))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut disp)
.unwrap();
Rectangle::new(Point::new(50, 50), Size::new(10, 10))
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut disp)
.unwrap();
draw_text(
&mut disp,
"Framework",
Point::new(LOGO_OFFSET_X, LOGO_OFFSET_Y + logo_rect.size.height as i32),
)
.unwrap();
}
disp.flush().unwrap();
let sleep = pins.sleep.into_pull_down_input();
let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
let mut prev_timer = timer.get_counter().ticks();
let mut ticks = 0;
let mut logo_pos = Point::new(LOGO_OFFSET_X, LOGO_OFFSET_Y);
loop {
// Go to sleep if the host is sleeping
let host_sleeping = sleep.is_low().unwrap();
handle_sleep(host_sleeping, &mut state, &mut delay, &mut disp);
// Handle period display updates. Don't do it too often
if timer.get_counter().ticks() > prev_timer + state.animation_period {
prev_timer = timer.get_counter().ticks();
if let Some(ref mut screensaver) = state.screensaver {
let seconds = ticks / (1_000_000 / state.animation_period);
#[allow(clippy::modulo_one)]
let second_decimals = ticks % (1_000_000 / state.animation_period);
Rectangle::new(Point::new(0, 0), Size::new(300, 50))
.into_styled(PrimitiveStyle::with_fill(Rgb565::WHITE))
.draw(&mut disp)
.unwrap();
let mut text: String<32> = String::new();
write!(
&mut text,
"{:>4} Ticks ({:>4}.{} s)",
ticks, seconds, second_decimals
)
.unwrap();
// Uncomment to draw the ticks on the screen
//draw_text(
// &mut disp,
// &text,
// Point::new(0, 0),
//).unwrap();
ticks += 1;
logo_pos = {
let (x, y) = (logo_pos.x, logo_pos.y);
let w = logo_rect.size.width as i32;
let h = logo_rect.size.height as i32;
// Bounce off the walls
if x <= 0 || x + w >= WIDTH {
screensaver.rightwards *= -1;
}
if y <= 0 || y + h >= HEIGHT {
screensaver.downwards *= -1;
}
Point::new(
x + screensaver.rightwards * SCRNS_DELTA,
y + screensaver.downwards * SCRNS_DELTA,
)
};
// Draw a border around the new logo, to clear previously drawn adjacent logos
let style = PrimitiveStyleBuilder::new()
.stroke_color(Rgb565::WHITE)
.stroke_width(2 * SCRNS_DELTA as u32)
.build();
Rectangle::new(
logo_pos - Point::new(SCRNS_DELTA, SCRNS_DELTA),
logo_rect.size + Size::new(2 * SCRNS_DELTA as u32, 2 * SCRNS_DELTA as u32),
)
.into_styled(style)
.draw(&mut disp)
.unwrap();
draw_logo(&mut disp, logo_pos).unwrap();
disp.flush().unwrap();
}
}
// Check for new data
if usb_dev.poll(&mut [&mut serial]) {
let mut buf = [0u8; 64];
match serial.read(&mut buf) {
Err(_e) => {
// Do nothing
}
Ok(0) => {
// Do nothing
}
Ok(count) => {
match (parse_command(count, &buf), &state.sleeping) {
(Some(Command::Sleep(go_sleeping)), _) => {
handle_sleep(go_sleeping, &mut state, &mut delay, &mut disp);
}
(Some(c @ Command::BootloaderReset), _)
| (Some(c @ Command::IsSleeping), _) => {
if let Some(response) =
handle_command(&c, &mut state, logo_rect, &mut disp, &mut delay)
{
let _ = serial.write(&response);
};
}
(Some(command), SimpleSleepState::Awake) => {
// While sleeping no command is handled, except waking up
if let Some(response) = handle_command(
&command, &mut state, logo_rect, &mut disp, &mut delay,
) {
let _ = serial.write(&response);
};
// Must write AFTER writing response, otherwise the
// client interprets this debug message as the response
//let mut text: String<64> = String::new();
//write!(
// &mut text,
// "Handled command {}:{}:{}:{}\r\n",
// buf[0], buf[1], buf[2], buf[3]
//)
//.unwrap();
//let _ = serial.write(text.as_bytes());
}
_ => {}
}
}
}
}
}
}
fn handle_sleep<SPI, DC, CS, RST, const COLS: usize, const ROWS: usize>(
go_sleeping: bool,
state: &mut B1DIsplayState,
delay: &mut Delay,
disp: &mut ST7306<SPI, DC, CS, RST, COLS, ROWS>,
) where
SPI: spi::Write<u8>,
DC: OutputPin,
CS: OutputPin,
RST: OutputPin,
<SPI as spi::Write<u8>>::Error: Debug,
{
match (state.sleeping.clone(), go_sleeping) {
(SimpleSleepState::Awake, false) => (),
(SimpleSleepState::Awake, true) => {
state.sleeping = SimpleSleepState::Sleeping;
// Turn off display
//disp.on_off(false).unwrap();
disp.sleep_in(delay).unwrap();
// TODO: Power Display controller down
// TODO: Set up SLEEP# pin as interrupt and wfi
//cortex_m::asm::wfi();
}
(SimpleSleepState::Sleeping, true) => (),
(SimpleSleepState::Sleeping, false) => {
// Restore back grid before sleeping
state.sleeping = SimpleSleepState::Awake;
// Turn display back on
//disp.on_off(true).unwrap();
disp.sleep_out(delay).unwrap();
// Sleep-in has to go into HPM first, so we'll be in HPM after wake-up as well
if state.power_mode == PowerMode::Lpm {
disp.switch_mode(delay, PowerMode::Lpm).unwrap();
}
// Turn screensaver on when resuming from sleep
// TODO Subject to change, but currently I want to avoid burn-in by default
state.screensaver = Some(ScreenSaverState::default());
// TODO: Power display controller back on
}
}
}