GD2.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*
  2. * Copyright (C) 2013-2017 by James Bowman <jamesb@excamera.com>
  3. * Gameduino 2/3 library for Arduino, Arduino Due, Raspberry Pi,
  4. * Teensy 3.2 and ESP8266.
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include "SPI.h"
  9. #if !defined(__SAM3X8E__)
  10. #include "EEPROM.h"
  11. #endif
  12. #define VERBOSE 0
  13. #include <GD2.h>
  14. #if defined(ESP8266)
  15. #define SD_PIN D9 // pin used for the microSD enable signal
  16. #else
  17. #define SD_PIN 9 // pin used for the microSD enable signal
  18. #endif
  19. #define BOARD_FTDI_80x 0
  20. #define BOARD_GAMEDUINO23 1
  21. #define BOARD BOARD_GAMEDUINO23 // board, from above
  22. #define STORAGE 1 // Want SD storage?
  23. #define CALIBRATION 1 // Want touchscreen?
  24. // FTDI boards do not have storage
  25. #if (BOARD == BOARD_FTDI_80x) || defined(RASPBERRY_PI) || defined(DUMPDEV) || defined(SPIDRIVER)
  26. #undef STORAGE
  27. #define STORAGE 0
  28. #endif
  29. #ifdef DUMPDEV
  30. #include <assert.h>
  31. #include "transports/dump.h"
  32. #endif
  33. #ifdef RASPBERRY_PI
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <stdint.h>
  40. #include <sys/ioctl.h>
  41. #include <linux/types.h>
  42. #include <linux/spi/spidev.h>
  43. #include "transports/spidev.h"
  44. #endif
  45. byte ft8xx_model;
  46. #if defined(ARDUINO)
  47. #include "transports/wiring.h"
  48. #endif
  49. #if defined(SPIDRIVER)
  50. #include "transports/tr-spidriver.h"
  51. #endif
  52. ////////////////////////////////////////////////////////////////////////
  53. void xy::set(int _x, int _y)
  54. {
  55. x = _x;
  56. y = _y;
  57. }
  58. void xy::rmove(int distance, int angle)
  59. {
  60. x -= GD.rsin(distance, angle);
  61. y += GD.rcos(distance, angle);
  62. }
  63. int xy::angleto(class xy &other)
  64. {
  65. int dx = other.x - x, dy = other.y - y;
  66. return GD.atan2(dy, dx);
  67. }
  68. void xy::draw(byte offset)
  69. {
  70. GD.Vertex2f(x - PIXELS(offset), y - PIXELS(offset));
  71. }
  72. int xy::onscreen(void)
  73. {
  74. return (0 <= x) &&
  75. (x < PIXELS(GD.w)) &&
  76. (0 <= y) &&
  77. (y < PIXELS(GD.h));
  78. }
  79. class xy xy::operator+=(class xy &other)
  80. {
  81. x += other.x;
  82. y += other.y;
  83. return *this;
  84. }
  85. class xy xy::operator-=(class xy &other)
  86. {
  87. x -= other.x;
  88. y -= other.y;
  89. return *this;
  90. }
  91. class xy xy::operator<<=(int d)
  92. {
  93. x <<= d;
  94. y <<= d;
  95. return *this;
  96. }
  97. long xy::operator*(class xy &other)
  98. {
  99. return (long(x) * other.x) + (long(y) * other.y);
  100. }
  101. class xy xy::operator*=(int s)
  102. {
  103. x *= s;
  104. y *= s;
  105. return *this;
  106. }
  107. int xy::nearer_than(int distance, xy &other)
  108. {
  109. int lx = abs(x - other.x);
  110. if (lx > distance)
  111. return 0;
  112. int ly = abs(y - other.y);
  113. if (ly > distance)
  114. return 0;
  115. // trivial accept: 5/8 is smaller than 1/sqrt(2)
  116. int d2 = (5 * distance) >> 3;
  117. if ((lx < d2) && (ly < d2))
  118. return 1;
  119. #define SQ(c) (long(c) * (c))
  120. return (SQ(lx) + SQ(ly)) < SQ(distance);
  121. #undef SQ
  122. }
  123. void xy::rotate(int angle)
  124. {
  125. // the hardware's convention that rotation is clockwise
  126. int32_t s = GD.rsin(32767, angle);
  127. int32_t c = GD.rcos(32767, angle);
  128. int xr = ((x * c) - (y * s)) >> 15;
  129. int yr = ((x * s) + (y * c)) >> 15;
  130. x = xr;
  131. y = yr;
  132. }
  133. ////////////////////////////////////////////////////////////////////////
  134. void Bitmap::fromtext(int font, const char* s)
  135. {
  136. GD.textsize(size.x, size.y, font, s);
  137. int pclk = GD.rd16(REG_PCLK);
  138. int vsize = GD.rd16(REG_VSIZE);
  139. int hsize = GD.rd16(REG_HSIZE);
  140. GD.finish();
  141. GD.wr(REG_PCLK, 0);
  142. delay(1);
  143. GD.wr16(REG_HSIZE, size.x);
  144. GD.wr16(REG_VSIZE, size.y);
  145. GD.cmd_dlstart();
  146. GD.Clear();
  147. GD.BlendFunc(1,1);
  148. GD.cmd_text(0, 0, font, 0, s);
  149. GD.swap();
  150. GD.loadptr = (GD.loadptr + 1) & ~1;
  151. GD.cmd_snapshot(GD.loadptr);
  152. GD.finish();
  153. GD.wr16(REG_HSIZE, hsize);
  154. GD.wr16(REG_VSIZE, vsize);
  155. GD.wr16(REG_PCLK, pclk);
  156. defaults(ARGB4);
  157. }
  158. void Bitmap::fromfile(const char* filename, int format)
  159. {
  160. GD.loadptr = (GD.loadptr + 1) & ~1;
  161. GD.cmd_loadimage(GD.loadptr, OPT_NODL);
  162. GD.load(filename);
  163. uint32_t ptr, w, h;
  164. GD.cmd_getprops(ptr, w, h);
  165. GD.finish();
  166. size.x = GD.rd16(w);
  167. size.y = GD.rd16(h);
  168. defaults(format);
  169. }
  170. static const PROGMEM uint8_t bpltab[] = {
  171. /* 0 ARGB1555 */ 0,
  172. /* 1 L1 */ 4,
  173. /* 2 L4 */ 2,
  174. /* 3 L8 */ 1,
  175. /* 4 RGB332 */ 1,
  176. /* 5 ARGB2 */ 1,
  177. /* 6 ARGB4 */ 0,
  178. /* 7 RGB565 */ 0,
  179. /* 8 PALETTED */ 1,
  180. /* 9 TEXT8X8 */ 0,
  181. /* 10 TEXTVGA */ 0,
  182. /* 11 BARGRAPH */ 1,
  183. /* 12 */ 0,
  184. /* 13 */ 0,
  185. /* 14 */ 0,
  186. /* 15 */ 0,
  187. /* 16 */ 0,
  188. /* 17 L2 */ 3
  189. };
  190. void Bitmap::defaults(uint8_t f)
  191. {
  192. source = GD.loadptr;
  193. format = f;
  194. handle = -1;
  195. center.x = size.x / 2;
  196. center.y = size.y / 2;
  197. GD.loadptr += (long)((size.x << 1) >> pgm_read_byte_near(bpltab + f)) * size.y;
  198. }
  199. void Bitmap::setup(void)
  200. {
  201. GD.BitmapSource(source);
  202. int bpl = (size.x << 1) >> pgm_read_byte_near(bpltab + format);
  203. GD.BitmapLayout(format, bpl, size.y);
  204. GD.BitmapSize(NEAREST, BORDER, BORDER, size.x, size.y);
  205. }
  206. void Bitmap::bind(uint8_t h)
  207. {
  208. handle = h;
  209. GD.BitmapHandle(handle);
  210. setup();
  211. }
  212. #define IS_POWER_2(x) (((x) & ((x) - 1)) == 0)
  213. void Bitmap::wallpaper()
  214. {
  215. if (handle == -1) {
  216. GD.BitmapHandle(15);
  217. setup();
  218. } else {
  219. GD.BitmapHandle(handle);
  220. }
  221. GD.Begin(BITMAPS);
  222. // if power-of-2, can just use REPEAT,REPEAT
  223. // otherwise must draw it across whole screen
  224. if (IS_POWER_2(size.x) && IS_POWER_2(size.y)) {
  225. GD.BitmapSize(NEAREST, REPEAT, REPEAT, GD.w, GD.h);
  226. GD.Vertex2f(0, 0);
  227. } else {
  228. for (int x = 0; x < GD.w; x += size.x)
  229. for (int y = 0; y < GD.h; y += size.y)
  230. GD.Vertex2f(x << 4, y << 4);
  231. }
  232. }
  233. void Bitmap::draw(int x, int y, int16_t angle)
  234. {
  235. xy pos;
  236. pos.set(x, y);
  237. pos <<= 4;
  238. draw(pos, angle);
  239. }
  240. void Bitmap::draw(const xy &p, int16_t angle)
  241. {
  242. xy pos = p;
  243. if (handle == -1) {
  244. GD.BitmapHandle(15);
  245. setup();
  246. } else {
  247. GD.BitmapHandle(handle);
  248. }
  249. GD.Begin(BITMAPS);
  250. if (angle == 0) {
  251. xy c4 = center;
  252. c4 <<= 4;
  253. pos -= c4;
  254. GD.BitmapSize(NEAREST, BORDER, BORDER, size.x, size.y);
  255. GD.Vertex2f(pos.x, pos.y);
  256. } else {
  257. // Compute the screen positions of 4 corners of the bitmap
  258. xy corners[4] = {
  259. {0,0 },
  260. {size.x, 0 },
  261. {0, size.y },
  262. {size.x, size.y },
  263. };
  264. for (int i = 0; i < 4; i++) {
  265. xy &c = corners[i];
  266. c -= center;
  267. c <<= 4;
  268. c.rotate(angle);
  269. c += pos;
  270. }
  271. // Find top-left and bottom-right boundaries
  272. xy topleft, bottomright;
  273. topleft.set(
  274. min(min(corners[0].x, corners[1].x), min(corners[2].x, corners[3].x)),
  275. min(min(corners[0].y, corners[1].y), min(corners[2].y, corners[3].y)));
  276. bottomright.set(
  277. max(max(corners[0].x, corners[1].x), max(corners[2].x, corners[3].x)),
  278. max(max(corners[0].y, corners[1].y), max(corners[2].y, corners[3].y)));
  279. // span is the total size of this region
  280. xy span = bottomright;
  281. span -= topleft;
  282. GD.BitmapSize(BILINEAR, BORDER, BORDER,
  283. (span.x + 15) >> 4, (span.y + 15) >> 4);
  284. // Set up the transform and draw the bitmap
  285. pos -= topleft;
  286. GD.SaveContext();
  287. GD.cmd_loadidentity();
  288. GD.cmd_translate((int32_t)pos.x << 12, (int32_t)pos.y << 12);
  289. GD.cmd_rotate(angle);
  290. GD.cmd_translate(F16(-center.x), F16(-center.y));
  291. GD.cmd_setmatrix();
  292. GD.Vertex2f(topleft.x, topleft.y);
  293. GD.RestoreContext();
  294. }
  295. }
  296. class Bitmap __fromatlas(uint32_t a)
  297. {
  298. Bitmap r;
  299. r.size.x = GD.rd16(a);
  300. r.size.y = GD.rd16(a + 2);
  301. r.center.x = GD.rd16(a + 4);
  302. r.center.y = GD.rd16(a + 6);
  303. r.source = GD.rd32(a + 8);
  304. r.format = GD.rd(a + 12);
  305. r.handle = -1;
  306. return r;
  307. }
  308. ////////////////////////////////////////////////////////////////////////
  309. static GDTransport GDTR;
  310. GDClass GD;
  311. ////////////////////////////////////////////////////////////////////////
  312. // The GD3 has a tiny configuration EEPROM - AT24C01D
  313. // It is programmed at manufacturing time with the setup
  314. // commands for the connected panel. The SCL,SDA lines
  315. // are connected to the FT81x GPIO0, GPIO1 signals.
  316. // This is a read-only driver for it. A single method
  317. // 'read()' initializes the RAM and reads all 128 bytes
  318. // into an array.
  319. class ConfigRam {
  320. private:
  321. uint8_t gpio, gpio_dir, sda;
  322. void set_SDA(byte n)
  323. {
  324. if (sda != n) {
  325. GDTR.__wr16(REG_GPIO_DIR, gpio_dir | (0x03 - n)); // Drive SCL, SDA low
  326. sda = n;
  327. }
  328. }
  329. void set_SCL(byte n)
  330. {
  331. GDTR.__wr16(REG_GPIO, gpio | (n << 1));
  332. }
  333. int get_SDA(void)
  334. {
  335. return GDTR.__rd16(REG_GPIO) & 1;
  336. }
  337. void i2c_start(void)
  338. {
  339. set_SDA(1);
  340. set_SCL(1);
  341. set_SDA(0);
  342. set_SCL(0);
  343. }
  344. void i2c_stop(void)
  345. {
  346. set_SDA(0);
  347. set_SCL(1);
  348. set_SDA(1);
  349. set_SCL(1);
  350. }
  351. int i2c_rx1()
  352. {
  353. set_SDA(1);
  354. set_SCL(1);
  355. byte r = get_SDA();
  356. set_SCL(0);
  357. return r;
  358. }
  359. void i2c_tx1(byte b)
  360. {
  361. set_SDA(b);
  362. set_SCL(1);
  363. set_SCL(0);
  364. }
  365. int i2c_tx(byte x)
  366. {
  367. for (byte i = 0; i < 8; i++, x <<= 1)
  368. i2c_tx1(x >> 7);
  369. return i2c_rx1();
  370. }
  371. int i2c_rx(int nak)
  372. {
  373. byte r = 0;
  374. for (byte i = 0; i < 8; i++)
  375. r = (r << 1) | i2c_rx1();
  376. i2c_tx1(nak);
  377. return r;
  378. }
  379. public:
  380. void read(byte *v)
  381. {
  382. GDTR.__end();
  383. gpio = GDTR.__rd16(REG_GPIO) & ~3;
  384. gpio_dir = GDTR.__rd16(REG_GPIO_DIR) & ~3;
  385. sda = 2;
  386. // 2-wire software reset
  387. i2c_start();
  388. i2c_rx(1);
  389. i2c_start();
  390. i2c_stop();
  391. int ADDR = 0xa0;
  392. i2c_start();
  393. if (i2c_tx(ADDR))
  394. return;
  395. if (i2c_tx(0))
  396. return;
  397. i2c_start();
  398. if (i2c_tx(ADDR | 1))
  399. return;
  400. for (int i = 0; i < 128; i++) {
  401. *v++ = i2c_rx(i == 127);
  402. // Serial.println(v[-1], DEC);
  403. }
  404. i2c_stop();
  405. GDTR.resume();
  406. }
  407. };
  408. void GDClass::flush(void)
  409. {
  410. GDTR.flush();
  411. }
  412. void GDClass::swap(void) {
  413. Display();
  414. cmd_swap();
  415. cmd_loadidentity();
  416. cmd_dlstart();
  417. GDTR.flush();
  418. #ifdef DUMPDEV
  419. GDTR.swap();
  420. #endif
  421. }
  422. uint32_t GDClass::measure_freq(void)
  423. {
  424. unsigned long t0 = GDTR.rd32(REG_CLOCK);
  425. delayMicroseconds(15625);
  426. unsigned long t1 = GDTR.rd32(REG_CLOCK);
  427. // Serial.println((t1 - t0) << 6);
  428. return (t1 - t0) << 6;
  429. }
  430. #define LOW_FREQ_BOUND 47040000UL
  431. // #define LOW_FREQ_BOUND 32040000UL
  432. void GDClass::tune(void)
  433. {
  434. uint32_t f;
  435. for (byte i = 0; (i < 31) && ((f = measure_freq()) < LOW_FREQ_BOUND); i++) {
  436. GDTR.wr(REG_TRIM, i);
  437. }
  438. GDTR.wr32(REG_FREQUENCY, f);
  439. }
  440. void GDClass::begin(uint8_t options) {
  441. #if defined(ARDUINO) || defined(ESP8266) || defined(SPIDRIVER)
  442. GDTR.begin0();
  443. if (STORAGE && (options & GD_STORAGE)) {
  444. GDTR.ios();
  445. SD.begin(SD_PIN);
  446. }
  447. #endif
  448. byte external_crystal = 0;
  449. begin1:
  450. GDTR.begin1();
  451. #if 0
  452. Serial.println("ID REGISTER:");
  453. Serial.println(GDTR.rd(REG_ID), HEX);
  454. #endif
  455. #if (BOARD == BOARD_FTDI_80x)
  456. GDTR.wr(REG_PCLK_POL, 1);
  457. GDTR.wr(REG_PCLK, 5);
  458. #endif
  459. GDTR.wr(REG_PWM_DUTY, 0);
  460. GDTR.wr(REG_GPIO_DIR, 0x83);
  461. GDTR.wr(REG_GPIO, GDTR.rd(REG_GPIO) | 0x80);
  462. #if (BOARD == BOARD_GAMEDUINO23)
  463. ConfigRam cr;
  464. byte v8[128] = {0};
  465. cr.read(v8);
  466. if ((v8[1] == 0xff) && (v8[2] == 0x01)) {
  467. options &= ~(GD_TRIM | GD_CALIBRATE);
  468. if (!external_crystal && (v8[3] & 2)) {
  469. GDTR.external_crystal();
  470. external_crystal = 1;
  471. goto begin1;
  472. }
  473. copyram(v8 + 4, 124);
  474. finish();
  475. } else {
  476. GDTR.wr(REG_PCLK_POL, 1);
  477. GDTR.wr(REG_PCLK, 5);
  478. GDTR.wr(REG_ROTATE, 1);
  479. GDTR.wr(REG_SWIZZLE, 3);
  480. }
  481. #endif
  482. w = GDTR.rd16(REG_HSIZE);
  483. h = GDTR.rd16(REG_VSIZE);
  484. loadptr = 0;
  485. // Work-around issue with bitmap sizes not being reset
  486. for (byte i = 0; i < 32; i++) {
  487. BitmapHandle(i);
  488. cI(0x28000000UL);
  489. cI(0x29000000UL);
  490. }
  491. Clear(); swap();
  492. Clear(); swap();
  493. Clear(); swap();
  494. cmd_regwrite(REG_PWM_DUTY, 128);
  495. flush();
  496. if (CALIBRATION & (options & GD_CALIBRATE)) {
  497. #if defined(ARDUINO) && !defined(__DUE__)
  498. if ((EEPROM.read(0) != 0x7c)) {
  499. self_calibrate();
  500. // for (int i = 0; i < 24; i++) Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  501. for (int i = 0; i < 24; i++)
  502. EEPROM.write(1 + i, GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
  503. EEPROM.write(0, 0x7c); // is written!
  504. } else {
  505. for (int i = 0; i < 24; i++)
  506. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, EEPROM.read(1 + i));
  507. }
  508. #endif
  509. #ifdef __DUE__
  510. // The Due has no persistent storage. So instead use a "canned"
  511. // calibration.
  512. // self_calibrate();
  513. // for (int i = 0; i < 24; i++)
  514. // Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  515. static const byte canned_calibration[24] = {
  516. 0xCC, 0x7C, 0xFF, 0xFF, 0x57, 0xFE, 0xFF, 0xFF,
  517. 0xA1, 0x04, 0xF9, 0x01, 0x93, 0x00, 0x00, 0x00,
  518. 0x5E, 0x4B, 0x00, 0x00, 0x08, 0x8B, 0xF1, 0xFF };
  519. for (int i = 0; i < 24; i++)
  520. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, canned_calibration[i]);
  521. #endif
  522. #if defined(RASPBERRY_PI)
  523. {
  524. uint8_t cal[24];
  525. FILE *calfile = fopen(".calibration", "r");
  526. if (calfile == NULL) {
  527. calfile = fopen(".calibration", "w");
  528. if (calfile != NULL) {
  529. self_calibrate();
  530. for (int i = 0; i < 24; i++)
  531. cal[i] = GDTR.rd(REG_TOUCH_TRANSFORM_A + i);
  532. fwrite(cal, 1, sizeof(cal), calfile);
  533. fclose(calfile);
  534. }
  535. } else {
  536. fread(cal, 1, sizeof(cal), calfile);
  537. for (int i = 0; i < 24; i++)
  538. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, cal[i]);
  539. fclose(calfile);
  540. }
  541. }
  542. #endif
  543. }
  544. GDTR.wr16(REG_TOUCH_RZTHRESH, 1200);
  545. rseed = 0x77777777;
  546. if ((BOARD == BOARD_GAMEDUINO23) && (options & GD_TRIM)) {
  547. tune();
  548. }
  549. }
  550. void GDClass::storage(void) {
  551. GDTR.__end();
  552. SD.begin(SD_PIN);
  553. GDTR.resume();
  554. }
  555. void GDClass::self_calibrate(void) {
  556. cmd_dlstart();
  557. Clear();
  558. cmd_text(w / 2, h / 2, 30, OPT_CENTER, "please tap on the dot");
  559. cmd_calibrate();
  560. finish();
  561. cmd_loadidentity();
  562. cmd_dlstart();
  563. GDTR.flush();
  564. }
  565. void GDClass::seed(uint16_t n) {
  566. rseed = n ? n : 7;
  567. }
  568. uint16_t GDClass::random() {
  569. rseed ^= rseed << 2;
  570. rseed ^= rseed >> 5;
  571. rseed ^= rseed << 1;
  572. return rseed;
  573. }
  574. uint16_t GDClass::random(uint16_t n) {
  575. uint16_t p = random();
  576. if (n == (n & -n))
  577. return p & (n - 1);
  578. return (uint32_t(p) * n) >> 16;
  579. }
  580. uint16_t GDClass::random(uint16_t n0, uint16_t n1) {
  581. return n0 + random(n1 - n0);
  582. }
  583. // >>> [int(65535*math.sin(math.pi * 2 * i / 1024)) for i in range(257)]
  584. static const PROGMEM uint16_t sintab[257] = {
  585. 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
  586. };
  587. int16_t GDClass::rsin(int16_t r, uint16_t th) {
  588. th >>= 6; // angle 0-1023
  589. // return int(r * sin((2 * M_PI) * th / 1024.));
  590. int th4 = th & 511;
  591. if (th4 & 256)
  592. th4 = 512 - th4; // 256->256 257->255, etc
  593. uint16_t s = pgm_read_word_near(sintab + th4);
  594. int16_t p = ((uint32_t)s * r) >> 16;
  595. if (th & 512)
  596. p = -p;
  597. return p;
  598. }
  599. int16_t GDClass::rcos(int16_t r, uint16_t th) {
  600. return rsin(r, th + 0x4000);
  601. }
  602. void GDClass::polar(int &x, int &y, int16_t r, uint16_t th) {
  603. x = (int)(-GD.rsin(r, th));
  604. y = (int)( GD.rcos(r, th));
  605. }
  606. // >>> [int(round(1024 * math.atan(i / 256.) / math.pi)) for i in range(256)]
  607. static const PROGMEM uint8_t atan8[] = {
  608. 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
  609. };
  610. uint16_t GDClass::atan2(int16_t y, int16_t x)
  611. {
  612. uint16_t a;
  613. uint16_t xx = 0;
  614. /* These values are tricky. So pretend they are not */
  615. if (x == -32768)
  616. x++;
  617. if (y == -32768)
  618. y++;
  619. if ((x <= 0) ^ (y > 0)) {
  620. int16_t t; t = x; x = y; y = t;
  621. xx ^= 0x4000;
  622. }
  623. if (x <= 0) {
  624. x = -x;
  625. } else {
  626. xx ^= 0x8000;
  627. }
  628. y = abs(y);
  629. if (x > y) {
  630. int16_t t; t = x; x = y; y = t;
  631. xx ^= 0x3fff;
  632. }
  633. while ((x | y) & 0xff80) {
  634. x >>= 1;
  635. y >>= 1;
  636. }
  637. if (y == 0) {
  638. a = 0;
  639. } else if (x == y) {
  640. a = 0x2000;
  641. } else {
  642. // assert(x <= y);
  643. int r = ((x << 8) / y);
  644. // assert(0 <= r);
  645. // assert(r < 256);
  646. a = pgm_read_byte(atan8 + r) << 5;
  647. }
  648. a ^= xx;
  649. return a;
  650. }
  651. void GDClass::align(byte n) {
  652. while ((n++) & 3)
  653. GDTR.cmdbyte(0);
  654. }
  655. void GDClass::cH(uint16_t v) {
  656. GDTR.cmdbyte(v & 0xff);
  657. GDTR.cmdbyte((v >> 8) & 0xff);
  658. }
  659. void GDClass::ch(int16_t v) {
  660. cH((uint16_t)v);
  661. }
  662. void GDClass::cI(uint32_t v) {
  663. GDTR.cmd32(v);
  664. }
  665. void GDClass::cFFFFFF(byte v) {
  666. union {
  667. uint32_t c;
  668. uint8_t b[4];
  669. };
  670. b[0] = v;
  671. b[1] = 0xff;
  672. b[2] = 0xff;
  673. b[3] = 0xff;
  674. GDTR.cmd32(c);
  675. }
  676. void GDClass::ci(int32_t v) {
  677. cI((uint32_t) v);
  678. }
  679. void GDClass::cs(const char *s) {
  680. int count = 0;
  681. while (*s) {
  682. char c = *s++;
  683. GDTR.cmdbyte(c);
  684. count++;
  685. }
  686. GDTR.cmdbyte(0);
  687. align(count + 1);
  688. }
  689. #if !defined(ESP8266)
  690. void GDClass::copy(const PROGMEM uint8_t *src, int count) {
  691. #else
  692. void GDClass::copy(const uint8_t *src, int count) {
  693. #endif
  694. byte a = count & 3;
  695. while (count--) {
  696. GDTR.cmdbyte(pgm_read_byte_near(src));
  697. src++;
  698. }
  699. align(a);
  700. }
  701. void GDClass::copyram(byte *src, int count) {
  702. byte a = count & 3;
  703. GDTR.cmd_n(src, count);
  704. align(a);
  705. }
  706. void GDClass::AlphaFunc(byte func, byte ref) {
  707. cI((9UL << 24) | ((func & 7L) << 8) | ((ref & 255L) << 0));
  708. }
  709. void GDClass::Begin(byte prim) {
  710. cI((31UL << 24) | prim);
  711. }
  712. void GDClass::BitmapHandle(byte handle) {
  713. cI((5UL << 24) | handle);
  714. }
  715. void GDClass::BitmapLayout(byte format, uint16_t linestride, uint16_t height) {
  716. // cI((7UL << 24) | ((format & 31L) << 19) | ((linestride & 1023L) << 9) | ((height & 511L) << 0));
  717. union {
  718. uint32_t c;
  719. uint8_t b[4];
  720. };
  721. b[0] = height;
  722. b[1] = (1 & (height >> 8)) | (linestride << 1);
  723. b[2] = (7 & (linestride >> 7)) | (format << 3);
  724. b[3] = 7;
  725. cI(c);
  726. }
  727. void GDClass::BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height) {
  728. byte fxy = (filter << 2) | (wrapx << 1) | (wrapy);
  729. // cI((8UL << 24) | ((uint32_t)fxy << 18) | ((width & 511L) << 9) | ((height & 511L) << 0));
  730. union {
  731. uint32_t c;
  732. uint8_t b[4];
  733. };
  734. b[0] = height;
  735. b[1] = (1 & (height >> 8)) | (width << 1);
  736. b[2] = (3 & (width >> 7)) | (fxy << 2);
  737. b[3] = 8;
  738. cI(c);
  739. if (ft8xx_model) {
  740. b[0] = ((width >> 9) << 2) | (3 & (height >> 9));
  741. b[3] = 0x29;
  742. cI(c);
  743. }
  744. }
  745. void GDClass::BitmapSource(uint32_t addr) {
  746. cI((1UL << 24) | ((addr & 1048575L) << 0));
  747. }
  748. void GDClass::BitmapTransformA(int32_t a) {
  749. cI((21UL << 24) | ((a & 131071L) << 0));
  750. }
  751. void GDClass::BitmapTransformB(int32_t b) {
  752. cI((22UL << 24) | ((b & 131071L) << 0));
  753. }
  754. void GDClass::BitmapTransformC(int32_t c) {
  755. cI((23UL << 24) | ((c & 16777215L) << 0));
  756. }
  757. void GDClass::BitmapTransformD(int32_t d) {
  758. cI((24UL << 24) | ((d & 131071L) << 0));
  759. }
  760. void GDClass::BitmapTransformE(int32_t e) {
  761. cI((25UL << 24) | ((e & 131071L) << 0));
  762. }
  763. void GDClass::BitmapTransformF(int32_t f) {
  764. cI((26UL << 24) | ((f & 16777215L) << 0));
  765. }
  766. void GDClass::BlendFunc(byte src, byte dst) {
  767. cI((11UL << 24) | ((src & 7L) << 3) | ((dst & 7L) << 0));
  768. }
  769. void GDClass::Call(uint16_t dest) {
  770. cI((29UL << 24) | ((dest & 2047L) << 0));
  771. }
  772. void GDClass::Cell(byte cell) {
  773. cI((6UL << 24) | ((cell & 127L) << 0));
  774. }
  775. void GDClass::ClearColorA(byte alpha) {
  776. cI((15UL << 24) | ((alpha & 255L) << 0));
  777. }
  778. void GDClass::ClearColorRGB(byte red, byte green, byte blue) {
  779. cI((2UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  780. }
  781. void GDClass::ClearColorRGB(uint32_t rgb) {
  782. cI((2UL << 24) | (rgb & 0xffffffL));
  783. }
  784. void GDClass::Clear(byte c, byte s, byte t) {
  785. byte m = (c << 2) | (s << 1) | t;
  786. cI((38UL << 24) | m);
  787. }
  788. void GDClass::Clear(void) {
  789. cI((38UL << 24) | 7);
  790. }
  791. void GDClass::ClearStencil(byte s) {
  792. cI((17UL << 24) | ((s & 255L) << 0));
  793. }
  794. void GDClass::ClearTag(byte s) {
  795. cI((18UL << 24) | ((s & 255L) << 0));
  796. }
  797. void GDClass::ColorA(byte alpha) {
  798. cI((16UL << 24) | ((alpha & 255L) << 0));
  799. }
  800. void GDClass::ColorMask(byte r, byte g, byte b, byte a) {
  801. cI((32UL << 24) | ((r & 1L) << 3) | ((g & 1L) << 2) | ((b & 1L) << 1) | ((a & 1L) << 0));
  802. }
  803. void GDClass::ColorRGB(byte red, byte green, byte blue) {
  804. // cI((4UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  805. union {
  806. uint32_t c;
  807. uint8_t b[4];
  808. };
  809. b[0] = blue;
  810. b[1] = green;
  811. b[2] = red;
  812. b[3] = 4;
  813. cI(c);
  814. }
  815. void GDClass::ColorRGB(uint32_t rgb) {
  816. cI((4UL << 24) | (rgb & 0xffffffL));
  817. }
  818. void GDClass::Display(void) {
  819. cI((0UL << 24));
  820. }
  821. void GDClass::End(void) {
  822. cI((33UL << 24));
  823. }
  824. void GDClass::Jump(uint16_t dest) {
  825. cI((30UL << 24) | ((dest & 2047L) << 0));
  826. }
  827. void GDClass::LineWidth(uint16_t width) {
  828. cI((14UL << 24) | ((width & 4095L) << 0));
  829. }
  830. void GDClass::Macro(byte m) {
  831. cI((37UL << 24) | ((m & 1L) << 0));
  832. }
  833. void GDClass::PointSize(uint16_t size) {
  834. cI((13UL << 24) | ((size & 8191L) << 0));
  835. }
  836. void GDClass::RestoreContext(void) {
  837. cI((35UL << 24));
  838. }
  839. void GDClass::Return(void) {
  840. cI((36UL << 24));
  841. }
  842. void GDClass::SaveContext(void) {
  843. cI((34UL << 24));
  844. }
  845. void GDClass::ScissorSize(uint16_t width, uint16_t height) {
  846. if (ft8xx_model == 0)
  847. cI((28UL << 24) | ((width & 1023L) << 10) | ((height & 1023L) << 0));
  848. else
  849. cI((28UL << 24) | ((width & 4095L) << 12) | ((height & 4095L) << 0));
  850. }
  851. void GDClass::ScissorXY(uint16_t x, uint16_t y) {
  852. if (ft8xx_model == 0)
  853. cI((27UL << 24) | ((x & 511L) << 9) | ((y & 511L) << 0));
  854. else
  855. cI((27UL << 24) | ((x & 2047L) << 11) | ((y & 2047L) << 0));
  856. }
  857. void GDClass::StencilFunc(byte func, byte ref, byte mask) {
  858. cI((10UL << 24) | ((func & 7L) << 16) | ((ref & 255L) << 8) | ((mask & 255L) << 0));
  859. }
  860. void GDClass::StencilMask(byte mask) {
  861. cI((19UL << 24) | ((mask & 255L) << 0));
  862. }
  863. void GDClass::StencilOp(byte sfail, byte spass) {
  864. cI((12UL << 24) | ((sfail & 7L) << 3) | ((spass & 7L) << 0));
  865. }
  866. void GDClass::TagMask(byte mask) {
  867. cI((20UL << 24) | ((mask & 1L) << 0));
  868. }
  869. void GDClass::Tag(byte s) {
  870. cI((3UL << 24) | ((s & 255L) << 0));
  871. }
  872. void GDClass::Vertex2f(int16_t x, int16_t y) {
  873. // x = int(16 * x);
  874. // y = int(16 * y);
  875. cI((1UL << 30) | ((x & 32767L) << 15) | ((y & 32767L) << 0));
  876. }
  877. void GDClass::Vertex2ii(uint16_t x, uint16_t y, byte handle, byte cell) {
  878. // cI((2UL << 30) | ((x & 511L) << 21) | ((y & 511L) << 12) | ((handle & 31L) << 7) | ((cell & 127L) << 0));
  879. union {
  880. uint32_t c;
  881. uint8_t b[4];
  882. };
  883. b[0] = (cell & 127) | ((handle & 1) << 7);
  884. b[1] = (handle >> 1) | (y << 4);
  885. b[2] = (y >> 4) | (x << 5);
  886. b[3] = (2 << 6) | (x >> 3);
  887. cI(c);
  888. }
  889. void GDClass::VertexFormat(byte frac) {
  890. cI((39UL << 24) | (((frac) & 7) << 0));
  891. }
  892. void GDClass::BitmapLayoutH(byte linestride, byte height) {
  893. cI((40UL << 24) | (((linestride) & 3) << 2) | (((height) & 3) << 0));
  894. }
  895. void GDClass::BitmapSizeH(byte width, byte height) {
  896. cI((41UL << 24) | (((width) & 3) << 2) | (((height) & 3) << 0));
  897. }
  898. void GDClass::PaletteSource(uint32_t addr) {
  899. cI((42UL << 24) | (((addr) & 4194303UL) << 0));
  900. }
  901. void GDClass::VertexTranslateX(uint32_t x) {
  902. cI((43UL << 24) | (((x) & 131071UL) << 0));
  903. }
  904. void GDClass::VertexTranslateY(uint32_t y) {
  905. cI((44UL << 24) | (((y) & 131071UL) << 0));
  906. }
  907. void GDClass::Nop(void) {
  908. cI((45UL << 24));
  909. }
  910. void GDClass::cmd_append(uint32_t ptr, uint32_t num) {
  911. cFFFFFF(0x1e);
  912. cI(ptr);
  913. cI(num);
  914. }
  915. void GDClass::cmd_bgcolor(uint32_t c) {
  916. cFFFFFF(0x09);
  917. cI(c);
  918. }
  919. void GDClass::cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s) {
  920. cFFFFFF(0x0d);
  921. ch(x);
  922. ch(y);
  923. ch(w);
  924. ch(h);
  925. ch(font);
  926. cH(options);
  927. cs(s);
  928. }
  929. void GDClass::cmd_calibrate(void) {
  930. cFFFFFF(0x15);
  931. cFFFFFF(0xff);
  932. }
  933. 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) {
  934. cFFFFFF(0x14);
  935. ch(x);
  936. ch(y);
  937. ch(r);
  938. cH(options);
  939. cH(h);
  940. cH(m);
  941. cH(s);
  942. cH(ms);
  943. }
  944. void GDClass::cmd_coldstart(void) {
  945. cFFFFFF(0x32);
  946. }
  947. void GDClass::cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val) {
  948. cFFFFFF(0x2d);
  949. ch(x);
  950. ch(y);
  951. ch(r);
  952. cH(options);
  953. cH(val);
  954. cH(0);
  955. }
  956. void GDClass::cmd_dlstart(void) {
  957. cFFFFFF(0x00);
  958. }
  959. void GDClass::cmd_fgcolor(uint32_t c) {
  960. cFFFFFF(0x0a);
  961. cI(c);
  962. }
  963. 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) {
  964. cFFFFFF(0x13);
  965. ch(x);
  966. ch(y);
  967. ch(r);
  968. cH(options);
  969. cH(major);
  970. cH(minor);
  971. cH(val);
  972. cH(range);
  973. }
  974. void GDClass::cmd_getmatrix(void) {
  975. cFFFFFF(0x33);
  976. ci(0);
  977. ci(0);
  978. ci(0);
  979. ci(0);
  980. ci(0);
  981. ci(0);
  982. }
  983. void GDClass::cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h) {
  984. cFFFFFF(0x25);
  985. ptr = GDTR.getwp();
  986. cI(0);
  987. w = GDTR.getwp();
  988. cI(0);
  989. h = GDTR.getwp();
  990. cI(0);
  991. }
  992. void GDClass::cmd_getptr(void) {
  993. cFFFFFF(0x23);
  994. cI(0);
  995. }
  996. void GDClass::cmd_gradcolor(uint32_t c) {
  997. cFFFFFF(0x34);
  998. cI(c);
  999. }
  1000. void GDClass::cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1) {
  1001. cFFFFFF(0x0b);
  1002. ch(x0);
  1003. ch(y0);
  1004. cI(rgb0);
  1005. ch(x1);
  1006. ch(y1);
  1007. cI(rgb1);
  1008. }
  1009. void GDClass::cmd_inflate(uint32_t ptr) {
  1010. cFFFFFF(0x22);
  1011. cI(ptr);
  1012. }
  1013. void GDClass::cmd_interrupt(uint32_t ms) {
  1014. cFFFFFF(0x02);
  1015. cI(ms);
  1016. }
  1017. void GDClass::cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s) {
  1018. cFFFFFF(0x0e);
  1019. ch(x);
  1020. ch(y);
  1021. ch(w);
  1022. ch(h);
  1023. ch(font);
  1024. cH(options);
  1025. cs(s);
  1026. }
  1027. void GDClass::cmd_loadidentity(void) {
  1028. cFFFFFF(0x26);
  1029. }
  1030. void GDClass::cmd_loadimage(uint32_t ptr, int32_t options) {
  1031. cFFFFFF(0x24);
  1032. cI(ptr);
  1033. cI(options);
  1034. }
  1035. void GDClass::cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num) {
  1036. cFFFFFF(0x1d);
  1037. cI(dest);
  1038. cI(src);
  1039. cI(num);
  1040. }
  1041. void GDClass::cmd_memset(uint32_t ptr, byte value, uint32_t num) {
  1042. cFFFFFF(0x1b);
  1043. cI(ptr);
  1044. cI((uint32_t)value);
  1045. cI(num);
  1046. }
  1047. uint32_t GDClass::cmd_memcrc(uint32_t ptr, uint32_t num) {
  1048. cFFFFFF(0x18);
  1049. cI(ptr);
  1050. cI(num);
  1051. uint32_t r = GDTR.getwp();
  1052. cI(0xFFFFFFFF);
  1053. return r;
  1054. }
  1055. void GDClass::cmd_memwrite(uint32_t ptr, uint32_t num) {
  1056. cFFFFFF(0x1a);
  1057. cI(ptr);
  1058. cI(num);
  1059. }
  1060. void GDClass::cmd_regwrite(uint32_t ptr, uint32_t val) {
  1061. cFFFFFF(0x1a);
  1062. cI(ptr);
  1063. cI(4UL);
  1064. cI(val);
  1065. }
  1066. void GDClass::cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n) {
  1067. cFFFFFF(0x2e);
  1068. ch(x);
  1069. ch(y);
  1070. ch(font);
  1071. cH(options);
  1072. ci(n);
  1073. }
  1074. 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) {
  1075. cFFFFFF(0x0f);
  1076. ch(x);
  1077. ch(y);
  1078. ch(w);
  1079. ch(h);
  1080. cH(options);
  1081. cH(val);
  1082. cH(range);
  1083. cH(0);
  1084. }
  1085. void GDClass::cmd_regread(uint32_t ptr) {
  1086. cFFFFFF(0x19);
  1087. cI(ptr);
  1088. cI(0);
  1089. }
  1090. void GDClass::cmd_rotate(int32_t a) {
  1091. cFFFFFF(0x29);
  1092. ci(a);
  1093. }
  1094. void GDClass::cmd_scale(int32_t sx, int32_t sy) {
  1095. cFFFFFF(0x28);
  1096. ci(sx);
  1097. ci(sy);
  1098. }
  1099. void GDClass::cmd_screensaver(void) {
  1100. cFFFFFF(0x2f);
  1101. }
  1102. 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) {
  1103. cFFFFFF(0x11);
  1104. ch(x);
  1105. ch(y);
  1106. ch(w);
  1107. ch(h);
  1108. cH(options);
  1109. cH(val);
  1110. cH(size);
  1111. cH(range);
  1112. }
  1113. void GDClass::cmd_setfont(byte font, uint32_t ptr) {
  1114. cFFFFFF(0x2b);
  1115. cI(font);
  1116. cI(ptr);
  1117. }
  1118. void GDClass::cmd_setmatrix(void) {
  1119. cFFFFFF(0x2a);
  1120. }
  1121. void GDClass::cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format) {
  1122. cFFFFFF(0x30);
  1123. ch(x);
  1124. ch(y);
  1125. cH(w);
  1126. cH(h);
  1127. cI(ptr);
  1128. cI(format);
  1129. }
  1130. 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) {
  1131. cFFFFFF(0x10);
  1132. ch(x);
  1133. ch(y);
  1134. ch(w);
  1135. ch(h);
  1136. cH(options);
  1137. cH(val);
  1138. cH(range);
  1139. cH(0);
  1140. }
  1141. void GDClass::cmd_snapshot(uint32_t ptr) {
  1142. cFFFFFF(0x1f);
  1143. cI(ptr);
  1144. }
  1145. void GDClass::cmd_spinner(int16_t x, int16_t y, byte style, byte scale) {
  1146. cFFFFFF(0x16);
  1147. ch(x);
  1148. ch(y);
  1149. cH(style);
  1150. cH(scale);
  1151. }
  1152. void GDClass::cmd_stop(void) {
  1153. cFFFFFF(0x17);
  1154. }
  1155. void GDClass::cmd_swap(void) {
  1156. cFFFFFF(0x01);
  1157. }
  1158. void GDClass::cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s) {
  1159. cFFFFFF(0x0c);
  1160. ch(x);
  1161. ch(y);
  1162. ch(font);
  1163. cH(options);
  1164. cs(s);
  1165. }
  1166. void GDClass::cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s) {
  1167. cFFFFFF(0x12);
  1168. ch(x);
  1169. ch(y);
  1170. ch(w);
  1171. ch(font);
  1172. cH(options);
  1173. cH(state);
  1174. cs(s);
  1175. }
  1176. void GDClass::cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag) {
  1177. cFFFFFF(0x2c);
  1178. ch(x);
  1179. ch(y);
  1180. ch(w);
  1181. ch(h);
  1182. ch(tag);
  1183. ch(0);
  1184. }
  1185. void GDClass::cmd_translate(int32_t tx, int32_t ty) {
  1186. cFFFFFF(0x27);
  1187. ci(tx);
  1188. ci(ty);
  1189. }
  1190. void GDClass::cmd_playvideo(int32_t options) {
  1191. cFFFFFF(0x3a);
  1192. cI(options);
  1193. }
  1194. void GDClass::cmd_romfont(uint32_t font, uint32_t romslot) {
  1195. cFFFFFF(0x3f);
  1196. cI(font);
  1197. cI(romslot);
  1198. }
  1199. void GDClass::cmd_mediafifo(uint32_t ptr, uint32_t size) {
  1200. cFFFFFF(0x39);
  1201. cI(ptr);
  1202. cI(size);
  1203. }
  1204. void GDClass::cmd_setbase(uint32_t b) {
  1205. cFFFFFF(0x38);
  1206. cI(b);
  1207. }
  1208. void GDClass::cmd_videoframe(uint32_t dst, uint32_t ptr) {
  1209. cFFFFFF(0x41);
  1210. cI(dst);
  1211. cI(ptr);
  1212. }
  1213. void GDClass::cmd_snapshot2(uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, int16_t w, int16_t h) {
  1214. cFFFFFF(0x37);
  1215. cI(fmt);
  1216. cI(ptr);
  1217. ch(x);
  1218. ch(y);
  1219. ch(w);
  1220. ch(h);
  1221. }
  1222. void GDClass::cmd_setfont2(uint32_t font, uint32_t ptr, uint32_t firstchar) {
  1223. cFFFFFF(0x3b);
  1224. cI(font);
  1225. cI(ptr);
  1226. cI(firstchar);
  1227. }
  1228. void GDClass::cmd_setbitmap(uint32_t source, uint16_t fmt, uint16_t w, uint16_t h) {
  1229. cFFFFFF(0x43);
  1230. cI(source);
  1231. ch(fmt);
  1232. ch(w);
  1233. ch(h);
  1234. ch(0);
  1235. }
  1236. void GDClass::cmd_setrotate(uint32_t r) {
  1237. cFFFFFF(0x36);
  1238. cI(r);
  1239. // As a special favor, update variables w and h according to this
  1240. // rotation
  1241. w = GDTR.rd16(REG_HSIZE);
  1242. h = GDTR.rd16(REG_VSIZE);
  1243. if (r & 2) {
  1244. int t = h;
  1245. h = w;
  1246. w = t;
  1247. }
  1248. }
  1249. void GDClass::cmd_videostart() {
  1250. cFFFFFF(0x40);
  1251. }
  1252. void GDClass::cmd_sync() {
  1253. cFFFFFF(0x42);
  1254. }
  1255. byte GDClass::rd(uint32_t addr) {
  1256. return GDTR.rd(addr);
  1257. }
  1258. void GDClass::wr(uint32_t addr, uint8_t v) {
  1259. GDTR.wr(addr, v);
  1260. }
  1261. uint16_t GDClass::rd16(uint32_t addr) {
  1262. return GDTR.rd16(addr);
  1263. }
  1264. void GDClass::wr16(uint32_t addr, uint16_t v) {
  1265. GDTR.wr16(addr, v);
  1266. }
  1267. uint32_t GDClass::rd32(uint32_t addr) {
  1268. return GDTR.rd32(addr);
  1269. }
  1270. void GDClass::wr32(uint32_t addr, uint32_t v) {
  1271. GDTR.wr32(addr, v);
  1272. }
  1273. void GDClass::wr_n(uint32_t addr, byte *src, uint32_t n) {
  1274. GDTR.wr_n(addr, src, n);
  1275. }
  1276. void GDClass::cmdbyte(uint8_t b) {
  1277. GDTR.cmdbyte(b);
  1278. }
  1279. void GDClass::cmd32(uint32_t b) {
  1280. GDTR.cmd32(b);
  1281. }
  1282. void GDClass::finish(void) {
  1283. GDTR.finish();
  1284. }
  1285. void GDClass::get_accel(int &x, int &y, int &z) {
  1286. static int f[3];
  1287. for (byte i = 0; i < 3; i++) {
  1288. int a = analogRead(A0 + i);
  1289. int s = (-160 * (a - 376)) >> 6;
  1290. f[i] = ((3 * f[i]) >> 2) + (s >> 2);
  1291. }
  1292. x = f[2];
  1293. y = f[1];
  1294. z = f[0];
  1295. }
  1296. void GDClass::get_inputs(void) {
  1297. GDTR.finish();
  1298. byte *bi = (byte*)&inputs;
  1299. #if defined(DUMPDEV)
  1300. extern FILE* stimfile;
  1301. if (stimfile) {
  1302. byte tag;
  1303. fscanf(stimfile, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
  1304. &bi[0],
  1305. &bi[1],
  1306. &bi[2],
  1307. &bi[3],
  1308. &bi[4],
  1309. &bi[5],
  1310. &bi[6],
  1311. &bi[7],
  1312. &bi[8],
  1313. &bi[9],
  1314. &bi[10],
  1315. &bi[11],
  1316. &bi[12],
  1317. &bi[13],
  1318. &bi[14],
  1319. &bi[15],
  1320. &bi[16],
  1321. &bi[17]);
  1322. GDTR.wr(REG_TAG, tag);
  1323. } else {
  1324. inputs.x = inputs.y = -32768;
  1325. }
  1326. #else
  1327. GDTR.rd_n(bi, REG_TRACKER, 4);
  1328. GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
  1329. GDTR.rd_n(bi + 17, REG_TAG, 1);
  1330. inputs.touching = (inputs.x != -32768);
  1331. inputs.xytouch.set(PIXELS(inputs.x), PIXELS(inputs.y));
  1332. #ifdef DUMP_INPUTS
  1333. for (size_t i = 0; i < sizeof(inputs); i++) {
  1334. Serial.print(bi[i], HEX);
  1335. Serial.print(" ");
  1336. }
  1337. Serial.println();
  1338. #endif
  1339. #endif
  1340. }
  1341. void GDClass::bulkrd(uint32_t a) {
  1342. GDTR.bulk(a);
  1343. }
  1344. void GDClass::resume(void) {
  1345. GDTR.resume();
  1346. }
  1347. void GDClass::__end(void) {
  1348. #if !defined(DUMPDEV) && !defined(RASPBERRY_PI)
  1349. GDTR.__end();
  1350. #endif
  1351. }
  1352. void GDClass::play(uint8_t instrument, uint8_t note) {
  1353. wr16(REG_SOUND, (note << 8) | instrument);
  1354. wr(REG_PLAY, 1);
  1355. }
  1356. void GDClass::sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop) {
  1357. GD.wr32(REG_PLAYBACK_START, start);
  1358. GD.wr32(REG_PLAYBACK_LENGTH, len);
  1359. GD.wr16(REG_PLAYBACK_FREQ, freq);
  1360. GD.wr(REG_PLAYBACK_FORMAT, format);
  1361. GD.wr(REG_PLAYBACK_LOOP, loop);
  1362. GD.wr(REG_PLAYBACK_PLAY, 1);
  1363. }
  1364. void GDClass::reset() {
  1365. GDTR.__end();
  1366. GDTR.wr(REG_CPURESET, 1);
  1367. GDTR.wr(REG_CPURESET, 0);
  1368. GDTR.resume();
  1369. }
  1370. // Load named file from storage
  1371. // returns 0 on failure (e.g. file not found), 1 on success
  1372. byte GDClass::load(const char *filename, void (*progress)(long, long))
  1373. {
  1374. #if defined(RASPBERRY_PI) || defined(DUMPDEV) || defined(SPIDRIVER)
  1375. char full_name[2048] = "sdcard/";
  1376. strcat(full_name, filename);
  1377. FILE *f = fopen(full_name, "rb");
  1378. if (!f) {
  1379. perror(full_name);
  1380. exit(1);
  1381. }
  1382. byte buf[512];
  1383. int n;
  1384. while ((n = fread(buf, 1, 512, f)) > 0) {
  1385. GDTR.cmd_n(buf, (n + 3) & ~3);
  1386. }
  1387. fclose(f);
  1388. return 1;
  1389. #else
  1390. GD.__end();
  1391. Reader r;
  1392. if (r.openfile(filename)) {
  1393. byte buf[512];
  1394. while (r.offset < r.size) {
  1395. uint16_t n = min(512U, r.size - r.offset);
  1396. n = (n + 3) & ~3; // force 32-bit alignment
  1397. r.readsector(buf);
  1398. GD.resume();
  1399. if (progress)
  1400. (*progress)(r.offset, r.size);
  1401. GD.copyram(buf, n);
  1402. GDTR.stop();
  1403. }
  1404. GD.resume();
  1405. return 1;
  1406. }
  1407. GD.resume();
  1408. return 0;
  1409. #endif
  1410. }
  1411. // Generated by mk_bsod.py. Blue screen with 'ERROR' text
  1412. static const PROGMEM uint8_t __bsod[32] = {
  1413. 0, 255, 255, 255, 96, 0, 0, 2, 7, 0, 0, 38, 12, 255, 255, 255, 240, 0,
  1414. 90, 0, 31, 0, 0, 6, 69, 82, 82, 79, 82, 0, 0, 0
  1415. };
  1416. static const PROGMEM uint8_t __bsod_badfile[32] = {
  1417. 12, 255, 255, 255, 240, 0, 148, 0, 29, 0, 0, 6, 67, 97, 110, 110, 111,
  1418. 116, 32, 111, 112, 101, 110, 32, 102, 105, 108, 101, 58, 0, 0, 0
  1419. };
  1420. // Fatal error alert.
  1421. // Show a blue screen with message.
  1422. // This method never returns.
  1423. void GDClass::alert(const char *message)
  1424. {
  1425. begin(0);
  1426. copy(__bsod, sizeof(__bsod));
  1427. cmd_text(240, 176, 29, OPT_CENTER, message);
  1428. swap();
  1429. GD.finish();
  1430. for (;;)
  1431. ;
  1432. }
  1433. void GDClass::safeload(const char *filename)
  1434. {
  1435. if (!load(filename)) {
  1436. copy(__bsod, sizeof(__bsod));
  1437. copy(__bsod_badfile, sizeof(__bsod_badfile));
  1438. cmd_text(240, 190, 29, OPT_CENTER, filename);
  1439. swap();
  1440. for (;;)
  1441. ;
  1442. }
  1443. }
  1444. void GDClass::textsize(int &w, int &h, int font, const char *s)
  1445. {
  1446. uint32_t font_addr = rd32(0x309074 + 4 * font);
  1447. w = 0;
  1448. while (*s)
  1449. w += GD.rd(font_addr + *s++);
  1450. h = GD.rd(font_addr + 140);
  1451. }
  1452. #define REG_SCREENSHOT_EN (ft8xx_model ? 0x302010UL : 0x102410UL) // Set to enable screenshot mode
  1453. #define REG_SCREENSHOT_Y (ft8xx_model ? 0x302014UL : 0x102414UL) // Y line register
  1454. #define REG_SCREENSHOT_START (ft8xx_model ? 0x302018UL : 0x102418UL) // Screenshot start trigger
  1455. #define REG_SCREENSHOT_BUSY (ft8xx_model ? 0x3020e8UL : 0x1024d8UL) // Screenshot ready flags
  1456. #define REG_SCREENSHOT_READ (ft8xx_model ? 0x302174UL : 0x102554UL) // Set to enable readout
  1457. #define RAM_SCREENSHOT (ft8xx_model ? 0x3c2000UL : 0x1C2000UL) // Screenshot readout buffer
  1458. #ifndef DUMPDEV
  1459. void GDClass::dumpscreen(void)
  1460. {
  1461. {
  1462. finish();
  1463. int w = GD.rd16(REG_HSIZE), h = GD.rd16(REG_VSIZE);
  1464. wr(REG_SCREENSHOT_EN, 1);
  1465. if (ft8xx_model)
  1466. wr(0x0030201c, 32);
  1467. Serial.write(0xa5);
  1468. Serial.write(w & 0xff);
  1469. Serial.write((w >> 8) & 0xff);
  1470. Serial.write(h & 0xff);
  1471. Serial.write((h >> 8) & 0xff);
  1472. for (int ly = 0; ly < h; ly++) {
  1473. wr16(REG_SCREENSHOT_Y, ly);
  1474. wr(REG_SCREENSHOT_START, 1);
  1475. delay(2);
  1476. while (rd32(REG_SCREENSHOT_BUSY) | rd32(REG_SCREENSHOT_BUSY + 4))
  1477. ;
  1478. wr(REG_SCREENSHOT_READ, 1);
  1479. bulkrd(RAM_SCREENSHOT);
  1480. SPI.transfer(0xff);
  1481. for (int x = 0; x < w; x += 8) {
  1482. union {
  1483. uint32_t v;
  1484. struct {
  1485. uint8_t b, g, r, a;
  1486. };
  1487. } block[8];
  1488. for (int i = 0; i < 8; i++) {
  1489. block[i].b = SPI.transfer(0xff);
  1490. block[i].g = SPI.transfer(0xff);
  1491. block[i].r = SPI.transfer(0xff);
  1492. block[i].a = SPI.transfer(0xff);
  1493. }
  1494. // if (x == 0) block[0].r = 0xff;
  1495. byte difference = 1;
  1496. for (int i = 1, mask = 2; i < 8; i++, mask <<= 1)
  1497. if (block[i].v != block[i-1].v)
  1498. difference |= mask;
  1499. Serial.write(difference);
  1500. for (int i = 0; i < 8; i++)
  1501. if (1 & (difference >> i)) {
  1502. Serial.write(block[i].b);
  1503. Serial.write(block[i].g);
  1504. Serial.write(block[i].r);
  1505. }
  1506. }
  1507. resume();
  1508. wr(REG_SCREENSHOT_READ, 0);
  1509. }
  1510. wr16(REG_SCREENSHOT_EN, 0);
  1511. }
  1512. }
  1513. #endif