-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
rtic_dvd.rs
176 lines (147 loc) · 4.99 KB
/
rtic_dvd.rs
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
//! Bounce a DVD player logo around the screen
//!
//! Like this, but with no color changing: https://bouncingdvdlogo.com/
//!
//! For best results, run with the `--release` flag.
#![no_std]
#![no_main]
#[rtic::app(device = stm32f1xx_hal::pac, peripherals = true, dispatchers = [EXTI0])]
mod app {
use display_interface_spi::SPIInterfaceNoCS;
use embedded_graphics::{
geometry::Point,
image::Image,
pixelcolor::{BinaryColor, Rgb565},
prelude::*,
primitives::{PrimitiveStyle, Rectangle},
};
use panic_halt as _;
use ssd1306::{mode::BufferedGraphicsMode, prelude::*, Ssd1306};
use stm32f1xx_hal::{
gpio,
pac::{self, SPI1},
prelude::*,
spi::{self, Mode, Phase, Polarity, Spi},
timer::{CounterMs, Event, Timer},
};
use tinybmp::Bmp;
type Display = Ssd1306<
SPIInterfaceNoCS<
spi::Spi<
SPI1,
spi::Spi1NoRemap,
(
gpio::gpioa::PA5<gpio::Alternate<gpio::PushPull>>,
gpio::gpioa::PA6<gpio::Input<gpio::Floating>>,
gpio::gpioa::PA7<gpio::Alternate<gpio::PushPull>>,
),
u8,
>,
gpio::gpiob::PB1<gpio::Output<gpio::PushPull>>,
>,
DisplaySize128x64,
BufferedGraphicsMode<DisplaySize128x64>,
>;
#[shared]
struct SharedResources {}
#[local]
struct Resources {
display: Display,
timer: CounterMs<pac::TIM1>,
top_left: Point,
velocity: Point,
bmp: Bmp<Rgb565, 'static>,
}
#[init]
fn init(cx: init::Context) -> (SharedResources, Resources, init::Monotonics) {
let dp = cx.device;
let core = cx.core;
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
let clocks = rcc
.cfgr
.use_hse(8.MHz())
.sysclk(72.MHz())
.pclk1(36.MHz())
.freeze(&mut flash.acr);
let mut afio = dp.AFIO.constrain();
let mut gpiob = dp.GPIOB.split();
let mut gpioa = dp.GPIOA.split();
// SPI1
let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
let miso = gpioa.pa6;
let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
let mut delay = Timer::syst(core.SYST, &clocks).delay();
let mut rst = gpiob.pb0.into_push_pull_output(&mut gpiob.crl);
let dc = gpiob.pb1.into_push_pull_output(&mut gpiob.crl);
let spi = Spi::spi1(
dp.SPI1,
(sck, miso, mosi),
&mut afio.mapr,
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
},
8.MHz(),
clocks,
);
let interface = display_interface_spi::SPIInterfaceNoCS::new(spi, dc);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate180)
.into_buffered_graphics_mode();
display.reset(&mut rst, &mut delay).unwrap();
display.init().unwrap();
// Update framerate
let mut timer = dp.TIM1.counter_ms(&clocks);
timer.start(50.millis()).unwrap(); // 20 FPS
timer.listen(Event::Update);
let bmp = Bmp::from_slice(include_bytes!("dvd.bmp")).unwrap();
// Init the static resources to use them later through RTIC
(
SharedResources {},
Resources {
timer,
display,
top_left: Point::new(5, 3),
velocity: Point::new(1, 1),
bmp,
},
init::Monotonics(),
)
}
#[task(binds = TIM1_UP, local = [display, top_left, velocity, timer, bmp])]
fn update(cx: update::Context) {
let update::LocalResources {
display,
top_left,
velocity,
timer,
bmp,
..
} = cx.local;
let bottom_right = *top_left + bmp.bounding_box().size;
// Erase previous image position with a filled black rectangle
Rectangle::with_corners(*top_left, bottom_right)
.into_styled(PrimitiveStyle::with_fill(BinaryColor::Off))
.draw(display)
.unwrap();
// Check if the image collided with a screen edge
{
if bottom_right.x > display.size().width as i32 || top_left.x < 0 {
velocity.x = -velocity.x;
}
if bottom_right.y > display.size().height as i32 || top_left.y < 0 {
velocity.y = -velocity.y;
}
}
// Move the image
*top_left += *velocity;
// Draw image at new position
Image::new(bmp, *top_left)
.draw(&mut display.color_converted())
.unwrap();
// Write changes to the display
display.flush().unwrap();
// Clears the update flag
timer.clear_interrupt(Event::Update);
}
}