GD2.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. #include <Arduino.h>
  2. #include "SPI.h"
  3. #if !defined(__SAM3X8E__)
  4. #include "EEPROM.h"
  5. #endif
  6. #define VERBOSE 0
  7. #include <GD2.h>
  8. #define SD_PIN 9 // pin used for the microSD enable signal
  9. #define BOARD_FTDI_80x 0
  10. #define BOARD_GAMEDUINO23 1
  11. #define BOARD_EVITA_0 2
  12. #define BOARD BOARD_GAMEDUINO23 // board, from above
  13. #define STORAGE 1 // Want SD storage?
  14. #define CALIBRATION 1 // Want touchscreen?
  15. // EVITA_0 has no storage or calibration
  16. #if (BOARD == BOARD_EVITA_0)
  17. // #undef STORAGE
  18. // #define STORAGE 0
  19. #undef CALIBRATION
  20. #define CALIBRATION 0
  21. #endif
  22. // FTDI boards do not have storage
  23. #if (BOARD == BOARD_FTDI_80x)
  24. #undef STORAGE
  25. #define STORAGE 0
  26. #endif
  27. #ifdef DUMPDEV
  28. #include <assert.h>
  29. #include "transports/dump.h"
  30. #endif
  31. #ifdef RASPBERRY_PI
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <stdint.h>
  38. #include <sys/ioctl.h>
  39. #include <linux/types.h>
  40. #include <linux/spi/spidev.h>
  41. #include "transports/spidev.h"
  42. #endif
  43. byte ft8xx_model;
  44. #if defined(ARDUINO)
  45. #include "transports/wiring.h"
  46. #endif
  47. static GDTransport GDTR;
  48. GDClass GD;
  49. // The GD3 has a tiny configuration EEPROM - AT24C01D
  50. // It is programmed at manufacturing time with the setup
  51. // commands for the connected panel. The SCL,SDA lines
  52. // are connected to thye FT81x GPIO0, GPIO1 signals.
  53. // This is a read-only driver for it. A single method
  54. // 'read()' initializes the RAM and reads all 128 bytes
  55. // into an array.
  56. class ConfigRam {
  57. private:
  58. uint8_t gpio, gpio_dir, sda;
  59. void set_SDA(byte n)
  60. {
  61. if (sda != n) {
  62. GDTR.__wr16(REG_GPIO_DIR, gpio_dir | (0x03 - n)); // Drive SCL, SDA low
  63. sda = n;
  64. }
  65. }
  66. void set_SCL(byte n)
  67. {
  68. GDTR.__wr16(REG_GPIO, gpio | (n << 1));
  69. }
  70. int get_SDA(void)
  71. {
  72. return GDTR.__rd16(REG_GPIO) & 1;
  73. }
  74. void i2c_start(void)
  75. {
  76. set_SDA(1);
  77. set_SCL(1);
  78. set_SDA(0);
  79. set_SCL(0);
  80. }
  81. void i2c_stop(void)
  82. {
  83. set_SDA(0);
  84. set_SCL(1);
  85. set_SDA(1);
  86. set_SCL(1);
  87. }
  88. int i2c_rx1()
  89. {
  90. set_SDA(1);
  91. set_SCL(1);
  92. byte r = get_SDA();
  93. set_SCL(0);
  94. return r;
  95. }
  96. void i2c_tx1(byte b)
  97. {
  98. set_SDA(b);
  99. set_SCL(1);
  100. set_SCL(0);
  101. }
  102. int i2c_tx(byte x)
  103. {
  104. for (byte i = 0; i < 8; i++, x <<= 1)
  105. i2c_tx1(x >> 7);
  106. return i2c_rx1();
  107. }
  108. int i2c_rx(int nak)
  109. {
  110. byte r = 0;
  111. for (byte i = 0; i < 8; i++)
  112. r = (r << 1) | i2c_rx1();
  113. i2c_tx1(nak);
  114. return r;
  115. }
  116. public:
  117. void read(byte *v)
  118. {
  119. GDTR.__end();
  120. gpio = GDTR.__rd16(REG_GPIO) & ~3;
  121. gpio_dir = GDTR.__rd16(REG_GPIO_DIR) & ~3;
  122. sda = 2;
  123. // 2-wire software reset
  124. i2c_start();
  125. i2c_rx(1);
  126. i2c_start();
  127. i2c_stop();
  128. int ADDR = 0xa0;
  129. i2c_start();
  130. if (i2c_tx(ADDR))
  131. return;
  132. if (i2c_tx(0))
  133. return;
  134. i2c_start();
  135. if (i2c_tx(ADDR | 1))
  136. return;
  137. for (int i = 0; i < 128; i++) {
  138. *v++ = i2c_rx(i == 127);
  139. // Serial.println(v[-1], DEC);
  140. }
  141. i2c_stop();
  142. GDTR.resume();
  143. }
  144. };
  145. void GDClass::flush(void)
  146. {
  147. GDTR.flush();
  148. }
  149. void GDClass::swap(void) {
  150. Display();
  151. cmd_swap();
  152. cmd_loadidentity();
  153. cmd_dlstart();
  154. GDTR.flush();
  155. #ifdef DUMPDEV
  156. GDTR.swap();
  157. #endif
  158. }
  159. uint32_t GDClass::measure_freq(void)
  160. {
  161. unsigned long t0 = GDTR.rd32(REG_CLOCK);
  162. delayMicroseconds(15625);
  163. unsigned long t1 = GDTR.rd32(REG_CLOCK);
  164. // Serial.println((t1 - t0) << 6);
  165. return (t1 - t0) << 6;
  166. }
  167. #define LOW_FREQ_BOUND 47040000UL
  168. // #define LOW_FREQ_BOUND 32040000UL
  169. void GDClass::tune(void)
  170. {
  171. uint32_t f;
  172. for (byte i = 0; (i < 31) && ((f = measure_freq()) < LOW_FREQ_BOUND); i++) {
  173. GDTR.wr(REG_TRIM, i);
  174. }
  175. GDTR.wr32(REG_FREQUENCY, f);
  176. }
  177. void GDClass::begin(uint8_t options) {
  178. #if STORAGE && defined(ARDUINO)
  179. GDTR.begin0();
  180. if (options & GD_STORAGE) {
  181. GDTR.ios();
  182. SD.begin(SD_PIN);
  183. }
  184. #endif
  185. GDTR.begin1();
  186. #if 0
  187. Serial.println("ID REGISTER:");
  188. Serial.println(GDTR.rd(REG_ID), HEX);
  189. #endif
  190. #if (BOARD == BOARD_FTDI_80x)
  191. GDTR.wr(REG_PCLK_POL, 1);
  192. GDTR.wr(REG_PCLK, 5);
  193. #endif
  194. GDTR.wr(REG_PWM_DUTY, 0);
  195. GDTR.wr(REG_GPIO_DIR, 0x83);
  196. GDTR.wr(REG_GPIO, GDTR.rd(REG_GPIO) | 0x80);
  197. #if (BOARD == BOARD_GAMEDUINO23)
  198. ConfigRam cr;
  199. byte v8[128] = {0};
  200. cr.read(v8);
  201. if ((v8[1] == 0xff) && (v8[2] == 0x01)) {
  202. options &= ~(GD_TRIM | GD_CALIBRATE);
  203. if (v8[3] & 2) {
  204. GDTR.__end();
  205. GDTR.hostcmd(0x44); // switch to external crystal
  206. GDTR.resume();
  207. }
  208. copyram(v8 + 4, 124);
  209. finish();
  210. } else {
  211. GDTR.wr(REG_PCLK_POL, 1);
  212. GDTR.wr(REG_PCLK, 5);
  213. GDTR.wr(REG_ROTATE, 1);
  214. GDTR.wr(REG_SWIZZLE, 3);
  215. }
  216. #endif
  217. if (0) {
  218. GDTR.wr16(REG_HCYCLE, 928);
  219. GDTR.wr16(REG_HOFFSET, 88);
  220. GDTR.wr16(REG_HSIZE, 800);
  221. GDTR.wr16(REG_HSYNC0, 0);
  222. GDTR.wr16(REG_HSYNC1, 48);
  223. GDTR.wr16(REG_VCYCLE, 525);
  224. GDTR.wr16(REG_VOFFSET, 32);
  225. GDTR.wr16(REG_VSIZE, 480);
  226. GDTR.wr16(REG_VSYNC0, 0);
  227. GDTR.wr16(REG_VSYNC1, 3);
  228. GDTR.wr16(REG_CSPREAD, 0);
  229. GDTR.wr16(REG_DITHER, 1);
  230. GDTR.wr16(REG_PCLK_POL, 1);
  231. GDTR.wr16(REG_PCLK, 2);
  232. }
  233. #if (BOARD == BOARD_EVITA_0)
  234. GDTR.wr16(REG_HCYCLE, 1344);
  235. GDTR.wr16(REG_HSIZE, 1024);
  236. GDTR.wr16(REG_HSYNC0, 0 );
  237. GDTR.wr16(REG_HSYNC1, 136 );
  238. GDTR.wr16(REG_HOFFSET, 136+160);
  239. GDTR.wr16(REG_VCYCLE, 806 );
  240. GDTR.wr16(REG_VSIZE, 768 );
  241. GDTR.wr16(REG_VSYNC0, 0 );
  242. GDTR.wr16(REG_VSYNC1, 6 );
  243. GDTR.wr16(REG_VOFFSET, 6+29 );
  244. GDTR.wr16(REG_CSPREAD, 0 );
  245. GDTR.wr16(REG_PCLK_POL,0 );
  246. GDTR.wr16(REG_PCLK, 1 );
  247. GDTR.wr(REG_GPIO, GDTR.rd(REG_GPIO) | 0x10);
  248. #endif
  249. w = GDTR.rd16(REG_HSIZE);
  250. h = GDTR.rd16(REG_VSIZE);
  251. // w = 480, h = 272;
  252. Clear(); swap();
  253. Clear(); swap();
  254. Clear(); swap();
  255. cmd_regwrite(REG_PWM_DUTY, 128);
  256. GD.flush();
  257. // Serial.println("STOP"); for(;;);
  258. if (CALIBRATION & (options & GD_CALIBRATE)) {
  259. #if defined(ARDUINO) && !defined(__DUE__)
  260. if ((EEPROM.read(0) != 0x7c)) {
  261. self_calibrate();
  262. // for (int i = 0; i < 24; i++) Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  263. for (int i = 0; i < 24; i++)
  264. EEPROM.write(1 + i, GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
  265. EEPROM.write(0, 0x7c); // is written!
  266. } else {
  267. for (int i = 0; i < 24; i++)
  268. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, EEPROM.read(1 + i));
  269. }
  270. #endif
  271. #ifdef __DUE__
  272. // The Due has no persistent storage. So instead use a "canned"
  273. // calibration.
  274. // self_calibrate();
  275. // for (int i = 0; i < 24; i++)
  276. // Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  277. static const byte canned_calibration[24] = {
  278. 0xCC, 0x7C, 0xFF, 0xFF, 0x57, 0xFE, 0xFF, 0xFF,
  279. 0xA1, 0x04, 0xF9, 0x01, 0x93, 0x00, 0x00, 0x00,
  280. 0x5E, 0x4B, 0x00, 0x00, 0x08, 0x8B, 0xF1, 0xFF };
  281. for (int i = 0; i < 24; i++)
  282. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, canned_calibration[i]);
  283. #endif
  284. #if defined(RASPBERRY_PI)
  285. {
  286. uint8_t cal[24];
  287. FILE *calfile = fopen(".calibration", "r");
  288. if (calfile == NULL) {
  289. calfile = fopen(".calibration", "w");
  290. if (calfile != NULL) {
  291. self_calibrate();
  292. for (int i = 0; i < 24; i++)
  293. cal[i] = GDTR.rd(REG_TOUCH_TRANSFORM_A + i);
  294. fwrite(cal, 1, sizeof(cal), calfile);
  295. fclose(calfile);
  296. }
  297. } else {
  298. fread(cal, 1, sizeof(cal), calfile);
  299. for (int i = 0; i < 24; i++)
  300. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, cal[i]);
  301. fclose(calfile);
  302. }
  303. }
  304. #endif
  305. }
  306. GDTR.wr16(REG_TOUCH_RZTHRESH, 1200);
  307. rseed = 0x77777777;
  308. if ((BOARD == BOARD_GAMEDUINO23) && (options & GD_TRIM)) {
  309. tune();
  310. }
  311. }
  312. void GDClass::storage(void) {
  313. GDTR.__end();
  314. SD.begin(SD_PIN);
  315. GDTR.resume();
  316. }
  317. void GDClass::self_calibrate(void) {
  318. cmd_dlstart();
  319. Clear();
  320. cmd_text(240, 100, 30, OPT_CENTERX, "please tap on the dot");
  321. cmd_calibrate();
  322. finish();
  323. cmd_loadidentity();
  324. cmd_dlstart();
  325. GDTR.flush();
  326. }
  327. void GDClass::seed(uint16_t n) {
  328. rseed = n ? n : 7;
  329. }
  330. uint16_t GDClass::random() {
  331. rseed ^= rseed << 2;
  332. rseed ^= rseed >> 5;
  333. rseed ^= rseed << 1;
  334. return rseed;
  335. }
  336. uint16_t GDClass::random(uint16_t n) {
  337. uint16_t p = random();
  338. if (n == (n & -n))
  339. return p & (n - 1);
  340. return (uint32_t(p) * n) >> 16;
  341. }
  342. // >>> [int(65535*math.sin(math.pi * 2 * i / 1024)) for i in range(257)]
  343. static const PROGMEM uint16_t sintab[257] = {
  344. 0, 402, 804, 1206, 1608, 2010, 2412, 2813, 3215, 3617, 4018, 4419, 4821, 5221, 5622, 6023, 6423, 6823, 7223, 7622, 8022, 8421, 8819, 9218, 9615, 10013, 10410, 10807, 11203, 11599, 11995, 12390, 12785, 13179, 13573, 13966, 14358, 14750, 15142, 15533, 15923, 16313, 16702, 17091, 17479, 17866, 18252, 18638, 19023, 19408, 19791, 20174, 20557, 20938, 21319, 21699, 22078, 22456, 22833, 23210, 23585, 23960, 24334, 24707, 25079, 25450, 25820, 26189, 26557, 26924, 27290, 27655, 28019, 28382, 28744, 29105, 29465, 29823, 30181, 30537, 30892, 31247, 31599, 31951, 32302, 32651, 32999, 33346, 33691, 34035, 34378, 34720, 35061, 35400, 35737, 36074, 36409, 36742, 37075, 37406, 37735, 38063, 38390, 38715, 39039, 39361, 39682, 40001, 40319, 40635, 40950, 41263, 41574, 41885, 42193, 42500, 42805, 43109, 43411, 43711, 44010, 44307, 44603, 44896, 45189, 45479, 45768, 46055, 46340, 46623, 46905, 47185, 47463, 47739, 48014, 48287, 48558, 48827, 49094, 49360, 49623, 49885, 50145, 50403, 50659, 50913, 51165, 51415, 51664, 51910, 52155, 52397, 52638, 52876, 53113, 53347, 53580, 53810, 54039, 54265, 54490, 54712, 54933, 55151, 55367, 55581, 55793, 56003, 56211, 56416, 56620, 56821, 57021, 57218, 57413, 57606, 57796, 57985, 58171, 58355, 58537, 58717, 58894, 59069, 59242, 59413, 59582, 59748, 59912, 60074, 60234, 60391, 60546, 60699, 60849, 60997, 61143, 61287, 61428, 61567, 61704, 61838, 61970, 62100, 62227, 62352, 62474, 62595, 62713, 62828, 62941, 63052, 63161, 63267, 63370, 63472, 63570, 63667, 63761, 63853, 63942, 64029, 64114, 64196, 64275, 64353, 64427, 64500, 64570, 64637, 64702, 64765, 64825, 64883, 64938, 64991, 65042, 65090, 65135, 65178, 65219, 65257, 65293, 65326, 65357, 65385, 65411, 65435, 65456, 65474, 65490, 65504, 65515, 65523, 65530, 65533, 65535
  345. };
  346. int16_t GDClass::rsin(int16_t r, uint16_t th) {
  347. th >>= 6; // angle 0-1023
  348. // return int(r * sin((2 * M_PI) * th / 1024.));
  349. int th4 = th & 511;
  350. if (th4 & 256)
  351. th4 = 512 - th4; // 256->256 257->255, etc
  352. uint16_t s = pgm_read_word_near(sintab + th4);
  353. int16_t p = ((uint32_t)s * r) >> 16;
  354. if (th & 512)
  355. p = -p;
  356. return p;
  357. }
  358. int16_t GDClass::rcos(int16_t r, uint16_t th) {
  359. return rsin(r, th + 0x4000);
  360. }
  361. void GDClass::polar(int &x, int &y, int16_t r, uint16_t th) {
  362. x = (int)(-GD.rsin(r, th));
  363. y = (int)( GD.rcos(r, th));
  364. }
  365. // >>> [int(round(1024 * math.atan(i / 256.) / math.pi)) for i in range(256)]
  366. static const PROGMEM uint8_t atan8[] = {
  367. 0,1,3,4,5,6,8,9,10,11,13,14,15,17,18,19,20,22,23,24,25,27,28,29,30,32,33,34,36,37,38,39,41,42,43,44,46,47,48,49,51,52,53,54,55,57,58,59,60,62,63,64,65,67,68,69,70,71,73,74,75,76,77,79,80,81,82,83,85,86,87,88,89,91,92,93,94,95,96,98,99,100,101,102,103,104,106,107,108,109,110,111,112,114,115,116,117,118,119,120,121,122,124,125,126,127,128,129,130,131,132,133,134,135,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,177,178,179,180,181,182,183,184,185,186,187,188,188,189,190,191,192,193,194,195,195,196,197,198,199,200,201,201,202,203,204,205,206,206,207,208,209,210,211,211,212,213,214,215,215,216,217,218,219,219,220,221,222,222,223,224,225,225,226,227,228,228,229,230,231,231,232,233,234,234,235,236,236,237,238,239,239,240,241,241,242,243,243,244,245,245,246,247,248,248,249,250,250,251,251,252,253,253,254,255,255
  368. };
  369. uint16_t GDClass::atan2(int16_t y, int16_t x)
  370. {
  371. uint16_t a;
  372. uint16_t xx = 0;
  373. /* These values are tricky. So pretend they are not */
  374. if (x == -32768)
  375. x++;
  376. if (y == -32768)
  377. y++;
  378. if ((x <= 0) ^ (y > 0)) {
  379. int16_t t; t = x; x = y; y = t;
  380. xx ^= 0x4000;
  381. }
  382. if (x <= 0) {
  383. x = -x;
  384. } else {
  385. xx ^= 0x8000;
  386. }
  387. y = abs(y);
  388. if (x > y) {
  389. int16_t t; t = x; x = y; y = t;
  390. xx ^= 0x3fff;
  391. }
  392. while ((x | y) & 0xff80) {
  393. x >>= 1;
  394. y >>= 1;
  395. }
  396. if (y == 0) {
  397. a = 0;
  398. } else if (x == y) {
  399. a = 0x2000;
  400. } else {
  401. // assert(x <= y);
  402. int r = ((x << 8) / y);
  403. // assert(0 <= r);
  404. // assert(r < 256);
  405. a = pgm_read_byte(atan8 + r) << 5;
  406. }
  407. a ^= xx;
  408. return a;
  409. }
  410. void GDClass::align(byte n) {
  411. while ((n++) & 3)
  412. GDTR.cmdbyte(0);
  413. }
  414. void GDClass::cH(uint16_t v) {
  415. GDTR.cmdbyte(v & 0xff);
  416. GDTR.cmdbyte((v >> 8) & 0xff);
  417. }
  418. void GDClass::ch(int16_t v) {
  419. cH((uint16_t)v);
  420. }
  421. void GDClass::cI(uint32_t v) {
  422. GDTR.cmd32(v);
  423. }
  424. void GDClass::cFFFFFF(byte v) {
  425. union {
  426. uint32_t c;
  427. uint8_t b[4];
  428. };
  429. b[0] = v;
  430. b[1] = 0xff;
  431. b[2] = 0xff;
  432. b[3] = 0xff;
  433. GDTR.cmd32(c);
  434. }
  435. void GDClass::ci(int32_t v) {
  436. cI((uint32_t) v);
  437. }
  438. void GDClass::cs(const char *s) {
  439. int count = 0;
  440. while (*s) {
  441. char c = *s++;
  442. GDTR.cmdbyte(c);
  443. count++;
  444. }
  445. GDTR.cmdbyte(0);
  446. align(count + 1);
  447. }
  448. void GDClass::copy(const PROGMEM uint8_t *src, int count) {
  449. byte a = count & 3;
  450. while (count--) {
  451. GDTR.cmdbyte(pgm_read_byte_near(src));
  452. src++;
  453. }
  454. align(a);
  455. }
  456. void GDClass::copyram(byte *src, int count) {
  457. byte a = count & 3;
  458. GDTR.cmd_n(src, count);
  459. align(a);
  460. }
  461. void GDClass::AlphaFunc(byte func, byte ref) {
  462. cI((9UL << 24) | ((func & 7L) << 8) | ((ref & 255L) << 0));
  463. }
  464. void GDClass::Begin(byte prim) {
  465. cI((31UL << 24) | prim);
  466. }
  467. void GDClass::BitmapHandle(byte handle) {
  468. cI((5UL << 24) | handle);
  469. }
  470. void GDClass::BitmapLayout(byte format, uint16_t linestride, uint16_t height) {
  471. // cI((7UL << 24) | ((format & 31L) << 19) | ((linestride & 1023L) << 9) | ((height & 511L) << 0));
  472. union {
  473. uint32_t c;
  474. uint8_t b[4];
  475. };
  476. b[0] = height;
  477. b[1] = (1 & (height >> 8)) | (linestride << 1);
  478. b[2] = (7 & (linestride >> 7)) | (format << 3);
  479. b[3] = 7;
  480. cI(c);
  481. }
  482. void GDClass::BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height) {
  483. byte fxy = (filter << 2) | (wrapx << 1) | (wrapy);
  484. // cI((8UL << 24) | ((uint32_t)fxy << 18) | ((width & 511L) << 9) | ((height & 511L) << 0));
  485. union {
  486. uint32_t c;
  487. uint8_t b[4];
  488. };
  489. b[0] = height;
  490. b[1] = (1 & (height >> 8)) | (width << 1);
  491. b[2] = (3 & (width >> 7)) | (fxy << 2);
  492. b[3] = 8;
  493. cI(c);
  494. if (ft8xx_model) {
  495. b[0] = ((width >> 9) << 2) | (3 & (height >> 9));
  496. b[3] = 0x29;
  497. cI(c);
  498. }
  499. }
  500. void GDClass::BitmapSource(uint32_t addr) {
  501. cI((1UL << 24) | ((addr & 1048575L) << 0));
  502. }
  503. void GDClass::BitmapTransformA(int32_t a) {
  504. cI((21UL << 24) | ((a & 131071L) << 0));
  505. }
  506. void GDClass::BitmapTransformB(int32_t b) {
  507. cI((22UL << 24) | ((b & 131071L) << 0));
  508. }
  509. void GDClass::BitmapTransformC(int32_t c) {
  510. cI((23UL << 24) | ((c & 16777215L) << 0));
  511. }
  512. void GDClass::BitmapTransformD(int32_t d) {
  513. cI((24UL << 24) | ((d & 131071L) << 0));
  514. }
  515. void GDClass::BitmapTransformE(int32_t e) {
  516. cI((25UL << 24) | ((e & 131071L) << 0));
  517. }
  518. void GDClass::BitmapTransformF(int32_t f) {
  519. cI((26UL << 24) | ((f & 16777215L) << 0));
  520. }
  521. void GDClass::BlendFunc(byte src, byte dst) {
  522. cI((11UL << 24) | ((src & 7L) << 3) | ((dst & 7L) << 0));
  523. }
  524. void GDClass::Call(uint16_t dest) {
  525. cI((29UL << 24) | ((dest & 2047L) << 0));
  526. }
  527. void GDClass::Cell(byte cell) {
  528. cI((6UL << 24) | ((cell & 127L) << 0));
  529. }
  530. void GDClass::ClearColorA(byte alpha) {
  531. cI((15UL << 24) | ((alpha & 255L) << 0));
  532. }
  533. void GDClass::ClearColorRGB(byte red, byte green, byte blue) {
  534. cI((2UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  535. }
  536. void GDClass::ClearColorRGB(uint32_t rgb) {
  537. cI((2UL << 24) | (rgb & 0xffffffL));
  538. }
  539. void GDClass::Clear(byte c, byte s, byte t) {
  540. byte m = (c << 2) | (s << 1) | t;
  541. cI((38UL << 24) | m);
  542. }
  543. void GDClass::Clear(void) {
  544. cI((38UL << 24) | 7);
  545. }
  546. void GDClass::ClearStencil(byte s) {
  547. cI((17UL << 24) | ((s & 255L) << 0));
  548. }
  549. void GDClass::ClearTag(byte s) {
  550. cI((18UL << 24) | ((s & 255L) << 0));
  551. }
  552. void GDClass::ColorA(byte alpha) {
  553. cI((16UL << 24) | ((alpha & 255L) << 0));
  554. }
  555. void GDClass::ColorMask(byte r, byte g, byte b, byte a) {
  556. cI((32UL << 24) | ((r & 1L) << 3) | ((g & 1L) << 2) | ((b & 1L) << 1) | ((a & 1L) << 0));
  557. }
  558. void GDClass::ColorRGB(byte red, byte green, byte blue) {
  559. // cI((4UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  560. union {
  561. uint32_t c;
  562. uint8_t b[4];
  563. };
  564. b[0] = blue;
  565. b[1] = green;
  566. b[2] = red;
  567. b[3] = 4;
  568. cI(c);
  569. }
  570. void GDClass::ColorRGB(uint32_t rgb) {
  571. cI((4UL << 24) | (rgb & 0xffffffL));
  572. }
  573. void GDClass::Display(void) {
  574. cI((0UL << 24));
  575. }
  576. void GDClass::End(void) {
  577. cI((33UL << 24));
  578. }
  579. void GDClass::Jump(uint16_t dest) {
  580. cI((30UL << 24) | ((dest & 2047L) << 0));
  581. }
  582. void GDClass::LineWidth(uint16_t width) {
  583. cI((14UL << 24) | ((width & 4095L) << 0));
  584. }
  585. void GDClass::Macro(byte m) {
  586. cI((37UL << 24) | ((m & 1L) << 0));
  587. }
  588. void GDClass::PointSize(uint16_t size) {
  589. cI((13UL << 24) | ((size & 8191L) << 0));
  590. }
  591. void GDClass::RestoreContext(void) {
  592. cI((35UL << 24));
  593. }
  594. void GDClass::Return(void) {
  595. cI((36UL << 24));
  596. }
  597. void GDClass::SaveContext(void) {
  598. cI((34UL << 24));
  599. }
  600. void GDClass::ScissorSize(uint16_t width, uint16_t height) {
  601. if (ft8xx_model == 0)
  602. cI((28UL << 24) | ((width & 1023L) << 10) | ((height & 1023L) << 0));
  603. else
  604. cI((28UL << 24) | ((width & 4095L) << 12) | ((height & 4095L) << 0));
  605. }
  606. void GDClass::ScissorXY(uint16_t x, uint16_t y) {
  607. if (ft8xx_model == 0)
  608. cI((27UL << 24) | ((x & 511L) << 9) | ((y & 511L) << 0));
  609. else
  610. cI((27UL << 24) | ((x & 2047L) << 11) | ((y & 2047L) << 0));
  611. }
  612. void GDClass::StencilFunc(byte func, byte ref, byte mask) {
  613. cI((10UL << 24) | ((func & 7L) << 16) | ((ref & 255L) << 8) | ((mask & 255L) << 0));
  614. }
  615. void GDClass::StencilMask(byte mask) {
  616. cI((19UL << 24) | ((mask & 255L) << 0));
  617. }
  618. void GDClass::StencilOp(byte sfail, byte spass) {
  619. cI((12UL << 24) | ((sfail & 7L) << 3) | ((spass & 7L) << 0));
  620. }
  621. void GDClass::TagMask(byte mask) {
  622. cI((20UL << 24) | ((mask & 1L) << 0));
  623. }
  624. void GDClass::Tag(byte s) {
  625. cI((3UL << 24) | ((s & 255L) << 0));
  626. }
  627. void GDClass::Vertex2f(int16_t x, int16_t y) {
  628. // x = int(16 * x);
  629. // y = int(16 * y);
  630. cI((1UL << 30) | ((x & 32767L) << 15) | ((y & 32767L) << 0));
  631. }
  632. void GDClass::Vertex2ii(uint16_t x, uint16_t y, byte handle, byte cell) {
  633. // cI((2UL << 30) | ((x & 511L) << 21) | ((y & 511L) << 12) | ((handle & 31L) << 7) | ((cell & 127L) << 0));
  634. union {
  635. uint32_t c;
  636. uint8_t b[4];
  637. };
  638. b[0] = cell | ((handle & 1) << 7);
  639. b[1] = (handle >> 1) | (y << 4);
  640. b[2] = (y >> 4) | (x << 5);
  641. b[3] = (2 << 6) | (x >> 3);
  642. cI(c);
  643. }
  644. void GDClass::VertexFormat(byte frac) {
  645. cI((39UL << 24) | (((frac) & 7) << 0));
  646. }
  647. void GDClass::BitmapLayoutH(byte linestride, byte height) {
  648. cI((40 << 24) | (((linestride) & 3) << 2) | (((height) & 3) << 0));
  649. }
  650. void GDClass::BitmapSizeH(byte width, byte height) {
  651. cI((41UL << 24) | (((width) & 3) << 2) | (((height) & 3) << 0));
  652. }
  653. void GDClass::PaletteSource(uint32_t addr) {
  654. cI((42UL << 24) | (((addr) & 4194303UL) << 0));
  655. }
  656. void GDClass::VertexTranslateX(uint32_t x) {
  657. cI((43UL << 24) | (((x) & 131071UL) << 0));
  658. }
  659. void GDClass::VertexTranslateY(uint32_t y) {
  660. cI((44UL << 24) | (((y) & 131071UL) << 0));
  661. }
  662. void GDClass::Nop(void) {
  663. cI((45UL << 24));
  664. }
  665. void GDClass::cmd_append(uint32_t ptr, uint32_t num) {
  666. cFFFFFF(0x1e);
  667. cI(ptr);
  668. cI(num);
  669. }
  670. void GDClass::cmd_bgcolor(uint32_t c) {
  671. cFFFFFF(0x09);
  672. cI(c);
  673. }
  674. void GDClass::cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s) {
  675. cFFFFFF(0x0d);
  676. ch(x);
  677. ch(y);
  678. ch(w);
  679. ch(h);
  680. ch(font);
  681. cH(options);
  682. cs(s);
  683. }
  684. void GDClass::cmd_calibrate(void) {
  685. cFFFFFF(0x15);
  686. cFFFFFF(0xff);
  687. }
  688. void GDClass::cmd_clock(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t h, uint16_t m, uint16_t s, uint16_t ms) {
  689. cFFFFFF(0x14);
  690. ch(x);
  691. ch(y);
  692. ch(r);
  693. cH(options);
  694. cH(h);
  695. cH(m);
  696. cH(s);
  697. cH(ms);
  698. }
  699. void GDClass::cmd_coldstart(void) {
  700. cFFFFFF(0x32);
  701. }
  702. void GDClass::cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val) {
  703. cFFFFFF(0x2d);
  704. ch(x);
  705. ch(y);
  706. ch(r);
  707. cH(options);
  708. cH(val);
  709. cH(0);
  710. }
  711. void GDClass::cmd_dlstart(void) {
  712. cFFFFFF(0x00);
  713. }
  714. void GDClass::cmd_fgcolor(uint32_t c) {
  715. cFFFFFF(0x0a);
  716. cI(c);
  717. }
  718. void GDClass::cmd_gauge(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t major, uint16_t minor, uint16_t val, uint16_t range) {
  719. cFFFFFF(0x13);
  720. ch(x);
  721. ch(y);
  722. ch(r);
  723. cH(options);
  724. cH(major);
  725. cH(minor);
  726. cH(val);
  727. cH(range);
  728. }
  729. void GDClass::cmd_getmatrix(void) {
  730. cFFFFFF(0x33);
  731. ci(0);
  732. ci(0);
  733. ci(0);
  734. ci(0);
  735. ci(0);
  736. ci(0);
  737. }
  738. void GDClass::cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h) {
  739. cFFFFFF(0x25);
  740. ptr = GDTR.getwp();
  741. cI(0);
  742. w = GDTR.getwp();
  743. cI(0);
  744. h = GDTR.getwp();
  745. cI(0);
  746. }
  747. void GDClass::cmd_getptr(void) {
  748. cFFFFFF(0x23);
  749. cI(0);
  750. }
  751. void GDClass::cmd_gradcolor(uint32_t c) {
  752. cFFFFFF(0x34);
  753. cI(c);
  754. }
  755. void GDClass::cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1) {
  756. cFFFFFF(0x0b);
  757. ch(x0);
  758. ch(y0);
  759. cI(rgb0);
  760. ch(x1);
  761. ch(y1);
  762. cI(rgb1);
  763. }
  764. void GDClass::cmd_inflate(uint32_t ptr) {
  765. cFFFFFF(0x22);
  766. cI(ptr);
  767. }
  768. void GDClass::cmd_interrupt(uint32_t ms) {
  769. cFFFFFF(0x02);
  770. cI(ms);
  771. }
  772. void GDClass::cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s) {
  773. cFFFFFF(0x0e);
  774. ch(x);
  775. ch(y);
  776. ch(w);
  777. ch(h);
  778. ch(font);
  779. cH(options);
  780. cs(s);
  781. }
  782. void GDClass::cmd_loadidentity(void) {
  783. cFFFFFF(0x26);
  784. }
  785. void GDClass::cmd_loadimage(uint32_t ptr, int32_t options) {
  786. cFFFFFF(0x24);
  787. cI(ptr);
  788. cI(options);
  789. }
  790. void GDClass::cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num) {
  791. cFFFFFF(0x1d);
  792. cI(dest);
  793. cI(src);
  794. cI(num);
  795. }
  796. void GDClass::cmd_memset(uint32_t ptr, byte value, uint32_t num) {
  797. cFFFFFF(0x1b);
  798. cI(ptr);
  799. cI((uint32_t)value);
  800. cI(num);
  801. }
  802. uint32_t GDClass::cmd_memcrc(uint32_t ptr, uint32_t num) {
  803. cFFFFFF(0x18);
  804. cI(ptr);
  805. cI(num);
  806. uint32_t r = GDTR.getwp();
  807. cI(0xFFFFFFFF);
  808. return r;
  809. }
  810. void GDClass::cmd_memwrite(uint32_t ptr, uint32_t num) {
  811. cFFFFFF(0x1a);
  812. cI(ptr);
  813. cI(num);
  814. }
  815. void GDClass::cmd_regwrite(uint32_t ptr, uint32_t val) {
  816. cFFFFFF(0x1a);
  817. cI(ptr);
  818. cI(4UL);
  819. cI(val);
  820. }
  821. void GDClass::cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n) {
  822. cFFFFFF(0x2e);
  823. ch(x);
  824. ch(y);
  825. ch(font);
  826. cH(options);
  827. ci(n);
  828. }
  829. void GDClass::cmd_progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t range) {
  830. cFFFFFF(0x0f);
  831. ch(x);
  832. ch(y);
  833. ch(w);
  834. ch(h);
  835. cH(options);
  836. cH(val);
  837. cH(range);
  838. cH(0);
  839. }
  840. void GDClass::cmd_regread(uint32_t ptr) {
  841. cFFFFFF(0x19);
  842. cI(ptr);
  843. cI(0);
  844. }
  845. void GDClass::cmd_rotate(int32_t a) {
  846. cFFFFFF(0x29);
  847. ci(a);
  848. }
  849. void GDClass::cmd_scale(int32_t sx, int32_t sy) {
  850. cFFFFFF(0x28);
  851. ci(sx);
  852. ci(sy);
  853. }
  854. void GDClass::cmd_screensaver(void) {
  855. cFFFFFF(0x2f);
  856. }
  857. void GDClass::cmd_scrollbar(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t size, uint16_t range) {
  858. cFFFFFF(0x11);
  859. ch(x);
  860. ch(y);
  861. ch(w);
  862. ch(h);
  863. cH(options);
  864. cH(val);
  865. cH(size);
  866. cH(range);
  867. }
  868. void GDClass::cmd_setfont(byte font, uint32_t ptr) {
  869. cFFFFFF(0x2b);
  870. cI(font);
  871. cI(ptr);
  872. }
  873. void GDClass::cmd_setmatrix(void) {
  874. cFFFFFF(0x2a);
  875. }
  876. void GDClass::cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format) {
  877. cFFFFFF(0x30);
  878. ch(x);
  879. ch(y);
  880. cH(w);
  881. cH(h);
  882. cI(ptr);
  883. cI(format);
  884. }
  885. void GDClass::cmd_slider(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range) {
  886. cFFFFFF(0x10);
  887. ch(x);
  888. ch(y);
  889. ch(w);
  890. ch(h);
  891. cH(options);
  892. cH(val);
  893. cH(range);
  894. cH(0);
  895. }
  896. void GDClass::cmd_snapshot(uint32_t ptr) {
  897. cFFFFFF(0x1f);
  898. cI(ptr);
  899. }
  900. void GDClass::cmd_spinner(int16_t x, int16_t y, byte style, byte scale) {
  901. cFFFFFF(0x16);
  902. ch(x);
  903. ch(y);
  904. cH(style);
  905. cH(scale);
  906. }
  907. void GDClass::cmd_stop(void) {
  908. cFFFFFF(0x17);
  909. }
  910. void GDClass::cmd_swap(void) {
  911. cFFFFFF(0x01);
  912. }
  913. void GDClass::cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s) {
  914. cFFFFFF(0x0c);
  915. ch(x);
  916. ch(y);
  917. ch(font);
  918. cH(options);
  919. cs(s);
  920. }
  921. void GDClass::cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s) {
  922. cFFFFFF(0x12);
  923. ch(x);
  924. ch(y);
  925. ch(w);
  926. ch(font);
  927. cH(options);
  928. cH(state);
  929. cs(s);
  930. }
  931. void GDClass::cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag) {
  932. cFFFFFF(0x2c);
  933. ch(x);
  934. ch(y);
  935. ch(w);
  936. ch(h);
  937. ch(tag);
  938. ch(0);
  939. }
  940. void GDClass::cmd_translate(int32_t tx, int32_t ty) {
  941. cFFFFFF(0x27);
  942. ci(tx);
  943. ci(ty);
  944. }
  945. void GDClass::cmd_playvideo(int32_t options) {
  946. cFFFFFF(0x3a);
  947. cI(options);
  948. }
  949. void GDClass::cmd_romfont(uint32_t font, uint32_t romslot) {
  950. cFFFFFF(0x3f);
  951. cI(font);
  952. cI(romslot);
  953. }
  954. void GDClass::cmd_mediafifo(uint32_t ptr, uint32_t size) {
  955. cFFFFFF(0x39);
  956. cI(ptr);
  957. cI(size);
  958. }
  959. void GDClass::cmd_setbase(uint32_t b) {
  960. cFFFFFF(0x38);
  961. cI(b);
  962. }
  963. void GDClass::cmd_videoframe(uint32_t dst, uint32_t ptr) {
  964. cFFFFFF(0x41);
  965. cI(dst);
  966. cI(ptr);
  967. }
  968. void GDClass::cmd_snapshot2(uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, int16_t w, int16_t h) {
  969. cFFFFFF(0x37);
  970. cI(fmt);
  971. cI(ptr);
  972. ch(x);
  973. ch(y);
  974. ch(w);
  975. ch(h);
  976. }
  977. void GDClass::cmd_setfont2(uint32_t font, uint32_t ptr, uint32_t firstchar) {
  978. cFFFFFF(0x3b);
  979. cI(font);
  980. cI(ptr);
  981. cI(firstchar);
  982. }
  983. void GDClass::cmd_setbitmap(uint32_t source, uint16_t fmt, uint16_t w, uint16_t h) {
  984. cFFFFFF(0x43);
  985. cI(source);
  986. ch(fmt);
  987. ch(w);
  988. ch(h);
  989. ch(0);
  990. }
  991. void GDClass::cmd_setrotate(uint32_t r) {
  992. cFFFFFF(0x36);
  993. cI(r);
  994. // As a special favor, update variables w and h according to this
  995. // rotation
  996. w = GDTR.rd16(REG_HSIZE);
  997. h = GDTR.rd16(REG_VSIZE);
  998. if (r & 2) {
  999. int t = h;
  1000. h = w;
  1001. w = t;
  1002. }
  1003. }
  1004. void GDClass::cmd_videostart() {
  1005. cFFFFFF(0x40);
  1006. }
  1007. byte GDClass::rd(uint32_t addr) {
  1008. return GDTR.rd(addr);
  1009. }
  1010. void GDClass::wr(uint32_t addr, uint8_t v) {
  1011. GDTR.wr(addr, v);
  1012. }
  1013. uint16_t GDClass::rd16(uint32_t addr) {
  1014. return GDTR.rd16(addr);
  1015. }
  1016. void GDClass::wr16(uint32_t addr, uint16_t v) {
  1017. GDTR.wr16(addr, v);
  1018. }
  1019. uint32_t GDClass::rd32(uint32_t addr) {
  1020. return GDTR.rd32(addr);
  1021. }
  1022. void GDClass::wr32(uint32_t addr, uint32_t v) {
  1023. GDTR.wr32(addr, v);
  1024. }
  1025. void GDClass::wr_n(uint32_t addr, byte *src, uint32_t n) {
  1026. GDTR.wr_n(addr, src, n);
  1027. }
  1028. void GDClass::cmdbyte(uint8_t b) {
  1029. GDTR.cmdbyte(b);
  1030. }
  1031. void GDClass::cmd32(uint32_t b) {
  1032. GDTR.cmd32(b);
  1033. }
  1034. void GDClass::finish(void) {
  1035. GDTR.finish();
  1036. }
  1037. void GDClass::get_accel(int &x, int &y, int &z) {
  1038. static int f[3];
  1039. for (byte i = 0; i < 3; i++) {
  1040. int a = analogRead(A0 + i);
  1041. int s = (-160 * (a - 376)) >> 6;
  1042. f[i] = ((3 * f[i]) >> 2) + (s >> 2);
  1043. }
  1044. x = f[2];
  1045. y = f[1];
  1046. z = f[0];
  1047. }
  1048. void GDClass::get_inputs(void) {
  1049. GDTR.finish();
  1050. byte *bi = (byte*)&inputs;
  1051. #if defined(DUMPDEV)
  1052. extern FILE* stimfile;
  1053. if (stimfile) {
  1054. byte tag;
  1055. fscanf(stimfile, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
  1056. &bi[0],
  1057. &bi[1],
  1058. &bi[2],
  1059. &bi[3],
  1060. &bi[4],
  1061. &bi[5],
  1062. &bi[6],
  1063. &bi[7],
  1064. &bi[8],
  1065. &bi[9],
  1066. &bi[10],
  1067. &bi[11],
  1068. &bi[12],
  1069. &bi[13],
  1070. &bi[14],
  1071. &bi[15],
  1072. &bi[16],
  1073. &bi[17]);
  1074. GDTR.wr(REG_TAG, tag);
  1075. } else {
  1076. inputs.x = inputs.y = -32768;
  1077. }
  1078. #else
  1079. GDTR.rd_n(bi, REG_TRACKER, 4);
  1080. GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
  1081. GDTR.rd_n(bi + 17, REG_TAG, 1);
  1082. #ifdef DUMP_INPUTS
  1083. for (size_t i = 0; i < sizeof(inputs); i++) {
  1084. Serial.print(bi[i], HEX);
  1085. Serial.print(" ");
  1086. }
  1087. Serial.println();
  1088. #endif
  1089. #endif
  1090. }
  1091. void GDClass::bulkrd(uint32_t a) {
  1092. GDTR.bulk(a);
  1093. }
  1094. void GDClass::resume(void) {
  1095. GDTR.resume();
  1096. }
  1097. void GDClass::__end(void) {
  1098. #if !defined(DUMPDEV) && !defined(RASPBERRY_PI)
  1099. GDTR.__end();
  1100. #endif
  1101. }
  1102. void GDClass::play(uint8_t instrument, uint8_t note) {
  1103. wr16(REG_SOUND, (note << 8) | instrument);
  1104. wr(REG_PLAY, 1);
  1105. }
  1106. void GDClass::sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop) {
  1107. GD.wr32(REG_PLAYBACK_START, start);
  1108. GD.wr32(REG_PLAYBACK_LENGTH, len);
  1109. GD.wr16(REG_PLAYBACK_FREQ, freq);
  1110. GD.wr(REG_PLAYBACK_FORMAT, format);
  1111. GD.wr(REG_PLAYBACK_LOOP, loop);
  1112. GD.wr(REG_PLAYBACK_PLAY, 1);
  1113. }
  1114. void GDClass::reset() {
  1115. GDTR.__end();
  1116. GDTR.wr(REG_CPURESET, 1);
  1117. GDTR.wr(REG_CPURESET, 0);
  1118. GDTR.resume();
  1119. }
  1120. // Load named file from storage
  1121. // returns 0 on failure (e.g. file not found), 1 on success
  1122. byte GDClass::load(const char *filename, void (*progress)(long, long))
  1123. {
  1124. #if defined(RASPBERRY_PI) || defined(DUMPDEV)
  1125. char full_name[2048] = "sdcard/";
  1126. strcat(full_name, filename);
  1127. FILE *f = fopen(full_name, "rb");
  1128. if (!f) {
  1129. perror(full_name);
  1130. exit(1);
  1131. }
  1132. byte buf[512];
  1133. int n;
  1134. while ((n = fread(buf, 1, 512, f)) > 0) {
  1135. GDTR.cmd_n(buf, (n + 3) & ~3);
  1136. }
  1137. fclose(f);
  1138. return 1;
  1139. #else
  1140. GD.__end();
  1141. Reader r;
  1142. if (r.openfile(filename)) {
  1143. byte buf[512];
  1144. while (r.offset < r.size) {
  1145. uint16_t n = min(512U, r.size - r.offset);
  1146. n = (n + 3) & ~3; // force 32-bit alignment
  1147. r.readsector(buf);
  1148. GD.resume();
  1149. if (progress)
  1150. (*progress)(r.offset, r.size);
  1151. GD.copyram(buf, n);
  1152. GDTR.stop();
  1153. }
  1154. GD.resume();
  1155. return 1;
  1156. }
  1157. GD.resume();
  1158. return 0;
  1159. #endif
  1160. }
  1161. // Generated by mk_bsod.py. Blue screen with 'ERROR' text
  1162. static const PROGMEM uint8_t __bsod[32] = {
  1163. 0, 255, 255, 255, 96, 0, 0, 2, 7, 0, 0, 38, 12, 255, 255, 255, 240, 0,
  1164. 90, 0, 31, 0, 0, 6, 69, 82, 82, 79, 82, 0, 0, 0
  1165. };
  1166. static const PROGMEM uint8_t __bsod_badfile[32] = {
  1167. 12, 255, 255, 255, 240, 0, 148, 0, 29, 0, 0, 6, 67, 97, 110, 110, 111,
  1168. 116, 32, 111, 112, 101, 110, 32, 102, 105, 108, 101, 58, 0, 0, 0
  1169. };
  1170. // Fatal error alert.
  1171. // Show a blue screen with message.
  1172. // This method never returns.
  1173. void GDClass::alert(const char *message)
  1174. {
  1175. begin(0);
  1176. copy(__bsod, sizeof(__bsod));
  1177. cmd_text(240, 176, 29, OPT_CENTER, message);
  1178. swap();
  1179. GD.finish();
  1180. for (;;)
  1181. ;
  1182. }
  1183. void GDClass::safeload(const char *filename)
  1184. {
  1185. if (!load(filename)) {
  1186. copy(__bsod, sizeof(__bsod));
  1187. copy(__bsod_badfile, sizeof(__bsod_badfile));
  1188. cmd_text(240, 190, 29, OPT_CENTER, filename);
  1189. swap();
  1190. for (;;)
  1191. ;
  1192. }
  1193. }
  1194. #define REG_SCREENSHOT_EN (ft8xx_model ? 0x302010UL : 0x102410UL) // Set to enable screenshot mode
  1195. #define REG_SCREENSHOT_Y (ft8xx_model ? 0x302014UL : 0x102414UL) // Y line register
  1196. #define REG_SCREENSHOT_START (ft8xx_model ? 0x302018UL : 0x102418UL) // Screenshot start trigger
  1197. #define REG_SCREENSHOT_BUSY (ft8xx_model ? 0x3020e8UL : 0x1024d8UL) // Screenshot ready flags
  1198. #define REG_SCREENSHOT_READ (ft8xx_model ? 0x302174UL : 0x102554UL) // Set to enable readout
  1199. #define RAM_SCREENSHOT (ft8xx_model ? 0x3c2000UL : 0x1C2000UL) // Screenshot readout buffer
  1200. #ifndef DUMPDEV
  1201. void GDClass::dumpscreen(void)
  1202. {
  1203. {
  1204. finish();
  1205. wr(REG_SCREENSHOT_EN, 1);
  1206. Serial.write(0xa5);
  1207. Serial.write(GD.w & 0xff);
  1208. Serial.write((GD.w >> 8) & 0xff);
  1209. Serial.write(GD.h & 0xff);
  1210. Serial.write((GD.h >> 8) & 0xff);
  1211. for (int ly = 0; ly < GD.h; ly++) {
  1212. wr16(REG_SCREENSHOT_Y, ly);
  1213. wr(REG_SCREENSHOT_START, 1);
  1214. delay(2);
  1215. while (rd32(REG_SCREENSHOT_BUSY) | rd32(REG_SCREENSHOT_BUSY + 4))
  1216. ;
  1217. wr(REG_SCREENSHOT_READ, 1);
  1218. bulkrd(RAM_SCREENSHOT);
  1219. SPI.transfer(0xff);
  1220. for (int x = 0; x < GD.w; x += 8) {
  1221. union {
  1222. uint32_t v;
  1223. struct {
  1224. uint8_t b, g, r, a;
  1225. };
  1226. } block[8];
  1227. for (int i = 0; i < 8; i++) {
  1228. block[i].b = SPI.transfer(0xff);
  1229. block[i].g = SPI.transfer(0xff);
  1230. block[i].r = SPI.transfer(0xff);
  1231. block[i].a = SPI.transfer(0xff);
  1232. }
  1233. // if (x == 0) block[0].r = 0xff;
  1234. byte difference = 1;
  1235. for (int i = 1, mask = 2; i < 8; i++, mask <<= 1)
  1236. if (block[i].v != block[i-1].v)
  1237. difference |= mask;
  1238. Serial.write(difference);
  1239. for (int i = 0; i < 8; i++)
  1240. if (1 & (difference >> i)) {
  1241. Serial.write(block[i].b);
  1242. Serial.write(block[i].g);
  1243. Serial.write(block[i].r);
  1244. }
  1245. }
  1246. resume();
  1247. wr(REG_SCREENSHOT_READ, 0);
  1248. }
  1249. wr16(REG_SCREENSHOT_EN, 0);
  1250. }
  1251. }
  1252. #endif