GD2.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. #include <Arduino.h>
  2. #include "SPI.h"
  3. #if !defined(__SAM3X8E__)
  4. #include "EEPROM.h"
  5. #endif
  6. #include <GD2.h>
  7. #define SD_PIN 9 // pin used for the microSD enable signal
  8. #define PROTO 1
  9. #define STORAGE 1
  10. #define CALIBRATION 1
  11. #define DUMP_INPUTS 0
  12. #define VERBOSE 0
  13. #ifdef DUMPDEV
  14. #include <assert.h>
  15. #include "transports/dump.h"
  16. #endif
  17. #ifdef RASPBERRY_PI
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <stdint.h>
  24. #include <sys/ioctl.h>
  25. #include <linux/types.h>
  26. #include <linux/spi/spidev.h>
  27. #include "transports/spidev.h"
  28. #endif
  29. byte ft8xx_model;
  30. #if defined(ARDUINO)
  31. #include "transports/wiring.h"
  32. #endif
  33. static GDTransport GDTR;
  34. GDClass GD;
  35. void GDClass::flush(void)
  36. {
  37. GDTR.flush();
  38. }
  39. void GDClass::swap(void) {
  40. Display();
  41. cmd_swap();
  42. cmd_loadidentity();
  43. cmd_dlstart();
  44. GDTR.flush();
  45. }
  46. uint32_t GDClass::measure_freq(void)
  47. {
  48. unsigned long t0 = GDTR.rd32(REG_CLOCK);
  49. delayMicroseconds(15625);
  50. unsigned long t1 = GDTR.rd32(REG_CLOCK);
  51. // Serial.println((t1 - t0) << 6);
  52. return (t1 - t0) << 6;
  53. }
  54. #define LOW_FREQ_BOUND 47040000UL
  55. // #define LOW_FREQ_BOUND 32040000UL
  56. void GDClass::tune(void)
  57. {
  58. uint32_t f;
  59. for (byte i = 0; (i < 31) && ((f = measure_freq()) < LOW_FREQ_BOUND); i++)
  60. GDTR.wr(REG_TRIM, i);
  61. GDTR.wr32(REG_FREQUENCY, f);
  62. }
  63. void GDClass::begin(uint8_t options) {
  64. #if STORAGE && defined(ARDUINO)
  65. if (options & GD_STORAGE) {
  66. GDTR.ios();
  67. SD.begin(SD_PIN);
  68. }
  69. #endif
  70. GDTR.begin();
  71. #if VERBOSE
  72. Serial.println("ID REGISTER:");
  73. Serial.println(GDTR.rd(REG_ID), HEX);
  74. #endif
  75. // Generate a blank screen
  76. cmd_dlstart();
  77. #ifndef DUMPDEV
  78. Clear();
  79. swap();
  80. #endif
  81. finish();
  82. GDTR.wr(REG_PCLK_POL, 1);
  83. GDTR.wr(REG_PCLK, 5);
  84. #if PROTO == 1
  85. GDTR.wr(REG_ROTATE, 1);
  86. GDTR.wr(REG_SWIZZLE, 3);
  87. #endif
  88. GDTR.wr(REG_GPIO_DIR, 0x83);
  89. GDTR.wr(REG_GPIO, 0x80);
  90. if (CALIBRATION & (options & GD_CALIBRATE)) {
  91. #if defined(ARDUINO) && !defined(__DUE__)
  92. if (EEPROM.read(0) != 0x7c) {
  93. self_calibrate();
  94. // for (int i = 0; i < 24; i++) Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  95. for (int i = 0; i < 24; i++)
  96. EEPROM.write(1 + i, GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
  97. EEPROM.write(0, 0x7c); // is written!
  98. } else {
  99. for (int i = 0; i < 24; i++)
  100. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, EEPROM.read(1 + i));
  101. }
  102. #endif
  103. #ifdef __DUE__
  104. // The Due has no persistent storage. So instead use a "canned"
  105. // calibration.
  106. // self_calibrate();
  107. // for (int i = 0; i < 24; i++)
  108. // Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  109. static const byte canned_calibration[24] = {
  110. 0xCC, 0x7C, 0xFF, 0xFF, 0x57, 0xFE, 0xFF, 0xFF,
  111. 0xA1, 0x04, 0xF9, 0x01, 0x93, 0x00, 0x00, 0x00,
  112. 0x5E, 0x4B, 0x00, 0x00, 0x08, 0x8B, 0xF1, 0xFF };
  113. for (int i = 0; i < 24; i++)
  114. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, canned_calibration[i]);
  115. #endif
  116. #if defined(RASPBERRY_PI)
  117. {
  118. uint8_t cal[24];
  119. FILE *calfile = fopen(".calibration", "r");
  120. if (calfile == NULL) {
  121. calfile = fopen(".calibration", "w");
  122. if (calfile != NULL) {
  123. self_calibrate();
  124. for (int i = 0; i < 24; i++)
  125. cal[i] = GDTR.rd(REG_TOUCH_TRANSFORM_A + i);
  126. fwrite(cal, 1, sizeof(cal), calfile);
  127. fclose(calfile);
  128. }
  129. } else {
  130. fread(cal, 1, sizeof(cal), calfile);
  131. for (int i = 0; i < 24; i++)
  132. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, cal[i]);
  133. fclose(calfile);
  134. }
  135. }
  136. #endif
  137. }
  138. GDTR.wr16(REG_TOUCH_RZTHRESH, 1200);
  139. rseed = 0x77777777;
  140. if (options & GD_TRIM) {
  141. tune();
  142. }
  143. }
  144. void GDClass::storage(void) {
  145. GDTR.__end();
  146. SD.begin(SD_PIN);
  147. GDTR.resume();
  148. }
  149. void GDClass::self_calibrate(void) {
  150. cmd_dlstart();
  151. Clear();
  152. cmd_text(240, 100, 30, OPT_CENTERX, "please tap on the dot");
  153. cmd_calibrate();
  154. finish();
  155. cmd_loadidentity();
  156. cmd_dlstart();
  157. GDTR.flush();
  158. }
  159. void GDClass::seed(uint16_t n) {
  160. rseed = n ? n : 7;
  161. }
  162. uint16_t GDClass::random() {
  163. rseed ^= rseed << 13;
  164. rseed ^= rseed >> 17;
  165. rseed ^= rseed << 5;
  166. return rseed;
  167. }
  168. uint16_t GDClass::random(uint16_t n) {
  169. uint32_t p = random();
  170. return (p * n) >> 16;
  171. }
  172. // >>> [int(65535*math.sin(math.pi * 2 * i / 1024)) for i in range(257)]
  173. static const PROGMEM uint16_t sintab[257] = {
  174. 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
  175. };
  176. int16_t GDClass::rsin(int16_t r, uint16_t th) {
  177. th >>= 6; // angle 0-123
  178. // return int(r * sin((2 * M_PI) * th / 1024.));
  179. int th4 = th & 511;
  180. if (th4 & 256)
  181. th4 = 512 - th4; // 256->256 257->255, etc
  182. uint16_t s = pgm_read_word_near(sintab + th4);
  183. int16_t p = ((uint32_t)s * r) >> 16;
  184. if (th & 512)
  185. p = -p;
  186. return p;
  187. }
  188. int16_t GDClass::rcos(int16_t r, uint16_t th) {
  189. return rsin(r, th + 0x4000);
  190. }
  191. void GDClass::polar(int &x, int &y, int16_t r, uint16_t th) {
  192. x = (int)(-GD.rsin(r, th));
  193. y = (int)( GD.rcos(r, th));
  194. }
  195. // >>> [int(round(1024 * math.atan(i / 256.) / math.pi)) for i in range(256)]
  196. static const PROGMEM uint8_t atan8[] = {
  197. 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
  198. };
  199. uint16_t GDClass::atan2(int16_t y, int16_t x)
  200. {
  201. uint16_t a;
  202. uint16_t xx = 0;
  203. /* These values are tricky. So pretend they are not */
  204. if (x == -32768)
  205. x++;
  206. if (y == -32768)
  207. y++;
  208. if ((x <= 0) ^ (y > 0)) {
  209. int16_t t; t = x; x = y; y = t;
  210. xx ^= 0x4000;
  211. }
  212. if (x <= 0) {
  213. x = -x;
  214. } else {
  215. xx ^= 0x8000;
  216. }
  217. y = abs(y);
  218. if (x > y) {
  219. int16_t t; t = x; x = y; y = t;
  220. xx ^= 0x3fff;
  221. }
  222. while ((x | y) & 0xff80) {
  223. x >>= 1;
  224. y >>= 1;
  225. }
  226. if (y == 0) {
  227. a = 0;
  228. } else if (x == y) {
  229. a = 0x2000;
  230. } else {
  231. // assert(x <= y);
  232. int r = ((x << 8) / y);
  233. // assert(0 <= r);
  234. // assert(r < 256);
  235. a = pgm_read_byte(atan8 + r) << 5;
  236. }
  237. a ^= xx;
  238. return a;
  239. }
  240. void GDClass::align(byte n) {
  241. while ((n++) & 3)
  242. GDTR.cmdbyte(0);
  243. }
  244. void GDClass::cH(uint16_t v) {
  245. GDTR.cmdbyte(v & 0xff);
  246. GDTR.cmdbyte((v >> 8) & 0xff);
  247. }
  248. void GDClass::ch(int16_t v) {
  249. cH((uint16_t)v);
  250. }
  251. void GDClass::cI(uint32_t v) {
  252. GDTR.cmd32(v);
  253. }
  254. void GDClass::cFFFFFF(byte v) {
  255. union {
  256. uint32_t c;
  257. uint8_t b[4];
  258. };
  259. b[0] = v;
  260. b[1] = 0xff;
  261. b[2] = 0xff;
  262. b[3] = 0xff;
  263. GDTR.cmd32(c);
  264. }
  265. void GDClass::ci(int32_t v) {
  266. cI((uint32_t) v);
  267. }
  268. void GDClass::cs(const char *s) {
  269. int count = 0;
  270. while (*s) {
  271. char c = *s++;
  272. GDTR.cmdbyte(c);
  273. count++;
  274. }
  275. GDTR.cmdbyte(0);
  276. align(count + 1);
  277. }
  278. void GDClass::copy(const PROGMEM uint8_t *src, int count) {
  279. byte a = count & 3;
  280. while (count--) {
  281. GDTR.cmdbyte(pgm_read_byte_near(src));
  282. src++;
  283. }
  284. align(a);
  285. }
  286. void GDClass::copyram(byte *src, int count) {
  287. byte a = count & 3;
  288. GDTR.cmd_n(src, count);
  289. align(a);
  290. }
  291. void GDClass::AlphaFunc(byte func, byte ref) {
  292. cI((9UL << 24) | ((func & 7L) << 8) | ((ref & 255L) << 0));
  293. }
  294. void GDClass::Begin(byte prim) {
  295. cI((31UL << 24) | prim);
  296. }
  297. void GDClass::BitmapHandle(byte handle) {
  298. cI((5UL << 24) | handle);
  299. }
  300. void GDClass::BitmapLayout(byte format, uint16_t linestride, uint16_t height) {
  301. // cI((7UL << 24) | ((format & 31L) << 19) | ((linestride & 1023L) << 9) | ((height & 511L) << 0));
  302. union {
  303. uint32_t c;
  304. uint8_t b[4];
  305. };
  306. b[0] = height;
  307. b[1] = (1 & (height >> 8)) | (linestride << 1);
  308. b[2] = (7 & (linestride >> 7)) | (format << 3);
  309. b[3] = 7;
  310. cI(c);
  311. }
  312. void GDClass::BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height) {
  313. byte fxy = (filter << 2) | (wrapx << 1) | (wrapy);
  314. // cI((8UL << 24) | ((uint32_t)fxy << 18) | ((width & 511L) << 9) | ((height & 511L) << 0));
  315. union {
  316. uint32_t c;
  317. uint8_t b[4];
  318. };
  319. b[0] = height;
  320. b[1] = (1 & (height >> 8)) | (width << 1);
  321. b[2] = (3 & (width >> 7)) | (fxy << 2);
  322. b[3] = 8;
  323. cI(c);
  324. }
  325. void GDClass::BitmapSource(uint32_t addr) {
  326. cI((1UL << 24) | ((addr & 1048575L) << 0));
  327. }
  328. void GDClass::BitmapTransformA(int32_t a) {
  329. cI((21UL << 24) | ((a & 131071L) << 0));
  330. }
  331. void GDClass::BitmapTransformB(int32_t b) {
  332. cI((22UL << 24) | ((b & 131071L) << 0));
  333. }
  334. void GDClass::BitmapTransformC(int32_t c) {
  335. cI((23UL << 24) | ((c & 16777215L) << 0));
  336. }
  337. void GDClass::BitmapTransformD(int32_t d) {
  338. cI((24UL << 24) | ((d & 131071L) << 0));
  339. }
  340. void GDClass::BitmapTransformE(int32_t e) {
  341. cI((25UL << 24) | ((e & 131071L) << 0));
  342. }
  343. void GDClass::BitmapTransformF(int32_t f) {
  344. cI((26UL << 24) | ((f & 16777215L) << 0));
  345. }
  346. void GDClass::BlendFunc(byte src, byte dst) {
  347. cI((11UL << 24) | ((src & 7L) << 3) | ((dst & 7L) << 0));
  348. }
  349. void GDClass::Call(uint16_t dest) {
  350. cI((29UL << 24) | ((dest & 2047L) << 0));
  351. }
  352. void GDClass::Cell(byte cell) {
  353. cI((6UL << 24) | ((cell & 127L) << 0));
  354. }
  355. void GDClass::ClearColorA(byte alpha) {
  356. cI((15UL << 24) | ((alpha & 255L) << 0));
  357. }
  358. void GDClass::ClearColorRGB(byte red, byte green, byte blue) {
  359. cI((2UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  360. }
  361. void GDClass::ClearColorRGB(uint32_t rgb) {
  362. cI((2UL << 24) | (rgb & 0xffffffL));
  363. }
  364. void GDClass::Clear(byte c, byte s, byte t) {
  365. byte m = (c << 2) | (s << 1) | t;
  366. cI((38UL << 24) | m);
  367. }
  368. void GDClass::Clear(void) {
  369. cI((38UL << 24) | 7);
  370. }
  371. void GDClass::ClearStencil(byte s) {
  372. cI((17UL << 24) | ((s & 255L) << 0));
  373. }
  374. void GDClass::ClearTag(byte s) {
  375. cI((18UL << 24) | ((s & 255L) << 0));
  376. }
  377. void GDClass::ColorA(byte alpha) {
  378. cI((16UL << 24) | ((alpha & 255L) << 0));
  379. }
  380. void GDClass::ColorMask(byte r, byte g, byte b, byte a) {
  381. cI((32UL << 24) | ((r & 1L) << 3) | ((g & 1L) << 2) | ((b & 1L) << 1) | ((a & 1L) << 0));
  382. }
  383. void GDClass::ColorRGB(byte red, byte green, byte blue) {
  384. // cI((4UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  385. union {
  386. uint32_t c;
  387. uint8_t b[4];
  388. };
  389. b[0] = blue;
  390. b[1] = green;
  391. b[2] = red;
  392. b[3] = 4;
  393. cI(c);
  394. }
  395. void GDClass::ColorRGB(uint32_t rgb) {
  396. cI((4UL << 24) | (rgb & 0xffffffL));
  397. }
  398. void GDClass::Display(void) {
  399. cI((0UL << 24));
  400. }
  401. void GDClass::End(void) {
  402. cI((33UL << 24));
  403. }
  404. void GDClass::Jump(uint16_t dest) {
  405. cI((30UL << 24) | ((dest & 2047L) << 0));
  406. }
  407. void GDClass::LineWidth(uint16_t width) {
  408. cI((14UL << 24) | ((width & 4095L) << 0));
  409. }
  410. void GDClass::Macro(byte m) {
  411. cI((37UL << 24) | ((m & 1L) << 0));
  412. }
  413. void GDClass::PointSize(uint16_t size) {
  414. cI((13UL << 24) | ((size & 8191L) << 0));
  415. }
  416. void GDClass::RestoreContext(void) {
  417. cI((35UL << 24));
  418. }
  419. void GDClass::Return(void) {
  420. cI((36UL << 24));
  421. }
  422. void GDClass::SaveContext(void) {
  423. cI((34UL << 24));
  424. }
  425. void GDClass::ScissorSize(uint16_t width, uint16_t height) {
  426. if (ft8xx_model == 0)
  427. cI((28UL << 24) | ((width & 1023L) << 10) | ((height & 1023L) << 0));
  428. else
  429. cI((28UL << 24) | ((width & 4095L) << 12) | ((height & 4095L) << 0));
  430. }
  431. void GDClass::ScissorXY(uint16_t x, uint16_t y) {
  432. if (ft8xx_model == 0)
  433. cI((27UL << 24) | ((x & 511L) << 9) | ((y & 511L) << 0));
  434. else
  435. cI((27UL << 24) | ((x & 2047L) << 11) | ((y & 2047L) << 0));
  436. }
  437. void GDClass::StencilFunc(byte func, byte ref, byte mask) {
  438. cI((10UL << 24) | ((func & 7L) << 16) | ((ref & 255L) << 8) | ((mask & 255L) << 0));
  439. }
  440. void GDClass::StencilMask(byte mask) {
  441. cI((19UL << 24) | ((mask & 255L) << 0));
  442. }
  443. void GDClass::StencilOp(byte sfail, byte spass) {
  444. cI((12UL << 24) | ((sfail & 7L) << 3) | ((spass & 7L) << 0));
  445. }
  446. void GDClass::TagMask(byte mask) {
  447. cI((20UL << 24) | ((mask & 1L) << 0));
  448. }
  449. void GDClass::Tag(byte s) {
  450. cI((3UL << 24) | ((s & 255L) << 0));
  451. }
  452. void GDClass::Vertex2f(int16_t x, int16_t y) {
  453. // x = int(16 * x);
  454. // y = int(16 * y);
  455. cI((1UL << 30) | ((x & 32767L) << 15) | ((y & 32767L) << 0));
  456. }
  457. void GDClass::Vertex2ii(uint16_t x, uint16_t y, byte handle, byte cell) {
  458. // cI((2UL << 30) | ((x & 511L) << 21) | ((y & 511L) << 12) | ((handle & 31L) << 7) | ((cell & 127L) << 0));
  459. union {
  460. uint32_t c;
  461. uint8_t b[4];
  462. };
  463. b[0] = cell | ((handle & 1) << 7);
  464. b[1] = (handle >> 1) | (y << 4);
  465. b[2] = (y >> 4) | (x << 5);
  466. b[3] = (2 << 6) | (x >> 3);
  467. cI(c);
  468. }
  469. void GDClass::cmd_append(uint32_t ptr, uint32_t num) {
  470. cFFFFFF(0x1e);
  471. cI(ptr);
  472. cI(num);
  473. }
  474. void GDClass::cmd_bgcolor(uint32_t c) {
  475. cFFFFFF(0x09);
  476. cI(c);
  477. }
  478. void GDClass::cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s) {
  479. cFFFFFF(0x0d);
  480. ch(x);
  481. ch(y);
  482. ch(w);
  483. ch(h);
  484. ch(font);
  485. cH(options);
  486. cs(s);
  487. }
  488. void GDClass::cmd_calibrate(void) {
  489. cFFFFFF(0x15);
  490. cFFFFFF(0xff);
  491. }
  492. 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) {
  493. cFFFFFF(0x14);
  494. ch(x);
  495. ch(y);
  496. ch(r);
  497. cH(options);
  498. cH(h);
  499. cH(m);
  500. cH(s);
  501. cH(ms);
  502. }
  503. void GDClass::cmd_coldstart(void) {
  504. cFFFFFF(0x32);
  505. }
  506. void GDClass::cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val) {
  507. cFFFFFF(0x2d);
  508. ch(x);
  509. ch(y);
  510. ch(r);
  511. cH(options);
  512. cH(val);
  513. cH(0);
  514. }
  515. void GDClass::cmd_dlstart(void) {
  516. cFFFFFF(0x00);
  517. }
  518. void GDClass::cmd_fgcolor(uint32_t c) {
  519. cFFFFFF(0x0a);
  520. cI(c);
  521. }
  522. 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) {
  523. cFFFFFF(0x13);
  524. ch(x);
  525. ch(y);
  526. ch(r);
  527. cH(options);
  528. cH(major);
  529. cH(minor);
  530. cH(val);
  531. cH(range);
  532. }
  533. void GDClass::cmd_getmatrix(void) {
  534. cFFFFFF(0x33);
  535. ci(0);
  536. ci(0);
  537. ci(0);
  538. ci(0);
  539. ci(0);
  540. ci(0);
  541. }
  542. void GDClass::cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h) {
  543. cFFFFFF(0x25);
  544. ptr = GDTR.getwp();
  545. cI(0);
  546. w = GDTR.getwp();
  547. cI(0);
  548. h = GDTR.getwp();
  549. cI(0);
  550. }
  551. void GDClass::cmd_getptr(void) {
  552. cFFFFFF(0x23);
  553. cI(0);
  554. }
  555. void GDClass::cmd_gradcolor(uint32_t c) {
  556. cFFFFFF(0x34);
  557. cI(c);
  558. }
  559. void GDClass::cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1) {
  560. cFFFFFF(0x0b);
  561. ch(x0);
  562. ch(y0);
  563. cI(rgb0);
  564. ch(x1);
  565. ch(y1);
  566. cI(rgb1);
  567. }
  568. void GDClass::cmd_inflate(uint32_t ptr) {
  569. cFFFFFF(0x22);
  570. cI(ptr);
  571. }
  572. void GDClass::cmd_interrupt(uint32_t ms) {
  573. cFFFFFF(0x02);
  574. cI(ms);
  575. }
  576. void GDClass::cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s) {
  577. cFFFFFF(0x0e);
  578. ch(x);
  579. ch(y);
  580. ch(w);
  581. ch(h);
  582. ch(font);
  583. cH(options);
  584. cs(s);
  585. }
  586. void GDClass::cmd_loadidentity(void) {
  587. cFFFFFF(0x26);
  588. }
  589. void GDClass::cmd_loadimage(uint32_t ptr, int32_t options) {
  590. cFFFFFF(0x24);
  591. cI(ptr);
  592. cI(options);
  593. }
  594. void GDClass::cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num) {
  595. cFFFFFF(0x1d);
  596. cI(dest);
  597. cI(src);
  598. cI(num);
  599. }
  600. void GDClass::cmd_memset(uint32_t ptr, byte value, uint32_t num) {
  601. cFFFFFF(0x1b);
  602. cI(ptr);
  603. cI((uint32_t)value);
  604. cI(num);
  605. }
  606. uint32_t GDClass::cmd_memcrc(uint32_t ptr, uint32_t num) {
  607. cFFFFFF(0x18);
  608. cI(ptr);
  609. cI(num);
  610. uint32_t r = GDTR.getwp();
  611. cI(0xFFFFFFFF);
  612. return r;
  613. }
  614. void GDClass::cmd_memwrite(uint32_t ptr, uint32_t num) {
  615. cFFFFFF(0x1a);
  616. cI(ptr);
  617. cI(num);
  618. }
  619. void GDClass::cmd_regwrite(uint32_t ptr, uint32_t val) {
  620. cFFFFFF(0x1a);
  621. cI(ptr);
  622. cI(4UL);
  623. cI(val);
  624. }
  625. void GDClass::cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n) {
  626. cFFFFFF(0x2e);
  627. ch(x);
  628. ch(y);
  629. ch(font);
  630. cH(options);
  631. ci(n);
  632. }
  633. 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) {
  634. cFFFFFF(0x0f);
  635. ch(x);
  636. ch(y);
  637. ch(w);
  638. ch(h);
  639. cH(options);
  640. cH(val);
  641. cH(range);
  642. cH(0);
  643. }
  644. void GDClass::cmd_regread(uint32_t ptr) {
  645. cFFFFFF(0x19);
  646. cI(ptr);
  647. cI(0);
  648. }
  649. void GDClass::cmd_rotate(int32_t a) {
  650. cFFFFFF(0x29);
  651. ci(a);
  652. }
  653. void GDClass::cmd_scale(int32_t sx, int32_t sy) {
  654. cFFFFFF(0x28);
  655. ci(sx);
  656. ci(sy);
  657. }
  658. void GDClass::cmd_screensaver(void) {
  659. cFFFFFF(0x2f);
  660. }
  661. 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) {
  662. cFFFFFF(0x11);
  663. ch(x);
  664. ch(y);
  665. ch(w);
  666. ch(h);
  667. cH(options);
  668. cH(val);
  669. cH(size);
  670. cH(range);
  671. }
  672. void GDClass::cmd_setfont(byte font, uint32_t ptr) {
  673. cFFFFFF(0x2b);
  674. cI(font);
  675. cI(ptr);
  676. }
  677. void GDClass::cmd_setmatrix(void) {
  678. cFFFFFF(0x2a);
  679. }
  680. void GDClass::cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format) {
  681. cFFFFFF(0x30);
  682. ch(x);
  683. ch(y);
  684. cH(w);
  685. cH(h);
  686. cI(ptr);
  687. cI(format);
  688. }
  689. 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) {
  690. cFFFFFF(0x10);
  691. ch(x);
  692. ch(y);
  693. ch(w);
  694. ch(h);
  695. cH(options);
  696. cH(val);
  697. cH(range);
  698. cH(0);
  699. }
  700. void GDClass::cmd_snapshot(uint32_t ptr) {
  701. cFFFFFF(0x1f);
  702. cI(ptr);
  703. }
  704. void GDClass::cmd_spinner(int16_t x, int16_t y, byte style, byte scale) {
  705. cFFFFFF(0x16);
  706. ch(x);
  707. ch(y);
  708. cH(style);
  709. cH(scale);
  710. }
  711. void GDClass::cmd_stop(void) {
  712. cFFFFFF(0x17);
  713. }
  714. void GDClass::cmd_swap(void) {
  715. cFFFFFF(0x01);
  716. }
  717. void GDClass::cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s) {
  718. cFFFFFF(0x0c);
  719. ch(x);
  720. ch(y);
  721. ch(font);
  722. cH(options);
  723. cs(s);
  724. }
  725. void GDClass::cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s) {
  726. cFFFFFF(0x12);
  727. ch(x);
  728. ch(y);
  729. ch(w);
  730. ch(font);
  731. cH(options);
  732. cH(state);
  733. cs(s);
  734. }
  735. void GDClass::cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag) {
  736. cFFFFFF(0x2c);
  737. ch(x);
  738. ch(y);
  739. ch(w);
  740. ch(h);
  741. ch(tag);
  742. ch(0);
  743. }
  744. void GDClass::cmd_translate(int32_t tx, int32_t ty) {
  745. cFFFFFF(0x27);
  746. ci(tx);
  747. ci(ty);
  748. }
  749. byte GDClass::rd(uint32_t addr) {
  750. return GDTR.rd(addr);
  751. }
  752. void GDClass::wr(uint32_t addr, uint8_t v) {
  753. GDTR.wr(addr, v);
  754. }
  755. uint16_t GDClass::rd16(uint32_t addr) {
  756. return GDTR.rd16(addr);
  757. }
  758. void GDClass::wr16(uint32_t addr, uint16_t v) {
  759. GDTR.wr16(addr, v);
  760. }
  761. uint32_t GDClass::rd32(uint32_t addr) {
  762. return GDTR.rd32(addr);
  763. }
  764. void GDClass::wr32(uint32_t addr, uint32_t v) {
  765. GDTR.wr32(addr, v);
  766. }
  767. void GDClass::wr_n(uint32_t addr, byte *src, uint32_t n) {
  768. GDTR.wr_n(addr, src, n);
  769. }
  770. void GDClass::cmdbyte(uint8_t b) {
  771. GDTR.cmdbyte(b);
  772. }
  773. void GDClass::cmd32(uint32_t b) {
  774. GDTR.cmd32(b);
  775. }
  776. void GDClass::finish(void) {
  777. GDTR.finish();
  778. }
  779. void GDClass::get_accel(int &x, int &y, int &z) {
  780. static int f[3];
  781. for (byte i = 0; i < 3; i++) {
  782. int a = analogRead(A0 + i);
  783. int s = (-160 * (a - 376)) >> 6;
  784. f[i] = ((3 * f[i]) >> 2) + (s >> 2);
  785. }
  786. x = f[2];
  787. y = f[1];
  788. z = f[0];
  789. }
  790. void GDClass::get_inputs(void) {
  791. GDTR.finish();
  792. byte *bi = (byte*)&inputs;
  793. #if defined(DUMPDEV)
  794. extern FILE* stimfile;
  795. if (stimfile) {
  796. byte tag;
  797. fscanf(stimfile, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
  798. &bi[0],
  799. &bi[1],
  800. &bi[2],
  801. &bi[3],
  802. &bi[4],
  803. &bi[5],
  804. &bi[6],
  805. &bi[7],
  806. &bi[8],
  807. &bi[9],
  808. &bi[10],
  809. &bi[11],
  810. &bi[12],
  811. &bi[13],
  812. &bi[14],
  813. &bi[15],
  814. &bi[16],
  815. &bi[17]);
  816. GDTR.wr(REG_TAG, tag);
  817. } else {
  818. inputs.x = inputs.y = -32768;
  819. }
  820. #else
  821. GDTR.rd_n(bi, REG_TRACKER, 4);
  822. GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
  823. GDTR.rd_n(bi + 17, REG_TAG, 1);
  824. #if DUMP_INPUTS
  825. for (size_t i = 0; i < sizeof(inputs); i++) {
  826. Serial.print(bi[i], HEX);
  827. Serial.print(" ");
  828. }
  829. Serial.println();
  830. #endif
  831. #endif
  832. }
  833. void GDClass::bulkrd(uint32_t a) {
  834. GDTR.bulk(a);
  835. }
  836. void GDClass::resume(void) {
  837. GDTR.resume();
  838. }
  839. void GDClass::__end(void) {
  840. #if !defined(DUMPDEV) && !defined(RASPBERRY_PI)
  841. GDTR.__end();
  842. #endif
  843. }
  844. void GDClass::play(uint8_t instrument, uint8_t note) {
  845. wr16(REG_SOUND, (note << 8) | instrument);
  846. wr(REG_PLAY, 1);
  847. }
  848. void GDClass::sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop) {
  849. GD.wr32(REG_PLAYBACK_START, start);
  850. GD.wr32(REG_PLAYBACK_LENGTH, len);
  851. GD.wr16(REG_PLAYBACK_FREQ, freq);
  852. GD.wr(REG_PLAYBACK_FORMAT, format);
  853. GD.wr(REG_PLAYBACK_LOOP, loop);
  854. GD.wr(REG_PLAYBACK_PLAY, 1);
  855. }
  856. void GDClass::reset() {
  857. GDTR.__end();
  858. GDTR.wr(REG_CPURESET, 1);
  859. GDTR.wr(REG_CPURESET, 0);
  860. GDTR.resume();
  861. }
  862. // Load named file from storage
  863. // returns 0 on failure (e.g. file not found), 1 on success
  864. byte GDClass::load(const char *filename, void (*progress)(long, long))
  865. {
  866. #if defined(RASPBERRY_PI) || defined(DUMPDEV)
  867. FILE *f = fopen(filename, "rb");
  868. if (!f) {
  869. perror(filename);
  870. exit(1);
  871. }
  872. byte buf[512];
  873. int n;
  874. while ((n = fread(buf, 1, 512, f)) > 0) {
  875. GDTR.cmd_n(buf, (n + 3) & ~3);
  876. }
  877. fclose(f);
  878. return 1;
  879. #else
  880. GD.__end();
  881. Reader r;
  882. if (r.openfile(filename)) {
  883. byte buf[512];
  884. while (r.offset < r.size) {
  885. uint16_t n = min(512, r.size - r.offset);
  886. n = (n + 3) & ~3; // force 32-bit alignment
  887. r.readsector(buf);
  888. GD.resume();
  889. if (progress)
  890. (*progress)(r.offset, r.size);
  891. GD.copyram(buf, n);
  892. GDTR.stop();
  893. }
  894. GD.resume();
  895. return 1;
  896. }
  897. GD.resume();
  898. return 0;
  899. #endif
  900. }
  901. // Generated by mk_bsod.py. Blue screen with 'ERROR' text
  902. static const PROGMEM uint8_t __bsod[32] = {
  903. 0, 255, 255, 255, 96, 0, 0, 2, 7, 0, 0, 38, 12, 255, 255, 255, 240, 0,
  904. 90, 0, 31, 0, 0, 6, 69, 82, 82, 79, 82, 0, 0, 0
  905. };
  906. static const PROGMEM uint8_t __bsod_badfile[32] = {
  907. 12, 255, 255, 255, 240, 0, 148, 0, 29, 0, 0, 6, 67, 97, 110, 110, 111,
  908. 116, 32, 111, 112, 101, 110, 32, 102, 105, 108, 101, 58, 0, 0, 0
  909. };
  910. // Fatal error alert.
  911. // Show a blue screen with message.
  912. // This method never returns.
  913. void GDClass::alert(const char *message)
  914. {
  915. begin(0);
  916. copy(__bsod, sizeof(__bsod));
  917. cmd_text(240, 176, 29, OPT_CENTER, message);
  918. swap();
  919. GD.finish();
  920. for (;;)
  921. ;
  922. }
  923. void GDClass::safeload(const char *filename)
  924. {
  925. if (!load(filename)) {
  926. copy(__bsod, sizeof(__bsod));
  927. copy(__bsod_badfile, sizeof(__bsod_badfile));
  928. cmd_text(240, 190, 29, OPT_CENTER, filename);
  929. swap();
  930. for (;;)
  931. ;
  932. }
  933. }