GD2.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * Copyright (C) 2013-2018 by James Bowman <jamesb@excamera.com>
  3. * Gameduino 2/3 library for Arduino, Arduino Due, Teensy 3.2 and
  4. * ESP8266.
  5. */
  6. #ifndef _GD2_H_INCLUDED
  7. #define _GD2_H_INCLUDED
  8. #define GD2_VERSION "%VERSION"
  9. #if defined(RASPBERRY_PI) || defined(DUMPDEV)
  10. #include "wiring.h"
  11. #endif
  12. #include "Arduino.h"
  13. #include <stdarg.h>
  14. #define RGB(r, g, b) ((uint32_t)((((r) & 0xffL) << 16) | (((g) & 0xffL) << 8) | ((b) & 0xffL)))
  15. #define F8(x) (int((x) * 256L))
  16. #define F16(x) ((int32_t)((x) * 65536L))
  17. #define GD_CALIBRATE 1
  18. #define GD_TRIM 2
  19. #define GD_STORAGE 4
  20. #ifdef __SAM3X8E__
  21. #define __DUE__ 1
  22. #endif
  23. ////////////////////////////////////////////////////////////////////////
  24. // Decide if we want to compile in SDcard support
  25. //
  26. // For stock Arduino models: yes
  27. // Raspberry PI: no
  28. // Arduino Due: no
  29. //
  30. #if !defined(RASPBERRY_PI) && !defined(DUMPDEV)
  31. #define SDCARD 1
  32. #else
  33. #define SDCARD 0
  34. #endif
  35. #if defined(__DUE__)
  36. #define MOSI 11
  37. #define MISO 12
  38. #define SCK 13 // B.27
  39. class ASPI_t {
  40. public:
  41. void begin(void) {
  42. pinMode(MOSI, OUTPUT);
  43. pinMode(MISO, INPUT);
  44. pinMode(SCK, OUTPUT);
  45. digitalWrite(SCK, 0);
  46. // PIOB->PIO_PER = PIO_PB27;
  47. // PIOB->PIO_CODR = PIO_PB27;
  48. // PIOB->PIO_PUDR = PIO_PB27;
  49. }
  50. byte transfer(byte x ) {
  51. byte r = 0;
  52. for (byte i = 8; i; i--) {
  53. if (x & 0x80)
  54. PIOD->PIO_SODR = PIO_PD7;
  55. else
  56. PIOD->PIO_CODR = PIO_PD7;
  57. // digitalWrite(MOSI, (x >> 7) & 1);
  58. x <<= 1;
  59. // digitalWrite(SCK, 1);
  60. PIOB->PIO_SODR = PIO_PB27;
  61. r <<= 1;
  62. r |= digitalRead(MISO);
  63. // digitalWrite(SCK, 0);
  64. PIOB->PIO_CODR = PIO_PB27;
  65. }
  66. return r;
  67. }
  68. void transfer(byte*m, int s) {
  69. while (s--) {
  70. *m = transfer(*m);
  71. m++;
  72. }
  73. }
  74. };
  75. static class ASPI_t ASPI;
  76. #define SPI ASPI
  77. #endif
  78. #if defined(ARDUINO_STM32L4_BLACKICE)
  79. // BlackIce Board uses SPI1 on the Arduino header.
  80. #define SPI SPI1
  81. // Board Support:
  82. // JSON: http://www.hamnavoe.com/package_millerresearch_mystorm_index.json
  83. // Source: https://github.com/millerresearch/arduino-mystorm
  84. #endif
  85. #if SDCARD
  86. #if defined(VERBOSE) && (VERBOSE > 0)
  87. #define INFO(X) Serial.println((X))
  88. #if defined(RASPBERRY_PI)
  89. #define REPORT(VAR) fprintf(stderr, #VAR "=%d\n", (VAR))
  90. #else
  91. #define REPORT(VAR) (Serial.print(#VAR "="), Serial.print(VAR, DEC), Serial.print(' '), Serial.println(VAR, HEX))
  92. #endif
  93. #else
  94. #define INFO(X)
  95. #define REPORT(X)
  96. #endif
  97. struct dirent {
  98. char name[8];
  99. char ext[3];
  100. byte attribute;
  101. byte reserved[8];
  102. uint16_t cluster_hi; // FAT32 only
  103. uint16_t time;
  104. uint16_t date;
  105. uint16_t cluster;
  106. uint32_t size;
  107. };
  108. // https://www.sdcard.org/downloads/pls/simplified_specs/Part_1_Physical_Layer_Simplified_Specification_Ver_3.01_Final_100518.pdf
  109. // page 22
  110. // http://mac6.ma.psu.edu/space2008/RockSat/microController/sdcard_appnote_foust.pdf
  111. // http://elm-chan.org/docs/mmc/mmc_e.html
  112. // http://www.pjrc.com/tech/8051/ide/fat32.html
  113. #define FAT16 0
  114. #define FAT32 1
  115. #define DD
  116. class sdcard {
  117. public:
  118. void sel() {
  119. digitalWrite(pin, LOW);
  120. delay(1);
  121. }
  122. void desel() {
  123. digitalWrite(pin, HIGH);
  124. SPI.transfer(0xff); // force DO release
  125. }
  126. void sd_delay(byte n) {
  127. while (n--) {
  128. DD SPI.transfer(0xff);
  129. }
  130. }
  131. void cmd(byte cmd, uint32_t lba = 0, uint8_t crc = 0x95) {
  132. #if VERBOSE > 1
  133. Serial.print("cmd ");
  134. Serial.print(cmd, DEC);
  135. Serial.print(" ");
  136. Serial.print(lba, HEX);
  137. Serial.println();
  138. #endif
  139. sel();
  140. // DD SPI.transfer(0xff);
  141. DD SPI.transfer(0x40 | cmd);
  142. DD SPI.transfer(0xff & (lba >> 24));
  143. DD SPI.transfer(0xff & (lba >> 16));
  144. DD SPI.transfer(0xff & (lba >> 8));
  145. DD SPI.transfer(0xff & (lba));
  146. DD SPI.transfer(crc);
  147. // DD SPI.transfer(0xff);
  148. }
  149. byte response() {
  150. byte r;
  151. DD
  152. r = SPI.transfer(0xff);
  153. while (r & 0x80) {
  154. DD
  155. r = SPI.transfer(0xff);
  156. }
  157. return r;
  158. }
  159. byte R1() { // read response R1
  160. byte r = response();
  161. desel();
  162. SPI.transfer(0xff); // trailing byte
  163. return r;
  164. }
  165. byte sdR3(uint32_t &ocr) { // read response R3
  166. byte r = response();
  167. for (byte i = 4; i; i--)
  168. ocr = (ocr << 8) | SPI.transfer(0xff);
  169. SPI.transfer(0xff); // trailing byte
  170. desel();
  171. return r;
  172. }
  173. byte sdR7() { // read response R3
  174. byte r = response();
  175. for (byte i = 4; i; i--)
  176. // Serial.println(SPI.transfer(0xff), HEX);
  177. SPI.transfer(0xff);
  178. desel();
  179. return r;
  180. }
  181. void appcmd(byte cc, uint32_t lba = 0) {
  182. cmd(55); R1();
  183. cmd(cc, lba);
  184. }
  185. void begin(byte p) {
  186. byte type_code;
  187. byte sdhc;
  188. pin = p;
  189. pinMode(pin, OUTPUT);
  190. #if !defined(__DUE__) && !defined(TEENSYDUINO) && !defined(ARDUINO_ARCH_STM32L4)
  191. SPI.setClockDivider(SPI_CLOCK_DIV64);
  192. #endif
  193. desel();
  194. // for (;;) SPI.transfer(0xff);
  195. delay(50); // wait for boot
  196. sd_delay(10); // deselected, 80 pulses
  197. INFO("Attempting card reset... ");
  198. byte r1;
  199. static int attempts;
  200. attempts = 0;
  201. do { // reset, enter idle
  202. cmd(0);
  203. while ((r1 = SPI.transfer(0xff)) & 0x80)
  204. if (++attempts == 1000)
  205. goto finished;
  206. desel();
  207. SPI.transfer(0xff); // trailing byte
  208. REPORT(r1);
  209. } while (r1 != 1);
  210. INFO("reset ok\n");
  211. sdhc = 0;
  212. cmd(8, 0x1aa, 0x87);
  213. r1 = sdR7();
  214. sdhc = (r1 == 1);
  215. REPORT(sdhc);
  216. INFO("Sending card init command");
  217. attempts = 0;
  218. while (1) {
  219. appcmd(41, sdhc ? (1UL << 30) : 0); // card init
  220. r1 = R1();
  221. #if VERBOSE
  222. Serial.println(r1, HEX);
  223. #endif
  224. if ((r1 & 1) == 0)
  225. break;
  226. if (++attempts == 300)
  227. goto finished;
  228. delay(1);
  229. }
  230. INFO("OK");
  231. if (sdhc) {
  232. uint32_t OCR = 0;
  233. for (int i = 10; i; i--) {
  234. cmd(58);
  235. sdR3(OCR);
  236. REPORT(OCR);
  237. }
  238. ccs = 1UL & (OCR >> 30);
  239. } else {
  240. ccs = 0;
  241. }
  242. REPORT(ccs);
  243. // Test point: dump sector 0 to serial.
  244. // should see first 512 bytes of card, ending 55 AA.
  245. #if 0
  246. cmd17(0);
  247. for (int i = 0; i < 512; i++) {
  248. delay(10);
  249. byte b = SPI.transfer(0xff);
  250. Serial.print(b, HEX);
  251. Serial.print(' ');
  252. if ((i & 15) == 15)
  253. Serial.println();
  254. }
  255. desel();
  256. for (;;);
  257. #endif
  258. #if !defined(__DUE__) && !defined(ESP8266) && !defined(ARDUINO_ARCH_STM32L4)
  259. SPI.setClockDivider(SPI_CLOCK_DIV2);
  260. SPSR = (1 << SPI2X);
  261. #endif
  262. #if defined(ESP8266)
  263. SPI.setFrequency(40000000L);
  264. #endif
  265. type_code = rd(0x1be + 0x4);
  266. switch (type_code) {
  267. default:
  268. type = FAT16;
  269. break;
  270. case 0x0b:
  271. case 0x0c:
  272. type = FAT32;
  273. break;
  274. }
  275. REPORT(type_code);
  276. o_partition = 512L * rd4(0x1be + 0x8);
  277. sectors_per_cluster = rd(o_partition + 0xd);
  278. reserved_sectors = rd2(o_partition + 0xe);
  279. cluster_size = 512L * sectors_per_cluster;
  280. REPORT(sectors_per_cluster);
  281. // Serial.println("Bytes per sector: %d\n", rd2(o_partition + 0xb));
  282. // Serial.println("Sectors per cluster: %d\n", sectors_per_cluster);
  283. if (type == FAT16) {
  284. max_root_dir_entries = rd2(o_partition + 0x11);
  285. sectors_per_fat = rd2(o_partition + 0x16);
  286. o_fat = o_partition + 512L * reserved_sectors;
  287. o_root = o_fat + (2 * 512L * sectors_per_fat);
  288. // data area starts with cluster 2, so offset it here
  289. o_data = o_root + (max_root_dir_entries * 32L) - (2L * cluster_size);
  290. } else {
  291. uint32_t sectors_per_fat = rd4(o_partition + 0x24);
  292. root_dir_first_cluster = rd4(o_partition + 0x2c);
  293. uint32_t fat_begin_lba = (o_partition >> 9) + reserved_sectors;
  294. uint32_t cluster_begin_lba = (o_partition >> 9) + reserved_sectors + (2 * sectors_per_fat);
  295. o_fat = 512L * fat_begin_lba;
  296. o_root = (512L * (cluster_begin_lba + (root_dir_first_cluster - 2) * sectors_per_cluster));
  297. o_data = (512L * (cluster_begin_lba - 2 * sectors_per_cluster));
  298. }
  299. finished:
  300. INFO("finished");
  301. ;
  302. }
  303. void cmd17(uint32_t off) {
  304. if (ccs)
  305. cmd(17, off >> 9);
  306. else
  307. cmd(17, off & ~511L);
  308. R1();
  309. sel();
  310. while (SPI.transfer(0xff) != 0xfe)
  311. ;
  312. }
  313. void rdn(byte *d, uint32_t off, uint16_t n) {
  314. cmd17(off);
  315. uint16_t i;
  316. uint16_t bo = (off & 511);
  317. for (i = 0; i < bo; i++)
  318. SPI.transfer(0xff);
  319. for (i = 0; i < n; i++)
  320. *d++ = SPI.transfer(0xff);
  321. for (i = 0; i < (514 - bo - n); i++)
  322. SPI.transfer(0xff);
  323. desel();
  324. }
  325. uint32_t rd4(uint32_t off) {
  326. uint32_t r;
  327. rdn((byte*)&r, off, sizeof(r));
  328. return r;
  329. }
  330. uint16_t rd2(uint32_t off) {
  331. uint16_t r;
  332. rdn((byte*)&r, off, sizeof(r));
  333. return r;
  334. }
  335. byte rd(uint32_t off) {
  336. byte r;
  337. rdn((byte*)&r, off, sizeof(r));
  338. return r;
  339. }
  340. byte pin;
  341. byte ccs;
  342. byte type;
  343. uint16_t sectors_per_cluster;
  344. uint16_t reserved_sectors;
  345. uint16_t max_root_dir_entries;
  346. uint16_t sectors_per_fat;
  347. uint16_t cluster_size;
  348. uint32_t root_dir_first_cluster;
  349. // These are all linear addresses, hence the o_ prefix
  350. uint32_t o_partition;
  351. uint32_t o_fat;
  352. uint32_t o_root;
  353. uint32_t o_data;
  354. };
  355. static void dos83(byte dst[11], const char *ps)
  356. {
  357. byte i = 0;
  358. while (*ps) {
  359. if (*ps != '.')
  360. dst[i++] = toupper(*ps);
  361. else {
  362. while (i < 8)
  363. dst[i++] = ' ';
  364. }
  365. ps++;
  366. }
  367. while (i < 11)
  368. dst[i++] = ' ';
  369. }
  370. #else
  371. class sdcard {
  372. public:
  373. void begin(int p) {};
  374. };
  375. #endif
  376. ////////////////////////////////////////////////////////////////////////
  377. class xy {
  378. public:
  379. int x, y;
  380. void set(int _x, int _y);
  381. void rmove(int distance, int angle);
  382. int angleto(class xy &other);
  383. void draw(byte offset = 0);
  384. void rotate(int angle);
  385. int onscreen(void);
  386. class xy operator<<=(int d);
  387. class xy operator+=(class xy &other);
  388. class xy operator-=(class xy &other);
  389. long operator*(class xy &other);
  390. class xy operator*=(int);
  391. int nearer_than(int distance, xy &other);
  392. };
  393. class Bitmap {
  394. public:
  395. xy size, center;
  396. uint32_t source;
  397. uint8_t format;
  398. int8_t handle;
  399. void fromtext(int font, const char* s);
  400. void fromfile(const char *filename, int format = 7);
  401. void bind(uint8_t handle);
  402. void wallpaper();
  403. void draw(int x, int y, int16_t angle = 0);
  404. void draw(const xy &pos, int16_t angle = 0);
  405. private:
  406. void defaults(uint8_t f);
  407. void setup(void);
  408. };
  409. class Bitmap __fromatlas(uint32_t addr);
  410. ////////////////////////////////////////////////////////////////////////
  411. class GDClass {
  412. public:
  413. int w, h;
  414. uint32_t loadptr;
  415. void begin(uint8_t options = (GD_CALIBRATE | GD_TRIM | GD_STORAGE));
  416. uint16_t random();
  417. uint16_t random(uint16_t n);
  418. uint16_t random(uint16_t n0, uint16_t n1);
  419. void seed(uint16_t n);
  420. int16_t rsin(int16_t r, uint16_t th);
  421. int16_t rcos(int16_t r, uint16_t th);
  422. void polar(int &x, int &y, int16_t r, uint16_t th);
  423. uint16_t atan2(int16_t y, int16_t x);
  424. #if !defined(ESP8266)
  425. void copy(const PROGMEM uint8_t *src, int count);
  426. #else
  427. void copy(const uint8_t *src, int count);
  428. #endif
  429. void copyram(byte *src, int count);
  430. void self_calibrate(void);
  431. void swap(void);
  432. void flush(void);
  433. void finish(void);
  434. void play(uint8_t instrument, uint8_t note = 0);
  435. void sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop = 0);
  436. void get_inputs(void);
  437. void get_accel(int &x, int &y, int &z);
  438. struct {
  439. uint16_t track_tag;
  440. uint16_t track_val;
  441. uint16_t rz;
  442. uint16_t __dummy_1;
  443. int16_t y;
  444. int16_t x;
  445. int16_t tag_y;
  446. int16_t tag_x;
  447. uint8_t tag;
  448. uint8_t ptag;
  449. uint8_t touching;
  450. xy xytouch;
  451. } inputs;
  452. void AlphaFunc(byte func, byte ref);
  453. void Begin(byte prim);
  454. void BitmapHandle(byte handle);
  455. void BitmapLayout(byte format, uint16_t linestride, uint16_t height);
  456. void BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height);
  457. void BitmapSource(uint32_t addr);
  458. void BitmapTransformA(int32_t a);
  459. void BitmapTransformB(int32_t b);
  460. void BitmapTransformC(int32_t c);
  461. void BitmapTransformD(int32_t d);
  462. void BitmapTransformE(int32_t e);
  463. void BitmapTransformF(int32_t f);
  464. void BlendFunc(byte src, byte dst);
  465. void Call(uint16_t dest);
  466. void Cell(byte cell);
  467. void ClearColorA(byte alpha);
  468. void ClearColorRGB(byte red, byte green, byte blue);
  469. void ClearColorRGB(uint32_t rgb);
  470. void Clear(byte c, byte s, byte t);
  471. void Clear(void);
  472. void ClearStencil(byte s);
  473. void ClearTag(byte s);
  474. void ColorA(byte alpha);
  475. void ColorMask(byte r, byte g, byte b, byte a);
  476. void ColorRGB(byte red, byte green, byte blue);
  477. void ColorRGB(uint32_t rgb);
  478. void Display(void);
  479. void End(void);
  480. void Jump(uint16_t dest);
  481. void LineWidth(uint16_t width);
  482. void Macro(byte m);
  483. void PointSize(uint16_t size);
  484. void RestoreContext(void);
  485. void Return(void);
  486. void SaveContext(void);
  487. void ScissorSize(uint16_t width, uint16_t height);
  488. void ScissorXY(uint16_t x, uint16_t y);
  489. void StencilFunc(byte func, byte ref, byte mask);
  490. void StencilMask(byte mask);
  491. void StencilOp(byte sfail, byte spass);
  492. void TagMask(byte mask);
  493. void Tag(byte s);
  494. void Vertex2f(int16_t x, int16_t y);
  495. void Vertex2ii(uint16_t x, uint16_t y, byte handle = 0, byte cell = 0);
  496. void VertexFormat(byte frac);
  497. void BitmapLayoutH(byte linestride, byte height);
  498. void BitmapSizeH(byte width, byte height);
  499. void PaletteSource(uint32_t addr);
  500. void VertexTranslateX(uint32_t x);
  501. void VertexTranslateY(uint32_t y);
  502. void Nop(void);
  503. // Higher-level graphics commands
  504. void cmd_append(uint32_t ptr, uint32_t num);
  505. void cmd_bgcolor(uint32_t c);
  506. void cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s);
  507. void cmd_calibrate(void);
  508. 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);
  509. void cmd_coldstart(void);
  510. void cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val);
  511. void cmd_dlstart(void);
  512. void cmd_fgcolor(uint32_t c);
  513. 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);
  514. void cmd_getmatrix(void);
  515. void cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h);
  516. void cmd_getptr(void);
  517. void cmd_gradcolor(uint32_t c);
  518. void cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1);
  519. void cmd_inflate(uint32_t ptr);
  520. void cmd_interrupt(uint32_t ms);
  521. void cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s);
  522. void cmd_loadidentity(void);
  523. void cmd_loadimage(uint32_t ptr, int32_t options);
  524. void cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num);
  525. void cmd_memset(uint32_t ptr, byte value, uint32_t num);
  526. uint32_t cmd_memcrc(uint32_t ptr, uint32_t num);
  527. void cmd_memwrite(uint32_t ptr, uint32_t num);
  528. void cmd_regwrite(uint32_t ptr, uint32_t val);
  529. void cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n);
  530. void cmd_progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t range);
  531. void cmd_regread(uint32_t ptr);
  532. void cmd_rotate(int32_t a);
  533. void cmd_scale(int32_t sx, int32_t sy);
  534. void cmd_screensaver(void);
  535. 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);
  536. void cmd_setfont(byte font, uint32_t ptr);
  537. void cmd_setmatrix(void);
  538. void cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format);
  539. void cmd_slider(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range);
  540. void cmd_snapshot(uint32_t ptr);
  541. void cmd_spinner(int16_t x, int16_t y, byte style, byte scale);
  542. void cmd_stop(void);
  543. void cmd_swap(void);
  544. void cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s);
  545. void cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s);
  546. void cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag);
  547. void cmd_translate(int32_t tx, int32_t ty);
  548. void cmd_playvideo(int32_t options);
  549. void cmd_romfont(uint32_t font, uint32_t romslot);
  550. void cmd_mediafifo(uint32_t ptr, uint32_t size);
  551. void cmd_setbase(uint32_t b);
  552. void cmd_videoframe(uint32_t dst, uint32_t ptr);
  553. void cmd_snapshot2(uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, int16_t w, int16_t h);
  554. void cmd_setfont2(uint32_t font, uint32_t ptr, uint32_t firstchar);
  555. void cmd_setrotate(uint32_t r);
  556. void cmd_videostart();
  557. void cmd_setbitmap(uint32_t source, uint16_t fmt, uint16_t w, uint16_t h);
  558. void cmd_sync();
  559. byte rd(uint32_t addr);
  560. void wr(uint32_t addr, uint8_t v);
  561. uint16_t rd16(uint32_t addr);
  562. void wr16(uint32_t addr, uint16_t v);
  563. uint32_t rd32(uint32_t addr);
  564. void wr32(uint32_t addr, uint32_t v);
  565. void wr_n(uint32_t addr, byte *src, uint32_t n);
  566. void cmd32(uint32_t b);
  567. void bulkrd(uint32_t a);
  568. void resume(void);
  569. void __end(void);
  570. void reset(void);
  571. void dumpscreen(void);
  572. byte load(const char *filename, void (*progress)(long, long) = NULL);
  573. void safeload(const char *filename);
  574. void alert(const char *message);
  575. void textsize(int &w, int &h, int font, const char *s);
  576. sdcard SD;
  577. void storage(void);
  578. void tune(void);
  579. private:
  580. static void cFFFFFF(byte v);
  581. static void cI(uint32_t);
  582. static void ci(int32_t);
  583. static void cH(uint16_t);
  584. static void ch(int16_t);
  585. static void cs(const char *);
  586. static void fmtcmd(const char *fmt, ...);
  587. static void align(byte n);
  588. void cmdbyte(uint8_t b);
  589. uint32_t measure_freq(void);
  590. uint32_t rseed;
  591. };
  592. extern GDClass GD;
  593. extern byte ft8xx_model;
  594. #if SDCARD
  595. class Reader {
  596. public:
  597. int openfile(const char *filename) {
  598. int i = 0;
  599. byte dosname[11];
  600. dirent de;
  601. dos83(dosname, filename);
  602. do {
  603. GD.SD.rdn((byte*)&de, GD.SD.o_root + i * 32, sizeof(de));
  604. // Serial.println(de.name);
  605. if (0 == memcmp(de.name, dosname, 11)) {
  606. begin(de);
  607. return 1;
  608. }
  609. i++;
  610. } while (de.name[0]);
  611. return 0;
  612. }
  613. void begin(dirent &de) {
  614. nseq = 0;
  615. size = de.size;
  616. cluster0 = de.cluster;
  617. if (GD.SD.type == FAT32)
  618. cluster0 |= ((long)de.cluster_hi << 16);
  619. rewind();
  620. }
  621. void rewind(void) {
  622. cluster = cluster0;
  623. sector = 0;
  624. offset = 0;
  625. }
  626. void nextcluster() {
  627. if (GD.SD.type == FAT16)
  628. cluster = GD.SD.rd2(GD.SD.o_fat + 2 * cluster);
  629. else
  630. cluster = GD.SD.rd4(GD.SD.o_fat + 4 * cluster);
  631. #if VERBOSE
  632. Serial.print("nextcluster=");
  633. Serial.println(cluster, DEC);
  634. #endif
  635. }
  636. void fetch512(byte *dst) {
  637. #if defined(__DUE__) || defined(TEENSYDUINO) || defined(ESP8266) || 1
  638. #if defined(ESP8266)
  639. SPI.transferBytes(NULL, dst, 512);
  640. #else
  641. // for (int i = 0; i < 512; i++) *dst++ = SPI.transfer(0xff);
  642. memset(dst, 0xff, 512); SPI.transfer(dst, 512);
  643. #endif
  644. SPI.transfer(0xff); // consume CRC
  645. SPI.transfer(0xff);
  646. #else
  647. SPDR = 0xff;
  648. asm volatile("nop"); while (!(SPSR & _BV(SPIF))) ;
  649. for (int i = 0; i < 512; i++) {
  650. while (!(SPSR & _BV(SPIF))) ;
  651. asm volatile("nop");
  652. asm volatile("nop");
  653. asm volatile("nop");
  654. asm volatile("nop");
  655. asm volatile("nop");
  656. asm volatile("nop");
  657. asm volatile("nop");
  658. asm volatile("nop");
  659. asm volatile("nop");
  660. asm volatile("nop");
  661. asm volatile("nop");
  662. *dst++ = SPDR;
  663. SPDR = 0xff;
  664. }
  665. asm volatile("nop"); while (!(SPSR & _BV(SPIF))) ;
  666. SPI.transfer(0xff);
  667. #endif
  668. GD.SD.desel();
  669. }
  670. void nextcluster2(byte *dst) {
  671. if (nseq) {
  672. nseq--;
  673. cluster++;
  674. return;
  675. }
  676. uint32_t off = GD.SD.o_fat + 4 * cluster;
  677. GD.SD.cmd17(off & ~511L);
  678. fetch512(dst);
  679. int i = off & 511;
  680. cluster = *(uint32_t*)&dst[i];
  681. nseq = 0;
  682. for (uint32_t c = cluster;
  683. (i < 512) && *(uint32_t*)&dst[i] == c;
  684. i += 4, c++)
  685. nseq++;
  686. }
  687. void skipcluster() {
  688. nextcluster();
  689. offset += GD.SD.cluster_size;
  690. }
  691. void skipsector() {
  692. if (sector == GD.SD.sectors_per_cluster) {
  693. sector = 0;
  694. nextcluster();
  695. }
  696. sector++;
  697. offset += 512;
  698. }
  699. void seek(uint32_t o) {
  700. union {
  701. uint8_t buf[512];
  702. uint32_t fat32[128];
  703. uint16_t fat16[256];
  704. };
  705. uint32_t co = ~0;
  706. if (o < offset)
  707. rewind();
  708. while (offset < o) {
  709. if ((sector == GD.SD.sectors_per_cluster) && ((o - offset) > (long)GD.SD.cluster_size)) {
  710. uint32_t o;
  711. if (GD.SD.type == FAT16)
  712. o = (GD.SD.o_fat + 2 * cluster) & ~511;
  713. else
  714. o = (GD.SD.o_fat + 4 * cluster) & ~511;
  715. if (o != co) {
  716. GD.SD.rdn(buf, o, 512);
  717. co = o;
  718. }
  719. cluster = fat32[cluster & 127];
  720. offset += GD.SD.cluster_size;
  721. } else
  722. skipsector();
  723. }
  724. }
  725. void readsector(byte *dst) {
  726. if (sector == GD.SD.sectors_per_cluster) {
  727. sector = 0;
  728. nextcluster2(dst);
  729. }
  730. REPORT(cluster);
  731. uint32_t off = GD.SD.o_data + ((long)GD.SD.cluster_size * cluster) + (512L * sector);
  732. REPORT(off);
  733. GD.SD.cmd17(off & ~511L);
  734. REPORT(off);
  735. sector++;
  736. offset += 512;
  737. fetch512(dst);
  738. }
  739. int eof(void) {
  740. return size <= offset;
  741. }
  742. uint32_t cluster, cluster0;
  743. uint32_t offset;
  744. uint32_t size;
  745. byte sector;
  746. byte nseq;
  747. };
  748. #endif
  749. typedef struct {
  750. byte handle;
  751. uint16_t w, h;
  752. uint16_t size;
  753. } shape_t;
  754. // convert integer pixels to subpixels
  755. #define PIXELS(x) int((x) * 16)
  756. // Convert degrees to Furmans
  757. #define DEGREES(n) ((65536L * (n)) / 360)
  758. #define NEVER 0
  759. #define LESS 1
  760. #define LEQUAL 2
  761. #define GREATER 3
  762. #define GEQUAL 4
  763. #define EQUAL 5
  764. #define NOTEQUAL 6
  765. #define ALWAYS 7
  766. #define ARGB1555 0
  767. #define L1 1
  768. #define L4 2
  769. #define L8 3
  770. #define RGB332 4
  771. #define ARGB2 5
  772. #define ARGB4 6
  773. #define RGB565 7
  774. #define PALETTED 8
  775. #define TEXT8X8 9
  776. #define TEXTVGA 10
  777. #define BARGRAPH 11
  778. #define L2 17
  779. #define NEAREST 0
  780. #define BILINEAR 1
  781. #define BORDER 0
  782. #define REPEAT 1
  783. #define KEEP 1
  784. #define REPLACE 2
  785. #define INCR 3
  786. #define DECR 4
  787. #define INVERT 5
  788. #define DLSWAP_DONE 0
  789. #define DLSWAP_LINE 1
  790. #define DLSWAP_FRAME 2
  791. #define INT_SWAP 1
  792. #define INT_TOUCH 2
  793. #define INT_TAG 4
  794. #define INT_SOUND 8
  795. #define INT_PLAYBACK 16
  796. #define INT_CMDEMPTY 32
  797. #define INT_CMDFLAG 64
  798. #define INT_CONVCOMPLETE 128
  799. #define TOUCHMODE_OFF 0
  800. #define TOUCHMODE_ONESHOT 1
  801. #define TOUCHMODE_FRAME 2
  802. #define TOUCHMODE_CONTINUOUS 3
  803. #define ZERO 0
  804. #define ONE 1
  805. #define SRC_ALPHA 2
  806. #define DST_ALPHA 3
  807. #define ONE_MINUS_SRC_ALPHA 4
  808. #define ONE_MINUS_DST_ALPHA 5
  809. #define BITMAPS 1
  810. #define POINTS 2
  811. #define LINES 3
  812. #define LINE_STRIP 4
  813. #define EDGE_STRIP_R 5
  814. #define EDGE_STRIP_L 6
  815. #define EDGE_STRIP_A 7
  816. #define EDGE_STRIP_B 8
  817. #define RECTS 9
  818. #define OPT_MONO 1
  819. #define OPT_NODL 2
  820. #define OPT_FLAT 256
  821. #define OPT_CENTERX 512
  822. #define OPT_CENTERY 1024
  823. #define OPT_CENTER (OPT_CENTERX | OPT_CENTERY)
  824. #define OPT_NOBACK 4096
  825. #define OPT_NOTICKS 8192
  826. #define OPT_NOHM 16384
  827. #define OPT_NOPOINTER 16384
  828. #define OPT_NOSECS 32768
  829. #define OPT_NOHANDS 49152
  830. #define OPT_RIGHTX 2048
  831. #define OPT_SIGNED 256
  832. #define OPT_SOUND 32
  833. #define OPT_NOTEAR 4
  834. #define OPT_FULLSCREEN 8
  835. #define OPT_MEDIAFIFO 16
  836. #define LINEAR_SAMPLES 0
  837. #define ULAW_SAMPLES 1
  838. #define ADPCM_SAMPLES 2
  839. // 'instrument' argument to GD.play()
  840. #define SILENCE 0x00
  841. #define SQUAREWAVE 0x01
  842. #define SINEWAVE 0x02
  843. #define SAWTOOTH 0x03
  844. #define TRIANGLE 0x04
  845. #define BEEPING 0x05
  846. #define ALARM 0x06
  847. #define WARBLE 0x07
  848. #define CAROUSEL 0x08
  849. #define PIPS(n) (0x0f + (n))
  850. #define HARP 0x40
  851. #define XYLOPHONE 0x41
  852. #define TUBA 0x42
  853. #define GLOCKENSPIEL 0x43
  854. #define ORGAN 0x44
  855. #define TRUMPET 0x45
  856. #define PIANO 0x46
  857. #define CHIMES 0x47
  858. #define MUSICBOX 0x48
  859. #define BELL 0x49
  860. #define CLICK 0x50
  861. #define SWITCH 0x51
  862. #define COWBELL 0x52
  863. #define NOTCH 0x53
  864. #define HIHAT 0x54
  865. #define KICKDRUM 0x55
  866. #define POP 0x56
  867. #define CLACK 0x57
  868. #define CHACK 0x58
  869. #define MUTE 0x60
  870. #define UNMUTE 0x61
  871. #define RAM_PAL 1056768UL
  872. #define RAM_CMD (ft8xx_model ? 0x308000UL : 0x108000UL)
  873. #define RAM_DL (ft8xx_model ? 0x300000UL : 0x100000UL)
  874. #define REG_CLOCK (ft8xx_model ? 0x302008UL : 0x102408UL)
  875. #define REG_CMD_DL (ft8xx_model ? 0x302100UL : 0x1024ecUL)
  876. #define REG_CMD_READ (ft8xx_model ? 0x3020f8UL : 0x1024e4UL)
  877. #define REG_CMD_WRITE (ft8xx_model ? 0x3020fcUL : 0x1024e8UL)
  878. #define REG_CPURESET (ft8xx_model ? 0x302020UL : 0x10241cUL)
  879. #define REG_CSPREAD (ft8xx_model ? 0x302068UL : 0x102464UL)
  880. #define REG_DITHER (ft8xx_model ? 0x302060UL : 0x10245cUL)
  881. #define REG_DLSWAP (ft8xx_model ? 0x302054UL : 0x102450UL)
  882. #define REG_FRAMES (ft8xx_model ? 0x302004UL : 0x102404UL)
  883. #define REG_FREQUENCY (ft8xx_model ? 0x30200cUL : 0x10240cUL)
  884. #define REG_GPIO (ft8xx_model ? 0x302094UL : 0x102490UL)
  885. #define REG_GPIO_DIR (ft8xx_model ? 0x302090UL : 0x10248cUL)
  886. #define REG_HCYCLE (ft8xx_model ? 0x30202cUL : 0x102428UL)
  887. #define REG_HOFFSET (ft8xx_model ? 0x302030UL : 0x10242cUL)
  888. #define REG_HSIZE (ft8xx_model ? 0x302034UL : 0x102430UL)
  889. #define REG_HSYNC0 (ft8xx_model ? 0x302038UL : 0x102434UL)
  890. #define REG_HSYNC1 (ft8xx_model ? 0x30203cUL : 0x102438UL)
  891. #define REG_ID (ft8xx_model ? 0x302000UL : 0x102400UL)
  892. #define REG_INT_EN (ft8xx_model ? 0x3020acUL : 0x10249cUL)
  893. #define REG_INT_FLAGS (ft8xx_model ? 0x3020a8UL : 0x102498UL)
  894. #define REG_INT_MASK (ft8xx_model ? 0x3020b0UL : 0x1024a0UL)
  895. #define REG_MACRO_0 (ft8xx_model ? 0x3020d8UL : 0x1024c8UL)
  896. #define REG_MACRO_1 (ft8xx_model ? 0x3020dcUL : 0x1024ccUL)
  897. #define REG_OUTBITS (ft8xx_model ? 0x30205cUL : 0x102458UL)
  898. #define REG_PCLK (ft8xx_model ? 0x302070UL : 0x10246cUL)
  899. #define REG_PCLK_POL (ft8xx_model ? 0x30206cUL : 0x102468UL)
  900. #define REG_PLAY (ft8xx_model ? 0x30208cUL : 0x102488UL)
  901. #define REG_PLAYBACK_FORMAT (ft8xx_model ? 0x3020c4UL : 0x1024b4UL)
  902. #define REG_PLAYBACK_FREQ (ft8xx_model ? 0x3020c0UL : 0x1024b0UL)
  903. #define REG_PLAYBACK_LENGTH (ft8xx_model ? 0x3020b8UL : 0x1024a8UL)
  904. #define REG_PLAYBACK_LOOP (ft8xx_model ? 0x3020c8UL : 0x1024b8UL)
  905. #define REG_PLAYBACK_PLAY (ft8xx_model ? 0x3020ccUL : 0x1024bcUL)
  906. #define REG_PLAYBACK_READPTR (ft8xx_model ? 0x3020bcUL : 0x1024acUL)
  907. #define REG_PLAYBACK_START (ft8xx_model ? 0x3020b4UL : 0x1024a4UL)
  908. #define REG_PWM_DUTY (ft8xx_model ? 0x3020d4UL : 0x1024c4UL)
  909. #define REG_PWM_HZ (ft8xx_model ? 0x3020d0UL : 0x1024c0UL)
  910. #define REG_ROTATE (ft8xx_model ? 0x302058UL : 0x102454UL)
  911. #define REG_SOUND (ft8xx_model ? 0x302088UL : 0x102484UL)
  912. #define REG_SWIZZLE (ft8xx_model ? 0x302064UL : 0x102460UL)
  913. #define REG_TAG (ft8xx_model ? 0x30207cUL : 0x102478UL)
  914. #define REG_TAG_X (ft8xx_model ? 0x302074UL : 0x102470UL)
  915. #define REG_TAG_Y (ft8xx_model ? 0x302078UL : 0x102474UL)
  916. #define REG_TOUCH_ADC_MODE (ft8xx_model ? 0x302108UL : 0x1024f4UL)
  917. #define REG_TOUCH_CHARGE (ft8xx_model ? 0x30210cUL : 0x1024f8UL)
  918. #define REG_TOUCH_DIRECT_XY (ft8xx_model ? 0x30218cUL : 0x102574UL)
  919. #define REG_TOUCH_DIRECT_Z1Z2 (ft8xx_model ? 0x302190UL : 0x102578UL)
  920. #define REG_TOUCH_MODE (ft8xx_model ? 0x302104UL : 0x1024f0UL)
  921. #define REG_TOUCH_OVERSAMPLE (ft8xx_model ? 0x302114UL : 0x102500UL)
  922. #define REG_TOUCH_RAW_XY (ft8xx_model ? 0x30211cUL : 0x102508UL)
  923. #define REG_TOUCH_RZ (ft8xx_model ? 0x302120UL : 0x10250cUL)
  924. #define REG_TOUCH_RZTHRESH (ft8xx_model ? 0x302118UL : 0x102504UL)
  925. #define REG_TOUCH_SCREEN_XY (ft8xx_model ? 0x302124UL : 0x102510UL)
  926. #define REG_TOUCH_SETTLE (ft8xx_model ? 0x302110UL : 0x1024fcUL)
  927. #define REG_TOUCH_TAG (ft8xx_model ? 0x30212cUL : 0x102518UL)
  928. #define REG_TOUCH_TAG_XY (ft8xx_model ? 0x302128UL : 0x102514UL)
  929. #define REG_TOUCH_TRANSFORM_A (ft8xx_model ? 0x302150UL : 0x10251cUL)
  930. #define REG_TOUCH_TRANSFORM_B (ft8xx_model ? 0x302154UL : 0x102520UL)
  931. #define REG_TOUCH_TRANSFORM_C (ft8xx_model ? 0x302158UL : 0x102524UL)
  932. #define REG_TOUCH_TRANSFORM_D (ft8xx_model ? 0x30215cUL : 0x102528UL)
  933. #define REG_TOUCH_TRANSFORM_E (ft8xx_model ? 0x302160UL : 0x10252cUL)
  934. #define REG_TOUCH_TRANSFORM_F (ft8xx_model ? 0x302164UL : 0x102530UL)
  935. #define REG_TRACKER (ft8xx_model ? 0x309000UL : 0x109000UL)
  936. #define REG_TRIM (ft8xx_model ? 0x302180UL : 0x10256cUL)
  937. #define REG_VCYCLE (ft8xx_model ? 0x302040UL : 0x10243cUL)
  938. #define REG_VOFFSET (ft8xx_model ? 0x302044UL : 0x102440UL)
  939. #define REG_VOL_PB (ft8xx_model ? 0x302080UL : 0x10247cUL)
  940. #define REG_VOL_SOUND (ft8xx_model ? 0x302084UL : 0x102480UL)
  941. #define REG_VSIZE (ft8xx_model ? 0x302048UL : 0x102444UL)
  942. #define REG_VSYNC0 (ft8xx_model ? 0x30204cUL : 0x102448UL)
  943. #define REG_VSYNC1 (ft8xx_model ? 0x302050UL : 0x10244cUL)
  944. #define FONT_ROOT (ft8xx_model ? 0x2ffffcUL : 0x0ffffcUL)
  945. // FT81x only registers
  946. #define REG_CMDB_SPACE 0x302574UL
  947. #define REG_CMDB_WRITE 0x302578UL
  948. #define REG_MEDIAFIFO_READ 0x309014UL
  949. #define REG_MEDIAFIFO_WRITE 0x309018UL
  950. #define VERTEX2II(x, y, handle, cell) \
  951. ((2UL << 30) | (((x) & 511UL) << 21) | (((y) & 511UL) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0))
  952. #define ROM_PIXEL_FF 0xc0400UL
  953. class Poly {
  954. int x0, y0, x1, y1;
  955. int x[8], y[8];
  956. byte n;
  957. void restart() {
  958. n = 0;
  959. x0 = 16 * 480;
  960. x1 = 0;
  961. y0 = 16 * 272;
  962. y1 = 0;
  963. }
  964. void perim() {
  965. for (byte i = 0; i < n; i++)
  966. GD.Vertex2f(x[i], y[i]);
  967. GD.Vertex2f(x[0], y[0]);
  968. }
  969. public:
  970. void begin() {
  971. restart();
  972. GD.ColorMask(0,0,0,0);
  973. GD.StencilOp(KEEP, INVERT);
  974. GD.StencilFunc(ALWAYS, 255, 255);
  975. }
  976. void v(int _x, int _y) {
  977. x0 = min(x0, _x >> 4);
  978. x1 = max(x1, _x >> 4);
  979. y0 = min(y0, _y >> 4);
  980. y1 = max(y1, _y >> 4);
  981. x[n] = _x;
  982. y[n] = _y;
  983. n++;
  984. }
  985. void paint() {
  986. x0 = max(0, x0);
  987. y0 = max(0, y0);
  988. x1 = min(16 * 480, x1);
  989. y1 = min(16 * 272, y1);
  990. GD.ScissorXY(x0, y0);
  991. GD.ScissorSize(x1 - x0 + 1, y1 - y0 + 1);
  992. GD.Begin(EDGE_STRIP_B);
  993. perim();
  994. }
  995. void finish() {
  996. GD.ColorMask(1,1,1,1);
  997. GD.StencilFunc(EQUAL, 255, 255);
  998. GD.Begin(EDGE_STRIP_R);
  999. GD.Vertex2f(0, 0);
  1000. GD.Vertex2f(0, PIXELS(GD.h));
  1001. }
  1002. void draw() {
  1003. paint();
  1004. finish();
  1005. }
  1006. void outline() {
  1007. GD.Begin(LINE_STRIP);
  1008. perim();
  1009. }
  1010. };
  1011. #if SDCARD
  1012. class Streamer {
  1013. public:
  1014. void begin(const char *rawsamples,
  1015. uint16_t freq = 44100,
  1016. byte format = ADPCM_SAMPLES,
  1017. uint32_t _base = (0x40000UL - 8192), uint16_t size = 8192) {
  1018. GD.__end();
  1019. r.openfile(rawsamples);
  1020. GD.resume();
  1021. base = _base;
  1022. mask = size - 1;
  1023. wp = 0;
  1024. for (byte i = 10; i; i--)
  1025. feed();
  1026. GD.sample(base, size, freq, format, 1);
  1027. }
  1028. int feed() {
  1029. uint16_t rp = GD.rd32(REG_PLAYBACK_READPTR) - base;
  1030. uint16_t freespace = mask & ((rp - 1) - wp);
  1031. if (freespace >= 512) {
  1032. // REPORT(base);
  1033. // REPORT(rp);
  1034. // REPORT(wp);
  1035. // REPORT(freespace);
  1036. // Serial.println();
  1037. byte buf[512];
  1038. // uint16_t n = min(512, r.size - r.offset);
  1039. // n = (n + 3) & ~3; // force 32-bit alignment
  1040. GD.__end();
  1041. r.readsector(buf);
  1042. GD.resume();
  1043. GD.cmd_memwrite(base + wp, 512);
  1044. GD.copyram(buf, 512);
  1045. wp = (wp + 512) & mask;
  1046. }
  1047. return r.offset < r.size;
  1048. }
  1049. void progress(uint16_t &val, uint16_t &range) {
  1050. uint32_t m = r.size;
  1051. uint32_t p = min(r.offset, m);
  1052. while (m > 0x10000) {
  1053. m >>= 1;
  1054. p >>= 1;
  1055. }
  1056. val = p;
  1057. range = m;
  1058. }
  1059. private:
  1060. Reader r;
  1061. uint32_t base;
  1062. uint16_t mask;
  1063. uint16_t wp;
  1064. };
  1065. #else
  1066. class Streamer {
  1067. public:
  1068. void begin(const char *rawsamples,
  1069. uint16_t freq = 44100,
  1070. byte format = ADPCM_SAMPLES,
  1071. uint32_t _base = (0x40000UL - 4096), uint16_t size = 4096) {}
  1072. int feed() {}
  1073. void progress(uint16_t &val, uint16_t &range) {}
  1074. };
  1075. #endif
  1076. ////////////////////////////////////////////////////////////////////////
  1077. // TileMap: maps made with the "tiled" map editor
  1078. ////////////////////////////////////////////////////////////////////////
  1079. class TileMap {
  1080. uint32_t chunkstart;
  1081. int chunkw, chunkh;
  1082. int stride;
  1083. int bpc;
  1084. byte layers;
  1085. public:
  1086. uint16_t w, h;
  1087. void begin(uint32_t loadpoint) {
  1088. GD.finish();
  1089. w = GD.rd16(loadpoint + 0);
  1090. h = GD.rd16(loadpoint + 2);
  1091. chunkw = GD.rd16(loadpoint + 4);
  1092. chunkh = GD.rd16(loadpoint + 6);
  1093. stride = GD.rd16(loadpoint + 8);
  1094. layers = GD.rd16(loadpoint + 10);
  1095. bpc = (4 * 16);
  1096. chunkstart = loadpoint + 12;
  1097. }
  1098. void draw(uint16_t x, uint16_t y, uint16_t layermask = ~0) {
  1099. int16_t chunk_x = (x / chunkw);
  1100. int16_t ox0 = -(x % chunkw);
  1101. int16_t chunk_y = (y / chunkh);
  1102. int16_t oy = -(y % chunkh);
  1103. GD.Begin(BITMAPS);
  1104. GD.SaveContext();
  1105. GD.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA);
  1106. while (oy < GD.h) {
  1107. int16_t ox = ox0;
  1108. GD.VertexTranslateY(oy << 4);
  1109. uint32_t pos = chunkstart + (chunk_x + long(stride) * chunk_y) * layers * bpc;
  1110. while (ox < GD.w) {
  1111. GD.VertexTranslateX(ox << 4);
  1112. for (byte layer = 0; layer < layers; layer++)
  1113. if (layermask & (1 << layer))
  1114. GD.cmd_append(pos + bpc * layer, bpc);
  1115. pos += (layers * bpc);
  1116. ox += chunkw;
  1117. }
  1118. oy += chunkh;
  1119. chunk_y++;
  1120. }
  1121. GD.RestoreContext();
  1122. }
  1123. void draw(xy pos) {
  1124. draw(pos.x >> 4, pos.y >> 4);
  1125. }
  1126. uint32_t addr(uint16_t x, uint16_t y, byte layer) {
  1127. int16_t tx = (x / (chunkw >> 2));
  1128. int16_t ty = (y / (chunkh >> 2));
  1129. return
  1130. chunkstart +
  1131. ((tx >> 2) + long(stride) * (ty >> 2)) * layers * bpc +
  1132. (tx & 3) * 4 +
  1133. (ty & 3) * 16 +
  1134. layer * 64;
  1135. }
  1136. int read(uint16_t x, uint16_t y, byte layer) {
  1137. uint32_t op = GD.rd32(addr(x, y, layer));
  1138. if ((op >> 24) == 0x2d)
  1139. return 0;
  1140. else
  1141. return 1 + (op & 2047);
  1142. }
  1143. void write(uint16_t x, uint16_t y, byte layer, int tile) {
  1144. uint32_t op;
  1145. uint32_t a = addr(x, y, layer);
  1146. if (tile == 0)
  1147. op = 0x2d000000UL;
  1148. else
  1149. op = (GD.rd32(a) & ~2047) | ((tile - 1) & 2047);
  1150. GD.wr32(a, op);
  1151. }
  1152. int read(xy pos, byte layer) {
  1153. return read(pos.x >> 4, pos.y >> 4, layer);
  1154. }
  1155. void write(xy pos, byte layer, int tile) {
  1156. write(pos.x >> 4, pos.y >> 4, layer, tile);
  1157. }
  1158. };
  1159. class MoviePlayer
  1160. {
  1161. uint32_t mf_size, mf_base, wp;
  1162. Reader r;
  1163. void loadsector() {
  1164. byte buf[512];
  1165. GD.__end();
  1166. r.readsector(buf);
  1167. GD.resume();
  1168. GD.wr_n(mf_base + wp, buf, 512);
  1169. wp = (wp + 512) & (mf_size - 1);
  1170. }
  1171. public:
  1172. int begin(const char *filename) {
  1173. mf_size = 0x40000UL;
  1174. mf_base = 0x100000UL - mf_size;
  1175. GD.__end();
  1176. if (!r.openfile(filename)) {
  1177. // Serial.println("Open failed");
  1178. return 0;
  1179. }
  1180. GD.resume();
  1181. wp = 0;
  1182. while (wp < (mf_size - 512)) {
  1183. loadsector();
  1184. }
  1185. GD.cmd_mediafifo(mf_base, mf_size);
  1186. GD.cmd_regwrite(REG_MEDIAFIFO_WRITE, wp);
  1187. GD.finish();
  1188. return 1;
  1189. }
  1190. int service() {
  1191. if (r.eof()) {
  1192. return 0;
  1193. } else {
  1194. uint32_t fullness = (wp - GD.rd32(REG_MEDIAFIFO_READ)) & (mf_size - 1);
  1195. while (fullness < (mf_size - 512)) {
  1196. loadsector();
  1197. fullness += 512;
  1198. GD.wr32(REG_MEDIAFIFO_WRITE, wp);
  1199. }
  1200. return 1;
  1201. }
  1202. }
  1203. void play() {
  1204. GD.cmd_playvideo(OPT_MEDIAFIFO | OPT_FULLSCREEN);
  1205. GD.flush();
  1206. while (service())
  1207. ;
  1208. GD.cmd_memcpy(0, 0, 4);
  1209. GD.finish();
  1210. }
  1211. };
  1212. class Dirsearch {
  1213. struct dirent de;
  1214. int index;
  1215. public:
  1216. char name[13];
  1217. void begin() {
  1218. index = 0;
  1219. }
  1220. int get(const char *ext) {
  1221. byte i;
  1222. GD.__end();
  1223. char e3[3];
  1224. do {
  1225. GD.SD.rdn((byte*)&de, GD.SD.o_root + index++ * 32, sizeof(de));
  1226. for (i = 0; i < 3; i++)
  1227. e3[i] = tolower(de.ext[i]);
  1228. } while (de.name[0] &&
  1229. ((de.name[0] & 0x80) || (memcmp(ext, e3, 3) != 0)));
  1230. GD.resume();
  1231. char *pc = name;
  1232. for (i = 0; i < 8 && de.name[i] != ' '; i++)
  1233. *pc++ = tolower(de.name[i]);
  1234. *pc++ = '.';
  1235. for (i = 0; i < 3 && de.ext[i] != ' '; i++)
  1236. *pc++ = tolower(de.ext[i]);
  1237. *pc++ = 0;
  1238. return de.name[0];
  1239. }
  1240. };
  1241. /*
  1242. * PROGMEM declarations are currently not supported by the ESP8266
  1243. * compiler. So redefine PROGMEM to nothing.
  1244. */
  1245. #if defined(ESP8266) || defined(ARDUINO_ARCH_STM32L4)
  1246. #undef PROGMEM
  1247. #define PROGMEM
  1248. #endif
  1249. #endif