Skip to content

Commit ad6bbbe

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

5 files changed

Lines changed: 169 additions & 147 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: 68 additions & 69 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::{DisplayInterface, DelayNs, 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,30 @@ 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);
59+
self.interface.reset(delay, 10_000, 2_000).await;
6360

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)?;
61+
self.cmd_with_data(spi, Command::PanelSetting, &[0xEF, 0x08]).await?;
62+
self.cmd_with_data(spi, Command::PowerSetting, &[0x37, 0x00, 0x23, 0x23]).await?;
63+
self.cmd_with_data(spi, Command::PowerOffSequenceSetting, &[0x00]).await?;
64+
self.cmd_with_data(spi, Command::BoosterSoftStart, &[0xC7, 0xC7, 0x1D]).await?;
65+
self.cmd_with_data(spi, Command::PllControl, &[0x3C]).await?;
66+
self.cmd_with_data(spi, Command::TemperatureSensor, &[0x00]).await?;
67+
self.update_vcom(spi).await?;
68+
self.cmd_with_data(spi, Command::TconSetting, &[0x22]).await?;
69+
self.send_resolution(spi).await?;
7370

74-
self.cmd_with_data(spi, Command::FlashMode, &[0xAA])?;
71+
self.cmd_with_data(spi, Command::FlashMode, &[0xAA]).await?;
7572

76-
delay.delay_us(100_000);
73+
delay.delay_us(100_000).await;
7774

78-
self.update_vcom(spi)?;
75+
self.update_vcom(spi).await?;
7976
Ok(())
8077
}
8178
}
8279

80+
#[maybe_async::maybe_async(AFIT)]
8381
impl<SPI, BUSY, DC, RST, DELAY> WaveshareDisplay<SPI, BUSY, DC, RST, DELAY>
8482
for Epd5in65f<SPI, BUSY, DC, RST, DELAY>
8583
where
@@ -90,7 +88,7 @@ where
9088
DELAY: DelayNs,
9189
{
9290
type DisplayColor = OctColor;
93-
fn new(
91+
async fn new(
9492
spi: &mut SPI,
9593
busy: BUSY,
9694
dc: DC,
@@ -103,34 +101,34 @@ where
103101

104102
let mut epd = Epd5in65f { interface, color };
105103

106-
epd.init(spi, delay)?;
104+
epd.init(spi, delay).await?;
107105

108106
Ok(epd)
109107
}
110108

111-
fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
112-
self.init(spi, delay)
109+
async fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
110+
self.init(spi, delay).await
113111
}
114112

115-
fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
116-
self.cmd_with_data(spi, Command::DeepSleep, &[0xA5])?;
113+
async fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
114+
self.cmd_with_data(spi, Command::DeepSleep, &[0xA5]).await?;
117115
Ok(())
118116
}
119117

120-
fn update_frame(
118+
async fn update_frame(
121119
&mut self,
122120
spi: &mut SPI,
123121
buffer: &[u8],
124122
delay: &mut DELAY,
125123
) -> 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)?;
124+
self.wait_until_idle(spi, delay).await?;
125+
self.update_vcom(spi).await?;
126+
self.send_resolution(spi).await?;
127+
self.cmd_with_data(spi, Command::DataStartTransmission1, buffer).await?;
130128
Ok(())
131129
}
132130

133-
fn update_partial_frame(
131+
async fn update_partial_frame(
134132
&mut self,
135133
_spi: &mut SPI,
136134
_delay: &mut DELAY,
@@ -143,36 +141,36 @@ where
143141
unimplemented!();
144142
}
145143

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);
144+
async fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
145+
self.wait_until_idle(spi, delay).await?;
146+
self.command(spi, Command::PowerOn).await?;
147+
self.wait_until_idle(spi, delay).await?;
148+
self.command(spi, Command::DisplayRefresh).await?;
149+
self.wait_until_idle(spi, delay).await?;
150+
self.command(spi, Command::PowerOff).await?;
151+
self.wait_busy_low(delay).await;
154152
Ok(())
155153
}
156154

157-
fn update_and_display_frame(
155+
async fn update_and_display_frame(
158156
&mut self,
159157
spi: &mut SPI,
160158
buffer: &[u8],
161159
delay: &mut DELAY,
162160
) -> Result<(), SPI::Error> {
163-
self.update_frame(spi, buffer, delay)?;
164-
self.display_frame(spi, delay)?;
161+
self.update_frame(spi, buffer, delay).await?;
162+
self.display_frame(spi, delay).await?;
165163
Ok(())
166164
}
167165

168-
fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
166+
async fn clear_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
169167
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)?;
168+
self.wait_until_idle(spi, delay).await?;
169+
self.update_vcom(spi).await?;
170+
self.send_resolution(spi).await?;
171+
self.command(spi, Command::DataStartTransmission1).await?;
172+
self.interface.data_x_times(spi, bg, WIDTH * HEIGHT / 2).await?;
173+
self.display_frame(spi, delay).await?;
176174
Ok(())
177175
}
178176

@@ -192,7 +190,7 @@ where
192190
HEIGHT
193191
}
194192

195-
fn set_lut(
193+
async fn set_lut(
196194
&mut self,
197195
_spi: &mut SPI,
198196
_delay: &mut DELAY,
@@ -201,12 +199,13 @@ where
201199
unimplemented!();
202200
}
203201

204-
fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
205-
self.interface.wait_until_idle(delay, true);
202+
async fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
203+
self.interface.wait_until_idle(delay, true).await;
206204
Ok(())
207205
}
208206
}
209207

208+
#[maybe_async::maybe_async(AFIT)]
210209
impl<SPI, BUSY, DC, RST, DELAY> Epd5in65f<SPI, BUSY, DC, RST, DELAY>
211210
where
212211
SPI: SpiDevice,
@@ -215,40 +214,40 @@ where
215214
RST: OutputPin,
216215
DELAY: DelayNs,
217216
{
218-
fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> {
219-
self.interface.cmd(spi, command)
217+
async fn command(&mut self, spi: &mut SPI, command: Command) -> Result<(), SPI::Error> {
218+
self.interface.cmd(spi, command).await
220219
}
221220

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

226-
fn cmd_with_data(
225+
async fn cmd_with_data(
227226
&mut self,
228227
spi: &mut SPI,
229228
command: Command,
230229
data: &[u8],
231230
) -> Result<(), SPI::Error> {
232-
self.interface.cmd_with_data(spi, command, data)
231+
self.interface.cmd_with_data(spi, command, data).await
233232
}
234233

235-
fn wait_busy_low(&mut self, delay: &mut DELAY) {
236-
self.interface.wait_until_idle(delay, false);
234+
async fn wait_busy_low(&mut self, delay: &mut DELAY) {
235+
self.interface.wait_until_idle(delay, false).await;
237236
}
238-
fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
237+
async fn send_resolution(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
239238
let w = self.width();
240239
let h = self.height();
241240

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])
241+
self.command(spi, Command::TconResolution).await?;
242+
self.send_data(spi, &[(w >> 8) as u8]).await?;
243+
self.send_data(spi, &[w as u8]).await?;
244+
self.send_data(spi, &[(h >> 8) as u8]).await?;
245+
self.send_data(spi, &[h as u8]).await
247246
}
248247

249-
fn update_vcom(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
248+
async fn update_vcom(&mut self, spi: &mut SPI) -> Result<(), SPI::Error> {
250249
let bg_color = (self.color.get_nibble() & 0b111) << 5;
251-
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x17 | bg_color])?;
250+
self.cmd_with_data(spi, Command::VcomAndDataIntervalSetting, &[0x17 | bg_color]).await?;
252251
Ok(())
253252
}
254253
}

0 commit comments

Comments
 (0)