GD2.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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(EMUPC)
  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(EMUPC)
  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. sdcard SD;
  413. void storage(void);
  414. void tune(void);
  415. private:
  416. static void cFFFFFF(byte v);
  417. static void cI(uint32_t);
  418. static void ci(int32_t);
  419. static void cH(uint16_t);
  420. static void ch(int16_t);
  421. static void cs(const char *);
  422. static void fmtcmd(const char *fmt, ...);
  423. static void align(byte n);
  424. void cmdbyte(uint8_t b);
  425. uint32_t measure_freq(void);
  426. uint32_t rseed;
  427. };
  428. extern GDClass GD;
  429. #if !defined(RASPBERRY_PI) && !defined(EMUPC)
  430. class Reader {
  431. public:
  432. int openfile(const char *filename) {
  433. int i = 0;
  434. byte dosname[11];
  435. dirent de;
  436. dos83(dosname, filename);
  437. do {
  438. GD.SD.rdn((byte*)&de, GD.SD.o_root + i * 32, sizeof(de));
  439. // Serial.println(de.name);
  440. if (0 == memcmp(de.name, dosname, 11)) {
  441. begin(de);
  442. return 1;
  443. }
  444. i++;
  445. } while (de.name[0]);
  446. return 0;
  447. }
  448. void begin(dirent &de) {
  449. size = de.size;
  450. cluster = de.cluster;
  451. if (GD.SD.type == FAT32)
  452. cluster |= ((long)de.cluster_hi << 16);
  453. sector = 0;
  454. offset = 0;
  455. }
  456. void nextcluster() {
  457. if (GD.SD.type == FAT16)
  458. cluster = GD.SD.rd2(GD.SD.o_fat + 2 * cluster);
  459. else
  460. cluster = GD.SD.rd4(GD.SD.o_fat + 4 * cluster);
  461. #if VERBOSE
  462. Serial.print("nextcluster=");
  463. Serial.println(cluster, DEC);
  464. #endif
  465. }
  466. void skipcluster() {
  467. nextcluster();
  468. offset += GD.SD.cluster_size;
  469. }
  470. void skipsector() {
  471. if (sector == GD.SD.sectors_per_cluster) {
  472. sector = 0;
  473. nextcluster();
  474. }
  475. sector++;
  476. offset += 512;
  477. }
  478. void seek(uint32_t o) {
  479. while (offset < o) {
  480. if ((sector == GD.SD.sectors_per_cluster) && ((o - offset) > (long)GD.SD.cluster_size))
  481. skipcluster();
  482. else
  483. skipsector();
  484. }
  485. }
  486. void readsector() {
  487. if (sector == GD.SD.sectors_per_cluster) {
  488. sector = 0;
  489. nextcluster();
  490. }
  491. uint32_t off = GD.SD.o_data + ((long)GD.SD.cluster_size * cluster) + (512L * sector);
  492. #if VERBOSE
  493. Serial.print("off=0x");
  494. Serial.println(off, HEX);
  495. #endif
  496. GD.SD.cmd17(off & ~511L);
  497. // Serial.println(2 * (micros() - t0), DEC);
  498. sector++;
  499. offset += 512;
  500. }
  501. void readsector(byte *dst) {
  502. readsector();
  503. for (int i = 0; i < 64; i++) {
  504. *dst++ = SPI.transfer(0xff);
  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. }
  513. SPI.transfer(0xff); // consume CRC
  514. SPI.transfer(0xff);
  515. GD.SD.desel();
  516. }
  517. uint32_t cluster;
  518. uint32_t offset;
  519. uint32_t size;
  520. byte sector;
  521. };
  522. #endif
  523. typedef struct {
  524. byte handle;
  525. uint16_t w, h;
  526. uint16_t size;
  527. } shape_t;
  528. // Convert degrees to Furmans
  529. #define DEGREES(n) ((65536UL * (n)) / 360)
  530. #define NEVER 0
  531. #define LESS 1
  532. #define LEQUAL 2
  533. #define GREATER 3
  534. #define GEQUAL 4
  535. #define EQUAL 5
  536. #define NOTEQUAL 6
  537. #define ALWAYS 7
  538. #define ARGB1555 0
  539. #define L1 1
  540. #define L4 2
  541. #define L8 3
  542. #define RGB332 4
  543. #define ARGB2 5
  544. #define ARGB4 6
  545. #define RGB565 7
  546. #define PALETTED 8
  547. #define TEXT8X8 9
  548. #define TEXTVGA 10
  549. #define BARGRAPH 11
  550. #define NEAREST 0
  551. #define BILINEAR 1
  552. #define BORDER 0
  553. #define REPEAT 1
  554. #define KEEP 1
  555. #define REPLACE 2
  556. #define INCR 3
  557. #define DECR 4
  558. #define INVERT 5
  559. #define DLSWAP_DONE 0
  560. #define DLSWAP_LINE 1
  561. #define DLSWAP_FRAME 2
  562. #define INT_SWAP 1
  563. #define INT_TOUCH 2
  564. #define INT_TAG 4
  565. #define INT_SOUND 8
  566. #define INT_PLAYBACK 16
  567. #define INT_CMDEMPTY 32
  568. #define INT_CMDFLAG 64
  569. #define INT_CONVCOMPLETE 128
  570. #define TOUCHMODE_OFF 0
  571. #define TOUCHMODE_ONESHOT 1
  572. #define TOUCHMODE_FRAME 2
  573. #define TOUCHMODE_CONTINUOUS 3
  574. #define ZERO 0
  575. #define ONE 1
  576. #define SRC_ALPHA 2
  577. #define DST_ALPHA 3
  578. #define ONE_MINUS_SRC_ALPHA 4
  579. #define ONE_MINUS_DST_ALPHA 5
  580. #define BITMAPS 1
  581. #define POINTS 2
  582. #define LINES 3
  583. #define LINE_STRIP 4
  584. #define EDGE_STRIP_R 5
  585. #define EDGE_STRIP_L 6
  586. #define EDGE_STRIP_A 7
  587. #define EDGE_STRIP_B 8
  588. #define RECTS 9
  589. #define OPT_MONO 1
  590. #define OPT_NODL 2
  591. #define OPT_FLAT 256
  592. #define OPT_CENTERX 512
  593. #define OPT_CENTERY 1024
  594. #define OPT_CENTER (OPT_CENTERX | OPT_CENTERY)
  595. #define OPT_NOBACK 4096
  596. #define OPT_NOTICKS 8192
  597. #define OPT_NOHM 16384
  598. #define OPT_NOPOINTER 16384
  599. #define OPT_NOSECS 32768
  600. #define OPT_NOHANDS 49152
  601. #define OPT_RIGHTX 2048
  602. #define OPT_SIGNED 256
  603. #define LINEAR_SAMPLES 0
  604. #define ULAW_SAMPLES 1
  605. #define ADPCM_SAMPLES 2
  606. // 'instrument' argument to GD.play()
  607. #define SILENCE 0x00
  608. #define SQUAREWAVE 0x01
  609. #define SINEWAVE 0x02
  610. #define SAWTOOTH 0x03
  611. #define TRIANGLE 0x04
  612. #define BEEPING 0x05
  613. #define ALARM 0x06
  614. #define WARBLE 0x07
  615. #define CAROUSEL 0x08
  616. #define PIPS(n) (0x0f + (n))
  617. #define HARP 0x40
  618. #define XYLOPHONE 0x41
  619. #define TUBA 0x42
  620. #define GLOCKENSPIEL 0x43
  621. #define ORGAN 0x44
  622. #define TRUMPET 0x45
  623. #define PIANO 0x46
  624. #define CHIMES 0x47
  625. #define MUSICBOX 0x48
  626. #define BELL 0x49
  627. #define CLICK 0x50
  628. #define SWITCH 0x51
  629. #define COWBELL 0x52
  630. #define NOTCH 0x53
  631. #define HIHAT 0x54
  632. #define KICKDRUM 0x55
  633. #define POP 0x56
  634. #define CLACK 0x57
  635. #define CHACK 0x58
  636. #define MUTE 0x60
  637. #define UNMUTE 0x61
  638. #define RAM_CMD 1081344UL
  639. #define RAM_DL 1048576UL
  640. #define RAM_PAL 1056768UL
  641. #define REG_CLOCK 1057800UL
  642. #define REG_CMD_DL 1058028UL
  643. #define REG_CMD_READ 1058020UL
  644. #define REG_CMD_WRITE 1058024UL
  645. #define REG_CPURESET 1057820UL
  646. #define REG_CSPREAD 1057892UL
  647. #define REG_DITHER 1057884UL
  648. #define REG_DLSWAP 1057872UL
  649. #define REG_FRAMES 1057796UL
  650. #define REG_FREQUENCY 1057804UL
  651. #define REG_GPIO 1057936UL
  652. #define REG_GPIO_DIR 1057932UL
  653. #define REG_HCYCLE 1057832UL
  654. #define REG_HOFFSET 1057836UL
  655. #define REG_HSIZE 1057840UL
  656. #define REG_HSYNC0 1057844UL
  657. #define REG_HSYNC1 1057848UL
  658. #define REG_ID 1057792UL
  659. #define REG_INT_EN 1057948UL
  660. #define REG_INT_FLAGS 1057944UL
  661. #define REG_INT_MASK 1057952UL
  662. #define REG_MACRO_0 1057992UL
  663. #define REG_MACRO_1 1057996UL
  664. #define REG_OUTBITS 1057880UL
  665. #define REG_PCLK 1057900UL
  666. #define REG_PCLK_POL 1057896UL
  667. #define REG_PLAY 1057928UL
  668. #define REG_PLAYBACK_FORMAT 1057972UL
  669. #define REG_PLAYBACK_FREQ 1057968UL
  670. #define REG_PLAYBACK_LENGTH 1057960UL
  671. #define REG_PLAYBACK_LOOP 1057976UL
  672. #define REG_PLAYBACK_PLAY 1057980UL
  673. #define REG_PLAYBACK_READPTR 1057964UL
  674. #define REG_PLAYBACK_START 1057956UL
  675. #define REG_PWM_DUTY 1057988UL
  676. #define REG_PWM_HZ 1057984UL
  677. #define REG_ROTATE 1057876UL
  678. #define REG_SOUND 1057924UL
  679. #define REG_SWIZZLE 1057888UL
  680. #define REG_TAG 1057912UL
  681. #define REG_TAG_X 1057904UL
  682. #define REG_TAG_Y 1057908UL
  683. #define REG_TOUCH_ADC_MODE 1058036UL
  684. #define REG_TOUCH_CHARGE 1058040UL
  685. #define REG_TOUCH_DIRECT_XY 1058164UL
  686. #define REG_TOUCH_DIRECT_Z1Z2 1058168UL
  687. #define REG_TOUCH_MODE 1058032UL
  688. #define REG_TOUCH_OVERSAMPLE 1058048UL
  689. #define REG_TOUCH_RAW_XY 1058056UL
  690. #define REG_TOUCH_RZ 1058060UL
  691. #define REG_TOUCH_RZTHRESH 1058052UL
  692. #define REG_TOUCH_SCREEN_XY 1058064UL
  693. #define REG_TOUCH_SETTLE 1058044UL
  694. #define REG_TOUCH_TAG 1058072UL
  695. #define REG_TOUCH_TAG_XY 1058068UL
  696. #define REG_TOUCH_TRANSFORM_A 1058076UL
  697. #define REG_TOUCH_TRANSFORM_B 1058080UL
  698. #define REG_TOUCH_TRANSFORM_C 1058084UL
  699. #define REG_TOUCH_TRANSFORM_D 1058088UL
  700. #define REG_TOUCH_TRANSFORM_E 1058092UL
  701. #define REG_TOUCH_TRANSFORM_F 1058096UL
  702. #define REG_TRACKER 1085440UL
  703. #define REG_VCYCLE 1057852UL
  704. #define REG_VOFFSET 1057856UL
  705. #define REG_VOL_PB 1057916UL
  706. #define REG_VOL_SOUND 1057920UL
  707. #define REG_VSIZE 1057860UL
  708. #define REG_VSYNC0 1057864UL
  709. #define REG_VSYNC1 1057868UL
  710. #define VERTEX2II(x, y, handle, cell) \
  711. ((2UL << 30) | (((x) & 511UL) << 21) | (((y) & 511UL) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0))
  712. #define ROM_PIXEL_FF 0xc0400UL
  713. class Poly {
  714. int x0, y0, x1, y1;
  715. int x[8], y[8];
  716. byte n;
  717. void restart() {
  718. n = 0;
  719. x0 = 16 * 480;
  720. x1 = 0;
  721. y0 = 16 * 272;
  722. y1 = 0;
  723. }
  724. void perim() {
  725. for (byte i = 0; i < n; i++)
  726. GD.Vertex2f(x[i], y[i]);
  727. GD.Vertex2f(x[0], y[0]);
  728. }
  729. public:
  730. void begin() {
  731. restart();
  732. GD.ColorMask(0,0,0,0);
  733. GD.StencilOp(KEEP, INVERT);
  734. GD.StencilFunc(ALWAYS, 255, 255);
  735. }
  736. void v(int _x, int _y) {
  737. x0 = min(x0, _x >> 4);
  738. x1 = max(x1, _x >> 4);
  739. y0 = min(y0, _y >> 4);
  740. y1 = max(y1, _y >> 4);
  741. x[n] = _x;
  742. y[n] = _y;
  743. n++;
  744. }
  745. void paint() {
  746. x0 = max(0, x0);
  747. y0 = max(0, y0);
  748. x1 = min(16 * 480, x1);
  749. y1 = min(16 * 272, y1);
  750. GD.ScissorXY(x0, y0);
  751. GD.ScissorSize(x1 - x0 + 1, y1 - y0 + 1);
  752. GD.Begin(EDGE_STRIP_B);
  753. perim();
  754. }
  755. void finish() {
  756. GD.ColorMask(1,1,1,1);
  757. GD.StencilFunc(EQUAL, 255, 255);
  758. GD.Begin(EDGE_STRIP_B);
  759. GD.Vertex2ii(0, 0);
  760. GD.Vertex2ii(511, 0);
  761. }
  762. void draw() {
  763. paint();
  764. finish();
  765. }
  766. void outline() {
  767. GD.Begin(LINE_STRIP);
  768. perim();
  769. }
  770. };
  771. #if !defined(RASPBERRY_PI) && !defined(EMUPC)
  772. class Streamer {
  773. public:
  774. void begin(const char *rawsamples,
  775. uint16_t freq = 44100,
  776. byte format = ADPCM_SAMPLES,
  777. uint32_t _base = (0x40000UL - 8192), uint16_t size = 8192) {
  778. r.openfile(rawsamples);
  779. base = _base;
  780. mask = size - 1;
  781. wp = 0;
  782. for (byte i = 10; i; i--)
  783. feed();
  784. GD.sample(base, size, freq, format, 1);
  785. }
  786. int feed() {
  787. uint16_t rp = GD.rd32(REG_PLAYBACK_READPTR) - base;
  788. uint16_t freespace = mask & ((rp - 1) - wp);
  789. if (freespace >= 512) {
  790. // REPORT(base);
  791. // REPORT(rp);
  792. // REPORT(wp);
  793. // REPORT(freespace);
  794. // Serial.println();
  795. byte buf[512];
  796. // uint16_t n = min(512, r.size - r.offset);
  797. // n = (n + 3) & ~3; // force 32-bit alignment
  798. GD.__end();
  799. r.readsector(buf);
  800. GD.resume();
  801. GD.cmd_memwrite(base + wp, 512);
  802. GD.copyram(buf, 512);
  803. wp = (wp + 512) & mask;
  804. }
  805. return r.offset < r.size;
  806. }
  807. void progress(uint16_t &val, uint16_t &range) {
  808. uint32_t m = r.size;
  809. uint32_t p = min(r.offset, m);
  810. while (m > 0x10000) {
  811. m >>= 1;
  812. p >>= 1;
  813. }
  814. val = p;
  815. range = m;
  816. }
  817. private:
  818. Reader r;
  819. uint32_t base;
  820. uint16_t mask;
  821. uint16_t wp;
  822. };
  823. #else
  824. class Streamer {
  825. public:
  826. void begin(const char *rawsamples,
  827. uint16_t freq = 44100,
  828. byte format = ADPCM_SAMPLES,
  829. uint32_t _base = (0x40000UL - 4096), uint16_t size = 4096) {}
  830. int feed() {}
  831. void progress(uint16_t &val, uint16_t &range) {}
  832. };
  833. #endif
  834. static byte sinus(byte x)
  835. {
  836. return 128 + GD.rsin(128, -16384 + (x << 7));
  837. }
  838. // JCB{
  839. static void caption(int t, const char *text)
  840. {
  841. if ((0 <= t) && (t < 128)) {
  842. int sz = strlen(text) * 8;
  843. GD.RestoreContext();
  844. GD.Begin(LINES);
  845. GD.LineWidth(9 * 16);
  846. byte fade;
  847. if (t < 96)
  848. fade = 0xff;
  849. else
  850. fade = 0xff - ((t - 96) * 8);
  851. GD.ColorA(fade >> 1);
  852. GD.ColorRGB(0x000000);
  853. int y = 259;
  854. byte slide = sinus(min(255, t * 16));
  855. int x = ((slide * (long)sz) >> 8);
  856. GD.Vertex2ii(480, y);
  857. GD.Vertex2ii(470 - x, y);
  858. GD.ColorRGB(0xffffff);
  859. if (t < 16) {
  860. GD.ColorA(fade >> 2);
  861. for (int i = 8; i >= 0; i -= 2)
  862. GD.cmd_text(470 - x + sz + i, y, 26, OPT_CENTERY | OPT_RIGHTX, text);
  863. } else {
  864. GD.ColorA(fade);
  865. GD.cmd_text(470 - x + sz, y, 26, OPT_CENTERY | OPT_RIGHTX, text);
  866. }
  867. }
  868. }
  869. // }JCB
  870. #endif