GD2.cpp 23 KB

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