GD2.h 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * Copyright (C) 2013-2015 by James Bowman <jamesb@excamera.com>
  3. * Gameduino 2 library for Arduino, Arduino Due, Raspberry Pi.
  4. *
  5. */
  6. #ifndef _GD2_H_INCLUDED
  7. #define _GD2_H_INCLUDED
  8. #if defined(RASPBERRY_PI) || defined(DUMPDEV)
  9. #include "wiring.h"
  10. #endif
  11. #include "Arduino.h"
  12. #include <stdarg.h>
  13. #define RGB(r, g, b) ((uint32_t)((((r) & 0xffL) << 16) | (((g) & 0xffL) << 8) | ((b) & 0xffL)))
  14. #define F8(x) (int((x) * 256L))
  15. #define F16(x) ((int32_t)((x) * 65536L))
  16. #define GD_CALIBRATE 1
  17. #define GD_TRIM 2
  18. #define GD_STORAGE 4
  19. #ifdef __SAM3X8E__
  20. #define __DUE__ 1
  21. #endif
  22. ////////////////////////////////////////////////////////////////////////
  23. // Decide if we want to compile in SDcard support
  24. //
  25. // For stock Arduino models: yes
  26. // Raspberry PI: no
  27. // Arduino Due: no
  28. //
  29. #if !defined(RASPBERRY_PI) && !defined(DUMPDEV)
  30. #define SDCARD 1
  31. #else
  32. #define SDCARD 0
  33. #endif
  34. #if defined(__DUE__)
  35. #define MOSI 11
  36. #define MISO 12
  37. #define SCK 13 // B.27
  38. class ASPI_t {
  39. public:
  40. void begin(void) {
  41. pinMode(MOSI, OUTPUT);
  42. pinMode(MISO, INPUT);
  43. pinMode(SCK, OUTPUT);
  44. digitalWrite(SCK, 0);
  45. // PIOB->PIO_PER = PIO_PB27;
  46. // PIOB->PIO_CODR = PIO_PB27;
  47. // PIOB->PIO_PUDR = PIO_PB27;
  48. }
  49. byte transfer(byte x ) {
  50. byte r = 0;
  51. for (byte i = 8; i; i--) {
  52. if (x & 0x80)
  53. PIOD->PIO_SODR = PIO_PD7;
  54. else
  55. PIOD->PIO_CODR = PIO_PD7;
  56. // digitalWrite(MOSI, (x >> 7) & 1);
  57. x <<= 1;
  58. // digitalWrite(SCK, 1);
  59. PIOB->PIO_SODR = PIO_PB27;
  60. r <<= 1;
  61. r |= digitalRead(MISO);
  62. // digitalWrite(SCK, 0);
  63. PIOB->PIO_CODR = PIO_PB27;
  64. }
  65. return r;
  66. }
  67. };
  68. static class ASPI_t ASPI;
  69. #define SPI ASPI
  70. #endif
  71. #if SDCARD
  72. #if defined(VERBOSE) && (VERBOSE > 0)
  73. #define INFO(X) Serial.println((X))
  74. #if defined(RASPBERRY_PI)
  75. #define REPORT(VAR) fprintf(stderr, #VAR "=%d\n", (VAR))
  76. #else
  77. #define REPORT(VAR) (Serial.print(#VAR "="), Serial.print(VAR, DEC), Serial.print(' '), Serial.println(VAR, HEX))
  78. #endif
  79. #else
  80. #define INFO(X)
  81. #define REPORT(X)
  82. #endif
  83. struct dirent {
  84. char name[8];
  85. char ext[3];
  86. byte attribute;
  87. byte reserved[8];
  88. uint16_t cluster_hi; // FAT32 only
  89. uint16_t time;
  90. uint16_t date;
  91. uint16_t cluster;
  92. uint32_t size;
  93. };
  94. // https://www.sdcard.org/downloads/pls/simplified_specs/Part_1_Physical_Layer_Simplified_Specification_Ver_3.01_Final_100518.pdf
  95. // page 22
  96. // http://mac6.ma.psu.edu/space2008/RockSat/microController/sdcard_appnote_foust.pdf
  97. // http://elm-chan.org/docs/mmc/mmc_e.html
  98. // http://www.pjrc.com/tech/8051/ide/fat32.html
  99. #define FAT16 0
  100. #define FAT32 1
  101. #define DD
  102. class sdcard {
  103. public:
  104. void sel() {
  105. digitalWrite(pin, LOW);
  106. delay(1);
  107. }
  108. void desel() {
  109. digitalWrite(pin, HIGH);
  110. SPI.transfer(0xff); // force DO release
  111. }
  112. void sd_delay(byte n) {
  113. while (n--) {
  114. DD SPI.transfer(0xff);
  115. }
  116. }
  117. void cmd(byte cmd, uint32_t lba = 0, uint8_t crc = 0x95) {
  118. #if VERBOSE > 1
  119. Serial.print("cmd ");
  120. Serial.print(cmd, DEC);
  121. Serial.print(" ");
  122. Serial.print(lba, HEX);
  123. Serial.println();
  124. #endif
  125. sel();
  126. // DD SPI.transfer(0xff);
  127. DD SPI.transfer(0x40 | cmd);
  128. DD SPI.transfer(0xff & (lba >> 24));
  129. DD SPI.transfer(0xff & (lba >> 16));
  130. DD SPI.transfer(0xff & (lba >> 8));
  131. DD SPI.transfer(0xff & (lba));
  132. DD SPI.transfer(crc);
  133. // DD SPI.transfer(0xff);
  134. }
  135. byte response() {
  136. byte r;
  137. DD
  138. r = SPI.transfer(0xff);
  139. while (r & 0x80) {
  140. DD
  141. r = SPI.transfer(0xff);
  142. }
  143. return r;
  144. }
  145. byte R1() { // read response R1
  146. byte r = response();
  147. desel();
  148. SPI.transfer(0xff); // trailing byte
  149. return r;
  150. }
  151. byte sdR3(uint32_t &ocr) { // read response R3
  152. byte r = response();
  153. for (byte i = 4; i; i--)
  154. ocr = (ocr << 8) | SPI.transfer(0xff);
  155. SPI.transfer(0xff); // trailing byte
  156. desel();
  157. return r;
  158. }
  159. byte sdR7() { // read response R3
  160. byte r = response();
  161. for (byte i = 4; i; i--)
  162. // Serial.println(SPI.transfer(0xff), HEX);
  163. SPI.transfer(0xff);
  164. desel();
  165. return r;
  166. }
  167. void appcmd(byte cc, uint32_t lba = 0) {
  168. cmd(55); R1();
  169. cmd(cc, lba);
  170. }
  171. void begin(byte p) {
  172. byte type_code;
  173. byte sdhc;
  174. pin = p;
  175. pinMode(pin, OUTPUT);
  176. #if !defined(__DUE__) && !defined(TEENSYDUINO)
  177. SPI.setClockDivider(SPI_CLOCK_DIV64);
  178. #endif
  179. desel();
  180. // for (;;) SPI.transfer(0xff);
  181. delay(10); // wait for boot
  182. sd_delay(10); // deselected, 80 pulses
  183. INFO("Attempting card reset... ");
  184. byte r1;
  185. static int attempts;
  186. attempts = 0;
  187. do { // reset, enter idle
  188. cmd(0);
  189. while ((r1 = SPI.transfer(0xff)) & 0x80)
  190. if (++attempts == 1000)
  191. goto finished;
  192. desel();
  193. SPI.transfer(0xff); // trailing byte
  194. REPORT(r1);
  195. } while (r1 != 1);
  196. INFO("reset ok\n");
  197. sdhc = 0;
  198. cmd(8, 0x1aa, 0x87);
  199. r1 = sdR7();
  200. sdhc = (r1 == 1);
  201. REPORT(sdhc);
  202. INFO("Sending card init command");
  203. while (1) {
  204. appcmd(41, sdhc ? (1UL << 30) : 0); // card init
  205. r1 = R1();
  206. #if VERBOSE
  207. Serial.println(r1, HEX);
  208. #endif
  209. if ((r1 & 1) == 0)
  210. break;
  211. delay(100);
  212. }
  213. INFO("OK");
  214. if (sdhc) {
  215. cmd(58);
  216. uint32_t OCR = 0;
  217. sdR3(OCR);
  218. REPORT(OCR);
  219. ccs = 1UL & (OCR >> 30);
  220. } else {
  221. ccs = 0;
  222. }
  223. REPORT(ccs);
  224. // Test point: dump sector 0 to serial.
  225. // should see first 512 bytes of card, ending 55 AA.
  226. if (0) {
  227. cmd17(0);
  228. for (int i = 0; i < 512; i++) {
  229. delay(10);
  230. byte b = SPI.transfer(0xff);
  231. Serial.print(b, HEX);
  232. Serial.print(' ');
  233. if ((i & 15) == 15)
  234. Serial.println();
  235. }
  236. desel();
  237. for (;;);
  238. }
  239. type_code = rd(0x1be + 0x4);
  240. switch (type_code) {
  241. default:
  242. type = FAT16;
  243. break;
  244. case 0x0b:
  245. case 0x0c:
  246. type = FAT32;
  247. break;
  248. }
  249. REPORT(type_code);
  250. o_partition = 512L * rd4(0x1be + 0x8);
  251. sectors_per_cluster = rd(o_partition + 0xd);
  252. reserved_sectors = rd2(o_partition + 0xe);
  253. cluster_size = 512L * sectors_per_cluster;
  254. REPORT(sectors_per_cluster);
  255. // Serial.println("Bytes per sector: %d\n", rd2(o_partition + 0xb));
  256. // Serial.println("Sectors per cluster: %d\n", sectors_per_cluster);
  257. if (type == FAT16) {
  258. max_root_dir_entries = rd2(o_partition + 0x11);
  259. sectors_per_fat = rd2(o_partition + 0x16);
  260. o_fat = o_partition + 512L * reserved_sectors;
  261. o_root = o_fat + (2 * 512L * sectors_per_fat);
  262. // data area starts with cluster 2, so offset it here
  263. o_data = o_root + (max_root_dir_entries * 32L) - (2L * cluster_size);
  264. } else {
  265. uint32_t sectors_per_fat = rd4(o_partition + 0x24);
  266. root_dir_first_cluster = rd4(o_partition + 0x2c);
  267. uint32_t fat_begin_lba = (o_partition >> 9) + reserved_sectors;
  268. uint32_t cluster_begin_lba = (o_partition >> 9) + reserved_sectors + (2 * sectors_per_fat);
  269. o_fat = 512L * fat_begin_lba;
  270. o_root = (512L * (cluster_begin_lba + (root_dir_first_cluster - 2) * sectors_per_cluster));
  271. o_data = (512L * (cluster_begin_lba - 2 * sectors_per_cluster));
  272. }
  273. finished:
  274. INFO("finished");
  275. ;
  276. #if !defined(__DUE__)
  277. SPI.setClockDivider(SPI_CLOCK_DIV2);
  278. SPSR = (1 << SPI2X);
  279. #endif
  280. }
  281. void cmd17(uint32_t off) {
  282. if (ccs)
  283. cmd(17, off >> 9);
  284. else
  285. cmd(17, off & ~511L);
  286. R1();
  287. sel();
  288. while (SPI.transfer(0xff) != 0xfe)
  289. ;
  290. }
  291. void rdn(byte *d, uint32_t off, uint16_t n) {
  292. cmd17(off);
  293. uint16_t i;
  294. uint16_t bo = (off & 511);
  295. for (i = 0; i < bo; i++)
  296. SPI.transfer(0xff);
  297. for (i = 0; i < n; i++)
  298. *d++ = SPI.transfer(0xff);
  299. for (i = 0; i < (514 - bo - n); i++)
  300. SPI.transfer(0xff);
  301. desel();
  302. }
  303. uint32_t rd4(uint32_t off) {
  304. uint32_t r;
  305. rdn((byte*)&r, off, sizeof(r));
  306. return r;
  307. }
  308. uint16_t rd2(uint32_t off) {
  309. uint16_t r;
  310. rdn((byte*)&r, off, sizeof(r));
  311. return r;
  312. }
  313. byte rd(uint32_t off) {
  314. byte r;
  315. rdn((byte*)&r, off, sizeof(r));
  316. return r;
  317. }
  318. byte pin;
  319. byte ccs;
  320. byte type;
  321. uint16_t sectors_per_cluster;
  322. uint16_t reserved_sectors;
  323. uint16_t max_root_dir_entries;
  324. uint16_t sectors_per_fat;
  325. uint16_t cluster_size;
  326. uint32_t root_dir_first_cluster;
  327. // These are all linear addresses, hence the o_ prefix
  328. uint32_t o_partition;
  329. uint32_t o_fat;
  330. uint32_t o_root;
  331. uint32_t o_data;
  332. };
  333. static void dos83(byte dst[11], const char *ps)
  334. {
  335. byte i = 0;
  336. while (*ps) {
  337. if (*ps != '.')
  338. dst[i++] = toupper(*ps);
  339. else {
  340. while (i < 8)
  341. dst[i++] = ' ';
  342. }
  343. ps++;
  344. }
  345. while (i < 11)
  346. dst[i++] = ' ';
  347. }
  348. #else
  349. class sdcard {
  350. public:
  351. void begin(int p) {};
  352. };
  353. #endif
  354. ////////////////////////////////////////////////////////////////////////
  355. class GDClass {
  356. public:
  357. void begin(uint8_t options = (GD_CALIBRATE | GD_TRIM | GD_STORAGE));
  358. uint16_t random();
  359. uint16_t random(uint16_t n);
  360. void seed(uint16_t n);
  361. int16_t rsin(int16_t r, uint16_t th);
  362. int16_t rcos(int16_t r, uint16_t th);
  363. void polar(int &x, int &y, int16_t r, uint16_t th);
  364. uint16_t atan2(int16_t y, int16_t x);
  365. void copy(const PROGMEM uint8_t *src, int count);
  366. void copyram(byte *src, int count);
  367. void self_calibrate(void);
  368. void swap(void);
  369. void flush(void);
  370. void finish(void);
  371. void play(uint8_t instrument, uint8_t note = 0);
  372. void sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop = 0);
  373. void get_inputs(void);
  374. void get_accel(int &x, int &y, int &z);
  375. struct {
  376. uint16_t track_tag;
  377. uint16_t track_val;
  378. uint16_t rz;
  379. uint16_t __dummy_1;
  380. int16_t y;
  381. int16_t x;
  382. int16_t tag_y;
  383. int16_t tag_x;
  384. uint8_t tag;
  385. uint8_t ptag;
  386. } inputs;
  387. void AlphaFunc(byte func, byte ref);
  388. void Begin(byte prim);
  389. void BitmapHandle(byte handle);
  390. void BitmapLayout(byte format, uint16_t linestride, uint16_t height);
  391. void BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height);
  392. void BitmapSource(uint32_t addr);
  393. void BitmapTransformA(int32_t a);
  394. void BitmapTransformB(int32_t b);
  395. void BitmapTransformC(int32_t c);
  396. void BitmapTransformD(int32_t d);
  397. void BitmapTransformE(int32_t e);
  398. void BitmapTransformF(int32_t f);
  399. void BlendFunc(byte src, byte dst);
  400. void Call(uint16_t dest);
  401. void Cell(byte cell);
  402. void ClearColorA(byte alpha);
  403. void ClearColorRGB(byte red, byte green, byte blue);
  404. void ClearColorRGB(uint32_t rgb);
  405. void Clear(byte c, byte s, byte t);
  406. void Clear(void);
  407. void ClearStencil(byte s);
  408. void ClearTag(byte s);
  409. void ColorA(byte alpha);
  410. void ColorMask(byte r, byte g, byte b, byte a);
  411. void ColorRGB(byte red, byte green, byte blue);
  412. void ColorRGB(uint32_t rgb);
  413. void Display(void);
  414. void End(void);
  415. void Jump(uint16_t dest);
  416. void LineWidth(uint16_t width);
  417. void Macro(byte m);
  418. void PointSize(uint16_t size);
  419. void RestoreContext(void);
  420. void Return(void);
  421. void SaveContext(void);
  422. void ScissorSize(uint16_t width, uint16_t height);
  423. void ScissorXY(uint16_t x, uint16_t y);
  424. void StencilFunc(byte func, byte ref, byte mask);
  425. void StencilMask(byte mask);
  426. void StencilOp(byte sfail, byte spass);
  427. void TagMask(byte mask);
  428. void Tag(byte s);
  429. void Vertex2f(int16_t x, int16_t y);
  430. void Vertex2ii(uint16_t x, uint16_t y, byte handle = 0, byte cell = 0);
  431. // Higher-level graphics commands
  432. void cmd_append(uint32_t ptr, uint32_t num);
  433. void cmd_bgcolor(uint32_t c);
  434. void cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s);
  435. void cmd_calibrate(void);
  436. void 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);
  437. void cmd_coldstart(void);
  438. void cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val);
  439. void cmd_dlstart(void);
  440. void cmd_fgcolor(uint32_t c);
  441. void 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);
  442. void cmd_getmatrix(void);
  443. void cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h);
  444. void cmd_getptr(void);
  445. void cmd_gradcolor(uint32_t c);
  446. void cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1);
  447. void cmd_inflate(uint32_t ptr);
  448. void cmd_interrupt(uint32_t ms);
  449. void cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s);
  450. void cmd_loadidentity(void);
  451. void cmd_loadimage(uint32_t ptr, int32_t options);
  452. void cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num);
  453. void cmd_memset(uint32_t ptr, byte value, uint32_t num);
  454. uint32_t cmd_memcrc(uint32_t ptr, uint32_t num);
  455. void cmd_memwrite(uint32_t ptr, uint32_t num);
  456. void cmd_regwrite(uint32_t ptr, uint32_t val);
  457. void cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n);
  458. void cmd_progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t range);
  459. void cmd_regread(uint32_t ptr);
  460. void cmd_rotate(int32_t a);
  461. void cmd_scale(int32_t sx, int32_t sy);
  462. void cmd_screensaver(void);
  463. void 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);
  464. void cmd_setfont(byte font, uint32_t ptr);
  465. void cmd_setmatrix(void);
  466. void cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format);
  467. void cmd_slider(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range);
  468. void cmd_snapshot(uint32_t ptr);
  469. void cmd_spinner(int16_t x, int16_t y, byte style, byte scale);
  470. void cmd_stop(void);
  471. void cmd_swap(void);
  472. void cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s);
  473. void cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s);
  474. void cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag);
  475. void cmd_translate(int32_t tx, int32_t ty);
  476. byte rd(uint32_t addr);
  477. void wr(uint32_t addr, uint8_t v);
  478. uint16_t rd16(uint32_t addr);
  479. void wr16(uint32_t addr, uint16_t v);
  480. uint32_t rd32(uint32_t addr);
  481. void wr32(uint32_t addr, uint32_t v);
  482. void wr_n(uint32_t addr, byte *src, uint32_t n);
  483. void cmd32(uint32_t b);
  484. void bulkrd(uint32_t a);
  485. void resume(void);
  486. void __end(void);
  487. void reset(void);
  488. void dumpscreen(void);
  489. byte load(const char *filename, void (*progress)(long, long) = NULL);
  490. void safeload(const char *filename);
  491. void alert(const char *message);
  492. sdcard SD;
  493. void storage(void);
  494. void tune(void);
  495. private:
  496. static void cFFFFFF(byte v);
  497. static void cI(uint32_t);
  498. static void ci(int32_t);
  499. static void cH(uint16_t);
  500. static void ch(int16_t);
  501. static void cs(const char *);
  502. static void fmtcmd(const char *fmt, ...);
  503. static void align(byte n);
  504. void cmdbyte(uint8_t b);
  505. uint32_t measure_freq(void);
  506. uint32_t rseed;
  507. };
  508. extern GDClass GD;
  509. extern byte ft8xx_model;
  510. #if SDCARD
  511. class Reader {
  512. public:
  513. int openfile(const char *filename) {
  514. int i = 0;
  515. byte dosname[11];
  516. dirent de;
  517. dos83(dosname, filename);
  518. do {
  519. GD.SD.rdn((byte*)&de, GD.SD.o_root + i * 32, sizeof(de));
  520. // Serial.println(de.name);
  521. if (0 == memcmp(de.name, dosname, 11)) {
  522. begin(de);
  523. return 1;
  524. }
  525. i++;
  526. } while (de.name[0]);
  527. return 0;
  528. }
  529. void begin(dirent &de) {
  530. size = de.size;
  531. cluster = de.cluster;
  532. if (GD.SD.type == FAT32)
  533. cluster |= ((long)de.cluster_hi << 16);
  534. sector = 0;
  535. offset = 0;
  536. }
  537. void nextcluster() {
  538. if (GD.SD.type == FAT16)
  539. cluster = GD.SD.rd2(GD.SD.o_fat + 2 * cluster);
  540. else
  541. cluster = GD.SD.rd4(GD.SD.o_fat + 4 * cluster);
  542. #if VERBOSE
  543. Serial.print("nextcluster=");
  544. Serial.println(cluster, DEC);
  545. #endif
  546. }
  547. void skipcluster() {
  548. nextcluster();
  549. offset += GD.SD.cluster_size;
  550. }
  551. void skipsector() {
  552. if (sector == GD.SD.sectors_per_cluster) {
  553. sector = 0;
  554. nextcluster();
  555. }
  556. sector++;
  557. offset += 512;
  558. }
  559. void seek(uint32_t o) {
  560. while (offset < o) {
  561. if ((sector == GD.SD.sectors_per_cluster) && ((o - offset) > (long)GD.SD.cluster_size))
  562. skipcluster();
  563. else
  564. skipsector();
  565. }
  566. }
  567. void readsector() {
  568. if (sector == GD.SD.sectors_per_cluster) {
  569. sector = 0;
  570. nextcluster();
  571. }
  572. uint32_t off = GD.SD.o_data + ((long)GD.SD.cluster_size * cluster) + (512L * sector);
  573. #if VERBOSE
  574. Serial.print("off=0x");
  575. Serial.println(off, HEX);
  576. #endif
  577. GD.SD.cmd17(off & ~511L);
  578. // Serial.println(2 * (micros() - t0), DEC);
  579. sector++;
  580. offset += 512;
  581. }
  582. void readsector(byte *dst) {
  583. readsector();
  584. for (int i = 0; i < 64; i++) {
  585. *dst++ = SPI.transfer(0xff);
  586. *dst++ = SPI.transfer(0xff);
  587. *dst++ = SPI.transfer(0xff);
  588. *dst++ = SPI.transfer(0xff);
  589. *dst++ = SPI.transfer(0xff);
  590. *dst++ = SPI.transfer(0xff);
  591. *dst++ = SPI.transfer(0xff);
  592. *dst++ = SPI.transfer(0xff);
  593. }
  594. SPI.transfer(0xff); // consume CRC
  595. SPI.transfer(0xff);
  596. GD.SD.desel();
  597. }
  598. uint32_t cluster;
  599. uint32_t offset;
  600. uint32_t size;
  601. byte sector;
  602. };
  603. #endif
  604. typedef struct {
  605. byte handle;
  606. uint16_t w, h;
  607. uint16_t size;
  608. } shape_t;
  609. // Convert degrees to Furmans
  610. #define DEGREES(n) ((65536UL * (n)) / 360)
  611. #define NEVER 0
  612. #define LESS 1
  613. #define LEQUAL 2
  614. #define GREATER 3
  615. #define GEQUAL 4
  616. #define EQUAL 5
  617. #define NOTEQUAL 6
  618. #define ALWAYS 7
  619. #define ARGB1555 0
  620. #define L1 1
  621. #define L4 2
  622. #define L8 3
  623. #define RGB332 4
  624. #define ARGB2 5
  625. #define ARGB4 6
  626. #define RGB565 7
  627. #define PALETTED 8
  628. #define TEXT8X8 9
  629. #define TEXTVGA 10
  630. #define BARGRAPH 11
  631. #define NEAREST 0
  632. #define BILINEAR 1
  633. #define BORDER 0
  634. #define REPEAT 1
  635. #define KEEP 1
  636. #define REPLACE 2
  637. #define INCR 3
  638. #define DECR 4
  639. #define INVERT 5
  640. #define DLSWAP_DONE 0
  641. #define DLSWAP_LINE 1
  642. #define DLSWAP_FRAME 2
  643. #define INT_SWAP 1
  644. #define INT_TOUCH 2
  645. #define INT_TAG 4
  646. #define INT_SOUND 8
  647. #define INT_PLAYBACK 16
  648. #define INT_CMDEMPTY 32
  649. #define INT_CMDFLAG 64
  650. #define INT_CONVCOMPLETE 128
  651. #define TOUCHMODE_OFF 0
  652. #define TOUCHMODE_ONESHOT 1
  653. #define TOUCHMODE_FRAME 2
  654. #define TOUCHMODE_CONTINUOUS 3
  655. #define ZERO 0
  656. #define ONE 1
  657. #define SRC_ALPHA 2
  658. #define DST_ALPHA 3
  659. #define ONE_MINUS_SRC_ALPHA 4
  660. #define ONE_MINUS_DST_ALPHA 5
  661. #define BITMAPS 1
  662. #define POINTS 2
  663. #define LINES 3
  664. #define LINE_STRIP 4
  665. #define EDGE_STRIP_R 5
  666. #define EDGE_STRIP_L 6
  667. #define EDGE_STRIP_A 7
  668. #define EDGE_STRIP_B 8
  669. #define RECTS 9
  670. #define OPT_MONO 1
  671. #define OPT_NODL 2
  672. #define OPT_FLAT 256
  673. #define OPT_CENTERX 512
  674. #define OPT_CENTERY 1024
  675. #define OPT_CENTER (OPT_CENTERX | OPT_CENTERY)
  676. #define OPT_NOBACK 4096
  677. #define OPT_NOTICKS 8192
  678. #define OPT_NOHM 16384
  679. #define OPT_NOPOINTER 16384
  680. #define OPT_NOSECS 32768
  681. #define OPT_NOHANDS 49152
  682. #define OPT_RIGHTX 2048
  683. #define OPT_SIGNED 256
  684. #define LINEAR_SAMPLES 0
  685. #define ULAW_SAMPLES 1
  686. #define ADPCM_SAMPLES 2
  687. // 'instrument' argument to GD.play()
  688. #define SILENCE 0x00
  689. #define SQUAREWAVE 0x01
  690. #define SINEWAVE 0x02
  691. #define SAWTOOTH 0x03
  692. #define TRIANGLE 0x04
  693. #define BEEPING 0x05
  694. #define ALARM 0x06
  695. #define WARBLE 0x07
  696. #define CAROUSEL 0x08
  697. #define PIPS(n) (0x0f + (n))
  698. #define HARP 0x40
  699. #define XYLOPHONE 0x41
  700. #define TUBA 0x42
  701. #define GLOCKENSPIEL 0x43
  702. #define ORGAN 0x44
  703. #define TRUMPET 0x45
  704. #define PIANO 0x46
  705. #define CHIMES 0x47
  706. #define MUSICBOX 0x48
  707. #define BELL 0x49
  708. #define CLICK 0x50
  709. #define SWITCH 0x51
  710. #define COWBELL 0x52
  711. #define NOTCH 0x53
  712. #define HIHAT 0x54
  713. #define KICKDRUM 0x55
  714. #define POP 0x56
  715. #define CLACK 0x57
  716. #define CHACK 0x58
  717. #define MUTE 0x60
  718. #define UNMUTE 0x61
  719. #define RAM_PAL 1056768UL
  720. #define RAM_CMD (ft8xx_model ? 0x308000UL : 0x108000UL)
  721. #define RAM_DL (ft8xx_model ? 0x300000UL : 0x100000UL)
  722. #define REG_CLOCK (ft8xx_model ? 0x302008UL : 0x102408UL)
  723. #define REG_CMD_DL (ft8xx_model ? 0x302100UL : 0x1024ecUL)
  724. #define REG_CMD_READ (ft8xx_model ? 0x3020f8UL : 0x1024e4UL)
  725. #define REG_CMD_WRITE (ft8xx_model ? 0x3020fcUL : 0x1024e8UL)
  726. #define REG_CPURESET (ft8xx_model ? 0x302020UL : 0x10241cUL)
  727. #define REG_CSPREAD (ft8xx_model ? 0x302068UL : 0x102464UL)
  728. #define REG_DITHER (ft8xx_model ? 0x302060UL : 0x10245cUL)
  729. #define REG_DLSWAP (ft8xx_model ? 0x302054UL : 0x102450UL)
  730. #define REG_FRAMES (ft8xx_model ? 0x302004UL : 0x102404UL)
  731. #define REG_FREQUENCY (ft8xx_model ? 0x30200cUL : 0x10240cUL)
  732. #define REG_GPIO (ft8xx_model ? 0x302094UL : 0x102490UL)
  733. #define REG_GPIO_DIR (ft8xx_model ? 0x302090UL : 0x10248cUL)
  734. #define REG_HCYCLE (ft8xx_model ? 0x30202cUL : 0x102428UL)
  735. #define REG_HOFFSET (ft8xx_model ? 0x302030UL : 0x10242cUL)
  736. #define REG_HSIZE (ft8xx_model ? 0x302034UL : 0x102430UL)
  737. #define REG_HSYNC0 (ft8xx_model ? 0x302038UL : 0x102434UL)
  738. #define REG_HSYNC1 (ft8xx_model ? 0x30203cUL : 0x102438UL)
  739. #define REG_ID (ft8xx_model ? 0x302000UL : 0x102400UL)
  740. #define REG_INT_EN (ft8xx_model ? 0x3020acUL : 0x10249cUL)
  741. #define REG_INT_FLAGS (ft8xx_model ? 0x3020a8UL : 0x102498UL)
  742. #define REG_INT_MASK (ft8xx_model ? 0x3020b0UL : 0x1024a0UL)
  743. #define REG_MACRO_0 (ft8xx_model ? 0x3020d8UL : 0x1024c8UL)
  744. #define REG_MACRO_1 (ft8xx_model ? 0x3020dcUL : 0x1024ccUL)
  745. #define REG_OUTBITS (ft8xx_model ? 0x30205cUL : 0x102458UL)
  746. #define REG_PCLK (ft8xx_model ? 0x302070UL : 0x10246cUL)
  747. #define REG_PCLK_POL (ft8xx_model ? 0x30206cUL : 0x102468UL)
  748. #define REG_PLAY (ft8xx_model ? 0x30208cUL : 0x102488UL)
  749. #define REG_PLAYBACK_FORMAT (ft8xx_model ? 0x3020c4UL : 0x1024b4UL)
  750. #define REG_PLAYBACK_FREQ (ft8xx_model ? 0x3020c0UL : 0x1024b0UL)
  751. #define REG_PLAYBACK_LENGTH (ft8xx_model ? 0x3020b8UL : 0x1024a8UL)
  752. #define REG_PLAYBACK_LOOP (ft8xx_model ? 0x3020c8UL : 0x1024b8UL)
  753. #define REG_PLAYBACK_PLAY (ft8xx_model ? 0x3020ccUL : 0x1024bcUL)
  754. #define REG_PLAYBACK_READPTR (ft8xx_model ? 0x3020bcUL : 0x1024acUL)
  755. #define REG_PLAYBACK_START (ft8xx_model ? 0x3020b4UL : 0x1024a4UL)
  756. #define REG_PWM_DUTY (ft8xx_model ? 0x3020d4UL : 0x1024c4UL)
  757. #define REG_PWM_HZ (ft8xx_model ? 0x3020d0UL : 0x1024c0UL)
  758. #define REG_ROTATE (ft8xx_model ? 0x302058UL : 0x102454UL)
  759. #define REG_SOUND (ft8xx_model ? 0x302088UL : 0x102484UL)
  760. #define REG_SWIZZLE (ft8xx_model ? 0x302064UL : 0x102460UL)
  761. #define REG_TAG (ft8xx_model ? 0x30207cUL : 0x102478UL)
  762. #define REG_TAG_X (ft8xx_model ? 0x302074UL : 0x102470UL)
  763. #define REG_TAG_Y (ft8xx_model ? 0x302078UL : 0x102474UL)
  764. #define REG_TOUCH_ADC_MODE (ft8xx_model ? 0x302108UL : 0x1024f4UL)
  765. #define REG_TOUCH_CHARGE (ft8xx_model ? 0x30210cUL : 0x1024f8UL)
  766. #define REG_TOUCH_DIRECT_XY (ft8xx_model ? 0x30218cUL : 0x102574UL)
  767. #define REG_TOUCH_DIRECT_Z1Z2 (ft8xx_model ? 0x302190UL : 0x102578UL)
  768. #define REG_TOUCH_MODE (ft8xx_model ? 0x302104UL : 0x1024f0UL)
  769. #define REG_TOUCH_OVERSAMPLE (ft8xx_model ? 0x302114UL : 0x102500UL)
  770. #define REG_TOUCH_RAW_XY (ft8xx_model ? 0x30211cUL : 0x102508UL)
  771. #define REG_TOUCH_RZ (ft8xx_model ? 0x302120UL : 0x10250cUL)
  772. #define REG_TOUCH_RZTHRESH (ft8xx_model ? 0x302118UL : 0x102504UL)
  773. #define REG_TOUCH_SCREEN_XY (ft8xx_model ? 0x302124UL : 0x102510UL)
  774. #define REG_TOUCH_SETTLE (ft8xx_model ? 0x302110UL : 0x1024fcUL)
  775. #define REG_TOUCH_TAG (ft8xx_model ? 0x30212cUL : 0x102518UL)
  776. #define REG_TOUCH_TAG_XY (ft8xx_model ? 0x302128UL : 0x102514UL)
  777. #define REG_TOUCH_TRANSFORM_A (ft8xx_model ? 0x302150UL : 0x10251cUL)
  778. #define REG_TOUCH_TRANSFORM_B (ft8xx_model ? 0x302154UL : 0x102520UL)
  779. #define REG_TOUCH_TRANSFORM_C (ft8xx_model ? 0x302158UL : 0x102524UL)
  780. #define REG_TOUCH_TRANSFORM_D (ft8xx_model ? 0x30215cUL : 0x102528UL)
  781. #define REG_TOUCH_TRANSFORM_E (ft8xx_model ? 0x302160UL : 0x10252cUL)
  782. #define REG_TOUCH_TRANSFORM_F (ft8xx_model ? 0x302164UL : 0x102530UL)
  783. #define REG_TRACKER (ft8xx_model ? 0x309000UL : 0x109000UL)
  784. #define REG_TRIM (ft8xx_model ? 0x302180UL : 0x10256cUL)
  785. #define REG_VCYCLE (ft8xx_model ? 0x302040UL : 0x10243cUL)
  786. #define REG_VOFFSET (ft8xx_model ? 0x302044UL : 0x102440UL)
  787. #define REG_VOL_PB (ft8xx_model ? 0x302080UL : 0x10247cUL)
  788. #define REG_VOL_SOUND (ft8xx_model ? 0x302084UL : 0x102480UL)
  789. #define REG_VSIZE (ft8xx_model ? 0x302048UL : 0x102444UL)
  790. #define REG_VSYNC0 (ft8xx_model ? 0x30204cUL : 0x102448UL)
  791. #define REG_VSYNC1 (ft8xx_model ? 0x302050UL : 0x10244cUL)
  792. #define VERTEX2II(x, y, handle, cell) \
  793. ((2UL << 30) | (((x) & 511UL) << 21) | (((y) & 511UL) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0))
  794. #define ROM_PIXEL_FF 0xc0400UL
  795. class Poly {
  796. int x0, y0, x1, y1;
  797. int x[8], y[8];
  798. byte n;
  799. void restart() {
  800. n = 0;
  801. x0 = 16 * 480;
  802. x1 = 0;
  803. y0 = 16 * 272;
  804. y1 = 0;
  805. }
  806. void perim() {
  807. for (byte i = 0; i < n; i++)
  808. GD.Vertex2f(x[i], y[i]);
  809. GD.Vertex2f(x[0], y[0]);
  810. }
  811. public:
  812. void begin() {
  813. restart();
  814. GD.ColorMask(0,0,0,0);
  815. GD.StencilOp(KEEP, INVERT);
  816. GD.StencilFunc(ALWAYS, 255, 255);
  817. }
  818. void v(int _x, int _y) {
  819. x0 = min(x0, _x >> 4);
  820. x1 = max(x1, _x >> 4);
  821. y0 = min(y0, _y >> 4);
  822. y1 = max(y1, _y >> 4);
  823. x[n] = _x;
  824. y[n] = _y;
  825. n++;
  826. }
  827. void paint() {
  828. x0 = max(0, x0);
  829. y0 = max(0, y0);
  830. x1 = min(16 * 480, x1);
  831. y1 = min(16 * 272, y1);
  832. GD.ScissorXY(x0, y0);
  833. GD.ScissorSize(x1 - x0 + 1, y1 - y0 + 1);
  834. GD.Begin(EDGE_STRIP_B);
  835. perim();
  836. }
  837. void finish() {
  838. GD.ColorMask(1,1,1,1);
  839. GD.StencilFunc(EQUAL, 255, 255);
  840. GD.Begin(EDGE_STRIP_B);
  841. GD.Vertex2ii(0, 0);
  842. GD.Vertex2ii(511, 0);
  843. }
  844. void draw() {
  845. paint();
  846. finish();
  847. }
  848. void outline() {
  849. GD.Begin(LINE_STRIP);
  850. perim();
  851. }
  852. };
  853. #if SDCARD
  854. class Streamer {
  855. public:
  856. void begin(const char *rawsamples,
  857. uint16_t freq = 44100,
  858. byte format = ADPCM_SAMPLES,
  859. uint32_t _base = (0x40000UL - 8192), uint16_t size = 8192) {
  860. r.openfile(rawsamples);
  861. base = _base;
  862. mask = size - 1;
  863. wp = 0;
  864. for (byte i = 10; i; i--)
  865. feed();
  866. GD.sample(base, size, freq, format, 1);
  867. }
  868. int feed() {
  869. uint16_t rp = GD.rd32(REG_PLAYBACK_READPTR) - base;
  870. uint16_t freespace = mask & ((rp - 1) - wp);
  871. if (freespace >= 512) {
  872. // REPORT(base);
  873. // REPORT(rp);
  874. // REPORT(wp);
  875. // REPORT(freespace);
  876. // Serial.println();
  877. byte buf[512];
  878. // uint16_t n = min(512, r.size - r.offset);
  879. // n = (n + 3) & ~3; // force 32-bit alignment
  880. GD.__end();
  881. r.readsector(buf);
  882. GD.resume();
  883. GD.cmd_memwrite(base + wp, 512);
  884. GD.copyram(buf, 512);
  885. wp = (wp + 512) & mask;
  886. }
  887. return r.offset < r.size;
  888. }
  889. void progress(uint16_t &val, uint16_t &range) {
  890. uint32_t m = r.size;
  891. uint32_t p = min(r.offset, m);
  892. while (m > 0x10000) {
  893. m >>= 1;
  894. p >>= 1;
  895. }
  896. val = p;
  897. range = m;
  898. }
  899. private:
  900. Reader r;
  901. uint32_t base;
  902. uint16_t mask;
  903. uint16_t wp;
  904. };
  905. #else
  906. class Streamer {
  907. public:
  908. void begin(const char *rawsamples,
  909. uint16_t freq = 44100,
  910. byte format = ADPCM_SAMPLES,
  911. uint32_t _base = (0x40000UL - 4096), uint16_t size = 4096) {}
  912. int feed() {}
  913. void progress(uint16_t &val, uint16_t &range) {}
  914. };
  915. #endif
  916. #endif