|
| 1 | +#include "test.h" |
| 2 | + |
| 3 | +const char sdCardTestStringLength = 100; |
| 4 | +const char *testString = {"This is some test string..."}; |
| 5 | + |
| 6 | +const char *WSSID = {""}; |
| 7 | +const char *WPASS = {""}; |
| 8 | + |
| 9 | +void testPeripheral(uint8_t _skipSecondIO) |
| 10 | +{ |
| 11 | + // Set display for test report |
| 12 | + display.setTextSize(4); |
| 13 | + display.setTextColor(BLACK); |
| 14 | + display.setCursor(0, 0); |
| 15 | + display.println("INKPLATE CHECKLIST"); |
| 16 | + |
| 17 | + // Power up epaper PSU |
| 18 | + display.einkOn(); |
| 19 | + |
| 20 | + // Check if epaper PSU (TPS65186 EPD PMIC) is ok. |
| 21 | + Wire.beginTransmission(0x48); // Send address 0x48 on I2C |
| 22 | + if (!(Wire.endTransmission() == 0) || !(display.readPowerGood())) // Check if there was an error in communication |
| 23 | + { |
| 24 | + Serial.println("- TPS Fail!"); |
| 25 | + failHandler(); |
| 26 | + } |
| 27 | + display.println("- TPS65186: OK"); |
| 28 | + display.partialUpdate(0, 1); |
| 29 | + |
| 30 | + // IF needed, check of the second I/O expander can be skipped. |
| 31 | + int _n = _skipSecondIO? 1 : 2; |
| 32 | + for (int i = 0; i < _n; i++) |
| 33 | + { |
| 34 | + // Check I/O expander |
| 35 | + display.printf("- I/O Expander %d: ", i + 1); |
| 36 | + display.partialUpdate(0, 1); |
| 37 | + |
| 38 | + // Try to communicate with I/O expander |
| 39 | + Wire.beginTransmission(0x20 + (i * 2)); // Send address 0x20 or 0x22 |
| 40 | + if (Wire.endTransmission() == 0) // Check if there was an error in communication and print out the results on display. |
| 41 | + { |
| 42 | + display.println("OK"); |
| 43 | + display.partialUpdate(0, 1); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + // Hang the code only if first MCP is not found. |
| 48 | + display.println("FAIL"); |
| 49 | + failHandler(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Check the micro SD card slot |
| 54 | + display.print("- microSD card slot: "); |
| 55 | + display.partialUpdate(0, 1); |
| 56 | + if (checkMicroSDCard()) |
| 57 | + { |
| 58 | + display.println("OK"); |
| 59 | + display.partialUpdate(0, 1); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + display.println("FAIL"); |
| 64 | + failHandler(); |
| 65 | + } |
| 66 | + |
| 67 | + // Check the WiFi |
| 68 | + display.print("- WiFi: "); |
| 69 | + display.partialUpdate(0, 1); |
| 70 | + if (checkWiFi(WSSID, WPASS, WTIMEOUT)) |
| 71 | + { |
| 72 | + display.println("OK"); |
| 73 | + display.partialUpdate(0, 1); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + display.println("FAIL"); |
| 78 | + failHandler(); |
| 79 | + } |
| 80 | + |
| 81 | + // Check the RTC |
| 82 | + display.print("- PCF85063 RTC: "); |
| 83 | + if (rtcCheck()) |
| 84 | + { |
| 85 | + display.println("OK"); |
| 86 | + display.partialUpdate(0, 1); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + display.println("FAIL"); |
| 91 | + failHandler(); |
| 92 | + } |
| 93 | + |
| 94 | + // This test only must be run on older Inkplates (e-radionica.com Inkplates with touchpads) |
| 95 | +#ifdef ARDUINO_INKPLATE10 |
| 96 | + // Test Touchpads |
| 97 | + if (touchPads(TOUCHPADS_TIMEOUT)) |
| 98 | + { |
| 99 | + display.println(" OK"); |
| 100 | + display.partialUpdate(0, 1); |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + display.println(" FAIL"); |
| 105 | + failHandler(); |
| 106 | + } |
| 107 | +#endif |
| 108 | +} |
| 109 | + |
| 110 | +double getVCOMFromSerial(double *_vcom) |
| 111 | +{ |
| 112 | + double vcom = 1; |
| 113 | + char serialBuffer[50]; |
| 114 | + unsigned long serialTimeout; |
| 115 | + |
| 116 | + // Display a message on Inkplate |
| 117 | + display.print("\r\n- Write VCOM on UART: "); |
| 118 | + display.partialUpdate(0, 1); |
| 119 | + |
| 120 | + while (true) |
| 121 | + { |
| 122 | + Serial.println("Write VCOM voltage from epaper panel.\r\nDon't forget negative (-) sign!\r\nUse dot as the decimal point. For example -1.23\n"); |
| 123 | + while (!Serial.available()); |
| 124 | + |
| 125 | + serialTimeout = millis(); |
| 126 | + int i = 0; |
| 127 | + while ((Serial.available()) && ((unsigned long)(millis() - serialTimeout) < 500)) |
| 128 | + { |
| 129 | + if ((Serial.available()) && (i < 49)) |
| 130 | + { |
| 131 | + serialBuffer[i++] = Serial.read(); |
| 132 | + serialTimeout = millis(); |
| 133 | + } |
| 134 | + } |
| 135 | + serialBuffer[i] = 0; |
| 136 | + if (sscanf(serialBuffer, "%lf", &vcom) == 1) |
| 137 | + { |
| 138 | + *_vcom = vcom; |
| 139 | + return 1; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +int checkWiFi(const char *_ssid, const char *_pass, uint8_t _wifiTimeout) |
| 147 | +{ |
| 148 | + unsigned long _timeout = millis(); |
| 149 | + |
| 150 | + // Try to connect to WiFi network |
| 151 | + WiFi.begin(WSSID, WPASS); |
| 152 | + |
| 153 | + // Wait until WiFi connection is established or timeout has occured. |
| 154 | + while ((WiFi.status() != WL_CONNECTED) && ((unsigned long)(millis() - _timeout) < (_wifiTimeout * 1000UL))); |
| 155 | + |
| 156 | + // Check if board is connected to WiFi |
| 157 | + if (WiFi.status() == WL_CONNECTED) |
| 158 | + { |
| 159 | + return 1; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + return 0; |
| 164 | + } |
| 165 | + |
| 166 | + // Something is wrong if you got there. |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +int checkMicroSDCard() |
| 171 | +{ |
| 172 | + int sdInitOk = 0; |
| 173 | + sdInitOk = display.sdCardInit(); |
| 174 | + |
| 175 | + if (sdInitOk) |
| 176 | + { |
| 177 | + File file; |
| 178 | + |
| 179 | + if (file.open("/testFile.txt", O_CREAT | O_RDWR)) |
| 180 | + { |
| 181 | + file.print(testString); |
| 182 | + file.close(); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + return 0; |
| 187 | + } |
| 188 | + |
| 189 | + delay(250); |
| 190 | + |
| 191 | + if (file.open("/testFile.txt", O_RDWR)) |
| 192 | + { |
| 193 | + char sdCardString[sdCardTestStringLength]; |
| 194 | + file.read(sdCardString, sizeof(sdCardString)); |
| 195 | + sdCardString[file.fileSize()] = 0; |
| 196 | + int stringCompare = strcmp(testString, sdCardString); |
| 197 | + file.remove(); |
| 198 | + file.close(); |
| 199 | + if (stringCompare != 0) |
| 200 | + return 0; |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + return 0; |
| 205 | + } |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + return 0; |
| 210 | + } |
| 211 | + return 1; |
| 212 | +} |
| 213 | + |
| 214 | +int rtcCheck() |
| 215 | +{ |
| 216 | + // First "ping" RTC on the I2C protocol and reset the RTC |
| 217 | + Wire.beginTransmission(0x51); |
| 218 | + int _res = Wire.endTransmission(); |
| 219 | + |
| 220 | + // If result is from I2C is anything else than success (_res = 0), return 0 (error). |
| 221 | + if (_res != 0) return 0; |
| 222 | + |
| 223 | + // Reset and re-init RTC. |
| 224 | + display.rtcReset(); |
| 225 | + |
| 226 | + // Set some time in epoch in RTC. |
| 227 | + uint32_t _epoch = 1640995200ULL; |
| 228 | + display.rtcSetEpoch(_epoch); |
| 229 | + |
| 230 | + // Wait at least one and a half second |
| 231 | + delay(1500); |
| 232 | + |
| 233 | + // Read the epoch (if everything is ok, epoch from RTC should not be the same) |
| 234 | + if (display.rtcGetEpoch() != _epoch) |
| 235 | + { |
| 236 | + return 1; |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + return 0; |
| 241 | + } |
| 242 | + |
| 243 | + return 0; |
| 244 | +} |
| 245 | + |
| 246 | +int touchPads(uint8_t _timeoutTouchpads) |
| 247 | +{ |
| 248 | + // This test only must be run on older Inkplates (e-radionica.com Inkplates with touchpads) |
| 249 | +#ifdef ARDUINO_INKPLATE10 |
| 250 | + // Variable for storing detected touch |
| 251 | + int _flags = 0; |
| 252 | + |
| 253 | + // Show message on display |
| 254 | + display.print("- Touchpads [Press them!]: "); |
| 255 | + display.partialUpdate(0, 1); |
| 256 | + |
| 257 | + // Wait until all touchpads are pressed at least once in test. |
| 258 | + |
| 259 | + unsigned long _timeout = millis(); |
| 260 | + while ((_flags != 0b00000111) && ((unsigned long)(millis() - _timeout) < (_timeoutTouchpads * 1000UL))) |
| 261 | + { |
| 262 | + for (int i = 0; i < 3; i++) |
| 263 | + { |
| 264 | + if (display.readTouchpad(PAD1 + i) && (!(_flags & (1 << i)))) |
| 265 | + { |
| 266 | + _flags |= (1 << i); |
| 267 | + display.print(i + 1, DEC); |
| 268 | + display.partialUpdate(0, 1); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + if ((millis() - _timeout) >= (_timeoutTouchpads * 1000UL)) |
| 274 | + { |
| 275 | + return 0; |
| 276 | + } |
| 277 | + else |
| 278 | + { |
| 279 | + return 1; |
| 280 | + } |
| 281 | + |
| 282 | + return 0; |
| 283 | +#endif |
| 284 | + return 1; |
| 285 | +} |
| 286 | + |
| 287 | +// Show a message and stop the code from executing. |
| 288 | +void failHandler() |
| 289 | +{ |
| 290 | + display.print(" -> Test stopped!"); |
| 291 | + display.partialUpdate(0, 1); |
| 292 | + |
| 293 | + // Inf. loop... halt the program! |
| 294 | + while (true); |
| 295 | +} |
0 commit comments