SparkFun SSD168x Library  v1.0.2
Library for SSD1680 / SSD1681 e-paper displays on SPI
Loading...
Searching...
No Matches
sfe_bus_esp_idf.h
Go to the documentation of this file.
1
23#pragma once
24
25#include "sfe_platform.h"
26
27#if defined(SFE_SSD168X_ESP_IDF)
28
29#include "sdkconfig.h"
30#include "driver/spi_master.h"
31#include "driver/uart.h"
32#include "driver/gpio.h"
33
34// Defaults - can be changed with idf.py menuconfig (see Kconfig)
35#ifndef CONFIG_SFE_SSD168X_SPI_CLOCK_HZ
36#define CONFIG_SFE_SSD168X_SPI_CLOCK_HZ 4000000
37#endif
38
39namespace SparkFun_SSD168x
40{
41
45 const uint32_t kDefaultSpiClockHz = CONFIG_SFE_SSD168X_SPI_CLOCK_HZ;
46
47 // SfeSPI: SPI, using the ESP-IDF spi_master driver.
48 // Chip select (CS#) and data / command (D/C#) are driven manually as GPIOs, so that a command
49 // and its data can be sent in a single CS# assertion with D/C# changing part way through.
50 class SfeSPI : public SSD168xDeviceBus
51 {
52 public:
56 SfeSPI(void);
60 ~SfeSPI(void);
61
74 bool init(spi_host_device_t host, gpio_num_t cs, gpio_num_t dc, uint32_t clockHz = kDefaultSpiClockHz);
75
87 bool init(spi_device_handle_t device, gpio_num_t cs, gpio_num_t dc);
88
97 bool writeCommand(uint8_t command, const uint8_t *data, uint16_t length);
98
99 private:
100 bool configurePins(gpio_num_t cs, gpio_num_t dc);
101 void removeDevice(void);
102
103 spi_device_handle_t _dev;
104 bool _ownDevice; // true if we added the device - and so must remove it
105 gpio_num_t _cs;
106 gpio_num_t _dc;
107 };
108
109 // SfeOutput: the ESP-IDF equivalent of the Arduino Print class. It is the destination for debug
110 // messages (enableDebugging).
111 // Derive your own class from SfeOutput to send the data anywhere you like (a file, a socket, a queue).
112 class SfeOutput
113 {
114 public:
115 virtual ~SfeOutput() {}
123 virtual size_t write(const uint8_t *buffer, size_t length) = 0;
124 };
125
126 // Output to stdout - the ESP-IDF console (UART0 or USB Serial/JTAG)
127 class SfeStdoutOutput : public SfeOutput
128 {
129 public:
131 size_t write(const uint8_t *buffer, size_t length);
132 };
133
134 // Output to a UART. The UART driver must already be installed.
135 class SfeUartOutput : public SfeOutput
136 {
137 public:
139 SfeUartOutput(uart_port_t port) : _port(port) {}
141 size_t write(const uint8_t *buffer, size_t length);
142
143 private:
144 uart_port_t _port;
145 };
146
147 // Output to a user callback function
148 class SfeCallbackOutput : public SfeOutput
149 {
150 public:
151 typedef void (*callback_t)(const uint8_t *buffer, size_t length, void *userContext);
153 SfeCallbackOutput(callback_t callback, void *userContext = nullptr) : _callback(callback), _userContext(userContext) {}
155 size_t write(const uint8_t *buffer, size_t length)
156 {
157 if (_callback != nullptr)
158 _callback(buffer, length, _userContext);
159 return length;
160 }
161
162 private:
163 callback_t _callback;
164 void *_userContext;
165 };
166
167 // The default debug output port
168 extern SfeStdoutOutput sfeStdout;
169
170 // SfePrint: the library's internal print helper. Wraps an SfeOutput and does the formatting.
171 class SfePrint
172 {
173 public:
174 SfePrint(void) { _outputPort = nullptr; }
175
176 void init(SfeOutput &outputPort) { _outputPort = &outputPort; }
177 inline bool operator==(SfePrint const &other) const { return _outputPort == other._outputPort; }
178 inline bool operator!=(SfePrint const &other) const { return !(*this == other); }
179
180 void write(uint8_t c)
181 {
182 if (_outputPort != nullptr)
183 _outputPort->write(&c, 1);
184 }
185 void print(const char *c)
186 {
187 if ((_outputPort != nullptr) && (c != nullptr))
188 _outputPort->write((const uint8_t *)c, strlen(c));
189 }
190 void print(uint32_t value, int base = DEC)
191 {
192 char buf[12];
193 if (base == HEX)
194 snprintf(buf, sizeof(buf), "%lX", (unsigned long)value);
195 else
196 snprintf(buf, sizeof(buf), "%lu", (unsigned long)value);
197 print(buf);
198 }
199 void println() { print("\r\n"); }
200 void println(const char *c)
201 {
202 print(c);
203 println();
204 }
205 void println(uint32_t value, int base = DEC)
206 {
207 print(value, base);
208 println();
209 }
210
211 private:
212 SfeOutput *_outputPort;
213 };
214
215};
216
217// The type used by the public API for the debug output port: enableDebugging()
218namespace SparkFun_SSD168x
219{
220typedef SfeOutput sfe_print_t;
221} // namespace SparkFun_SSD168x
222
223#endif // SFE_SSD168X_ESP_IDF
Definition: sfe_bus.h:34