GD2.cpp 38 KB

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