GD2.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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. byte external_crystal = 0;
  446. begin1:
  447. GDTR.begin1();
  448. #if 1
  449. Serial.println("ID REGISTER:");
  450. Serial.println(GDTR.rd(REG_ID), HEX);
  451. #endif
  452. #if (BOARD == BOARD_FTDI_80x)
  453. GDTR.wr(REG_PCLK_POL, 1);
  454. GDTR.wr(REG_PCLK, 5);
  455. #endif
  456. GDTR.wr(REG_PWM_DUTY, 0);
  457. GDTR.wr(REG_GPIO_DIR, 0x83);
  458. GDTR.wr(REG_GPIO, GDTR.rd(REG_GPIO) | 0x80);
  459. #if (BOARD == BOARD_GAMEDUINO23)
  460. ConfigRam cr;
  461. byte v8[128] = {0};
  462. cr.read(v8);
  463. if ((v8[1] == 0xff) && (v8[2] == 0x01)) {
  464. options &= ~(GD_TRIM | GD_CALIBRATE);
  465. if (!external_crystal && (v8[3] & 2)) {
  466. GDTR.external_crystal();
  467. external_crystal = 1;
  468. goto begin1;
  469. }
  470. copyram(v8 + 4, 124);
  471. finish();
  472. } else {
  473. GDTR.wr(REG_PCLK_POL, 1);
  474. GDTR.wr(REG_PCLK, 5);
  475. GDTR.wr(REG_ROTATE, 1);
  476. GDTR.wr(REG_SWIZZLE, 3);
  477. }
  478. #endif
  479. w = GDTR.rd16(REG_HSIZE);
  480. h = GDTR.rd16(REG_VSIZE);
  481. loadptr = 0;
  482. // Work-around issue with bitmap sizes not being reset
  483. for (byte i = 0; i < 32; i++) {
  484. BitmapHandle(i);
  485. cI(0x28000000UL);
  486. cI(0x29000000UL);
  487. }
  488. Clear(); swap();
  489. Clear(); swap();
  490. Clear(); swap();
  491. cmd_regwrite(REG_PWM_DUTY, 128);
  492. flush();
  493. if (CALIBRATION & (options & GD_CALIBRATE)) {
  494. #if defined(ARDUINO) && !defined(__DUE__)
  495. if ((EEPROM.read(0) != 0x7c)) {
  496. self_calibrate();
  497. // for (int i = 0; i < 24; i++) Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  498. for (int i = 0; i < 24; i++)
  499. EEPROM.write(1 + i, GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
  500. EEPROM.write(0, 0x7c); // is written!
  501. } else {
  502. for (int i = 0; i < 24; i++)
  503. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, EEPROM.read(1 + i));
  504. }
  505. #endif
  506. #ifdef __DUE__
  507. // The Due has no persistent storage. So instead use a "canned"
  508. // calibration.
  509. // self_calibrate();
  510. // for (int i = 0; i < 24; i++)
  511. // Serial.println(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
  512. static const byte canned_calibration[24] = {
  513. 0xCC, 0x7C, 0xFF, 0xFF, 0x57, 0xFE, 0xFF, 0xFF,
  514. 0xA1, 0x04, 0xF9, 0x01, 0x93, 0x00, 0x00, 0x00,
  515. 0x5E, 0x4B, 0x00, 0x00, 0x08, 0x8B, 0xF1, 0xFF };
  516. for (int i = 0; i < 24; i++)
  517. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, canned_calibration[i]);
  518. #endif
  519. #if defined(RASPBERRY_PI)
  520. {
  521. uint8_t cal[24];
  522. FILE *calfile = fopen(".calibration", "r");
  523. if (calfile == NULL) {
  524. calfile = fopen(".calibration", "w");
  525. if (calfile != NULL) {
  526. self_calibrate();
  527. for (int i = 0; i < 24; i++)
  528. cal[i] = GDTR.rd(REG_TOUCH_TRANSFORM_A + i);
  529. fwrite(cal, 1, sizeof(cal), calfile);
  530. fclose(calfile);
  531. }
  532. } else {
  533. fread(cal, 1, sizeof(cal), calfile);
  534. for (int i = 0; i < 24; i++)
  535. GDTR.wr(REG_TOUCH_TRANSFORM_A + i, cal[i]);
  536. fclose(calfile);
  537. }
  538. }
  539. #endif
  540. }
  541. GDTR.wr16(REG_TOUCH_RZTHRESH, 1200);
  542. rseed = 0x77777777;
  543. if ((BOARD == BOARD_GAMEDUINO23) && (options & GD_TRIM)) {
  544. tune();
  545. }
  546. }
  547. void GDClass::storage(void) {
  548. GDTR.__end();
  549. SD.begin(SD_PIN);
  550. GDTR.resume();
  551. }
  552. void GDClass::self_calibrate(void) {
  553. cmd_dlstart();
  554. Clear();
  555. cmd_text(w / 2, h / 2, 30, OPT_CENTER, "please tap on the dot");
  556. cmd_calibrate();
  557. finish();
  558. cmd_loadidentity();
  559. cmd_dlstart();
  560. GDTR.flush();
  561. }
  562. void GDClass::seed(uint16_t n) {
  563. rseed = n ? n : 7;
  564. }
  565. uint16_t GDClass::random() {
  566. rseed ^= rseed << 2;
  567. rseed ^= rseed >> 5;
  568. rseed ^= rseed << 1;
  569. return rseed;
  570. }
  571. uint16_t GDClass::random(uint16_t n) {
  572. uint16_t p = random();
  573. if (n == (n & -n))
  574. return p & (n - 1);
  575. return (uint32_t(p) * n) >> 16;
  576. }
  577. uint16_t GDClass::random(uint16_t n0, uint16_t n1) {
  578. return n0 + random(n1 - n0);
  579. }
  580. // >>> [int(65535*math.sin(math.pi * 2 * i / 1024)) for i in range(257)]
  581. static const PROGMEM uint16_t sintab[257] = {
  582. 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
  583. };
  584. int16_t GDClass::rsin(int16_t r, uint16_t th) {
  585. th >>= 6; // angle 0-1023
  586. // return int(r * sin((2 * M_PI) * th / 1024.));
  587. int th4 = th & 511;
  588. if (th4 & 256)
  589. th4 = 512 - th4; // 256->256 257->255, etc
  590. uint16_t s = pgm_read_word_near(sintab + th4);
  591. int16_t p = ((uint32_t)s * r) >> 16;
  592. if (th & 512)
  593. p = -p;
  594. return p;
  595. }
  596. int16_t GDClass::rcos(int16_t r, uint16_t th) {
  597. return rsin(r, th + 0x4000);
  598. }
  599. void GDClass::polar(int &x, int &y, int16_t r, uint16_t th) {
  600. x = (int)(-GD.rsin(r, th));
  601. y = (int)( GD.rcos(r, th));
  602. }
  603. // >>> [int(round(1024 * math.atan(i / 256.) / math.pi)) for i in range(256)]
  604. static const PROGMEM uint8_t atan8[] = {
  605. 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
  606. };
  607. uint16_t GDClass::atan2(int16_t y, int16_t x)
  608. {
  609. uint16_t a;
  610. uint16_t xx = 0;
  611. /* These values are tricky. So pretend they are not */
  612. if (x == -32768)
  613. x++;
  614. if (y == -32768)
  615. y++;
  616. if ((x <= 0) ^ (y > 0)) {
  617. int16_t t; t = x; x = y; y = t;
  618. xx ^= 0x4000;
  619. }
  620. if (x <= 0) {
  621. x = -x;
  622. } else {
  623. xx ^= 0x8000;
  624. }
  625. y = abs(y);
  626. if (x > y) {
  627. int16_t t; t = x; x = y; y = t;
  628. xx ^= 0x3fff;
  629. }
  630. while ((x | y) & 0xff80) {
  631. x >>= 1;
  632. y >>= 1;
  633. }
  634. if (y == 0) {
  635. a = 0;
  636. } else if (x == y) {
  637. a = 0x2000;
  638. } else {
  639. // assert(x <= y);
  640. int r = ((x << 8) / y);
  641. // assert(0 <= r);
  642. // assert(r < 256);
  643. a = pgm_read_byte(atan8 + r) << 5;
  644. }
  645. a ^= xx;
  646. return a;
  647. }
  648. void GDClass::align(byte n) {
  649. while ((n++) & 3)
  650. GDTR.cmdbyte(0);
  651. }
  652. void GDClass::cH(uint16_t v) {
  653. GDTR.cmdbyte(v & 0xff);
  654. GDTR.cmdbyte((v >> 8) & 0xff);
  655. }
  656. void GDClass::ch(int16_t v) {
  657. cH((uint16_t)v);
  658. }
  659. void GDClass::cI(uint32_t v) {
  660. GDTR.cmd32(v);
  661. }
  662. void GDClass::cFFFFFF(byte v) {
  663. union {
  664. uint32_t c;
  665. uint8_t b[4];
  666. };
  667. b[0] = v;
  668. b[1] = 0xff;
  669. b[2] = 0xff;
  670. b[3] = 0xff;
  671. GDTR.cmd32(c);
  672. }
  673. void GDClass::ci(int32_t v) {
  674. cI((uint32_t) v);
  675. }
  676. void GDClass::cs(const char *s) {
  677. int count = 0;
  678. while (*s) {
  679. char c = *s++;
  680. GDTR.cmdbyte(c);
  681. count++;
  682. }
  683. GDTR.cmdbyte(0);
  684. align(count + 1);
  685. }
  686. #if !defined(ESP8266)
  687. void GDClass::copy(const PROGMEM uint8_t *src, int count) {
  688. #else
  689. void GDClass::copy(const uint8_t *src, int count) {
  690. #endif
  691. byte a = count & 3;
  692. while (count--) {
  693. GDTR.cmdbyte(pgm_read_byte_near(src));
  694. src++;
  695. }
  696. align(a);
  697. }
  698. void GDClass::copyram(byte *src, int count) {
  699. byte a = count & 3;
  700. GDTR.cmd_n(src, count);
  701. align(a);
  702. }
  703. void GDClass::AlphaFunc(byte func, byte ref) {
  704. cI((9UL << 24) | ((func & 7L) << 8) | ((ref & 255L) << 0));
  705. }
  706. void GDClass::Begin(byte prim) {
  707. cI((31UL << 24) | prim);
  708. }
  709. void GDClass::BitmapHandle(byte handle) {
  710. cI((5UL << 24) | handle);
  711. }
  712. void GDClass::BitmapLayout(byte format, uint16_t linestride, uint16_t height) {
  713. // cI((7UL << 24) | ((format & 31L) << 19) | ((linestride & 1023L) << 9) | ((height & 511L) << 0));
  714. union {
  715. uint32_t c;
  716. uint8_t b[4];
  717. };
  718. b[0] = height;
  719. b[1] = (1 & (height >> 8)) | (linestride << 1);
  720. b[2] = (7 & (linestride >> 7)) | (format << 3);
  721. b[3] = 7;
  722. cI(c);
  723. }
  724. void GDClass::BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height) {
  725. byte fxy = (filter << 2) | (wrapx << 1) | (wrapy);
  726. // cI((8UL << 24) | ((uint32_t)fxy << 18) | ((width & 511L) << 9) | ((height & 511L) << 0));
  727. union {
  728. uint32_t c;
  729. uint8_t b[4];
  730. };
  731. b[0] = height;
  732. b[1] = (1 & (height >> 8)) | (width << 1);
  733. b[2] = (3 & (width >> 7)) | (fxy << 2);
  734. b[3] = 8;
  735. cI(c);
  736. if (ft8xx_model) {
  737. b[0] = ((width >> 9) << 2) | (3 & (height >> 9));
  738. b[3] = 0x29;
  739. cI(c);
  740. }
  741. }
  742. void GDClass::BitmapSource(uint32_t addr) {
  743. cI((1UL << 24) | ((addr & 1048575L) << 0));
  744. }
  745. void GDClass::BitmapTransformA(int32_t a) {
  746. cI((21UL << 24) | ((a & 131071L) << 0));
  747. }
  748. void GDClass::BitmapTransformB(int32_t b) {
  749. cI((22UL << 24) | ((b & 131071L) << 0));
  750. }
  751. void GDClass::BitmapTransformC(int32_t c) {
  752. cI((23UL << 24) | ((c & 16777215L) << 0));
  753. }
  754. void GDClass::BitmapTransformD(int32_t d) {
  755. cI((24UL << 24) | ((d & 131071L) << 0));
  756. }
  757. void GDClass::BitmapTransformE(int32_t e) {
  758. cI((25UL << 24) | ((e & 131071L) << 0));
  759. }
  760. void GDClass::BitmapTransformF(int32_t f) {
  761. cI((26UL << 24) | ((f & 16777215L) << 0));
  762. }
  763. void GDClass::BlendFunc(byte src, byte dst) {
  764. cI((11UL << 24) | ((src & 7L) << 3) | ((dst & 7L) << 0));
  765. }
  766. void GDClass::Call(uint16_t dest) {
  767. cI((29UL << 24) | ((dest & 2047L) << 0));
  768. }
  769. void GDClass::Cell(byte cell) {
  770. cI((6UL << 24) | ((cell & 127L) << 0));
  771. }
  772. void GDClass::ClearColorA(byte alpha) {
  773. cI((15UL << 24) | ((alpha & 255L) << 0));
  774. }
  775. void GDClass::ClearColorRGB(byte red, byte green, byte blue) {
  776. cI((2UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  777. }
  778. void GDClass::ClearColorRGB(uint32_t rgb) {
  779. cI((2UL << 24) | (rgb & 0xffffffL));
  780. }
  781. void GDClass::Clear(byte c, byte s, byte t) {
  782. byte m = (c << 2) | (s << 1) | t;
  783. cI((38UL << 24) | m);
  784. }
  785. void GDClass::Clear(void) {
  786. cI((38UL << 24) | 7);
  787. }
  788. void GDClass::ClearStencil(byte s) {
  789. cI((17UL << 24) | ((s & 255L) << 0));
  790. }
  791. void GDClass::ClearTag(byte s) {
  792. cI((18UL << 24) | ((s & 255L) << 0));
  793. }
  794. void GDClass::ColorA(byte alpha) {
  795. cI((16UL << 24) | ((alpha & 255L) << 0));
  796. }
  797. void GDClass::ColorMask(byte r, byte g, byte b, byte a) {
  798. cI((32UL << 24) | ((r & 1L) << 3) | ((g & 1L) << 2) | ((b & 1L) << 1) | ((a & 1L) << 0));
  799. }
  800. void GDClass::ColorRGB(byte red, byte green, byte blue) {
  801. // cI((4UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
  802. union {
  803. uint32_t c;
  804. uint8_t b[4];
  805. };
  806. b[0] = blue;
  807. b[1] = green;
  808. b[2] = red;
  809. b[3] = 4;
  810. cI(c);
  811. }
  812. void GDClass::ColorRGB(uint32_t rgb) {
  813. cI((4UL << 24) | (rgb & 0xffffffL));
  814. }
  815. void GDClass::Display(void) {
  816. cI((0UL << 24));
  817. }
  818. void GDClass::End(void) {
  819. cI((33UL << 24));
  820. }
  821. void GDClass::Jump(uint16_t dest) {
  822. cI((30UL << 24) | ((dest & 2047L) << 0));
  823. }
  824. void GDClass::LineWidth(uint16_t width) {
  825. cI((14UL << 24) | ((width & 4095L) << 0));
  826. }
  827. void GDClass::Macro(byte m) {
  828. cI((37UL << 24) | ((m & 1L) << 0));
  829. }
  830. void GDClass::PointSize(uint16_t size) {
  831. cI((13UL << 24) | ((size & 8191L) << 0));
  832. }
  833. void GDClass::RestoreContext(void) {
  834. cI((35UL << 24));
  835. }
  836. void GDClass::Return(void) {
  837. cI((36UL << 24));
  838. }
  839. void GDClass::SaveContext(void) {
  840. cI((34UL << 24));
  841. }
  842. void GDClass::ScissorSize(uint16_t width, uint16_t height) {
  843. if (ft8xx_model == 0)
  844. cI((28UL << 24) | ((width & 1023L) << 10) | ((height & 1023L) << 0));
  845. else
  846. cI((28UL << 24) | ((width & 4095L) << 12) | ((height & 4095L) << 0));
  847. }
  848. void GDClass::ScissorXY(uint16_t x, uint16_t y) {
  849. if (ft8xx_model == 0)
  850. cI((27UL << 24) | ((x & 511L) << 9) | ((y & 511L) << 0));
  851. else
  852. cI((27UL << 24) | ((x & 2047L) << 11) | ((y & 2047L) << 0));
  853. }
  854. void GDClass::StencilFunc(byte func, byte ref, byte mask) {
  855. cI((10UL << 24) | ((func & 7L) << 16) | ((ref & 255L) << 8) | ((mask & 255L) << 0));
  856. }
  857. void GDClass::StencilMask(byte mask) {
  858. cI((19UL << 24) | ((mask & 255L) << 0));
  859. }
  860. void GDClass::StencilOp(byte sfail, byte spass) {
  861. cI((12UL << 24) | ((sfail & 7L) << 3) | ((spass & 7L) << 0));
  862. }
  863. void GDClass::TagMask(byte mask) {
  864. cI((20UL << 24) | ((mask & 1L) << 0));
  865. }
  866. void GDClass::Tag(byte s) {
  867. cI((3UL << 24) | ((s & 255L) << 0));
  868. }
  869. void GDClass::Vertex2f(int16_t x, int16_t y) {
  870. // x = int(16 * x);
  871. // y = int(16 * y);
  872. cI((1UL << 30) | ((x & 32767L) << 15) | ((y & 32767L) << 0));
  873. }
  874. void GDClass::Vertex2ii(uint16_t x, uint16_t y, byte handle, byte cell) {
  875. // cI((2UL << 30) | ((x & 511L) << 21) | ((y & 511L) << 12) | ((handle & 31L) << 7) | ((cell & 127L) << 0));
  876. union {
  877. uint32_t c;
  878. uint8_t b[4];
  879. };
  880. b[0] = (cell & 127) | ((handle & 1) << 7);
  881. b[1] = (handle >> 1) | (y << 4);
  882. b[2] = (y >> 4) | (x << 5);
  883. b[3] = (2 << 6) | (x >> 3);
  884. cI(c);
  885. }
  886. void GDClass::VertexFormat(byte frac) {
  887. cI((39UL << 24) | (((frac) & 7) << 0));
  888. }
  889. void GDClass::BitmapLayoutH(byte linestride, byte height) {
  890. cI((40UL << 24) | (((linestride) & 3) << 2) | (((height) & 3) << 0));
  891. }
  892. void GDClass::BitmapSizeH(byte width, byte height) {
  893. cI((41UL << 24) | (((width) & 3) << 2) | (((height) & 3) << 0));
  894. }
  895. void GDClass::PaletteSource(uint32_t addr) {
  896. cI((42UL << 24) | (((addr) & 4194303UL) << 0));
  897. }
  898. void GDClass::VertexTranslateX(uint32_t x) {
  899. cI((43UL << 24) | (((x) & 131071UL) << 0));
  900. }
  901. void GDClass::VertexTranslateY(uint32_t y) {
  902. cI((44UL << 24) | (((y) & 131071UL) << 0));
  903. }
  904. void GDClass::Nop(void) {
  905. cI((45UL << 24));
  906. }
  907. void GDClass::cmd_append(uint32_t ptr, uint32_t num) {
  908. cFFFFFF(0x1e);
  909. cI(ptr);
  910. cI(num);
  911. }
  912. void GDClass::cmd_bgcolor(uint32_t c) {
  913. cFFFFFF(0x09);
  914. cI(c);
  915. }
  916. void GDClass::cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s) {
  917. cFFFFFF(0x0d);
  918. ch(x);
  919. ch(y);
  920. ch(w);
  921. ch(h);
  922. ch(font);
  923. cH(options);
  924. cs(s);
  925. }
  926. void GDClass::cmd_calibrate(void) {
  927. cFFFFFF(0x15);
  928. cFFFFFF(0xff);
  929. }
  930. 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) {
  931. cFFFFFF(0x14);
  932. ch(x);
  933. ch(y);
  934. ch(r);
  935. cH(options);
  936. cH(h);
  937. cH(m);
  938. cH(s);
  939. cH(ms);
  940. }
  941. void GDClass::cmd_coldstart(void) {
  942. cFFFFFF(0x32);
  943. }
  944. void GDClass::cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val) {
  945. cFFFFFF(0x2d);
  946. ch(x);
  947. ch(y);
  948. ch(r);
  949. cH(options);
  950. cH(val);
  951. cH(0);
  952. }
  953. void GDClass::cmd_dlstart(void) {
  954. cFFFFFF(0x00);
  955. }
  956. void GDClass::cmd_fgcolor(uint32_t c) {
  957. cFFFFFF(0x0a);
  958. cI(c);
  959. }
  960. 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) {
  961. cFFFFFF(0x13);
  962. ch(x);
  963. ch(y);
  964. ch(r);
  965. cH(options);
  966. cH(major);
  967. cH(minor);
  968. cH(val);
  969. cH(range);
  970. }
  971. void GDClass::cmd_getmatrix(void) {
  972. cFFFFFF(0x33);
  973. ci(0);
  974. ci(0);
  975. ci(0);
  976. ci(0);
  977. ci(0);
  978. ci(0);
  979. }
  980. void GDClass::cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h) {
  981. cFFFFFF(0x25);
  982. ptr = GDTR.getwp();
  983. cI(0);
  984. w = GDTR.getwp();
  985. cI(0);
  986. h = GDTR.getwp();
  987. cI(0);
  988. }
  989. void GDClass::cmd_getptr(void) {
  990. cFFFFFF(0x23);
  991. cI(0);
  992. }
  993. void GDClass::cmd_gradcolor(uint32_t c) {
  994. cFFFFFF(0x34);
  995. cI(c);
  996. }
  997. void GDClass::cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1) {
  998. cFFFFFF(0x0b);
  999. ch(x0);
  1000. ch(y0);
  1001. cI(rgb0);
  1002. ch(x1);
  1003. ch(y1);
  1004. cI(rgb1);
  1005. }
  1006. void GDClass::cmd_inflate(uint32_t ptr) {
  1007. cFFFFFF(0x22);
  1008. cI(ptr);
  1009. }
  1010. void GDClass::cmd_interrupt(uint32_t ms) {
  1011. cFFFFFF(0x02);
  1012. cI(ms);
  1013. }
  1014. void GDClass::cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s) {
  1015. cFFFFFF(0x0e);
  1016. ch(x);
  1017. ch(y);
  1018. ch(w);
  1019. ch(h);
  1020. ch(font);
  1021. cH(options);
  1022. cs(s);
  1023. }
  1024. void GDClass::cmd_loadidentity(void) {
  1025. cFFFFFF(0x26);
  1026. }
  1027. void GDClass::cmd_loadimage(uint32_t ptr, int32_t options) {
  1028. cFFFFFF(0x24);
  1029. cI(ptr);
  1030. cI(options);
  1031. }
  1032. void GDClass::cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num) {
  1033. cFFFFFF(0x1d);
  1034. cI(dest);
  1035. cI(src);
  1036. cI(num);
  1037. }
  1038. void GDClass::cmd_memset(uint32_t ptr, byte value, uint32_t num) {
  1039. cFFFFFF(0x1b);
  1040. cI(ptr);
  1041. cI((uint32_t)value);
  1042. cI(num);
  1043. }
  1044. uint32_t GDClass::cmd_memcrc(uint32_t ptr, uint32_t num) {
  1045. cFFFFFF(0x18);
  1046. cI(ptr);
  1047. cI(num);
  1048. uint32_t r = GDTR.getwp();
  1049. cI(0xFFFFFFFF);
  1050. return r;
  1051. }
  1052. void GDClass::cmd_memwrite(uint32_t ptr, uint32_t num) {
  1053. cFFFFFF(0x1a);
  1054. cI(ptr);
  1055. cI(num);
  1056. }
  1057. void GDClass::cmd_regwrite(uint32_t ptr, uint32_t val) {
  1058. cFFFFFF(0x1a);
  1059. cI(ptr);
  1060. cI(4UL);
  1061. cI(val);
  1062. }
  1063. void GDClass::cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n) {
  1064. cFFFFFF(0x2e);
  1065. ch(x);
  1066. ch(y);
  1067. ch(font);
  1068. cH(options);
  1069. ci(n);
  1070. }
  1071. 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) {
  1072. cFFFFFF(0x0f);
  1073. ch(x);
  1074. ch(y);
  1075. ch(w);
  1076. ch(h);
  1077. cH(options);
  1078. cH(val);
  1079. cH(range);
  1080. cH(0);
  1081. }
  1082. void GDClass::cmd_regread(uint32_t ptr) {
  1083. cFFFFFF(0x19);
  1084. cI(ptr);
  1085. cI(0);
  1086. }
  1087. void GDClass::cmd_rotate(int32_t a) {
  1088. cFFFFFF(0x29);
  1089. ci(a);
  1090. }
  1091. void GDClass::cmd_scale(int32_t sx, int32_t sy) {
  1092. cFFFFFF(0x28);
  1093. ci(sx);
  1094. ci(sy);
  1095. }
  1096. void GDClass::cmd_screensaver(void) {
  1097. cFFFFFF(0x2f);
  1098. }
  1099. 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) {
  1100. cFFFFFF(0x11);
  1101. ch(x);
  1102. ch(y);
  1103. ch(w);
  1104. ch(h);
  1105. cH(options);
  1106. cH(val);
  1107. cH(size);
  1108. cH(range);
  1109. }
  1110. void GDClass::cmd_setfont(byte font, uint32_t ptr) {
  1111. cFFFFFF(0x2b);
  1112. cI(font);
  1113. cI(ptr);
  1114. }
  1115. void GDClass::cmd_setmatrix(void) {
  1116. cFFFFFF(0x2a);
  1117. }
  1118. void GDClass::cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format) {
  1119. cFFFFFF(0x30);
  1120. ch(x);
  1121. ch(y);
  1122. cH(w);
  1123. cH(h);
  1124. cI(ptr);
  1125. cI(format);
  1126. }
  1127. 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) {
  1128. cFFFFFF(0x10);
  1129. ch(x);
  1130. ch(y);
  1131. ch(w);
  1132. ch(h);
  1133. cH(options);
  1134. cH(val);
  1135. cH(range);
  1136. cH(0);
  1137. }
  1138. void GDClass::cmd_snapshot(uint32_t ptr) {
  1139. cFFFFFF(0x1f);
  1140. cI(ptr);
  1141. }
  1142. void GDClass::cmd_spinner(int16_t x, int16_t y, byte style, byte scale) {
  1143. cFFFFFF(0x16);
  1144. ch(x);
  1145. ch(y);
  1146. cH(style);
  1147. cH(scale);
  1148. }
  1149. void GDClass::cmd_stop(void) {
  1150. cFFFFFF(0x17);
  1151. }
  1152. void GDClass::cmd_swap(void) {
  1153. cFFFFFF(0x01);
  1154. }
  1155. void GDClass::cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s) {
  1156. cFFFFFF(0x0c);
  1157. ch(x);
  1158. ch(y);
  1159. ch(font);
  1160. cH(options);
  1161. cs(s);
  1162. }
  1163. void GDClass::cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s) {
  1164. cFFFFFF(0x12);
  1165. ch(x);
  1166. ch(y);
  1167. ch(w);
  1168. ch(font);
  1169. cH(options);
  1170. cH(state);
  1171. cs(s);
  1172. }
  1173. void GDClass::cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag) {
  1174. cFFFFFF(0x2c);
  1175. ch(x);
  1176. ch(y);
  1177. ch(w);
  1178. ch(h);
  1179. ch(tag);
  1180. ch(0);
  1181. }
  1182. void GDClass::cmd_translate(int32_t tx, int32_t ty) {
  1183. cFFFFFF(0x27);
  1184. ci(tx);
  1185. ci(ty);
  1186. }
  1187. void GDClass::cmd_playvideo(int32_t options) {
  1188. cFFFFFF(0x3a);
  1189. cI(options);
  1190. }
  1191. void GDClass::cmd_romfont(uint32_t font, uint32_t romslot) {
  1192. cFFFFFF(0x3f);
  1193. cI(font);
  1194. cI(romslot);
  1195. }
  1196. void GDClass::cmd_mediafifo(uint32_t ptr, uint32_t size) {
  1197. cFFFFFF(0x39);
  1198. cI(ptr);
  1199. cI(size);
  1200. }
  1201. void GDClass::cmd_setbase(uint32_t b) {
  1202. cFFFFFF(0x38);
  1203. cI(b);
  1204. }
  1205. void GDClass::cmd_videoframe(uint32_t dst, uint32_t ptr) {
  1206. cFFFFFF(0x41);
  1207. cI(dst);
  1208. cI(ptr);
  1209. }
  1210. void GDClass::cmd_snapshot2(uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, int16_t w, int16_t h) {
  1211. cFFFFFF(0x37);
  1212. cI(fmt);
  1213. cI(ptr);
  1214. ch(x);
  1215. ch(y);
  1216. ch(w);
  1217. ch(h);
  1218. }
  1219. void GDClass::cmd_setfont2(uint32_t font, uint32_t ptr, uint32_t firstchar) {
  1220. cFFFFFF(0x3b);
  1221. cI(font);
  1222. cI(ptr);
  1223. cI(firstchar);
  1224. }
  1225. void GDClass::cmd_setbitmap(uint32_t source, uint16_t fmt, uint16_t w, uint16_t h) {
  1226. cFFFFFF(0x43);
  1227. cI(source);
  1228. ch(fmt);
  1229. ch(w);
  1230. ch(h);
  1231. ch(0);
  1232. }
  1233. void GDClass::cmd_setrotate(uint32_t r) {
  1234. cFFFFFF(0x36);
  1235. cI(r);
  1236. // As a special favor, update variables w and h according to this
  1237. // rotation
  1238. w = GDTR.rd16(REG_HSIZE);
  1239. h = GDTR.rd16(REG_VSIZE);
  1240. if (r & 2) {
  1241. int t = h;
  1242. h = w;
  1243. w = t;
  1244. }
  1245. }
  1246. void GDClass::cmd_videostart() {
  1247. cFFFFFF(0x40);
  1248. }
  1249. void GDClass::cmd_sync() {
  1250. cFFFFFF(0x42);
  1251. }
  1252. byte GDClass::rd(uint32_t addr) {
  1253. return GDTR.rd(addr);
  1254. }
  1255. void GDClass::wr(uint32_t addr, uint8_t v) {
  1256. GDTR.wr(addr, v);
  1257. }
  1258. uint16_t GDClass::rd16(uint32_t addr) {
  1259. return GDTR.rd16(addr);
  1260. }
  1261. void GDClass::wr16(uint32_t addr, uint16_t v) {
  1262. GDTR.wr16(addr, v);
  1263. }
  1264. uint32_t GDClass::rd32(uint32_t addr) {
  1265. return GDTR.rd32(addr);
  1266. }
  1267. void GDClass::wr32(uint32_t addr, uint32_t v) {
  1268. GDTR.wr32(addr, v);
  1269. }
  1270. void GDClass::wr_n(uint32_t addr, byte *src, uint32_t n) {
  1271. GDTR.wr_n(addr, src, n);
  1272. }
  1273. void GDClass::cmdbyte(uint8_t b) {
  1274. GDTR.cmdbyte(b);
  1275. }
  1276. void GDClass::cmd32(uint32_t b) {
  1277. GDTR.cmd32(b);
  1278. }
  1279. void GDClass::finish(void) {
  1280. GDTR.finish();
  1281. }
  1282. void GDClass::get_accel(int &x, int &y, int &z) {
  1283. static int f[3];
  1284. for (byte i = 0; i < 3; i++) {
  1285. int a = analogRead(A0 + i);
  1286. int s = (-160 * (a - 376)) >> 6;
  1287. f[i] = ((3 * f[i]) >> 2) + (s >> 2);
  1288. }
  1289. x = f[2];
  1290. y = f[1];
  1291. z = f[0];
  1292. }
  1293. void GDClass::get_inputs(void) {
  1294. GDTR.finish();
  1295. byte *bi = (byte*)&inputs;
  1296. #if defined(DUMPDEV)
  1297. extern FILE* stimfile;
  1298. if (stimfile) {
  1299. byte tag;
  1300. fscanf(stimfile, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
  1301. &bi[0],
  1302. &bi[1],
  1303. &bi[2],
  1304. &bi[3],
  1305. &bi[4],
  1306. &bi[5],
  1307. &bi[6],
  1308. &bi[7],
  1309. &bi[8],
  1310. &bi[9],
  1311. &bi[10],
  1312. &bi[11],
  1313. &bi[12],
  1314. &bi[13],
  1315. &bi[14],
  1316. &bi[15],
  1317. &bi[16],
  1318. &bi[17]);
  1319. GDTR.wr(REG_TAG, tag);
  1320. } else {
  1321. inputs.x = inputs.y = -32768;
  1322. }
  1323. #else
  1324. GDTR.rd_n(bi, REG_TRACKER, 4);
  1325. GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
  1326. GDTR.rd_n(bi + 17, REG_TAG, 1);
  1327. inputs.touching = (inputs.x != -32768);
  1328. inputs.xytouch.set(PIXELS(inputs.x), PIXELS(inputs.y));
  1329. #ifdef DUMP_INPUTS
  1330. for (size_t i = 0; i < sizeof(inputs); i++) {
  1331. Serial.print(bi[i], HEX);
  1332. Serial.print(" ");
  1333. }
  1334. Serial.println();
  1335. #endif
  1336. #endif
  1337. }
  1338. void GDClass::bulkrd(uint32_t a) {
  1339. GDTR.bulk(a);
  1340. }
  1341. void GDClass::resume(void) {
  1342. GDTR.resume();
  1343. }
  1344. void GDClass::__end(void) {
  1345. #if !defined(DUMPDEV) && !defined(RASPBERRY_PI)
  1346. GDTR.__end();
  1347. #endif
  1348. }
  1349. void GDClass::play(uint8_t instrument, uint8_t note) {
  1350. wr16(REG_SOUND, (note << 8) | instrument);
  1351. wr(REG_PLAY, 1);
  1352. }
  1353. void GDClass::sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop) {
  1354. GD.wr32(REG_PLAYBACK_START, start);
  1355. GD.wr32(REG_PLAYBACK_LENGTH, len);
  1356. GD.wr16(REG_PLAYBACK_FREQ, freq);
  1357. GD.wr(REG_PLAYBACK_FORMAT, format);
  1358. GD.wr(REG_PLAYBACK_LOOP, loop);
  1359. GD.wr(REG_PLAYBACK_PLAY, 1);
  1360. }
  1361. void GDClass::reset() {
  1362. GDTR.__end();
  1363. GDTR.wr(REG_CPURESET, 1);
  1364. GDTR.wr(REG_CPURESET, 0);
  1365. GDTR.resume();
  1366. }
  1367. // Load named file from storage
  1368. // returns 0 on failure (e.g. file not found), 1 on success
  1369. byte GDClass::load(const char *filename, void (*progress)(long, long))
  1370. {
  1371. #if defined(RASPBERRY_PI) || defined(DUMPDEV)
  1372. char full_name[2048] = "sdcard/";
  1373. strcat(full_name, filename);
  1374. FILE *f = fopen(full_name, "rb");
  1375. if (!f) {
  1376. perror(full_name);
  1377. exit(1);
  1378. }
  1379. byte buf[512];
  1380. int n;
  1381. while ((n = fread(buf, 1, 512, f)) > 0) {
  1382. GDTR.cmd_n(buf, (n + 3) & ~3);
  1383. }
  1384. fclose(f);
  1385. return 1;
  1386. #else
  1387. GD.__end();
  1388. Reader r;
  1389. if (r.openfile(filename)) {
  1390. byte buf[512];
  1391. while (r.offset < r.size) {
  1392. uint16_t n = min(512U, r.size - r.offset);
  1393. n = (n + 3) & ~3; // force 32-bit alignment
  1394. r.readsector(buf);
  1395. GD.resume();
  1396. if (progress)
  1397. (*progress)(r.offset, r.size);
  1398. GD.copyram(buf, n);
  1399. GDTR.stop();
  1400. }
  1401. GD.resume();
  1402. return 1;
  1403. }
  1404. GD.resume();
  1405. return 0;
  1406. #endif
  1407. }
  1408. // Generated by mk_bsod.py. Blue screen with 'ERROR' text
  1409. static const PROGMEM uint8_t __bsod[32] = {
  1410. 0, 255, 255, 255, 96, 0, 0, 2, 7, 0, 0, 38, 12, 255, 255, 255, 240, 0,
  1411. 90, 0, 31, 0, 0, 6, 69, 82, 82, 79, 82, 0, 0, 0
  1412. };
  1413. static const PROGMEM uint8_t __bsod_badfile[32] = {
  1414. 12, 255, 255, 255, 240, 0, 148, 0, 29, 0, 0, 6, 67, 97, 110, 110, 111,
  1415. 116, 32, 111, 112, 101, 110, 32, 102, 105, 108, 101, 58, 0, 0, 0
  1416. };
  1417. // Fatal error alert.
  1418. // Show a blue screen with message.
  1419. // This method never returns.
  1420. void GDClass::alert(const char *message)
  1421. {
  1422. begin(0);
  1423. copy(__bsod, sizeof(__bsod));
  1424. cmd_text(240, 176, 29, OPT_CENTER, message);
  1425. swap();
  1426. GD.finish();
  1427. for (;;)
  1428. ;
  1429. }
  1430. void GDClass::safeload(const char *filename)
  1431. {
  1432. if (!load(filename)) {
  1433. copy(__bsod, sizeof(__bsod));
  1434. copy(__bsod_badfile, sizeof(__bsod_badfile));
  1435. cmd_text(240, 190, 29, OPT_CENTER, filename);
  1436. swap();
  1437. for (;;)
  1438. ;
  1439. }
  1440. }
  1441. void GDClass::textsize(int &w, int &h, int font, const char *s)
  1442. {
  1443. uint32_t font_addr = rd32(0x309074 + 4 * font);
  1444. w = 0;
  1445. while (*s)
  1446. w += GD.rd(font_addr + *s++);
  1447. h = GD.rd(font_addr + 140);
  1448. }
  1449. #define REG_SCREENSHOT_EN (ft8xx_model ? 0x302010UL : 0x102410UL) // Set to enable screenshot mode
  1450. #define REG_SCREENSHOT_Y (ft8xx_model ? 0x302014UL : 0x102414UL) // Y line register
  1451. #define REG_SCREENSHOT_START (ft8xx_model ? 0x302018UL : 0x102418UL) // Screenshot start trigger
  1452. #define REG_SCREENSHOT_BUSY (ft8xx_model ? 0x3020e8UL : 0x1024d8UL) // Screenshot ready flags
  1453. #define REG_SCREENSHOT_READ (ft8xx_model ? 0x302174UL : 0x102554UL) // Set to enable readout
  1454. #define RAM_SCREENSHOT (ft8xx_model ? 0x3c2000UL : 0x1C2000UL) // Screenshot readout buffer
  1455. #ifndef DUMPDEV
  1456. void GDClass::dumpscreen(void)
  1457. {
  1458. {
  1459. finish();
  1460. int w = GD.rd16(REG_HSIZE), h = GD.rd16(REG_VSIZE);
  1461. wr(REG_SCREENSHOT_EN, 1);
  1462. if (ft8xx_model)
  1463. wr(0x0030201c, 32);
  1464. Serial.write(0xa5);
  1465. Serial.write(w & 0xff);
  1466. Serial.write((w >> 8) & 0xff);
  1467. Serial.write(h & 0xff);
  1468. Serial.write((h >> 8) & 0xff);
  1469. for (int ly = 0; ly < h; ly++) {
  1470. wr16(REG_SCREENSHOT_Y, ly);
  1471. wr(REG_SCREENSHOT_START, 1);
  1472. delay(2);
  1473. while (rd32(REG_SCREENSHOT_BUSY) | rd32(REG_SCREENSHOT_BUSY + 4))
  1474. ;
  1475. wr(REG_SCREENSHOT_READ, 1);
  1476. bulkrd(RAM_SCREENSHOT);
  1477. SPI.transfer(0xff);
  1478. for (int x = 0; x < w; x += 8) {
  1479. union {
  1480. uint32_t v;
  1481. struct {
  1482. uint8_t b, g, r, a;
  1483. };
  1484. } block[8];
  1485. for (int i = 0; i < 8; i++) {
  1486. block[i].b = SPI.transfer(0xff);
  1487. block[i].g = SPI.transfer(0xff);
  1488. block[i].r = SPI.transfer(0xff);
  1489. block[i].a = SPI.transfer(0xff);
  1490. }
  1491. // if (x == 0) block[0].r = 0xff;
  1492. byte difference = 1;
  1493. for (int i = 1, mask = 2; i < 8; i++, mask <<= 1)
  1494. if (block[i].v != block[i-1].v)
  1495. difference |= mask;
  1496. Serial.write(difference);
  1497. for (int i = 0; i < 8; i++)
  1498. if (1 & (difference >> i)) {
  1499. Serial.write(block[i].b);
  1500. Serial.write(block[i].g);
  1501. Serial.write(block[i].r);
  1502. }
  1503. }
  1504. resume();
  1505. wr(REG_SCREENSHOT_READ, 0);
  1506. }
  1507. wr16(REG_SCREENSHOT_EN, 0);
  1508. }
  1509. }
  1510. #endif