GD2.h 37 KB

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