GD2.h 26 KB

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