Skip to content

Commit b56c279

Browse files
committed
WIP async support
1 parent 1edb243 commit b56c279

5 files changed

Lines changed: 193 additions & 151 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ rust-version = "1.62"
1616
[dependencies]
1717
embedded-graphics-core = { version = "0.4", optional = true }
1818
embedded-hal = "1.0.0"
19+
embedded-hal-async = { version = "1.0.0", optional = true }
1920
bit_field = "0.10.1"
21+
maybe-async = { git = "https://github.com/XLPhere/maybe-async-rs.git", branch = "default_sync", features = ["default_sync"] }
2022

2123
[dev-dependencies]
2224
embedded-graphics = "0.8"
@@ -52,6 +54,7 @@ required-features = ["linux-dev"]
5254
default = ["graphics", "linux-dev", "epd2in13_v3"]
5355

5456
graphics = ["embedded-graphics-core"]
57+
async = ["embedded-hal-async", "maybe-async/is_async"]
5558
epd2in13_v2 = []
5659
epd2in13_v3 = []
5760
linux-dev = []

src/epd5in65f/mod.rs

Lines changed: 87 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@
66
//! - [Waveshare C driver](https://github.com/waveshare/e-Paper/blob/master/RaspberryPi%26JetsonNano/c/lib/e-Paper/EPD_5in65f.c)
77
//! - [Waveshare Python driver](https://github.com/waveshare/e-Paper/blob/master/RaspberryPi%26JetsonNano/python/lib/waveshare_epd/epd5in65f.py)
88
9-
use embedded_hal::{
10-
delay::DelayNs,
11-
digital::{InputPin, OutputPin},
12-
spi::SpiDevice,
13-
};
9+
use embedded_hal::digital::{InputPin, OutputPin};
1410

1511
use crate::color::OctColor;
16-
use crate::interface::DisplayInterface;
12+
use crate::interface::{DelayNs, DisplayInterface, SpiDevice};
1713
use crate::traits::{InternalWiAdditions, RefreshLut, WaveshareDisplay};
1814

1915
pub(crate) mod command;
@@ -48,6 +44,7 @@ pub struct Epd5in65f<SPI, BUSY, DC, RST, DELAY> {
4844
color: OctColor,
4945
}
5046

47+
#[maybe_async::maybe_async(AFIT)]
5148
impl<SPI, BUSY, DC, RST, DELAY> InternalWiAdditions<SPI, BUSY, DC, RST, DELAY>
5249
for Epd5in65f<SPI, BUSY, DC, RST, DELAY>
5350
where
@@ -57,29 +54,37 @@ where
5754
RST: OutputPin,
5855
DELAY: DelayNs,
5956
{
60-
fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
57+
async fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
6158
// Reset the device
62-
self.interface.reset(delay, 10_000, 2_000);
63-
64-
self.cmd_with_data(spi, Command::PanelSetting, &[0xEF, 0x08])?;
65-
self.cmd_with_data(spi, Command::PowerSetting, &[0x37, 0x00, 0x23, 0x23])?;
66-
self.cmd_with_data(spi, Command::PowerOffSequenceSetting, &[0x00])?;
67-
self.cmd_with_data(spi, Command::BoosterSoftStart, &[0xC7, 0xC7, 0x1D])?;
68-
self.cmd_with_data(spi, Command::PllControl, &[0x3C])?;
69-
self.cmd_with_data(spi, Command::TemperatureSensor, &[0x00])?;
70-
self.update_vcom(spi)?;
71-
self.cmd_with_data(spi, Command::TconSetting, &[0x22])?;
72-
self.send_resolution(spi)?;
73-
74-
self.cmd_with_data(spi, Command::FlashMode, &[0xAA])?;
75-
76-
delay.delay_us(100_000);
77-
78-
self.update_vcom(spi)?;
59+
self.interface.reset(delay, 10_000, 2_000).await;
60+
61+
self.cmd_with_data(spi, Command::PanelSetting, &[0xEF, 0x08])
62+
.await?;
63+
self.cmd_with_data(spi, Command::PowerSetting, &[0x37, 0x00, 0x23, 0x23])
64+
.await?;
65+
self.cmd_with_data(spi, Command::PowerOffSequenceSetting, &[0x00])
66+
.await?;
67+
self.cmd_with_data(spi, Command::BoosterSoftStart, &[0xC7, 0xC7, 0x1D])
68+
.await?;
69+
self.cmd_with_data(spi, Command::PllControl, &[0x3C])
70+
.await?;
71+
self.cmd_with_data(spi, Command::TemperatureSensor, &[0x00])
72+
.await?;
73+
self.update_vcom(spi).await?;
74+
self.cmd_with_data(spi, Command::TconSetting, &[0x22])
75+
.await?;
76+
self.send_resolution(spi).await?;
77+
78+
self.cmd_with_data(spi, Command::FlashMode, &[0xAA]).await?;
79+
80+
delay.delay_us(100_000).await;
81+
82+
self.update_vcom(spi).await?;
7983
Ok(())
8084
}
8185
}
8286

87+
#[maybe_async::maybe_async(AFIT)]
8388
impl<SPI, BUSY, DC, RST, DELAY> WaveshareDisplay<SPI, BUSY, DC, RST, DELAY>
8489
for Epd5in65f<SPI, BUSY, DC, RST, DELAY>
8590
where
@@ -90,7 +95,7 @@ where
9095
DELAY: DelayNs,
9196
{
9297
type DisplayColor = OctColor;
93-
fn new(
98+
async fn new(
9499
spi: &mut SPI,
95100
busy: BUSY,
96101
dc: DC,
@@ -103,34 +108,35 @@ where
103108

104109
let mut epd = Epd5in65f { interface, color };
105110

106-
epd.init(spi, delay)?;
111+
epd.init(spi, delay).await?;
107112

108113
Ok(epd)
109114
}
110115

111-
fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
112-
self.init(spi, delay)
116+
async fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
117+
self.init(spi, delay).await
113118
}
114119

115-
fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
116-
self.cmd_with_data(spi, Command::DeepSleep, &[0xA5])?;
120+
async fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
121+
self.cmd_with_data(spi, Command::DeepSleep, &[0xA5]).await?;
117122
Ok(())
118123
}
119124

120-
fn update_frame(
125+
async fn update_frame(
121126
&mut self,
122127
spi: &mut SPI,
123128
buffer: &[u8],
124129
delay: &mut DELAY,
125130
) -> Result<(), SPI::Error> {
126-
self.wait_until_idle(spi, delay)?;
127-
self.update_vcom(spi)?;
128-
self.send_resolution(spi)?;
129-
self.cmd_with_data(spi, Command::DataStartTransmission1, buffer)?;
131+
self.wait_until_idle(spi, delay).await?;
132+
self.update_vcom(spi).await?;
133+
self.send_resolution(spi).await?;
134+
self.cmd_with_data(spi, Command::DataStartTransmission1, buffer)
135+
.await?;
130136
Ok(())
131137
}
132138

133-
fn update_partial_frame(
139+
async fn update_partial_frame(
134140
&mut self,
135141
_spi: &mut SPI,
136142
_delay: &mut DELAY,
@@ -143,36 +149,38 @@ where
143149
unimplemented!();
144150
}
145151

146-
fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
147-
self.wait_until_idle(spi, delay)?;
148-
self.command(spi, Command::PowerOn)?;
149-
self.wait_until_idle(spi, delay)?;
150-
self.command(spi, Command::DisplayRefresh)?;
151-
self.wait_until_idle(spi, delay)?;
152-
self.command(spi, Command::PowerOff)?;
153-
self.wait_busy_low(delay);
152+
async fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
153+
self.wait_until_idle(spi, delay).await?;
154+
self.command(spi, Command::PowerOn).await?;
155+
self.wait_until_idle(spi, delay).await?;
156+
self.command(spi, Command::DisplayRefresh).await?;
157+
self.wait_until_idle(spi, delay).await?;
158+
self.command(spi, Command::PowerOff).await?;
159+
self.wait_busy_low(delay).await;
154160
Ok(())
155161
}
156162

157-
fn update_and_display_frame(
163+
async fn update_and_display_frame(
158164
&mut self,
159165
spi: &mut SPI,
160166
buffer: &[u8],
161167
delay: &mut DELAY,
162168
) -> Result<(), SPI::Error> {
163-
self.update_frame(spi, buffer, delay)?;
164-
self.display_frame(spi, delay)?;
169+
self.update_frame(spi, buffer, delay).await?;
170+
self.display_frame(spi, delay).await?;
165171
Ok(())
166172
}
167173

168-
fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
174+
async fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
169175
let bg = OctColor::colors_byte(self.color, self.color);
170-
self.wait_until_idle(spi, delay)?;
171-
self.update_vcom(spi)?;
172-
self.send_resolution(spi)?;
173-
self.command(spi, Command::DataStartTransmission1)?;
174-
self.interface.data_x_times(spi, bg, WIDTH * HEIGHT / 2)?;
175-
self.display_frame(spi, delay)?;
176+
self.wait_until_idle(spi, delay).await?;
177+
self.update_vcom(spi).await?;
178+
self.send_resolution(spi).await?;
179+
self.command(spi, Command::DataStartTransmission1).await?;
180+
self.interface
181+
.data_x_times(spi, bg, WIDTH * HEIGHT / 2)
182+
.await?;
183+
self.display_frame(spi, delay).await?;
176184
Ok(())
177185
}
178186

@@ -192,7 +200,7 @@ where
192200
HEIGHT
193201
}
194202

195-
fn set_lut(
203+
async fn set_lut(
196204
&mut self,
197205
_spi: &mut SPI,
198206
_delay: &mut DELAY,
@@ -201,12 +209,17 @@ where
201209
unimplemented!();
202210
}
203211

204-
fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
205-
self.interface.wait_until_idle(delay, true);
212+
async fn wait_until_idle(
213+
&mut self,
214+
_spi: &mut SPI,
215+
delay: &mut DELAY,
216+
) -> Result<(), SPI::Error> {
217+
self.interface.wait_until_idle(delay, true).await;
206218
Ok(())
207219
}
208220
}
209221

222+
#[maybe_async::maybe_async(AFIT)]
210223
impl<SPI, BUSY, DC, RST, DELAY> Epd5in65f<SPI, BUSY, DC, RST, DELAY>
211224
where
212225
SPI: SpiDevice,
@@ -215,40 +228,41 @@ where
215228
RST: OutputPin,
216229
DELAY: DelayNs,
217230
{
218-
fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> {
219-
self.interface.cmd(spi, command)
231+
async fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> {
232+
self.interface.cmd(spi, command).await
220233
}
221234

222-
fn send_data(&mut self, spi: &mut SPI, data: &[u8]) -> Result<(), SPI::Error> {
223-
self.interface.data(spi, data)
235+
async fn send_data(&mut self, spi: &mut SPI, data: &[u8]) -> Result<(), SPI::Error> {
236+
self.interface.data(spi, data).await
224237
}
225238

226-
fn cmd_with_data(
239+
async fn cmd_with_data(
227240
&mut self,
228241
spi: &mut SPI,
229242
command: Command,
230243
data: &[u8],
231244
) -> Result<(), SPI::Error> {
232-
self.interface.cmd_with_data(spi, command, data)
245+
self.interface.cmd_with_data(spi, command, data).await
233246
}
234247

235-
fn wait_busy_low(&mut self, delay: &mut DELAY) {
236-
self.interface.wait_until_idle(delay, false);
248+
async fn wait_busy_low(&mut self, delay: &mut DELAY) {
249+
self.interface.wait_until_idle(delay, false).await;
237250
}
238-
fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
251+
async fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
239252
let w = self.width();
240253
let h = self.height();
241254

242-
self.command(spi, Command::TconResolution)?;
243-
self.send_data(spi, &[(w >> 8) as u8])?;
244-
self.send_data(spi, &[w as u8])?;
245-
self.send_data(spi, &[(h >> 8) as u8])?;
246-
self.send_data(spi, &[h as u8])
255+
self.command(spi, Command::TconResolution).await?;
256+
self.send_data(spi, &[(w >> 8) as u8]).await?;
257+
self.send_data(spi, &[w as u8]).await?;
258+
self.send_data(spi, &[(h >> 8) as u8]).await?;
259+
self.send_data(spi, &[h as u8]).await
247260
}
248261

249-
fn update_vcom(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
262+
async fn update_vcom(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
250263
let bg_color = (self.color.get_nibble() & 0b111) << 5;
251-
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x17 | bg_color])?;
264+
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x17 | bg_color])
265+
.await?;
252266
Ok(())
253267
}
254268
}

0 commit comments

Comments
 (0)