|
1 | 1 | /* |
2 | | - Inkplate2_Peripheral_Mode example for Soldered Inkplate 2 |
3 | | - Select "Soldered Inkplate2" from Tools -> Board menu. |
4 | | - Don't have "Soldered Inkplate2" option? Follow our tutorial and add it: |
5 | | - https://soldered.com/learn/add-inkplate-6-board-definition-to-arduino-ide/ |
6 | | -
|
7 | | - Using this sketch, you don't have to program and control e-paper using Arduino code. |
8 | | - Instead, you can send UART command. This gives you flexibility that you can use this Inkplate 2 on any platform! |
9 | | -
|
10 | | - Because it uses UART, it's little bit slower and it's not recommended to send bunch of |
11 | | - drawPixel command to draw some image. Instead, load bitmaps and pictures on SD card and load image from SD. |
12 | | - If we missed some function, you can modify this and make your own. |
13 | | - Also, every Inkplate comes with this peripheral mode right from the factory. |
14 | | - |
15 | | - The size of Inkplate 2 is limited, just as its functionality. It doesn't have display temperature nor battery |
16 | | - voltage monitoring.I/O expander is also missing on this board, just as RTC and SD card support. So using this |
17 | | - example is similar to other Inkplates, just there are less functions for this Inkplate. In documentation found |
18 | | - here https://inkplate.readthedocs.io/en/latest/peripheral-mode.html is explained what functions Inkplate 2 supports. |
| 2 | + Inkplate2_Peripheral_Mode sketch for Soldered Inkplate 2 (IP2). |
| 3 | + For this example you will need only a USB-C cable and Inkplate 2. |
| 4 | + Select "Soldered Inkplate 2" from Tools -> Board menu. |
| 5 | + Don’t have "Soldered Inkplate 2" option? Follow our tutorial and add it: |
| 6 | + https://soldered.com/learn/add-inkplate-2-board-definition-to-arduino-ide/ |
| 7 | +
|
| 8 | + Using this sketch, you don’t have to program and control the e-paper display using Arduino code. |
| 9 | + Instead, you can send UART commands. This gives you the flexibility to use Inkplate 2 on any platform! |
| 10 | +
|
| 11 | + Because it uses UART, it’s a bit slower and not recommended to send a large number of |
| 12 | + drawPixel commands to render images. Instead, store bitmaps or pictures on an SD card |
| 13 | + and load them directly from there. |
| 14 | + If any functionality is missing, you can modify this code and make your own version. |
| 15 | + Every Inkplate 2 comes with this Peripheral Mode preloaded from the factory. |
19 | 16 |
|
20 | 17 | Learn more about Peripheral Mode: |
21 | 18 | https://inkplate.readthedocs.io/en/latest/peripheral-mode.html |
22 | 19 |
|
23 | | - UART settings are: 115200 baud, standard parity, ending with "\n\r" (both) (Choose "BOTH NL and CR" in Serial monitor settings) |
24 | | - You can send commands via USB port or by directly connecting to ESP32 TX and RX pins. |
25 | | - Don't forget you need to send #L(1)* after each command to show it on the display |
26 | | - (equal to display.display()). |
| 20 | + UART settings are: 115200 baud, standard parity, ending with "\n\r" (Both NL & CR) |
| 21 | + You can send commands via the USB port or by directly connecting to the ESP32 TX and RX pins. |
27 | 22 |
|
28 | | - Want to learn more about Inkplate? Visit www.inkplate.io |
29 | | - Looking to get support? Write on our forums: https://forum.soldered.com/ |
30 | | - 14 April 2022 by soldered.com |
| 23 | + Want to learn more about Inkplate? Visit: |
| 24 | + https://soldered.com/documentation/inkplate/ |
| 25 | +
|
| 26 | + 23 October 2025 by Soldered |
31 | 27 | */ |
32 | 28 |
|
33 | | -#include <Inkplate.h> // Include Inkplate library |
34 | | -Inkplate display; // Init Inkplate object |
| 29 | +// Include Inkplate library |
| 30 | +#include "Inkplate.h" |
| 31 | + |
| 32 | +// Include Peripheral Mode library |
| 33 | +#include "InkplatePeripheralMode.h" |
35 | 34 |
|
36 | | -#define BUFFER_SIZE 1000 |
37 | | -char commandBuffer[BUFFER_SIZE + 1]; |
38 | | -char strTemp[2001]; |
| 35 | +// Include the header file with sketch settings (buffer size, serial timeout, argument termination char, etc.) |
| 36 | +#include "settings.h" |
| 37 | + |
| 38 | +// Pointer to the singleton Peripheral Mode instance |
| 39 | +PeripheralMode *peripheral; |
| 40 | + |
| 41 | +// Create Inkplate 2 display object |
| 42 | +Inkplate display; |
39 | 43 |
|
40 | 44 | void setup() |
41 | 45 | { |
42 | | - display.begin(); // Init Inkplate library |
43 | | - Serial.begin(115200); // Init serial communication |
44 | | - memset(commandBuffer, 0, BUFFER_SIZE); // Clear the command buffer |
45 | | -} |
| 46 | + // Initialize Inkplate library |
| 47 | + display.begin(); |
46 | 48 |
|
47 | | -void loop() |
48 | | -{ |
49 | | - if (Serial.available()) // Check for the incoming data |
50 | | - { |
51 | | - while (Serial.available()) |
52 | | - { |
53 | | - for (int i = 0; i < (BUFFER_SIZE - 1); i++) |
54 | | - { |
55 | | - commandBuffer[i] = commandBuffer[i + 1]; |
56 | | - } |
57 | | - commandBuffer[BUFFER_SIZE - 1] = Serial.read(); |
58 | | - } |
59 | | - } |
60 | | - char *s = NULL; |
61 | | - char *e = NULL; |
62 | | - for (int i = 0; i < BUFFER_SIZE; i++) |
63 | | - { |
64 | | - if (commandBuffer[i] == '#' && s == NULL) |
65 | | - s = &commandBuffer[i]; |
66 | | - if (commandBuffer[i] == '*' && e == NULL) |
67 | | - e = &commandBuffer[i]; |
68 | | - } |
| 49 | + // Create instance of Peripheral Mode object |
| 50 | + peripheral = PeripheralMode::getInstance(); |
69 | 51 |
|
70 | | - if (s != NULL && e != NULL) |
| 52 | + // Initialize Peripheral Mode (UART, display, buffer, etc.) |
| 53 | + if (!peripheral->begin(&Serial, &display, 115200ULL, SERIAL_UART_RX_PIN, SERIAL_UART_TX_PIN, SERIAL_BUFFER_SIZE)) |
71 | 54 | { |
72 | | - if ((e - s) > 0) |
73 | | - { |
74 | | - int x, x1, x2, y, y1, y2, x3, y3, l, c, w, h, r, n, rx, ry, xc, yc; |
75 | | - char b; |
76 | | - char temp[150]; |
77 | | - switch (*(s + 1)) |
78 | | - { |
79 | | - case '?': |
80 | | - Serial.print("OK"); |
81 | | - break; |
82 | | - |
83 | | - case '0': |
84 | | - // Draw pixel |
85 | | - sscanf(s + 3, "%d,%d,%d", &x, &y, &c); |
86 | | - display.drawPixel(x, y, c); |
87 | | - break; |
88 | | - |
89 | | - case '1': |
90 | | - // Draw line |
91 | | - sscanf(s + 3, "%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &c); |
92 | | - display.drawLine(x1, y1, x2, y2, c); |
93 | | - break; |
94 | | - |
95 | | - case '2': |
96 | | - // Draw fast vertical line |
97 | | - sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &l, &c); |
98 | | - display.drawFastVLine(x, y, l, c); |
99 | | - break; |
100 | | - |
101 | | - case '3': |
102 | | - // Draw fast horizontal line |
103 | | - sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &l, &c); |
104 | | - display.drawFastHLine(x, y, l, c); |
105 | | - break; |
106 | | - |
107 | | - case '4': |
108 | | - // Draw rect. |
109 | | - sscanf(s + 3, "%d,%d,%d,%d,%d", &x, &y, &w, &h, &c); |
110 | | - display.drawRect(x, y, w, h, c); |
111 | | - break; |
112 | | - |
113 | | - case '5': |
114 | | - // Draw circle |
115 | | - sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &r, &c); |
116 | | - display.drawCircle(x, y, r, c); |
117 | | - break; |
118 | | - |
119 | | - case '6': |
120 | | - // Draw triangle |
121 | | - sscanf(s + 3, "%d,%d,%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &x3, &y3, &c); |
122 | | - display.drawTriangle(x1, y1, x2, y2, x3, y3, c); |
123 | | - break; |
124 | | - |
125 | | - case '7': |
126 | | - // Draw round rect. |
127 | | - sscanf(s + 3, "%d,%d,%d,%d,%d,%d", &x, &y, &w, &h, &r, &c); |
128 | | - display.drawRoundRect(x, y, w, h, r, c); |
129 | | - break; |
130 | | - |
131 | | - case '8': |
132 | | - // Draw filled rect. |
133 | | - sscanf(s + 3, "%d,%d,%d,%d,%d", &x, &y, &w, &h, &c); |
134 | | - display.fillRect(x, y, w, h, c); |
135 | | - break; |
136 | | - |
137 | | - case '9': |
138 | | - // Draw filled circle |
139 | | - sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &r, &c); |
140 | | - display.fillCircle(x, y, r, c); |
141 | | - break; |
142 | | - |
143 | | - case 'A': |
144 | | - // Draw filled triangle |
145 | | - sscanf(s + 3, "%d,%d,%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &x3, &y3, &c); |
146 | | - display.fillTriangle(x1, y1, x2, y2, x3, y3, c); |
147 | | - break; |
148 | | - |
149 | | - case 'B': |
150 | | - // Draw filled round triangle |
151 | | - sscanf(s + 3, "%d,%d,%d,%d,%d,%d", &x, &y, &w, &h, &r, &c); |
152 | | - display.fillRoundRect(x, y, w, h, r, c); |
153 | | - break; |
154 | | - |
155 | | - case 'C': |
156 | | - // Draw string |
157 | | - sscanf(s + 3, "\"%2000[^\"]\"", strTemp); |
158 | | - n = strlen(strTemp); |
159 | | - for (int i = 0; i < n; i++) |
160 | | - { |
161 | | - strTemp[i] = toupper(strTemp[i]); |
162 | | - } |
163 | | - for (int i = 0; i < n; i += 2) |
164 | | - { |
165 | | - strTemp[i / 2] = (hexToChar(strTemp[i]) << 4) | (hexToChar(strTemp[i + 1]) & 0x0F); |
166 | | - } |
167 | | - strTemp[n / 2] = 0; |
168 | | - display.print(strTemp); |
169 | | - break; |
170 | | - |
171 | | - case 'D': |
172 | | - // Set text size |
173 | | - sscanf(s + 3, "%d", &c); |
174 | | - display.setTextSize(c); |
175 | | - break; |
176 | | - |
177 | | - case 'E': |
178 | | - // Set print cursor position |
179 | | - sscanf(s + 3, "%d,%d", &x, &y); |
180 | | - display.setCursor(x, y); |
181 | | - break; |
182 | | - |
183 | | - case 'F': |
184 | | - // Enable or disable text wrapping. |
185 | | - sscanf(s + 3, "%c", &b); |
186 | | - if (b == 'T') |
187 | | - display.setTextWrap(true); |
188 | | - if (b == 'F') |
189 | | - display.setTextWrap(false); |
190 | | - break; |
191 | | - |
192 | | - case 'G': |
193 | | - // Set screen rotation |
194 | | - sscanf(s + 3, "%d", &c); |
195 | | - c &= 3; |
196 | | - display.setRotation(c); |
197 | | - break; |
198 | | - |
199 | | - |
200 | | - case 'K': |
201 | | - // Clear the display (frame buffer only) |
202 | | - sscanf(s + 3, "%c", &b); |
203 | | - if (b == '1') |
204 | | - { |
205 | | - display.clearDisplay(); |
206 | | - } |
207 | | - break; |
208 | | - |
209 | | - case 'L': |
210 | | - // Display image from the frame buffer |
211 | | - sscanf(s + 3, "%c", &b); |
212 | | - if (b == '1') |
213 | | - { |
214 | | - display.display(); |
215 | | - } |
216 | | - break; |
217 | | - |
218 | | - case 'T': |
219 | | - // Draw thick line. |
220 | | - int t; |
221 | | - sscanf(s + 3, "%d,%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &c, &t); |
222 | | - display.drawThickLine(x1, y1, x2, y2, c, t); |
223 | | - break; |
224 | | - case 'U': |
225 | | - // Draw elipse. |
226 | | - sscanf(s + 3, "%d,%d,%d,%d,%d", &rx, &ry, &xc, &yc, &c); |
227 | | - display.drawElipse(rx, ry, xc, yc, c); |
228 | | - break; |
229 | | - case 'V': |
230 | | - // Draw filled elipse |
231 | | - sscanf(s + 3, "%d,%d,%d,%d,%d", &rx, &ry, &xc, &yc, &c); |
232 | | - display.fillElipse(rx, ry, xc, yc, c); |
233 | | - break; |
234 | | - } |
235 | | - *s = 0; |
236 | | - *e = 0; |
237 | | - } |
| 55 | + // Send an error message to serial |
| 56 | + Serial.println("Peripheral Mode init failed!\nProgram halted!"); |
| 57 | + |
| 58 | + // Stop program execution |
| 59 | + while (1); |
238 | 60 | } |
| 61 | + |
| 62 | + Serial.println("READY"); |
239 | 63 | } |
240 | 64 |
|
241 | | -int hexToChar(char c) |
| 65 | +void loop() |
242 | 66 | { |
243 | | - if (c >= '0' && c <= '9') |
244 | | - return c - '0'; |
245 | | - if (c >= 'A' && c <= 'F') |
246 | | - return c - 'A' + 10; |
247 | | - if (c >= 'a' && c <= 'f') |
248 | | - return c - 'a' + 10; |
249 | | - return -1; |
| 67 | + // Check if there is incoming data on serial and process commands |
| 68 | + peripheral->getDataFromSerial(SERIAL_TIMEOUT_MS); |
250 | 69 | } |
0 commit comments