GD2.cpp 37 KB

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