GD2.cpp 34 KB

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