GD2.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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_NOTEAR 4
  820. #define OPT_FULLSCREEN 8
  821. #define OPT_MEDIAFIFO 16
  822. #define LINEAR_SAMPLES 0
  823. #define ULAW_SAMPLES 1
  824. #define ADPCM_SAMPLES 2
  825. // 'instrument' argument to GD.play()
  826. #define SILENCE 0x00
  827. #define SQUAREWAVE 0x01
  828. #define SINEWAVE 0x02
  829. #define SAWTOOTH 0x03
  830. #define TRIANGLE 0x04
  831. #define BEEPING 0x05
  832. #define ALARM 0x06
  833. #define WARBLE 0x07
  834. #define CAROUSEL 0x08
  835. #define PIPS(n) (0x0f + (n))
  836. #define HARP 0x40
  837. #define XYLOPHONE 0x41
  838. #define TUBA 0x42
  839. #define GLOCKENSPIEL 0x43
  840. #define ORGAN 0x44
  841. #define TRUMPET 0x45
  842. #define PIANO 0x46
  843. #define CHIMES 0x47
  844. #define MUSICBOX 0x48
  845. #define BELL 0x49
  846. #define CLICK 0x50
  847. #define SWITCH 0x51
  848. #define COWBELL 0x52
  849. #define NOTCH 0x53
  850. #define HIHAT 0x54
  851. #define KICKDRUM 0x55
  852. #define POP 0x56
  853. #define CLACK 0x57
  854. #define CHACK 0x58
  855. #define MUTE 0x60
  856. #define UNMUTE 0x61
  857. #define RAM_PAL 1056768UL
  858. #define RAM_CMD (ft8xx_model ? 0x308000UL : 0x108000UL)
  859. #define RAM_DL (ft8xx_model ? 0x300000UL : 0x100000UL)
  860. #define REG_CLOCK (ft8xx_model ? 0x302008UL : 0x102408UL)
  861. #define REG_CMD_DL (ft8xx_model ? 0x302100UL : 0x1024ecUL)
  862. #define REG_CMD_READ (ft8xx_model ? 0x3020f8UL : 0x1024e4UL)
  863. #define REG_CMD_WRITE (ft8xx_model ? 0x3020fcUL : 0x1024e8UL)
  864. #define REG_CPURESET (ft8xx_model ? 0x302020UL : 0x10241cUL)
  865. #define REG_CSPREAD (ft8xx_model ? 0x302068UL : 0x102464UL)
  866. #define REG_DITHER (ft8xx_model ? 0x302060UL : 0x10245cUL)
  867. #define REG_DLSWAP (ft8xx_model ? 0x302054UL : 0x102450UL)
  868. #define REG_FRAMES (ft8xx_model ? 0x302004UL : 0x102404UL)
  869. #define REG_FREQUENCY (ft8xx_model ? 0x30200cUL : 0x10240cUL)
  870. #define REG_GPIO (ft8xx_model ? 0x302094UL : 0x102490UL)
  871. #define REG_GPIO_DIR (ft8xx_model ? 0x302090UL : 0x10248cUL)
  872. #define REG_HCYCLE (ft8xx_model ? 0x30202cUL : 0x102428UL)
  873. #define REG_HOFFSET (ft8xx_model ? 0x302030UL : 0x10242cUL)
  874. #define REG_HSIZE (ft8xx_model ? 0x302034UL : 0x102430UL)
  875. #define REG_HSYNC0 (ft8xx_model ? 0x302038UL : 0x102434UL)
  876. #define REG_HSYNC1 (ft8xx_model ? 0x30203cUL : 0x102438UL)
  877. #define REG_ID (ft8xx_model ? 0x302000UL : 0x102400UL)
  878. #define REG_INT_EN (ft8xx_model ? 0x3020acUL : 0x10249cUL)
  879. #define REG_INT_FLAGS (ft8xx_model ? 0x3020a8UL : 0x102498UL)
  880. #define REG_INT_MASK (ft8xx_model ? 0x3020b0UL : 0x1024a0UL)
  881. #define REG_MACRO_0 (ft8xx_model ? 0x3020d8UL : 0x1024c8UL)
  882. #define REG_MACRO_1 (ft8xx_model ? 0x3020dcUL : 0x1024ccUL)
  883. #define REG_OUTBITS (ft8xx_model ? 0x30205cUL : 0x102458UL)
  884. #define REG_PCLK (ft8xx_model ? 0x302070UL : 0x10246cUL)
  885. #define REG_PCLK_POL (ft8xx_model ? 0x30206cUL : 0x102468UL)
  886. #define REG_PLAY (ft8xx_model ? 0x30208cUL : 0x102488UL)
  887. #define REG_PLAYBACK_FORMAT (ft8xx_model ? 0x3020c4UL : 0x1024b4UL)
  888. #define REG_PLAYBACK_FREQ (ft8xx_model ? 0x3020c0UL : 0x1024b0UL)
  889. #define REG_PLAYBACK_LENGTH (ft8xx_model ? 0x3020b8UL : 0x1024a8UL)
  890. #define REG_PLAYBACK_LOOP (ft8xx_model ? 0x3020c8UL : 0x1024b8UL)
  891. #define REG_PLAYBACK_PLAY (ft8xx_model ? 0x3020ccUL : 0x1024bcUL)
  892. #define REG_PLAYBACK_READPTR (ft8xx_model ? 0x3020bcUL : 0x1024acUL)
  893. #define REG_PLAYBACK_START (ft8xx_model ? 0x3020b4UL : 0x1024a4UL)
  894. #define REG_PWM_DUTY (ft8xx_model ? 0x3020d4UL : 0x1024c4UL)
  895. #define REG_PWM_HZ (ft8xx_model ? 0x3020d0UL : 0x1024c0UL)
  896. #define REG_ROTATE (ft8xx_model ? 0x302058UL : 0x102454UL)
  897. #define REG_SOUND (ft8xx_model ? 0x302088UL : 0x102484UL)
  898. #define REG_SWIZZLE (ft8xx_model ? 0x302064UL : 0x102460UL)
  899. #define REG_TAG (ft8xx_model ? 0x30207cUL : 0x102478UL)
  900. #define REG_TAG_X (ft8xx_model ? 0x302074UL : 0x102470UL)
  901. #define REG_TAG_Y (ft8xx_model ? 0x302078UL : 0x102474UL)
  902. #define REG_TOUCH_ADC_MODE (ft8xx_model ? 0x302108UL : 0x1024f4UL)
  903. #define REG_TOUCH_CHARGE (ft8xx_model ? 0x30210cUL : 0x1024f8UL)
  904. #define REG_TOUCH_DIRECT_XY (ft8xx_model ? 0x30218cUL : 0x102574UL)
  905. #define REG_TOUCH_DIRECT_Z1Z2 (ft8xx_model ? 0x302190UL : 0x102578UL)
  906. #define REG_TOUCH_MODE (ft8xx_model ? 0x302104UL : 0x1024f0UL)
  907. #define REG_TOUCH_OVERSAMPLE (ft8xx_model ? 0x302114UL : 0x102500UL)
  908. #define REG_TOUCH_RAW_XY (ft8xx_model ? 0x30211cUL : 0x102508UL)
  909. #define REG_TOUCH_RZ (ft8xx_model ? 0x302120UL : 0x10250cUL)
  910. #define REG_TOUCH_RZTHRESH (ft8xx_model ? 0x302118UL : 0x102504UL)
  911. #define REG_TOUCH_SCREEN_XY (ft8xx_model ? 0x302124UL : 0x102510UL)
  912. #define REG_TOUCH_SETTLE (ft8xx_model ? 0x302110UL : 0x1024fcUL)
  913. #define REG_TOUCH_TAG (ft8xx_model ? 0x30212cUL : 0x102518UL)
  914. #define REG_TOUCH_TAG_XY (ft8xx_model ? 0x302128UL : 0x102514UL)
  915. #define REG_TOUCH_TRANSFORM_A (ft8xx_model ? 0x302150UL : 0x10251cUL)
  916. #define REG_TOUCH_TRANSFORM_B (ft8xx_model ? 0x302154UL : 0x102520UL)
  917. #define REG_TOUCH_TRANSFORM_C (ft8xx_model ? 0x302158UL : 0x102524UL)
  918. #define REG_TOUCH_TRANSFORM_D (ft8xx_model ? 0x30215cUL : 0x102528UL)
  919. #define REG_TOUCH_TRANSFORM_E (ft8xx_model ? 0x302160UL : 0x10252cUL)
  920. #define REG_TOUCH_TRANSFORM_F (ft8xx_model ? 0x302164UL : 0x102530UL)
  921. #define REG_TRACKER (ft8xx_model ? 0x309000UL : 0x109000UL)
  922. #define REG_TRIM (ft8xx_model ? 0x302180UL : 0x10256cUL)
  923. #define REG_VCYCLE (ft8xx_model ? 0x302040UL : 0x10243cUL)
  924. #define REG_VOFFSET (ft8xx_model ? 0x302044UL : 0x102440UL)
  925. #define REG_VOL_PB (ft8xx_model ? 0x302080UL : 0x10247cUL)
  926. #define REG_VOL_SOUND (ft8xx_model ? 0x302084UL : 0x102480UL)
  927. #define REG_VSIZE (ft8xx_model ? 0x302048UL : 0x102444UL)
  928. #define REG_VSYNC0 (ft8xx_model ? 0x30204cUL : 0x102448UL)
  929. #define REG_VSYNC1 (ft8xx_model ? 0x302050UL : 0x10244cUL)
  930. #define FONT_ROOT (ft8xx_model ? 0x2ffffcUL : 0x0ffffcUL)
  931. #define REG_MEDIAFIFO_READ 0x309014
  932. #define REG_MEDIAFIFO_WRITE 0x309018
  933. #define VERTEX2II(x, y, handle, cell) \
  934. ((2UL << 30) | (((x) & 511UL) << 21) | (((y) & 511UL) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0))
  935. #define ROM_PIXEL_FF 0xc0400UL
  936. class Poly {
  937. int x0, y0, x1, y1;
  938. int x[8], y[8];
  939. byte n;
  940. void restart() {
  941. n = 0;
  942. x0 = 16 * 480;
  943. x1 = 0;
  944. y0 = 16 * 272;
  945. y1 = 0;
  946. }
  947. void perim() {
  948. for (byte i = 0; i < n; i++)
  949. GD.Vertex2f(x[i], y[i]);
  950. GD.Vertex2f(x[0], y[0]);
  951. }
  952. public:
  953. void begin() {
  954. restart();
  955. GD.ColorMask(0,0,0,0);
  956. GD.StencilOp(KEEP, INVERT);
  957. GD.StencilFunc(ALWAYS, 255, 255);
  958. }
  959. void v(int _x, int _y) {
  960. x0 = min(x0, _x >> 4);
  961. x1 = max(x1, _x >> 4);
  962. y0 = min(y0, _y >> 4);
  963. y1 = max(y1, _y >> 4);
  964. x[n] = _x;
  965. y[n] = _y;
  966. n++;
  967. }
  968. void paint() {
  969. x0 = max(0, x0);
  970. y0 = max(0, y0);
  971. x1 = min(16 * 480, x1);
  972. y1 = min(16 * 272, y1);
  973. GD.ScissorXY(x0, y0);
  974. GD.ScissorSize(x1 - x0 + 1, y1 - y0 + 1);
  975. GD.Begin(EDGE_STRIP_B);
  976. perim();
  977. }
  978. void finish() {
  979. GD.ColorMask(1,1,1,1);
  980. GD.StencilFunc(EQUAL, 255, 255);
  981. GD.Begin(EDGE_STRIP_B);
  982. GD.Vertex2ii(0, 0);
  983. GD.Vertex2ii(511, 0);
  984. }
  985. void draw() {
  986. paint();
  987. finish();
  988. }
  989. void outline() {
  990. GD.Begin(LINE_STRIP);
  991. perim();
  992. }
  993. };
  994. #if SDCARD
  995. class Streamer {
  996. public:
  997. void begin(const char *rawsamples,
  998. uint16_t freq = 44100,
  999. byte format = ADPCM_SAMPLES,
  1000. uint32_t _base = (0x40000UL - 8192), uint16_t size = 8192) {
  1001. GD.__end();
  1002. r.openfile(rawsamples);
  1003. GD.resume();
  1004. base = _base;
  1005. mask = size - 1;
  1006. wp = 0;
  1007. for (byte i = 10; i; i--)
  1008. feed();
  1009. GD.sample(base, size, freq, format, 1);
  1010. }
  1011. int feed() {
  1012. uint16_t rp = GD.rd32(REG_PLAYBACK_READPTR) - base;
  1013. uint16_t freespace = mask & ((rp - 1) - wp);
  1014. if (freespace >= 512) {
  1015. // REPORT(base);
  1016. // REPORT(rp);
  1017. // REPORT(wp);
  1018. // REPORT(freespace);
  1019. // Serial.println();
  1020. byte buf[512];
  1021. // uint16_t n = min(512, r.size - r.offset);
  1022. // n = (n + 3) & ~3; // force 32-bit alignment
  1023. GD.__end();
  1024. r.readsector(buf);
  1025. GD.resume();
  1026. GD.cmd_memwrite(base + wp, 512);
  1027. GD.copyram(buf, 512);
  1028. wp = (wp + 512) & mask;
  1029. }
  1030. return r.offset < r.size;
  1031. }
  1032. void progress(uint16_t &val, uint16_t &range) {
  1033. uint32_t m = r.size;
  1034. uint32_t p = min(r.offset, m);
  1035. while (m > 0x10000) {
  1036. m >>= 1;
  1037. p >>= 1;
  1038. }
  1039. val = p;
  1040. range = m;
  1041. }
  1042. private:
  1043. Reader r;
  1044. uint32_t base;
  1045. uint16_t mask;
  1046. uint16_t wp;
  1047. };
  1048. #else
  1049. class Streamer {
  1050. public:
  1051. void begin(const char *rawsamples,
  1052. uint16_t freq = 44100,
  1053. byte format = ADPCM_SAMPLES,
  1054. uint32_t _base = (0x40000UL - 4096), uint16_t size = 4096) {}
  1055. int feed() {}
  1056. void progress(uint16_t &val, uint16_t &range) {}
  1057. };
  1058. #endif
  1059. ////////////////////////////////////////////////////////////////////////
  1060. // TileMap: maps made with the "tiled" map editor
  1061. ////////////////////////////////////////////////////////////////////////
  1062. class TileMap {
  1063. uint32_t chunkstart;
  1064. int chunkw, chunkh;
  1065. int stride;
  1066. int bpc;
  1067. byte layers;
  1068. public:
  1069. uint16_t w, h;
  1070. void begin(uint32_t loadpoint) {
  1071. GD.finish();
  1072. w = GD.rd16(loadpoint + 0);
  1073. h = GD.rd16(loadpoint + 2);
  1074. chunkw = GD.rd16(loadpoint + 4);
  1075. chunkh = GD.rd16(loadpoint + 6);
  1076. stride = GD.rd16(loadpoint + 8);
  1077. layers = GD.rd16(loadpoint + 10);
  1078. bpc = (4 * 16);
  1079. chunkstart = loadpoint + 12;
  1080. }
  1081. void draw(uint16_t x, uint16_t y, uint16_t layermask = ~0) {
  1082. int16_t chunk_x = (x / chunkw);
  1083. int16_t ox0 = -(x % chunkw);
  1084. int16_t chunk_y = (y / chunkh);
  1085. int16_t oy = -(y % chunkh);
  1086. GD.Begin(BITMAPS);
  1087. GD.SaveContext();
  1088. GD.BlendFunc(ONE, ONE_MINUS_SRC_ALPHA);
  1089. while (oy < GD.h) {
  1090. int16_t ox = ox0;
  1091. GD.VertexTranslateY(oy << 4);
  1092. uint32_t pos = chunkstart + (chunk_x + long(stride) * chunk_y) * layers * bpc;
  1093. while (ox < GD.w) {
  1094. GD.VertexTranslateX(ox << 4);
  1095. for (byte layer = 0; layer < layers; layer++)
  1096. if (layermask & (1 << layer))
  1097. GD.cmd_append(pos + bpc * layer, bpc);
  1098. pos += (layers * bpc);
  1099. ox += chunkw;
  1100. }
  1101. oy += chunkh;
  1102. chunk_y++;
  1103. }
  1104. GD.RestoreContext();
  1105. }
  1106. void draw(xy pos) {
  1107. draw(pos.x >> 4, pos.y >> 4);
  1108. }
  1109. uint32_t addr(uint16_t x, uint16_t y, byte layer) {
  1110. int16_t tx = (x / (chunkw >> 2));
  1111. int16_t ty = (y / (chunkh >> 2));
  1112. return
  1113. chunkstart +
  1114. ((tx >> 2) + long(stride) * (ty >> 2)) * layers * bpc +
  1115. (tx & 3) * 4 +
  1116. (ty & 3) * 16 +
  1117. layer * 64;
  1118. }
  1119. int read(uint16_t x, uint16_t y, byte layer) {
  1120. uint32_t op = GD.rd32(addr(x, y, layer));
  1121. if ((op >> 24) == 0x2d)
  1122. return 0;
  1123. else
  1124. return 1 + (op & 2047);
  1125. }
  1126. void write(uint16_t x, uint16_t y, byte layer, int tile) {
  1127. uint32_t op;
  1128. uint32_t a = addr(x, y, layer);
  1129. if (tile == 0)
  1130. op = 0x2d000000UL;
  1131. else
  1132. op = (GD.rd32(a) & ~2047) | ((tile - 1) & 2047);
  1133. GD.wr32(a, op);
  1134. }
  1135. int read(xy pos, byte layer) {
  1136. return read(pos.x >> 4, pos.y >> 4, layer);
  1137. }
  1138. void write(xy pos, byte layer, int tile) {
  1139. write(pos.x >> 4, pos.y >> 4, layer, tile);
  1140. }
  1141. };
  1142. /*
  1143. * PROGMEM declarations are currently not supported by the ESP8266
  1144. * comppiler. So redefine PROGMEM to nothing.
  1145. */
  1146. #if defined(ESP8266)
  1147. #undef PROGMEM
  1148. #define PROGMEM
  1149. #endif
  1150. #endif