|
| 1 | +#include "EEPROM.h" |
| 2 | +#include "Inkplate.h" |
| 3 | +#include "image.h" |
| 4 | +#include <Wire.h> |
| 5 | + |
| 6 | +Inkplate display; |
| 7 | + |
| 8 | +// EEPROM Address must be equal or greater then 0 and less then 64. |
| 9 | +int EEPROMaddress = 0; |
| 10 | + |
| 11 | +// Peripheral mode variables and arrays |
| 12 | +#define BUFFER_SIZE 1000 |
| 13 | +char commandBuffer[BUFFER_SIZE + 1]; |
| 14 | +char strTemp[2001]; |
| 15 | + |
| 16 | +const char sdCardTestStringLength = 100; |
| 17 | +const char *testString = {"This is some test string..."}; |
| 18 | + |
| 19 | +void setup() |
| 20 | +{ |
| 21 | + display.begin(); |
| 22 | + Serial.begin(115200); |
| 23 | + EEPROM.begin(64); |
| 24 | + |
| 25 | + if (EEPROM.read(EEPROMaddress) != 170) |
| 26 | + { |
| 27 | + microSDCardTest(); |
| 28 | + display.rtcGetRtcData(); |
| 29 | + uint32_t rtcTime = display.rtcGetEpoch(); |
| 30 | + delay(3000); |
| 31 | + expanderCheck(); |
| 32 | + rtcCheck(rtcTime); |
| 33 | + EEPROM.write(EEPROMaddress, 170); |
| 34 | + EEPROM.commit(); |
| 35 | + } |
| 36 | + memset(commandBuffer, 0, BUFFER_SIZE); |
| 37 | + |
| 38 | + showSplashScreen(); |
| 39 | +} |
| 40 | + |
| 41 | +void loop() |
| 42 | +{ |
| 43 | + if (Serial.available()) |
| 44 | + { |
| 45 | + while (Serial.available()) |
| 46 | + { |
| 47 | + for (int i = 0; i < (BUFFER_SIZE - 1); i++) |
| 48 | + { |
| 49 | + commandBuffer[i] = commandBuffer[i + 1]; |
| 50 | + } |
| 51 | + commandBuffer[BUFFER_SIZE - 1] = Serial.read(); |
| 52 | + } |
| 53 | + } |
| 54 | + char *s = NULL; |
| 55 | + char *e = NULL; |
| 56 | + for (int i = 0; i < BUFFER_SIZE; i++) |
| 57 | + { |
| 58 | + if (commandBuffer[i] == '#' && s == NULL) |
| 59 | + s = &commandBuffer[i]; |
| 60 | + if (commandBuffer[i] == '*' && e == NULL) |
| 61 | + e = &commandBuffer[i]; |
| 62 | + } |
| 63 | + if (s != NULL && e != NULL) |
| 64 | + { |
| 65 | + if ((e - s) > 0) |
| 66 | + { |
| 67 | + int x, x1, x2, y, y1, y2, x3, y3, l, c, w, h, r, n; |
| 68 | + char b; |
| 69 | + // char temp[150]; |
| 70 | + switch (*(s + 1)) |
| 71 | + { |
| 72 | + case '?': |
| 73 | + Serial.print("OK"); |
| 74 | + break; |
| 75 | + |
| 76 | + case '0': |
| 77 | + sscanf(s + 3, "%d,%d,%d", &x, &y, &c); |
| 78 | + // sprintf(temp, "display.drawPixel(%d, %d, %d)\n\r", x, y, c); |
| 79 | + // Serial.print(temp); |
| 80 | + display.drawPixel(x, y, c); |
| 81 | + break; |
| 82 | + |
| 83 | + case '1': |
| 84 | + sscanf(s + 3, "%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &c); |
| 85 | + // sprintf(temp, "display.drawLine(%d, %d, %d, %d, %d)\n\r", x1, y1, x2, y2, c); |
| 86 | + // Serial.print(temp); |
| 87 | + display.drawLine(x1, y1, x2, y2, c); |
| 88 | + break; |
| 89 | + |
| 90 | + case '2': |
| 91 | + sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &l, &c); |
| 92 | + // sprintf(temp, "display.drawFastVLine(%d, %d, %d, %d)\n\r", x, y, l, c); |
| 93 | + // Serial.print(temp); |
| 94 | + display.drawFastVLine(x, y, l, c); |
| 95 | + break; |
| 96 | + |
| 97 | + case '3': |
| 98 | + sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &l, &c); |
| 99 | + // sprintf(temp, "display.drawFastHLine(%d, %d, %d, %d)\n\r", x, y, l, c); |
| 100 | + // Serial.print(temp); |
| 101 | + display.drawFastHLine(x, y, l, c); |
| 102 | + break; |
| 103 | + |
| 104 | + case '4': |
| 105 | + sscanf(s + 3, "%d,%d,%d,%d,%d", &x, &y, &w, &h, &c); |
| 106 | + // sprintf(temp, "display.drawRect(%d, %d, %d, %d, %d)\n\r", x, y, w, h, c); |
| 107 | + // Serial.print(temp); |
| 108 | + display.drawRect(x, y, w, h, c); |
| 109 | + break; |
| 110 | + |
| 111 | + case '5': |
| 112 | + sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &r, &c); |
| 113 | + // sprintf(temp, "display.drawCircle(%d, %d, %d, %d)\n\r", x, y, r, c); |
| 114 | + // Serial.print(temp); |
| 115 | + display.drawCircle(x, y, r, c); |
| 116 | + break; |
| 117 | + |
| 118 | + case '6': |
| 119 | + sscanf(s + 3, "%d,%d,%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &x3, &y3, &c); |
| 120 | + // sprintf(temp, "display.drawTriangle(%d, %d, %d, %d, %d, %d, %d)\n\r", x1, y1, x2, y2, x3, y3, c); |
| 121 | + // Serial.print(temp); |
| 122 | + display.drawTriangle(x1, y1, x2, y2, x3, y3, c); |
| 123 | + break; |
| 124 | + |
| 125 | + case '7': |
| 126 | + sscanf(s + 3, "%d,%d,%d,%d,%d,%d", &x, &y, &w, &h, &r, &c); |
| 127 | + // sprintf(temp, "display.drawRoundRect(%d, %d, %d, %d, %d, %d)\n\r", x, y, w, h, r, c); |
| 128 | + // Serial.print(temp); |
| 129 | + display.drawRoundRect(x, y, w, h, r, c); |
| 130 | + break; |
| 131 | + |
| 132 | + case '8': |
| 133 | + sscanf(s + 3, "%d,%d,%d,%d,%d", &x, &y, &w, &h, &c); |
| 134 | + // sprintf(temp, "display.fillRect(%d, %d, %d, %d, %d)\n\r", x, y, w, h, c); |
| 135 | + // Serial.print(temp); |
| 136 | + display.fillRect(x, y, w, h, c); |
| 137 | + break; |
| 138 | + |
| 139 | + case '9': |
| 140 | + sscanf(s + 3, "%d,%d,%d,%d", &x, &y, &r, &c); |
| 141 | + // sprintf(temp, "display.fillCircle(%d, %d, %d, %d)\n\r", x, y, r, c); |
| 142 | + // Serial.print(temp); |
| 143 | + display.fillCircle(x, y, r, c); |
| 144 | + break; |
| 145 | + |
| 146 | + case 'A': |
| 147 | + sscanf(s + 3, "%d,%d,%d,%d,%d,%d,%d", &x1, &y1, &x2, &y2, &x3, &y3, &c); |
| 148 | + // sprintf(temp, "display.fillTriangle(%d, %d, %d, %d, %d, %d, %d)\n\r", x1, y1, x2, y2, x3, y3, c); |
| 149 | + // Serial.print(temp); |
| 150 | + display.fillTriangle(x1, y1, x2, y2, x3, y3, c); |
| 151 | + break; |
| 152 | + |
| 153 | + case 'B': |
| 154 | + sscanf(s + 3, "%d,%d,%d,%d,%d,%d", &x, &y, &w, &h, &r, &c); |
| 155 | + // sprintf(temp, "display.fillRoundRect(%d, %d, %d, %d, %d, %d)\n\r", x, y, w, h, r, c); |
| 156 | + // Serial.print(temp); |
| 157 | + display.fillRoundRect(x, y, w, h, r, c); |
| 158 | + break; |
| 159 | + |
| 160 | + case 'C': |
| 161 | + sscanf(s + 3, "\"%2000[^\"]\"", strTemp); |
| 162 | + n = strlen(strTemp); |
| 163 | + for (int i = 0; i < n; i++) |
| 164 | + { |
| 165 | + strTemp[i] = toupper(strTemp[i]); |
| 166 | + } |
| 167 | + for (int i = 0; i < n; i += 2) |
| 168 | + { |
| 169 | + strTemp[i / 2] = (hexToChar(strTemp[i]) << 4) | (hexToChar(strTemp[i + 1]) & 0x0F); |
| 170 | + } |
| 171 | + strTemp[n / 2] = 0; |
| 172 | + // Serial.print("display.print(\""); |
| 173 | + // Serial.print(strTemp); |
| 174 | + // Serial.println("\");"); |
| 175 | + display.print(strTemp); |
| 176 | + break; |
| 177 | + |
| 178 | + case 'D': |
| 179 | + sscanf(s + 3, "%d", &c); |
| 180 | + // sprintf(temp, "display.setTextSize(%d)\n", c); |
| 181 | + // Serial.print(temp); |
| 182 | + display.setTextSize(c); |
| 183 | + break; |
| 184 | + |
| 185 | + case 'E': |
| 186 | + sscanf(s + 3, "%d,%d", &x, &y); |
| 187 | + // sprintf(temp, "display.setCursor(%d, %d)\n", x, y); |
| 188 | + // Serial.print(temp); |
| 189 | + display.setCursor(x, y); |
| 190 | + break; |
| 191 | + |
| 192 | + case 'F': |
| 193 | + sscanf(s + 3, "%c", &b); |
| 194 | + // sprintf(temp, "display.setTextWrap(%s)\n", b == 'T' ? "True" : "False"); |
| 195 | + // Serial.print(temp); |
| 196 | + if (b == 'T') |
| 197 | + display.setTextWrap(true); |
| 198 | + if (b == 'F') |
| 199 | + display.setTextWrap(false); |
| 200 | + break; |
| 201 | + |
| 202 | + case 'G': |
| 203 | + sscanf(s + 3, "%d", &c); |
| 204 | + c &= 3; |
| 205 | + // sprintf(temp, "display.setRotation(%d)\n", c); |
| 206 | + // Serial.print(temp); |
| 207 | + display.setRotation(c); |
| 208 | + break; |
| 209 | + |
| 210 | + case 'H': |
| 211 | + sscanf(s + 3, "%d,%d,\"%149[^\"]\"", &x, &y, strTemp); |
| 212 | + n = strlen(strTemp); |
| 213 | + for (int i = 0; i < n; i++) |
| 214 | + { |
| 215 | + strTemp[i] = toupper(strTemp[i]); |
| 216 | + } |
| 217 | + for (int i = 0; i < n; i += 2) |
| 218 | + { |
| 219 | + strTemp[i / 2] = (hexToChar(strTemp[i]) << 4) | (hexToChar(strTemp[i + 1]) & 0x0F); |
| 220 | + } |
| 221 | + strTemp[n / 2] = 0; |
| 222 | + r = display.sdCardInit(); |
| 223 | + if (r) |
| 224 | + { |
| 225 | + r = display.drawBitmapFromSd(strTemp, x, y); |
| 226 | + Serial.print("#H("); |
| 227 | + Serial.print(r, DEC); |
| 228 | + Serial.println(")*"); |
| 229 | + Serial.flush(); |
| 230 | + // sprintf(temp, "display.drawBitmap(%d, %d, %s)\n", x, y, strTemp); |
| 231 | + // Serial.print(temp); |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + Serial.println("#H(-1)*"); |
| 236 | + Serial.flush(); |
| 237 | + } |
| 238 | + break; |
| 239 | + |
| 240 | + case 'K': |
| 241 | + sscanf(s + 3, "%c", &b); |
| 242 | + if (b == '1') |
| 243 | + { |
| 244 | + // Serial.print("display.clearDisplay();\n"); |
| 245 | + display.clearDisplay(); |
| 246 | + } |
| 247 | + break; |
| 248 | + |
| 249 | + case 'L': |
| 250 | + sscanf(s + 3, "%c", &b); |
| 251 | + if (b == '1') |
| 252 | + { |
| 253 | + // Serial.print("display.display();\n"); |
| 254 | + display.display(); |
| 255 | + } |
| 256 | + break; |
| 257 | + |
| 258 | + case 'O': |
| 259 | + sscanf(s + 3, "%d", &c); |
| 260 | + if (c >= 0 && c <= 2) |
| 261 | + { |
| 262 | + Serial.print("#O("); |
| 263 | + Serial.print(display.readTouchpad(c), DEC); |
| 264 | + // Serial.print(0, DEC); |
| 265 | + Serial.println(")*"); |
| 266 | + Serial.flush(); |
| 267 | + } |
| 268 | + break; |
| 269 | + |
| 270 | + case 'P': |
| 271 | + sscanf(s + 3, "%c", &b); |
| 272 | + if (b == '?') |
| 273 | + { |
| 274 | + Serial.print("#P("); |
| 275 | + Serial.print(display.readBattery(), 2); |
| 276 | + // Serial.print(3.54, 2); |
| 277 | + Serial.println(")*"); |
| 278 | + Serial.flush(); |
| 279 | + } |
| 280 | + break; |
| 281 | + } |
| 282 | + *s = 0; |
| 283 | + *e = 0; |
| 284 | + } |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +int hexToChar(char c) |
| 289 | +{ |
| 290 | + if (c >= '0' && c <= '9') |
| 291 | + return c - '0'; |
| 292 | + if (c >= 'A' && c <= 'F') |
| 293 | + return c - 'A' + 10; |
| 294 | + if (c >= 'a' && c <= 'f') |
| 295 | + return c - 'a' + 10; |
| 296 | + return -1; |
| 297 | +} |
| 298 | + |
| 299 | +void showSplashScreen() |
| 300 | +{ |
| 301 | + display.drawBitmap3Bit(0, 0, splash, splash_w, splash_h); |
| 302 | + display.display(); |
| 303 | +} |
| 304 | + |
| 305 | +int checkMicroSDCard() |
| 306 | +{ |
| 307 | + int sdInitOk = 0; |
| 308 | + sdInitOk = display.sdCardInit(); |
| 309 | + |
| 310 | + if (sdInitOk) |
| 311 | + { |
| 312 | + File file; |
| 313 | + |
| 314 | + if (file.open("/testFile.txt", O_CREAT | O_RDWR)) |
| 315 | + { |
| 316 | + file.print(testString); |
| 317 | + file.close(); |
| 318 | + } |
| 319 | + else |
| 320 | + { |
| 321 | + return 0; |
| 322 | + } |
| 323 | + |
| 324 | + delay(250); |
| 325 | + |
| 326 | + if (file.open("/testFile.txt", O_RDWR)) |
| 327 | + { |
| 328 | + char sdCardString[sdCardTestStringLength]; |
| 329 | + file.read(sdCardString, sizeof(sdCardString)); |
| 330 | + sdCardString[file.fileSize()] = 0; |
| 331 | + int stringCompare = strcmp(testString, sdCardString); |
| 332 | + file.remove(); |
| 333 | + file.close(); |
| 334 | + if (stringCompare != 0) |
| 335 | + return 0; |
| 336 | + } |
| 337 | + else |
| 338 | + { |
| 339 | + return 0; |
| 340 | + } |
| 341 | + } |
| 342 | + else |
| 343 | + { |
| 344 | + return 0; |
| 345 | + } |
| 346 | + return 1; |
| 347 | +} |
| 348 | + |
| 349 | +void microSDCardTest() |
| 350 | +{ |
| 351 | + if (!checkMicroSDCard()) |
| 352 | + { |
| 353 | + display.setTextColor(INKPLATE_RED); |
| 354 | + display.setTextSize(3); |
| 355 | + display.setCursor(0, 0); |
| 356 | + display.print("microSD card slot test FAIL"); |
| 357 | + display.display(); |
| 358 | + while(1); |
| 359 | + } |
| 360 | + display.clearDisplay(); |
| 361 | +} |
| 362 | + |
| 363 | +void rtcCheck(uint32_t _rtc) |
| 364 | +{ |
| 365 | + display.rtcGetRtcData(); |
| 366 | + if (_rtc == display.rtcGetEpoch()) |
| 367 | + { |
| 368 | + display.setTextColor(INKPLATE_RED); |
| 369 | + display.setTextSize(3); |
| 370 | + display.setCursor(0, 0); |
| 371 | + display.print("RTC Fail"); |
| 372 | + display.display(); |
| 373 | + while(1); |
| 374 | + } |
| 375 | + display.clearDisplay(); |
| 376 | +} |
| 377 | + |
| 378 | +void expanderCheck() |
| 379 | +{ |
| 380 | + Wire.beginTransmission(0x20); |
| 381 | + uint8_t _ret = Wire.endTransmission(); |
| 382 | + if (_ret != 0) |
| 383 | + { |
| 384 | + display.setTextColor(INKPLATE_RED); |
| 385 | + display.setTextSize(3); |
| 386 | + display.setCursor(0, 0); |
| 387 | + display.print("I/O Expander FAIL"); |
| 388 | + display.display(); |
| 389 | + while(1); |
| 390 | + } |
| 391 | + display.clearDisplay(); |
| 392 | +} |
0 commit comments