selftest.ino 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. if (!GD.load("selftest.gd2"))
  139. return 0;
  140. uint32_t pcrc = GD.cmd_memcrc(0, ASSETS_END);
  141. GD.finish();
  142. uint32_t crc = GD.rd32(pcrc);
  143. if (crc != KITTEN_CRC)
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. static int collect(int &rx, int &ry, int &rz)
  149. {
  150. uint16_t ax = 0, ay = 0, az = 0;
  151. for (byte i = 32; i; i--) {
  152. int x = analogRead(A2);
  153. int y = analogRead(A1);
  154. int z = analogRead(A0);
  155. ax += x;
  156. ay += y;
  157. az += z;
  158. }
  159. rx = ax >> 5;
  160. ry = ay >> 5;
  161. rz = az >> 5;
  162. }
  163. static byte test_accel2(void)
  164. {
  165. while (1) {
  166. GD.finish();
  167. if ((millis() % 2000) < 1000)
  168. GD.wr(REG_GPIO, 0x80);
  169. else
  170. GD.wr(REG_GPIO, 0x81);
  171. int x, y, z;
  172. collect(x, y, z);
  173. GD.Clear();
  174. GD.cmd_number(0, 40, 26, 3, x);
  175. GD.cmd_slider(50, 40, 400, 10, 0, x, 512);
  176. GD.cmd_number(0, 70, 26, 3, y);
  177. GD.cmd_slider(50, 70, 400, 10, 0, y, 512);
  178. GD.cmd_number(0, 100, 26, 3, GD.rd(REG_GPIO));
  179. GD.cmd_slider(50, 100, 400, 10, 0, z, 512);
  180. GD.swap();
  181. }
  182. return 1;
  183. }
  184. static byte test_accel(void)
  185. {
  186. int x0, y0, z0;
  187. int x1, y1, z1;
  188. GD.wr(REG_GPIO, 0x80);
  189. collect(x0, y0, z0);
  190. delay(100);
  191. GD.wr(REG_GPIO, 0x81);
  192. delay(100);
  193. collect(x1, y1, z1);
  194. Serial.print(x0); Serial.print(" "); Serial.print(y0); Serial.print(" "); Serial.println(z0);
  195. Serial.print(x1); Serial.print(" "); Serial.print(y1); Serial.print(" "); Serial.println(z1);
  196. // if ((x0 > x1) || (y0 > y1) || (z0 > z1)) return 0;
  197. int d;
  198. d = abs(x0 - x1);
  199. if ((d < 30) || (120 < d))
  200. return 0;
  201. d = abs(y0 - y1);
  202. if ((d < 30) || (120 < d))
  203. return 0;
  204. d = abs(z0 - z1);
  205. if ((d < 50) || (200 < d))
  206. return 0;
  207. z0 %= 37;
  208. while (z0--)
  209. GD.random();
  210. return 1;
  211. }
  212. static void play(uint16_t n)
  213. {
  214. GD.wr16(REG_SOUND, n);
  215. GD.wr(REG_PLAY, 1);
  216. }
  217. static void play_wait(uint16_t n)
  218. {
  219. play(n);
  220. while (GD.rd(REG_PLAY))
  221. ;
  222. }
  223. static byte test_touch(void)
  224. {
  225. GD.Clear();
  226. GD.cmd_text(240, 100, 30, OPT_CENTERX, "please tap on the dot");
  227. GD.self_calibrate();
  228. // write the new calibration back to EEPROM
  229. #ifndef RASPBERRY_PI
  230. for (int i = 0; i < 24; i++)
  231. EEPROM.write(1 + i, GD.rd(REG_TOUCH_TRANSFORM_A + i));
  232. #endif
  233. byte hit = 0;
  234. while (hit != 0x0f) {
  235. GD.finish();
  236. byte tag = GD.rd(REG_TOUCH_TAG);
  237. if ((1 <= tag) && (tag <= 4)) {
  238. play(0x50);
  239. hit |= (1 << (tag - 1));
  240. }
  241. if (tag == 77)
  242. return 0;
  243. GD.ClearTag(77);
  244. GD.Clear();
  245. GD.PointSize(20 * 16);
  246. GD.Begin(POINTS);
  247. for (byte i = 1; i <= 4; i++) {
  248. if (hit & (1 << (i - 1))) {
  249. GD.ColorRGB(0x00ff00);
  250. GD.Tag(0xff);
  251. } else {
  252. GD.ColorRGB(0x808080);
  253. GD.Tag(i);
  254. }
  255. switch (i) {
  256. case 1: GD.Vertex2ii(20, 20, 0, 0); break;
  257. case 2: GD.Vertex2ii(460, 20, 0, 0); break;
  258. case 3: GD.Vertex2ii(20, 250, 0, 0); break;
  259. case 4: GD.Vertex2ii(460, 250, 0, 0); break;
  260. }
  261. }
  262. GD.random(); // scramble PRN state for later
  263. GD.swap();
  264. }
  265. return 1;
  266. }
  267. static const PROGMEM uint32_t digits[11] = {
  268. DIGIT_0,
  269. DIGIT_1,
  270. DIGIT_2,
  271. DIGIT_3,
  272. DIGIT_4,
  273. DIGIT_5,
  274. DIGIT_6,
  275. DIGIT_7,
  276. DIGIT_8,
  277. DIGIT_9,
  278. DIGIT_9 + DIGIT_9_LENGTH
  279. };
  280. static void saydigit(byte n)
  281. {
  282. GD.wr32(REG_PLAYBACK_FREQ, 8000);
  283. GD.wr32(REG_PLAYBACK_FORMAT, ADPCM_SAMPLES);
  284. uint32_t dstart = pgm_read_dword(digits + n);
  285. uint32_t dend = pgm_read_dword(digits + n + 1);
  286. GD.wr32(REG_PLAYBACK_START, dstart);
  287. GD.wr32(REG_PLAYBACK_LENGTH, dend - dstart);
  288. GD.wr(REG_PLAYBACK_PLAY, 1);
  289. }
  290. static void blank(int n)
  291. {
  292. for (int i = 0; i < n; i++) {
  293. GD.get_inputs();
  294. GD.cmd_gradient(0, 0, 0xb0b0a0, 0, 272, 0x404040);
  295. GD.swap();
  296. }
  297. }
  298. static byte getkey()
  299. {
  300. byte prev_tag;
  301. do {
  302. prev_tag = GD.inputs.tag;
  303. GD.get_inputs();
  304. if (GD.inputs.x & 1)
  305. GD.random();
  306. GD.cmd_gradient(0, 0, 0xb0b0a0, 0, 272, 0x404040);
  307. for (int i = 0; i < 9; i++) {
  308. byte digit = i + 1;
  309. int x = 120 + 80 * (i % 3);
  310. int y = 20 + 80 * (i / 3);
  311. GD.Tag(digit);
  312. char msg[2] = { '0' + digit, 0 };
  313. GD.cmd_fgcolor((digit == GD.inputs.tag) ? 0xc08000 : 0x003870);
  314. GD.cmd_button(x, y, 70, 70, 31, 0, msg);
  315. }
  316. GD.swap();
  317. } while (!((GD.inputs.tag == 0) && (1 <= prev_tag) && (prev_tag <= 9)));
  318. return prev_tag;
  319. }
  320. static byte test_audio(void)
  321. {
  322. // Stir up the PRN
  323. for (int i = micros() % 97; i; i--)
  324. GD.random();
  325. blank(20);
  326. for (int i = 0; i < 3; i++) {
  327. byte d = 1 + GD.random(9);
  328. saydigit(d);
  329. blank(12);
  330. if (getkey() != d)
  331. return 0;
  332. }
  333. return 1;
  334. }
  335. static struct {
  336. byte t, note;
  337. } pacman[] = {
  338. { 0, 71 },
  339. { 2, 83 },
  340. { 4, 78 },
  341. { 6, 75 },
  342. { 8, 83 },
  343. { 9, 78 },
  344. { 12, 75 },
  345. { 16, 72 },
  346. { 18, 84 },
  347. { 20, 79 },
  348. { 22, 76 },
  349. { 24, 84 },
  350. { 25, 79 },
  351. { 28, 76 },
  352. { 32, 71 },
  353. { 34, 83 },
  354. { 36, 78 },
  355. { 38, 75 },
  356. { 40, 83 },
  357. { 41, 78 },
  358. { 44, 75 },
  359. { 48, 75 },
  360. { 49, 76 },
  361. { 50, 77 },
  362. { 52, 77 },
  363. { 53, 78 },
  364. { 54, 79 },
  365. { 56, 79 },
  366. { 57, 80 },
  367. { 58, 81 },
  368. { 60, 83 },
  369. { 255, 255 }
  370. };
  371. void loop()
  372. {
  373. x = y = 0;
  374. testcard(1, "Starting tests");
  375. GD.finish();
  376. Serial.println("Starting self-test");
  377. byte r, pass = 1;
  378. {
  379. SCREENTEST(ident);
  380. SCREENTEST(tune);
  381. SCREENTEST(clock);
  382. SCREENTEST(RAM);
  383. SCREENTEST(PWM);
  384. if (1) {
  385. SCREENTEST(storage);
  386. SCREENTEST(SDcard);
  387. }
  388. // SCREENTEST(accel);
  389. if (1) {
  390. SCREENTEST(touch);
  391. SCREENTEST(audio);
  392. }
  393. testcard(1, "* ALL PASS *");
  394. {
  395. byte i = 0, t = 0;
  396. for (;;) {
  397. if (t == pacman[i].t)
  398. GD.play(HARP, pacman[i++].note - 12);
  399. delay(65);
  400. if (++t == 64) {
  401. t = 0;
  402. i = 0;
  403. }
  404. }
  405. }
  406. }
  407. if (pass) {
  408. char msg[60];
  409. log("All tests passed\n");
  410. long seconds = millis() / 1000;
  411. int minutes = seconds / 60;
  412. sprintf(msg, "%d minutes", minutes);
  413. log(msg);
  414. } else {
  415. for (;;)
  416. ;
  417. }
  418. delay(5000);
  419. }