selftest.ino 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <GD2.h>
  4. #define UART_SPEED 9600
  5. #include "selftest_assets.h"
  6. #define SCREEN_ADDR 0x30000UL
  7. byte x, y;
  8. static void log(const char*s)
  9. {
  10. while (*s) {
  11. char c = *s++;
  12. #ifndef RASPBERRY_PI
  13. Serial.write(c);
  14. #endif
  15. if (c == '\n') {
  16. x = 0;
  17. y++;
  18. } else {
  19. uint32_t dst = SCREEN_ADDR + (((x + (y * 48)) << 1));
  20. GD.wr16(dst, 0x0f00 | c);
  21. x++;
  22. }
  23. };
  24. }
  25. void setup()
  26. {
  27. Serial.begin(UART_SPEED);
  28. Serial.println("---- GAMEDUINO 2 SELFTEST ----");
  29. GD.begin(0);
  30. }
  31. static void ramp(int y, uint32_t color)
  32. {
  33. GD.ScissorSize(400, 8);
  34. GD.ScissorXY(40, y);
  35. GD.cmd_gradient(40, 0, 0x000000, 440, 0, color);
  36. }
  37. void testcard(int pass, const char *message)
  38. {
  39. // GD.ClearColorRGB(0x204060);
  40. GD.Clear();
  41. GD.cmd_text(240, 12, 28, OPT_CENTER, "Gameduino2 Self test");
  42. int y;
  43. y = 50;
  44. GD.Begin(POINTS);
  45. for (int i = 0; i < 6; i++) {
  46. byte l = 4 << i;
  47. GD.PointSize(280);
  48. GD.ColorRGB(0xffffff);
  49. GD.Vertex2ii(68 * (i + 1), y, 0, 0);
  50. GD.PointSize(240);
  51. GD.ColorRGB(l, l, l);
  52. GD.Vertex2ii(68 * (i + 1), y, 0, 0);
  53. }
  54. y += 30;
  55. ramp(y, 0xff0000); y += 12;
  56. ramp(y, 0x00ff00); y += 12;
  57. ramp(y, 0x0000ff); y += 12;
  58. ramp(y, 0xffffff); y += 12;
  59. GD.RestoreContext();
  60. // GD.Begin(BITMAPS);
  61. // GD.Vertex2ii(0, 272 - (8 * 16), 1, 0);
  62. // GD.Vertex2ii(480 - LENA_WIDTH, 272 - LENA_WIDTH, 0, 0);
  63. if (pass == -1)
  64. GD.ColorRGB(0x808000);
  65. else
  66. GD.ColorRGB(pass ? 0x40ff40 : 0xff4040);
  67. GD.cmd_text(120, 180, 31, OPT_CENTERY, message);
  68. GD.swap();
  69. }
  70. #define SCREENTEST(NAME) \
  71. do { \
  72. Serial.println(#NAME); \
  73. testcard(-1, #NAME); \
  74. r = test_##NAME(); \
  75. const char* msg = r ? (#NAME ": pass") : (#NAME ": FAIL"); \
  76. Serial.println(msg); \
  77. testcard(r, msg); \
  78. while (!r) ; \
  79. } while (0)
  80. int test_ident()
  81. {
  82. byte id = GD.rd(REG_ID);
  83. if (id != 0x7c) {
  84. Serial.println(id, HEX);
  85. return 0;
  86. }
  87. return 1;
  88. }
  89. int test_clock()
  90. {
  91. int SPEEDUP = 8;
  92. GD.rd32(REG_CLOCK); // warm-up
  93. delay(10);
  94. long t1 = GD.rd32(REG_CLOCK);
  95. delay(1000 / SPEEDUP);
  96. long t2 = GD.rd32(REG_CLOCK);
  97. float measured = float(t2 - t1);
  98. // measured should be 48e6, within 2%
  99. float expected = 48e6 / SPEEDUP;
  100. Serial.println(measured, DEC);
  101. Serial.println(expected, DEC);
  102. float diff = measured - expected;
  103. float percent = fabs(100 * (diff / expected));
  104. return percent < 5.0;
  105. }
  106. int test_tune()
  107. {
  108. GD.tune();
  109. return 1;
  110. }
  111. static byte test_RAM(void)
  112. {
  113. uint32_t a;
  114. for (a = 0; a < 0x40000U; a += 947)
  115. GD.wr(a, a);
  116. for (a = 0; a < 0x40000U; a += 947)
  117. if (GD.rd(a) != (a & 0xff))
  118. return 0;
  119. return 1;
  120. }
  121. static byte test_PWM(void)
  122. {
  123. for (int i = 128; i >= 0; i--) {
  124. GD.wr(REG_PWM_DUTY, i);
  125. delay(2);
  126. }
  127. GD.wr(REG_PWM_DUTY, 128);
  128. return 1;
  129. }
  130. static byte test_storage(void)
  131. {
  132. GD.storage();
  133. return test_ident();
  134. }
  135. static byte test_SDcard(void)
  136. {
  137. for (byte i = 0; i < 2; i++) {
  138. GD.safeload("selftest.gd2");
  139. uint32_t pcrc = GD.cmd_memcrc(0, ASSETS_END);
  140. GD.finish();
  141. uint32_t crc = GD.rd32(pcrc);
  142. if (crc != KITTEN_CRC)
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. static int collect(int &rx, int &ry, int &rz)
  148. {
  149. uint16_t ax = 0, ay = 0, az = 0;
  150. for (byte i = 32; i; i--) {
  151. int x = analogRead(A2);
  152. int y = analogRead(A1);
  153. int z = analogRead(A0);
  154. ax += x;
  155. ay += y;
  156. az += z;
  157. }
  158. rx = ax >> 5;
  159. ry = ay >> 5;
  160. rz = az >> 5;
  161. }
  162. static byte test_accel2(void)
  163. {
  164. while (1) {
  165. GD.finish();
  166. if ((millis() % 2000) < 1000)
  167. GD.wr(REG_GPIO, 0x80);
  168. else
  169. GD.wr(REG_GPIO, 0x81);
  170. int x, y, z;
  171. collect(x, y, z);
  172. GD.Clear();
  173. GD.cmd_number(0, 40, 26, 3, x);
  174. GD.cmd_slider(50, 40, 400, 10, 0, x, 512);
  175. GD.cmd_number(0, 70, 26, 3, y);
  176. GD.cmd_slider(50, 70, 400, 10, 0, y, 512);
  177. GD.cmd_number(0, 100, 26, 3, GD.rd(REG_GPIO));
  178. GD.cmd_slider(50, 100, 400, 10, 0, z, 512);
  179. GD.swap();
  180. }
  181. return 1;
  182. }
  183. static byte test_accel(void)
  184. {
  185. int x0, y0, z0;
  186. int x1, y1, z1;
  187. GD.wr(REG_GPIO, 0x80);
  188. collect(x0, y0, z0);
  189. delay(100);
  190. GD.wr(REG_GPIO, 0x81);
  191. delay(100);
  192. collect(x1, y1, z1);
  193. Serial.print(x0); Serial.print(" "); Serial.print(y0); Serial.print(" "); Serial.println(z0);
  194. Serial.print(x1); Serial.print(" "); Serial.print(y1); Serial.print(" "); Serial.println(z1);
  195. // if ((x0 > x1) || (y0 > y1) || (z0 > z1)) return 0;
  196. int d;
  197. d = abs(x0 - x1);
  198. if ((d < 30) || (120 < d))
  199. return 0;
  200. d = abs(y0 - y1);
  201. if ((d < 30) || (120 < d))
  202. return 0;
  203. d = abs(z0 - z1);
  204. if ((d < 50) || (200 < d))
  205. return 0;
  206. z0 %= 37;
  207. while (z0--)
  208. GD.random();
  209. return 1;
  210. }
  211. static void play(uint16_t n)
  212. {
  213. GD.wr16(REG_SOUND, n);
  214. GD.wr(REG_PLAY, 1);
  215. }
  216. static void play_wait(uint16_t n)
  217. {
  218. play(n);
  219. while (GD.rd(REG_PLAY))
  220. ;
  221. }
  222. static byte test_touch(void)
  223. {
  224. GD.Clear();
  225. GD.cmd_text(240, 100, 30, OPT_CENTERX, "please tap on the dot");
  226. GD.self_calibrate();
  227. // write the new calibration back to EEPROM
  228. #if !defined(RASPBERRY_PI) && !defined(__DUE__)
  229. for (int i = 0; i < 24; i++)
  230. EEPROM.write(1 + i, GD.rd(REG_TOUCH_TRANSFORM_A + i));
  231. #endif
  232. byte hit = 0;
  233. while (hit != 0x0f) {
  234. GD.finish();
  235. byte tag = GD.rd(REG_TOUCH_TAG);
  236. if ((1 <= tag) && (tag <= 4)) {
  237. play(0x50);
  238. hit |= (1 << (tag - 1));
  239. }
  240. if (tag == 77)
  241. return 0;
  242. GD.ClearTag(77);
  243. GD.Clear();
  244. GD.PointSize(20 * 16);
  245. GD.Begin(POINTS);
  246. for (byte i = 1; i <= 4; i++) {
  247. if (hit & (1 << (i - 1))) {
  248. GD.ColorRGB(0x00ff00);
  249. GD.Tag(0xff);
  250. } else {
  251. GD.ColorRGB(0x808080);
  252. GD.Tag(i);
  253. }
  254. switch (i) {
  255. case 1: GD.Vertex2ii(20, 20, 0, 0); break;
  256. case 2: GD.Vertex2ii(460, 20, 0, 0); break;
  257. case 3: GD.Vertex2ii(20, 250, 0, 0); break;
  258. case 4: GD.Vertex2ii(460, 250, 0, 0); break;
  259. }
  260. }
  261. GD.random(); // scramble PRN state for later
  262. GD.swap();
  263. }
  264. return 1;
  265. }
  266. static const PROGMEM uint32_t digits[11] = {
  267. DIGIT_0,
  268. DIGIT_1,
  269. DIGIT_2,
  270. DIGIT_3,
  271. DIGIT_4,
  272. DIGIT_5,
  273. DIGIT_6,
  274. DIGIT_7,
  275. DIGIT_8,
  276. DIGIT_9,
  277. DIGIT_9 + DIGIT_9_LENGTH
  278. };
  279. static void saydigit(byte n)
  280. {
  281. GD.wr32(REG_PLAYBACK_FREQ, 8000);
  282. GD.wr32(REG_PLAYBACK_FORMAT, ADPCM_SAMPLES);
  283. uint32_t dstart = pgm_read_dword(digits + n);
  284. uint32_t dend = pgm_read_dword(digits + n + 1);
  285. GD.wr32(REG_PLAYBACK_START, dstart);
  286. GD.wr32(REG_PLAYBACK_LENGTH, dend - dstart);
  287. GD.wr(REG_PLAYBACK_PLAY, 1);
  288. }
  289. static void blank(int n)
  290. {
  291. for (int i = 0; i < n; i++) {
  292. GD.get_inputs();
  293. GD.cmd_gradient(0, 0, 0xb0b0a0, 0, 272, 0x404040);
  294. GD.swap();
  295. }
  296. }
  297. static byte getkey()
  298. {
  299. byte prev_tag;
  300. do {
  301. prev_tag = GD.inputs.tag;
  302. GD.get_inputs();
  303. if (GD.inputs.x & 1)
  304. GD.random();
  305. GD.cmd_gradient(0, 0, 0xb0b0a0, 0, 272, 0x404040);
  306. for (int i = 0; i < 9; i++) {
  307. byte digit = i + 1;
  308. int x = 120 + 80 * (i % 3);
  309. int y = 20 + 80 * (i / 3);
  310. GD.Tag(digit);
  311. char msg[2] = { '0' + digit, 0 };
  312. GD.cmd_fgcolor((digit == GD.inputs.tag) ? 0xc08000 : 0x003870);
  313. GD.cmd_button(x, y, 70, 70, 31, 0, msg);
  314. }
  315. GD.swap();
  316. } while (!((GD.inputs.tag == 0) && (1 <= prev_tag) && (prev_tag <= 9)));
  317. return prev_tag;
  318. }
  319. static byte test_audio(void)
  320. {
  321. // Stir up the PRN
  322. for (int i = micros() % 97; i; i--)
  323. GD.random();
  324. blank(20);
  325. for (int i = 0; i < 3; i++) {
  326. byte d = 1 + GD.random(9);
  327. saydigit(d);
  328. blank(12);
  329. if (getkey() != d)
  330. return 0;
  331. }
  332. return 1;
  333. }
  334. static struct {
  335. byte t, note;
  336. } pacman[] = {
  337. { 0, 71 },
  338. { 2, 83 },
  339. { 4, 78 },
  340. { 6, 75 },
  341. { 8, 83 },
  342. { 9, 78 },
  343. { 12, 75 },
  344. { 16, 72 },
  345. { 18, 84 },
  346. { 20, 79 },
  347. { 22, 76 },
  348. { 24, 84 },
  349. { 25, 79 },
  350. { 28, 76 },
  351. { 32, 71 },
  352. { 34, 83 },
  353. { 36, 78 },
  354. { 38, 75 },
  355. { 40, 83 },
  356. { 41, 78 },
  357. { 44, 75 },
  358. { 48, 75 },
  359. { 49, 76 },
  360. { 50, 77 },
  361. { 52, 77 },
  362. { 53, 78 },
  363. { 54, 79 },
  364. { 56, 79 },
  365. { 57, 80 },
  366. { 58, 81 },
  367. { 60, 83 },
  368. { 255, 255 }
  369. };
  370. void loop()
  371. {
  372. x = y = 0;
  373. testcard(1, "Starting tests");
  374. GD.finish();
  375. Serial.println("Starting self-test");
  376. byte r, pass = 1;
  377. {
  378. SCREENTEST(ident);
  379. SCREENTEST(tune);
  380. SCREENTEST(clock);
  381. SCREENTEST(RAM);
  382. SCREENTEST(PWM);
  383. if (1) {
  384. SCREENTEST(storage);
  385. SCREENTEST(SDcard);
  386. }
  387. // SCREENTEST(accel);
  388. if (1) {
  389. SCREENTEST(touch);
  390. SCREENTEST(audio);
  391. }
  392. testcard(1, "* ALL PASS *");
  393. {
  394. byte i = 0, t = 0;
  395. for (;;) {
  396. if (t == pacman[i].t)
  397. GD.play(HARP, pacman[i++].note - 12);
  398. delay(65);
  399. if (++t == 64) {
  400. t = 0;
  401. i = 0;
  402. }
  403. }
  404. }
  405. }
  406. if (pass) {
  407. char msg[60];
  408. log("All tests passed\n");
  409. long seconds = millis() / 1000;
  410. int minutes = seconds / 60;
  411. sprintf(msg, "%d minutes", minutes);
  412. log(msg);
  413. } else {
  414. for (;;)
  415. ;
  416. }
  417. delay(5000);
  418. }