GD2.cpp 23 KB

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