GD2.cpp 33 KB

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