22
33//! Generic SPI interface for display drivers
44
5- use embedded_hal as hal ;
6- use hal :: digital:: v2:: OutputPin ;
5+ use embedded_hal:: blocking :: spi ;
6+ use embedded_hal :: digital:: v2:: OutputPin ;
77
88use display_interface:: { DataFormat , DisplayError , WriteOnlyDataCommand } ;
99
10- fn send_u8 < SPI : hal:: blocking:: spi:: Write < u8 > > (
11- spi : & mut SPI ,
12- words : DataFormat < ' _ > ,
13- ) -> Result < ( ) , DisplayError > {
10+ type Result = core:: result:: Result < ( ) , DisplayError > ;
11+
12+ fn send_u8 < SPI : spi:: Write < u8 > > ( spi : & mut SPI , words : DataFormat < ' _ > ) -> Result {
1413 match words {
1514 DataFormat :: U8 ( slice) => spi. write ( slice) . map_err ( |_| DisplayError :: BusWriteError ) ,
1615 DataFormat :: U16 ( slice) => {
@@ -110,65 +109,56 @@ fn send_u8<SPI: hal::blocking::spi::Write<u8>>(
110109///
111110/// This combines the SPI peripheral and a data/command as well as a chip-select pin
112111pub struct SPIInterface < SPI , DC , CS > {
113- spi : SPI ,
114- dc : DC ,
112+ spi_no_cs : SPIInterfaceNoCS < SPI , DC > ,
115113 cs : CS ,
116114}
117115
118116impl < SPI , DC , CS > SPIInterface < SPI , DC , CS >
119117where
120- SPI : hal :: blocking :: spi:: Write < u8 > ,
118+ SPI : spi:: Write < u8 > ,
121119 DC : OutputPin ,
122120 CS : OutputPin ,
123121{
124122 /// Create new SPI interface for communication with a display driver
125123 pub fn new ( spi : SPI , dc : DC , cs : CS ) -> Self {
126- Self { spi, dc, cs }
124+ Self {
125+ spi_no_cs : SPIInterfaceNoCS :: new ( spi, dc) ,
126+ cs,
127+ }
127128 }
128129
129130 /// Consume the display interface and return
130131 /// the underlying peripherial driver and GPIO pins used by it
131132 pub fn release ( self ) -> ( SPI , DC , CS ) {
132- ( self . spi , self . dc , self . cs )
133+ let ( spi, dc) = self . spi_no_cs . release ( ) ;
134+ ( spi, dc, self . cs )
133135 }
134- }
135136
136- impl < SPI , DC , CS > WriteOnlyDataCommand for SPIInterface < SPI , DC , CS >
137- where
138- SPI : hal:: blocking:: spi:: Write < u8 > ,
139- DC : OutputPin ,
140- CS : OutputPin ,
141- {
142- fn send_commands ( & mut self , cmds : DataFormat < ' _ > ) -> Result < ( ) , DisplayError > {
137+ fn with_cs ( & mut self , f : impl FnOnce ( & mut SPIInterfaceNoCS < SPI , DC > ) -> Result ) -> Result {
143138 // Assert chip select pin
144139 self . cs . set_low ( ) . map_err ( |_| DisplayError :: CSError ) ?;
145140
146- // 1 = data, 0 = command
147- self . dc . set_low ( ) . map_err ( |_| DisplayError :: DCError ) ?;
148-
149- // Send words over SPI
150- let err = send_u8 ( & mut self . spi , cmds) ;
141+ let result = f ( & mut self . spi_no_cs ) ;
151142
152143 // Deassert chip select pin
153144 self . cs . set_high ( ) . ok ( ) ;
154145
155- err
146+ result
156147 }
148+ }
157149
158- fn send_data ( & mut self , buf : DataFormat < ' _ > ) -> Result < ( ) , DisplayError > {
159- // Assert chip select pin
160- self . cs . set_low ( ) . map_err ( |_| DisplayError :: CSError ) ?;
161-
162- // 1 = data, 0 = command
163- self . dc . set_high ( ) . map_err ( |_| DisplayError :: DCError ) ?;
164-
165- // Send words over SPI
166- let err = send_u8 ( & mut self . spi , buf) ;
167-
168- // Deassert chip select pin
169- self . cs . set_high ( ) . ok ( ) ;
150+ impl < SPI , DC , CS > WriteOnlyDataCommand for SPIInterface < SPI , DC , CS >
151+ where
152+ SPI : spi:: Write < u8 > ,
153+ DC : OutputPin ,
154+ CS : OutputPin ,
155+ {
156+ fn send_commands ( & mut self , cmds : DataFormat < ' _ > ) -> Result {
157+ self . with_cs ( |spi_no_cs| spi_no_cs. send_commands ( cmds) )
158+ }
170159
171- err
160+ fn send_data ( & mut self , buf : DataFormat < ' _ > ) -> Result {
161+ self . with_cs ( |spi_no_cs| spi_no_cs. send_data ( buf) )
172162 }
173163}
174164
@@ -182,7 +172,7 @@ pub struct SPIInterfaceNoCS<SPI, DC> {
182172
183173impl < SPI , DC > SPIInterfaceNoCS < SPI , DC >
184174where
185- SPI : hal :: blocking :: spi:: Write < u8 > ,
175+ SPI : spi:: Write < u8 > ,
186176 DC : OutputPin ,
187177{
188178 /// Create new SPI interface for communciation with a display driver
@@ -199,18 +189,18 @@ where
199189
200190impl < SPI , DC > WriteOnlyDataCommand for SPIInterfaceNoCS < SPI , DC >
201191where
202- SPI : hal :: blocking :: spi:: Write < u8 > ,
192+ SPI : spi:: Write < u8 > ,
203193 DC : OutputPin ,
204194{
205- fn send_commands ( & mut self , cmds : DataFormat < ' _ > ) -> Result < ( ) , DisplayError > {
195+ fn send_commands ( & mut self , cmds : DataFormat < ' _ > ) -> Result {
206196 // 1 = data, 0 = command
207197 self . dc . set_low ( ) . map_err ( |_| DisplayError :: DCError ) ?;
208198
209199 // Send words over SPI
210200 send_u8 ( & mut self . spi , cmds)
211201 }
212202
213- fn send_data ( & mut self , buf : DataFormat < ' _ > ) -> Result < ( ) , DisplayError > {
203+ fn send_data ( & mut self , buf : DataFormat < ' _ > ) -> Result {
214204 // 1 = data, 0 = command
215205 self . dc . set_high ( ) . map_err ( |_| DisplayError :: DCError ) ?;
216206
0 commit comments