GD2.cpp 38 KB

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