Library(Beta) for Gameduino 2

Dependencies:   arduino

Dependents:   aa_gd_jw

Committer:
aluqard
Date:
Fri Apr 11 07:24:23 2014 +0000
Revision:
0:9c211972beb2
Initial release for Gameduino 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aluqard 0:9c211972beb2 1 #include "stdarg.h"
aluqard 0:9c211972beb2 2 #include "GD2.h"
aluqard 0:9c211972beb2 3 #include "Utils.h"
aluqard 0:9c211972beb2 4
aluqard 0:9c211972beb2 5 //AnalogIn accel[3] = {P0_23, P0_24, P0_25};
aluqard 0:9c211972beb2 6 byte touch_transform0[] = { 0xEA, 0x81, 0x00, 0x00, 0xAE, 0xFE, 0xFF, 0xFF, 0x49, 0xCC, 0xEC, 0xFF, 0x28, 0x00, 0x00, 0x00, 0x09, 0xB3, 0xFF, 0xFF, 0x42, 0x5D, 0x18, 0x01 };
aluqard 0:9c211972beb2 7 byte touch_transform1[] = { 0xAB, 0x7E, 0xFF, 0xFF, 0x0F, 0x01, 0x00, 0x00, 0x97, 0x22, 0xEE, 0x01, 0xA3, 0x00, 0x00, 0x00, 0xB3, 0x4B, 0x00, 0x00, 0xE9, 0xC9, 0xF6, 0xFF };
aluqard 0:9c211972beb2 8
aluqard 0:9c211972beb2 9 GDClass::GDClass(PinName mosi, PinName miso, PinName sclk, PinName graphic_cs, PinName sdcard_cs)
aluqard 0:9c211972beb2 10 : GDTR(mosi, miso, sclk, graphic_cs)
aluqard 0:9c211972beb2 11 {
aluqard 0:9c211972beb2 12 _sdcard_cs = sdcard_cs;
aluqard 0:9c211972beb2 13
aluqard 0:9c211972beb2 14 accel[0] = new AnalogIn(P0_23);
aluqard 0:9c211972beb2 15 accel[1] = new AnalogIn(P0_24);
aluqard 0:9c211972beb2 16 accel[2] = new AnalogIn(P0_25);
aluqard 0:9c211972beb2 17 }
aluqard 0:9c211972beb2 18
aluqard 0:9c211972beb2 19 void GDClass::begin(uint8_t options)
aluqard 0:9c211972beb2 20 {
aluqard 0:9c211972beb2 21 GDTR.begin();
aluqard 0:9c211972beb2 22
aluqard 0:9c211972beb2 23 // Generate a blank screen
aluqard 0:9c211972beb2 24 cmd_dlstart();
aluqard 0:9c211972beb2 25 Clear();
aluqard 0:9c211972beb2 26 swap();
aluqard 0:9c211972beb2 27 finish();
aluqard 0:9c211972beb2 28
aluqard 0:9c211972beb2 29 GDTR.wr(REG_PCLK_POL, 1);
aluqard 0:9c211972beb2 30
aluqard 0:9c211972beb2 31 GDTR.wr(REG_PCLK, 5);
aluqard 0:9c211972beb2 32 #if PROTO == 1
aluqard 0:9c211972beb2 33 GDTR.wr(REG_ROTATE, 0);
aluqard 0:9c211972beb2 34 GDTR.wr(REG_SWIZZLE, 3);
aluqard 0:9c211972beb2 35 #endif
aluqard 0:9c211972beb2 36 GDTR.wr(REG_GPIO_DIR, 0x83);
aluqard 0:9c211972beb2 37 GDTR.wr(REG_GPIO, 0x80);
aluqard 0:9c211972beb2 38
aluqard 0:9c211972beb2 39 for (int i = 0; i < sizeof(touch_transform0); i++)
aluqard 0:9c211972beb2 40 {
aluqard 0:9c211972beb2 41 if(GDTR.rd(REG_ROTATE) == 0)
aluqard 0:9c211972beb2 42 GDTR.wr(REG_TOUCH_TRANSFORM_A + i, touch_transform0[i]);
aluqard 0:9c211972beb2 43 else
aluqard 0:9c211972beb2 44 GDTR.wr(REG_TOUCH_TRANSFORM_A + i, touch_transform1[i]);
aluqard 0:9c211972beb2 45 }
aluqard 0:9c211972beb2 46
aluqard 0:9c211972beb2 47 if (options & GD_CALIBRATE)
aluqard 0:9c211972beb2 48 {
aluqard 0:9c211972beb2 49 #if CALIBRATION
aluqard 0:9c211972beb2 50
aluqard 0:9c211972beb2 51 self_calibrate();
aluqard 0:9c211972beb2 52 for (int i = 0; i < 24; i++)
aluqard 0:9c211972beb2 53 std::printf("0x%02X, ", GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
aluqard 0:9c211972beb2 54 /*
aluqard 0:9c211972beb2 55 if (EEPROM.read(0) != 0x7c) {
aluqard 0:9c211972beb2 56 self_calibrate();
aluqard 0:9c211972beb2 57 for (int i = 0; i < 24; i++)
aluqard 0:9c211972beb2 58 DEBUGOUT(GDTR.rd(REG_TOUCH_TRANSFORM_A + i), HEX);
aluqard 0:9c211972beb2 59 for (int i = 0; i < 24; i++)
aluqard 0:9c211972beb2 60 EEPROM.write(1 + i, GDTR.rd(REG_TOUCH_TRANSFORM_A + i));
aluqard 0:9c211972beb2 61 EEPROM.write(0, 0x7c); // is written!
aluqard 0:9c211972beb2 62 } else {
aluqard 0:9c211972beb2 63 for (int i = 0; i < 24; i++)
aluqard 0:9c211972beb2 64 GDTR.wr(REG_TOUCH_TRANSFORM_A + i, EEPROM.read(1 + i));
aluqard 0:9c211972beb2 65 }
aluqard 0:9c211972beb2 66 */
aluqard 0:9c211972beb2 67 #endif
aluqard 0:9c211972beb2 68 }
aluqard 0:9c211972beb2 69
aluqard 0:9c211972beb2 70 GDTR.wr16(REG_TOUCH_RZTHRESH, 1200);
aluqard 0:9c211972beb2 71
aluqard 0:9c211972beb2 72 lfsr = 0x5555;
aluqard 0:9c211972beb2 73 lcg = 0;
aluqard 0:9c211972beb2 74
aluqard 0:9c211972beb2 75 SD = new sdcard(GDTR.SPI(), _sdcard_cs);
aluqard 0:9c211972beb2 76
aluqard 0:9c211972beb2 77 #if STORAGE
aluqard 0:9c211972beb2 78 if (options & GD_STORAGE)
aluqard 0:9c211972beb2 79 {
aluqard 0:9c211972beb2 80 storage();
aluqard 0:9c211972beb2 81 }
aluqard 0:9c211972beb2 82 #endif
aluqard 0:9c211972beb2 83
aluqard 0:9c211972beb2 84 if (options & GD_TRIM)
aluqard 0:9c211972beb2 85 {
aluqard 0:9c211972beb2 86 tune();
aluqard 0:9c211972beb2 87 DEBUGOUT("tune()\r\n");
aluqard 0:9c211972beb2 88 }
aluqard 0:9c211972beb2 89 }
aluqard 0:9c211972beb2 90
aluqard 0:9c211972beb2 91 void GDClass::flush(void)
aluqard 0:9c211972beb2 92 {
aluqard 0:9c211972beb2 93 GDTR.flush();
aluqard 0:9c211972beb2 94 }
aluqard 0:9c211972beb2 95
aluqard 0:9c211972beb2 96 void GDClass::swap(void) {
aluqard 0:9c211972beb2 97 Display();
aluqard 0:9c211972beb2 98 cmd_swap();
aluqard 0:9c211972beb2 99 cmd_loadidentity();
aluqard 0:9c211972beb2 100 cmd_dlstart();
aluqard 0:9c211972beb2 101 GDTR.flush();
aluqard 0:9c211972beb2 102 }
aluqard 0:9c211972beb2 103
aluqard 0:9c211972beb2 104 uint32_t GDClass::measure_freq(void)
aluqard 0:9c211972beb2 105 {
aluqard 0:9c211972beb2 106 unsigned long t0 = GDTR.rd32(REG_CLOCK);
aluqard 0:9c211972beb2 107 delayMicroseconds(15625);
aluqard 0:9c211972beb2 108 unsigned long t1 = GDTR.rd32(REG_CLOCK);
aluqard 0:9c211972beb2 109 return (t1 - t0) << 6;
aluqard 0:9c211972beb2 110 }
aluqard 0:9c211972beb2 111
aluqard 0:9c211972beb2 112 void GDClass::tune(void)
aluqard 0:9c211972beb2 113 {
aluqard 0:9c211972beb2 114 uint32_t f;
aluqard 0:9c211972beb2 115 for (byte i = 0; (i < 31) && ((f = measure_freq()) < LOW_FREQ_BOUND); i++)
aluqard 0:9c211972beb2 116 GDTR.wr(REG_TRIM, i);
aluqard 0:9c211972beb2 117 GDTR.wr32(REG_FREQUENCY, f);
aluqard 0:9c211972beb2 118 }
aluqard 0:9c211972beb2 119
aluqard 0:9c211972beb2 120
aluqard 0:9c211972beb2 121 void GDClass::storage(void) {
aluqard 0:9c211972beb2 122 GDTR.__end();
aluqard 0:9c211972beb2 123 SD->begin();
aluqard 0:9c211972beb2 124 GDTR.resume();
aluqard 0:9c211972beb2 125 }
aluqard 0:9c211972beb2 126
aluqard 0:9c211972beb2 127 void GDClass::self_calibrate(void) {
aluqard 0:9c211972beb2 128 cmd_dlstart();
aluqard 0:9c211972beb2 129 Clear();
aluqard 0:9c211972beb2 130 cmd_text(240, 100, 30, OPT_CENTERX, "please tap on the dot");
aluqard 0:9c211972beb2 131 cmd_calibrate();
aluqard 0:9c211972beb2 132 finish();
aluqard 0:9c211972beb2 133 cmd_loadidentity();
aluqard 0:9c211972beb2 134 cmd_dlstart();
aluqard 0:9c211972beb2 135 GDTR.flush();
aluqard 0:9c211972beb2 136 }
aluqard 0:9c211972beb2 137
aluqard 0:9c211972beb2 138 void GDClass::seed(uint16_t n) {
aluqard 0:9c211972beb2 139 lfsr = n | 1;
aluqard 0:9c211972beb2 140 lcg = n ^ 0x7921;
aluqard 0:9c211972beb2 141 }
aluqard 0:9c211972beb2 142 uint16_t GDClass::random() {
aluqard 0:9c211972beb2 143 lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xB400u);
aluqard 0:9c211972beb2 144 lcg = (lcg * 47) + 60497;
aluqard 0:9c211972beb2 145 return (lcg ^ lfsr);
aluqard 0:9c211972beb2 146 }
aluqard 0:9c211972beb2 147 uint16_t GDClass::random(uint16_t n) {
aluqard 0:9c211972beb2 148 return GDClass::random() % n;
aluqard 0:9c211972beb2 149 }
aluqard 0:9c211972beb2 150
aluqard 0:9c211972beb2 151
aluqard 0:9c211972beb2 152 // >>> [int(65535*math.sin(math.pi * 2 * i / 1024)) for i in range(257)]
aluqard 0:9c211972beb2 153 static PROGMEM int sintab[257] = {
aluqard 0:9c211972beb2 154 0, 402, 804, 1206, 1608, 2010, 2412, 2813, 3215, 3617, 4018, 4419, 4821, 5221, 5622, 6023, 6423, 6823, 7223, 7622, 8022, 8421, 8819, 9218, 9615, 10013, 10410, 10807, 11203, 11599, 11995, 12390, 12785, 13179, 13573, 13966, 14358, 14750, 15142, 15533, 15923, 16313, 16702, 17091, 17479, 17866, 18252, 18638, 19023, 19408, 19791, 20174, 20557, 20938, 21319, 21699, 22078, 22456, 22833, 23210, 23585, 23960, 24334, 24707, 25079, 25450, 25820, 26189, 26557, 26924, 27290, 27655, 28019, 28382, 28744, 29105, 29465, 29823, 30181, 30537, 30892, 31247, 31599, 31951, 32302, 32651, 32999, 33346, 33691, 34035, 34378, 34720, 35061, 35400, 35737, 36074, 36409, 36742, 37075, 37406, 37735, 38063, 38390, 38715, 39039, 39361, 39682, 40001, 40319, 40635, 40950, 41263, 41574, 41885, 42193, 42500, 42805, 43109, 43411, 43711, 44010, 44307, 44603, 44896, 45189, 45479, 45768, 46055, 46340, 46623, 46905, 47185, 47463, 47739, 48014, 48287, 48558, 48827, 49094, 49360, 49623, 49885, 50145, 50403, 50659, 50913, 51165, 51415, 51664, 51910, 52155, 52397, 52638, 52876, 53113, 53347, 53580, 53810, 54039, 54265, 54490, 54712, 54933, 55151, 55367, 55581, 55793, 56003, 56211, 56416, 56620, 56821, 57021, 57218, 57413, 57606, 57796, 57985, 58171, 58355, 58537, 58717, 58894, 59069, 59242, 59413, 59582, 59748, 59912, 60074, 60234, 60391, 60546, 60699, 60849, 60997, 61143, 61287, 61428, 61567, 61704, 61838, 61970, 62100, 62227, 62352, 62474, 62595, 62713, 62828, 62941, 63052, 63161, 63267, 63370, 63472, 63570, 63667, 63761, 63853, 63942, 64029, 64114, 64196, 64275, 64353, 64427, 64500, 64570, 64637, 64702, 64765, 64825, 64883, 64938, 64991, 65042, 65090, 65135, 65178, 65219, 65257, 65293, 65326, 65357, 65385, 65411, 65435, 65456, 65474, 65490, 65504, 65515, 65523, 65530, 65533, 65535
aluqard 0:9c211972beb2 155 };
aluqard 0:9c211972beb2 156
aluqard 0:9c211972beb2 157 int16_t GDClass::rsin(int16_t r, uint16_t th) {
aluqard 0:9c211972beb2 158 th >>= 6; // angle 0-123
aluqard 0:9c211972beb2 159 // return int(r * sin((2 * M_PI) * th / 1024.));
aluqard 0:9c211972beb2 160 int th4 = th & 511;
aluqard 0:9c211972beb2 161 if (th4 & 256)
aluqard 0:9c211972beb2 162 th4 = 512 - th4; // 256->256 257->255, etc
aluqard 0:9c211972beb2 163 uint16_t s = pgm_read_word_near(sintab + th4);
aluqard 0:9c211972beb2 164 int16_t p = ((uint32_t)s * r) >> 16;
aluqard 0:9c211972beb2 165 if (th & 512)
aluqard 0:9c211972beb2 166 p = -p;
aluqard 0:9c211972beb2 167 return p;
aluqard 0:9c211972beb2 168 }
aluqard 0:9c211972beb2 169
aluqard 0:9c211972beb2 170 int16_t GDClass::rcos(int16_t r, uint16_t th) {
aluqard 0:9c211972beb2 171 return rsin(r, th + 0x4000);
aluqard 0:9c211972beb2 172 }
aluqard 0:9c211972beb2 173
aluqard 0:9c211972beb2 174 void GDClass::polar(int &x, int &y, int16_t r, uint16_t th) {
aluqard 0:9c211972beb2 175 x = (int)(-rsin(r, th));
aluqard 0:9c211972beb2 176 y = (int)( rcos(r, th));
aluqard 0:9c211972beb2 177 }
aluqard 0:9c211972beb2 178
aluqard 0:9c211972beb2 179 // >>> [int(round(1024 * math.atan(i / 256.) / math.pi)) for i in range(256)]
aluqard 0:9c211972beb2 180 static PROGMEM unsigned char atan8[] = {
aluqard 0:9c211972beb2 181 0,1,3,4,5,6,8,9,10,11,13,14,15,17,18,19,20,22,23,24,25,27,28,29,30,32,33,34,36,37,38,39,41,42,43,44,46,47,48,49,51,52,53,54,55,57,58,59,60,62,63,64,65,67,68,69,70,71,73,74,75,76,77,79,80,81,82,83,85,86,87,88,89,91,92,93,94,95,96,98,99,100,101,102,103,104,106,107,108,109,110,111,112,114,115,116,117,118,119,120,121,122,124,125,126,127,128,129,130,131,132,133,134,135,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,177,178,179,180,181,182,183,184,185,186,187,188,188,189,190,191,192,193,194,195,195,196,197,198,199,200,201,201,202,203,204,205,206,206,207,208,209,210,211,211,212,213,214,215,215,216,217,218,219,219,220,221,222,222,223,224,225,225,226,227,228,228,229,230,231,231,232,233,234,234,235,236,236,237,238,239,239,240,241,241,242,243,243,244,245,245,246,247,248,248,249,250,250,251,251,252,253,253,254,255,255
aluqard 0:9c211972beb2 182 };
aluqard 0:9c211972beb2 183
aluqard 0:9c211972beb2 184 uint16_t GDClass::atan2(int16_t y, int16_t x)
aluqard 0:9c211972beb2 185 {
aluqard 0:9c211972beb2 186 uint16_t a;
aluqard 0:9c211972beb2 187 uint16_t xx = 0;
aluqard 0:9c211972beb2 188
aluqard 0:9c211972beb2 189 if ((x <= 0) ^ (y > 0)) {
aluqard 0:9c211972beb2 190 int16_t t; t = x; x = y; y = t;
aluqard 0:9c211972beb2 191 xx ^= 0x4000;
aluqard 0:9c211972beb2 192 }
aluqard 0:9c211972beb2 193 if (x <= 0) {
aluqard 0:9c211972beb2 194 x = -x;
aluqard 0:9c211972beb2 195 } else {
aluqard 0:9c211972beb2 196 xx ^= 0x8000;
aluqard 0:9c211972beb2 197 }
aluqard 0:9c211972beb2 198 y = abs(y);
aluqard 0:9c211972beb2 199 if (x > y) {
aluqard 0:9c211972beb2 200 int16_t t; t = x; x = y; y = t;
aluqard 0:9c211972beb2 201 xx ^= 0x3fff;
aluqard 0:9c211972beb2 202 }
aluqard 0:9c211972beb2 203 while ((x | y) & 0xff80) {
aluqard 0:9c211972beb2 204 x >>= 1;
aluqard 0:9c211972beb2 205 y >>= 1;
aluqard 0:9c211972beb2 206 }
aluqard 0:9c211972beb2 207 if (y == 0) {
aluqard 0:9c211972beb2 208 a = 0;
aluqard 0:9c211972beb2 209 } else if (x == y) {
aluqard 0:9c211972beb2 210 a = 0x2000;
aluqard 0:9c211972beb2 211 } else {
aluqard 0:9c211972beb2 212 // assert(x <= y);
aluqard 0:9c211972beb2 213 int r = ((x << 8) / y);
aluqard 0:9c211972beb2 214 // assert(0 <= r);
aluqard 0:9c211972beb2 215 // assert(r < 256);
aluqard 0:9c211972beb2 216 a = pgm_read_byte(atan8 + r) << 5;
aluqard 0:9c211972beb2 217 }
aluqard 0:9c211972beb2 218 a ^= xx;
aluqard 0:9c211972beb2 219 return a;
aluqard 0:9c211972beb2 220 }
aluqard 0:9c211972beb2 221
aluqard 0:9c211972beb2 222 void GDClass::align(byte n) {
aluqard 0:9c211972beb2 223 while ((n++) & 3)
aluqard 0:9c211972beb2 224 GDTR.cmdbyte(0);
aluqard 0:9c211972beb2 225 }
aluqard 0:9c211972beb2 226
aluqard 0:9c211972beb2 227 void GDClass::cH(uint16_t v) {
aluqard 0:9c211972beb2 228 GDTR.cmdbyte(v & 0xff);
aluqard 0:9c211972beb2 229 GDTR.cmdbyte((v >> 8) & 0xff);
aluqard 0:9c211972beb2 230 }
aluqard 0:9c211972beb2 231
aluqard 0:9c211972beb2 232 void GDClass::ch(int16_t v) {
aluqard 0:9c211972beb2 233 cH((uint16_t)v);
aluqard 0:9c211972beb2 234 }
aluqard 0:9c211972beb2 235
aluqard 0:9c211972beb2 236 void GDClass::cI(uint32_t v) {
aluqard 0:9c211972beb2 237 GDTR.cmd32(v);
aluqard 0:9c211972beb2 238 }
aluqard 0:9c211972beb2 239
aluqard 0:9c211972beb2 240 void GDClass::cFFFFFF(byte v) {
aluqard 0:9c211972beb2 241 union {
aluqard 0:9c211972beb2 242 uint32_t c;
aluqard 0:9c211972beb2 243 uint8_t b[4];
aluqard 0:9c211972beb2 244 };
aluqard 0:9c211972beb2 245 b[0] = v;
aluqard 0:9c211972beb2 246 b[1] = 0xff;
aluqard 0:9c211972beb2 247 b[2] = 0xff;
aluqard 0:9c211972beb2 248 b[3] = 0xff;
aluqard 0:9c211972beb2 249 GDTR.cmd32(c);
aluqard 0:9c211972beb2 250 }
aluqard 0:9c211972beb2 251
aluqard 0:9c211972beb2 252 void GDClass::ci(int32_t v) {
aluqard 0:9c211972beb2 253 cI((uint32_t) v);
aluqard 0:9c211972beb2 254 }
aluqard 0:9c211972beb2 255
aluqard 0:9c211972beb2 256 void GDClass::cs(const char *s) {
aluqard 0:9c211972beb2 257 while (*s) {
aluqard 0:9c211972beb2 258 char c = *s++;
aluqard 0:9c211972beb2 259 GDTR.cmdbyte(c);
aluqard 0:9c211972beb2 260 }
aluqard 0:9c211972beb2 261 GDTR.cmdbyte(0);
aluqard 0:9c211972beb2 262 }
aluqard 0:9c211972beb2 263
aluqard 0:9c211972beb2 264 void GDClass::copy(const unsigned char *src, int count) {
aluqard 0:9c211972beb2 265 byte a = count & 3;
aluqard 0:9c211972beb2 266 while (count--) {
aluqard 0:9c211972beb2 267 GDTR.cmdbyte(pgm_read_byte_near(src));
aluqard 0:9c211972beb2 268 src++;
aluqard 0:9c211972beb2 269 }
aluqard 0:9c211972beb2 270 align(a);
aluqard 0:9c211972beb2 271 }
aluqard 0:9c211972beb2 272
aluqard 0:9c211972beb2 273 void GDClass::copyram(byte *src, int count) {
aluqard 0:9c211972beb2 274 byte a = count & 3;
aluqard 0:9c211972beb2 275 GDTR.cmd_n(src, count);
aluqard 0:9c211972beb2 276 align(a);
aluqard 0:9c211972beb2 277 }
aluqard 0:9c211972beb2 278
aluqard 0:9c211972beb2 279 void GDClass::AlphaFunc(byte func, byte ref) {
aluqard 0:9c211972beb2 280 cI((9UL << 24) | ((func & 7L) << 8) | ((ref & 255L) << 0));
aluqard 0:9c211972beb2 281 }
aluqard 0:9c211972beb2 282 void GDClass::Begin(byte prim) {
aluqard 0:9c211972beb2 283 cI((31UL << 24) | prim);
aluqard 0:9c211972beb2 284 }
aluqard 0:9c211972beb2 285 void GDClass::BitmapHandle(byte handle) {
aluqard 0:9c211972beb2 286 cI((5UL << 24) | handle);
aluqard 0:9c211972beb2 287 }
aluqard 0:9c211972beb2 288 void GDClass::BitmapLayout(byte format, uint16_t linestride, uint16_t height) {
aluqard 0:9c211972beb2 289 // cI((7UL << 24) | ((format & 31L) << 19) | ((linestride & 1023L) << 9) | ((height & 511L) << 0));
aluqard 0:9c211972beb2 290 union {
aluqard 0:9c211972beb2 291 uint32_t c;
aluqard 0:9c211972beb2 292 uint8_t b[4];
aluqard 0:9c211972beb2 293 };
aluqard 0:9c211972beb2 294 b[0] = height;
aluqard 0:9c211972beb2 295 b[1] = (1 & (height >> 8)) | (linestride << 1);
aluqard 0:9c211972beb2 296 b[2] = (7 & (linestride >> 7)) | (format << 3);
aluqard 0:9c211972beb2 297 b[3] = 7;
aluqard 0:9c211972beb2 298 cI(c);
aluqard 0:9c211972beb2 299 }
aluqard 0:9c211972beb2 300 void GDClass::BitmapSize(byte filter, byte wrapx, byte wrapy, uint16_t width, uint16_t height) {
aluqard 0:9c211972beb2 301 byte fxy = (filter << 2) | (wrapx << 1) | (wrapy);
aluqard 0:9c211972beb2 302 // cI((8UL << 24) | ((uint32_t)fxy << 18) | ((width & 511L) << 9) | ((height & 511L) << 0));
aluqard 0:9c211972beb2 303 union {
aluqard 0:9c211972beb2 304 uint32_t c;
aluqard 0:9c211972beb2 305 uint8_t b[4];
aluqard 0:9c211972beb2 306 };
aluqard 0:9c211972beb2 307 b[0] = height;
aluqard 0:9c211972beb2 308 b[1] = (1 & (height >> 8)) | (width << 1);
aluqard 0:9c211972beb2 309 b[2] = (3 & (width >> 7)) | (fxy << 2);
aluqard 0:9c211972beb2 310 b[3] = 8;
aluqard 0:9c211972beb2 311 cI(c);
aluqard 0:9c211972beb2 312 }
aluqard 0:9c211972beb2 313 void GDClass::BitmapSource(uint32_t addr) {
aluqard 0:9c211972beb2 314 cI((1UL << 24) | ((addr & 1048575L) << 0));
aluqard 0:9c211972beb2 315 }
aluqard 0:9c211972beb2 316 void GDClass::BitmapTransformA(int32_t a) {
aluqard 0:9c211972beb2 317 cI((21UL << 24) | ((a & 131071L) << 0));
aluqard 0:9c211972beb2 318 }
aluqard 0:9c211972beb2 319 void GDClass::BitmapTransformB(int32_t b) {
aluqard 0:9c211972beb2 320 cI((22UL << 24) | ((b & 131071L) << 0));
aluqard 0:9c211972beb2 321 }
aluqard 0:9c211972beb2 322 void GDClass::BitmapTransformC(int32_t c) {
aluqard 0:9c211972beb2 323 cI((23UL << 24) | ((c & 16777215L) << 0));
aluqard 0:9c211972beb2 324 }
aluqard 0:9c211972beb2 325 void GDClass::BitmapTransformD(int32_t d) {
aluqard 0:9c211972beb2 326 cI((24UL << 24) | ((d & 131071L) << 0));
aluqard 0:9c211972beb2 327 }
aluqard 0:9c211972beb2 328 void GDClass::BitmapTransformE(int32_t e) {
aluqard 0:9c211972beb2 329 cI((25UL << 24) | ((e & 131071L) << 0));
aluqard 0:9c211972beb2 330 }
aluqard 0:9c211972beb2 331 void GDClass::BitmapTransformF(int32_t f) {
aluqard 0:9c211972beb2 332 cI((26UL << 24) | ((f & 16777215L) << 0));
aluqard 0:9c211972beb2 333 }
aluqard 0:9c211972beb2 334 void GDClass::BlendFunc(byte src, byte dst) {
aluqard 0:9c211972beb2 335 cI((11UL << 24) | ((src & 7L) << 3) | ((dst & 7L) << 0));
aluqard 0:9c211972beb2 336 }
aluqard 0:9c211972beb2 337 void GDClass::Call(uint16_t dest) {
aluqard 0:9c211972beb2 338 cI((29UL << 24) | ((dest & 2047L) << 0));
aluqard 0:9c211972beb2 339 }
aluqard 0:9c211972beb2 340 void GDClass::Cell(byte cell) {
aluqard 0:9c211972beb2 341 cI((6UL << 24) | ((cell & 127L) << 0));
aluqard 0:9c211972beb2 342 }
aluqard 0:9c211972beb2 343 void GDClass::ClearColorA(byte alpha) {
aluqard 0:9c211972beb2 344 cI((15UL << 24) | ((alpha & 255L) << 0));
aluqard 0:9c211972beb2 345 }
aluqard 0:9c211972beb2 346 void GDClass::ClearColorRGB(byte red, byte green, byte blue) {
aluqard 0:9c211972beb2 347 cI((2UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
aluqard 0:9c211972beb2 348 }
aluqard 0:9c211972beb2 349 void GDClass::ClearColorRGB(uint32_t rgb) {
aluqard 0:9c211972beb2 350 cI((2UL << 24) | (rgb & 0xffffffL));
aluqard 0:9c211972beb2 351 }
aluqard 0:9c211972beb2 352 void GDClass::Clear(byte c, byte s, byte t) {
aluqard 0:9c211972beb2 353 byte m = (c << 2) | (s << 1) | t;
aluqard 0:9c211972beb2 354 cI((38UL << 24) | m);
aluqard 0:9c211972beb2 355 }
aluqard 0:9c211972beb2 356 void GDClass::Clear(void) {
aluqard 0:9c211972beb2 357 cI((38UL << 24) | 7);
aluqard 0:9c211972beb2 358 }
aluqard 0:9c211972beb2 359 void GDClass::ClearStencil(byte s) {
aluqard 0:9c211972beb2 360 cI((17UL << 24) | ((s & 255L) << 0));
aluqard 0:9c211972beb2 361 }
aluqard 0:9c211972beb2 362 void GDClass::ClearTag(byte s) {
aluqard 0:9c211972beb2 363 cI((18UL << 24) | ((s & 255L) << 0));
aluqard 0:9c211972beb2 364 }
aluqard 0:9c211972beb2 365 void GDClass::ColorA(byte alpha) {
aluqard 0:9c211972beb2 366 cI((16UL << 24) | ((alpha & 255L) << 0));
aluqard 0:9c211972beb2 367 }
aluqard 0:9c211972beb2 368 void GDClass::ColorMask(byte r, byte g, byte b, byte a) {
aluqard 0:9c211972beb2 369 cI((32UL << 24) | ((r & 1L) << 3) | ((g & 1L) << 2) | ((b & 1L) << 1) | ((a & 1L) << 0));
aluqard 0:9c211972beb2 370 }
aluqard 0:9c211972beb2 371 void GDClass::ColorRGB(byte red, byte green, byte blue) {
aluqard 0:9c211972beb2 372 // cI((4UL << 24) | ((red & 255L) << 16) | ((green & 255L) << 8) | ((blue & 255L) << 0));
aluqard 0:9c211972beb2 373 union {
aluqard 0:9c211972beb2 374 uint32_t c;
aluqard 0:9c211972beb2 375 uint8_t b[4];
aluqard 0:9c211972beb2 376 };
aluqard 0:9c211972beb2 377 b[0] = blue;
aluqard 0:9c211972beb2 378 b[1] = green;
aluqard 0:9c211972beb2 379 b[2] = red;
aluqard 0:9c211972beb2 380 b[3] = 4;
aluqard 0:9c211972beb2 381 cI(c);
aluqard 0:9c211972beb2 382 }
aluqard 0:9c211972beb2 383 void GDClass::ColorRGB(uint32_t rgb) {
aluqard 0:9c211972beb2 384 cI((4UL << 24) | (rgb & 0xffffffL));
aluqard 0:9c211972beb2 385 }
aluqard 0:9c211972beb2 386 void GDClass::Display(void) {
aluqard 0:9c211972beb2 387 cI((0UL << 24));
aluqard 0:9c211972beb2 388 }
aluqard 0:9c211972beb2 389 void GDClass::End(void) {
aluqard 0:9c211972beb2 390 cI((33UL << 24));
aluqard 0:9c211972beb2 391 }
aluqard 0:9c211972beb2 392 void GDClass::Jump(uint16_t dest) {
aluqard 0:9c211972beb2 393 cI((30UL << 24) | ((dest & 2047L) << 0));
aluqard 0:9c211972beb2 394 }
aluqard 0:9c211972beb2 395 void GDClass::LineWidth(uint16_t width) {
aluqard 0:9c211972beb2 396 cI((14UL << 24) | ((width & 4095L) << 0));
aluqard 0:9c211972beb2 397 }
aluqard 0:9c211972beb2 398 void GDClass::Macro(byte m) {
aluqard 0:9c211972beb2 399 cI((37UL << 24) | ((m & 1L) << 0));
aluqard 0:9c211972beb2 400 }
aluqard 0:9c211972beb2 401 void GDClass::PointSize(uint16_t size) {
aluqard 0:9c211972beb2 402 cI((13UL << 24) | ((size & 8191L) << 0));
aluqard 0:9c211972beb2 403 }
aluqard 0:9c211972beb2 404 void GDClass::RestoreContext(void) {
aluqard 0:9c211972beb2 405 cI((35UL << 24));
aluqard 0:9c211972beb2 406 }
aluqard 0:9c211972beb2 407 void GDClass::Return(void) {
aluqard 0:9c211972beb2 408 cI((36UL << 24));
aluqard 0:9c211972beb2 409 }
aluqard 0:9c211972beb2 410 void GDClass::SaveContext(void) {
aluqard 0:9c211972beb2 411 cI((34UL << 24));
aluqard 0:9c211972beb2 412 }
aluqard 0:9c211972beb2 413 void GDClass::ScissorSize(uint16_t width, uint16_t height) {
aluqard 0:9c211972beb2 414 cI((28UL << 24) | ((width & 1023L) << 10) | ((height & 1023L) << 0));
aluqard 0:9c211972beb2 415 }
aluqard 0:9c211972beb2 416 void GDClass::ScissorXY(uint16_t x, uint16_t y) {
aluqard 0:9c211972beb2 417 cI((27UL << 24) | ((x & 511L) << 9) | ((y & 511L) << 0));
aluqard 0:9c211972beb2 418 }
aluqard 0:9c211972beb2 419 void GDClass::StencilFunc(byte func, byte ref, byte mask) {
aluqard 0:9c211972beb2 420 cI((10UL << 24) | ((func & 7L) << 16) | ((ref & 255L) << 8) | ((mask & 255L) << 0));
aluqard 0:9c211972beb2 421 }
aluqard 0:9c211972beb2 422 void GDClass::StencilMask(byte mask) {
aluqard 0:9c211972beb2 423 cI((19UL << 24) | ((mask & 255L) << 0));
aluqard 0:9c211972beb2 424 }
aluqard 0:9c211972beb2 425 void GDClass::StencilOp(byte sfail, byte spass) {
aluqard 0:9c211972beb2 426 cI((12UL << 24) | ((sfail & 7L) << 3) | ((spass & 7L) << 0));
aluqard 0:9c211972beb2 427 }
aluqard 0:9c211972beb2 428 void GDClass::TagMask(byte mask) {
aluqard 0:9c211972beb2 429 cI((20UL << 24) | ((mask & 1L) << 0));
aluqard 0:9c211972beb2 430 }
aluqard 0:9c211972beb2 431 void GDClass::Tag(byte s) {
aluqard 0:9c211972beb2 432 cI((3UL << 24) | ((s & 255L) << 0));
aluqard 0:9c211972beb2 433 }
aluqard 0:9c211972beb2 434 void GDClass::Vertex2f(int16_t x, int16_t y) {
aluqard 0:9c211972beb2 435 // x = int(16 * x);
aluqard 0:9c211972beb2 436 // y = int(16 * y);
aluqard 0:9c211972beb2 437 cI((1UL << 30) | ((x & 32767L) << 15) | ((y & 32767L) << 0));
aluqard 0:9c211972beb2 438 }
aluqard 0:9c211972beb2 439 void GDClass::Vertex2ii(uint16_t x, uint16_t y, byte handle, byte cell) {
aluqard 0:9c211972beb2 440 // cI((2UL << 30) | ((x & 511L) << 21) | ((y & 511L) << 12) | ((handle & 31L) << 7) | ((cell & 127L) << 0));
aluqard 0:9c211972beb2 441 union {
aluqard 0:9c211972beb2 442 uint32_t c;
aluqard 0:9c211972beb2 443 uint8_t b[4];
aluqard 0:9c211972beb2 444 };
aluqard 0:9c211972beb2 445 b[0] = cell | ((handle & 1) << 7);
aluqard 0:9c211972beb2 446 b[1] = (handle >> 1) | (y << 4);
aluqard 0:9c211972beb2 447 b[2] = (y >> 4) | (x << 5);
aluqard 0:9c211972beb2 448 b[3] = (2 << 6) | (x >> 3);
aluqard 0:9c211972beb2 449 cI(c);
aluqard 0:9c211972beb2 450 }
aluqard 0:9c211972beb2 451
aluqard 0:9c211972beb2 452 void GDClass::fmtcmd(const char *fmt, ...) {
aluqard 0:9c211972beb2 453 va_list ap;
aluqard 0:9c211972beb2 454 va_start(ap, fmt);
aluqard 0:9c211972beb2 455 byte sz = 0; // Only the low 2 bits matter
aluqard 0:9c211972beb2 456 const char *s;
aluqard 0:9c211972beb2 457
aluqard 0:9c211972beb2 458 while (*fmt)
aluqard 0:9c211972beb2 459 switch (*fmt++) {
aluqard 0:9c211972beb2 460 case 'i':
aluqard 0:9c211972beb2 461 case 'I':
aluqard 0:9c211972beb2 462 cI(va_arg(ap, uint32_t));
aluqard 0:9c211972beb2 463 break;
aluqard 0:9c211972beb2 464 case 'h':
aluqard 0:9c211972beb2 465 case 'H':
aluqard 0:9c211972beb2 466 cH(va_arg(ap, unsigned int));
aluqard 0:9c211972beb2 467 sz += 2;
aluqard 0:9c211972beb2 468 break;
aluqard 0:9c211972beb2 469 case 's':
aluqard 0:9c211972beb2 470 s = va_arg(ap, const char*);
aluqard 0:9c211972beb2 471 cs(s);
aluqard 0:9c211972beb2 472 sz += strlen(s) + 1;
aluqard 0:9c211972beb2 473 break;
aluqard 0:9c211972beb2 474 }
aluqard 0:9c211972beb2 475 align(sz);
aluqard 0:9c211972beb2 476 }
aluqard 0:9c211972beb2 477
aluqard 0:9c211972beb2 478 void GDClass::cmd_append(uint32_t ptr, uint32_t num) {
aluqard 0:9c211972beb2 479 cFFFFFF(0x1e);
aluqard 0:9c211972beb2 480 cI(ptr);
aluqard 0:9c211972beb2 481 cI(num);
aluqard 0:9c211972beb2 482 }
aluqard 0:9c211972beb2 483 void GDClass::cmd_bgcolor(uint32_t c) {
aluqard 0:9c211972beb2 484 fmtcmd("II", 0xffffff09UL, c);
aluqard 0:9c211972beb2 485 }
aluqard 0:9c211972beb2 486 void GDClass::cmd_button(int16_t x, int16_t y, uint16_t w, uint16_t h, byte font, uint16_t options, const char *s) {
aluqard 0:9c211972beb2 487 fmtcmd("IhhhhhHs", 0xffffff0dUL, x, y, w, h, font, options, s);
aluqard 0:9c211972beb2 488 }
aluqard 0:9c211972beb2 489 void GDClass::cmd_calibrate(void) {
aluqard 0:9c211972beb2 490 cFFFFFF(0x15);
aluqard 0:9c211972beb2 491 cFFFFFF(0xff);
aluqard 0:9c211972beb2 492 }
aluqard 0:9c211972beb2 493 void GDClass::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) {
aluqard 0:9c211972beb2 494 fmtcmd("IhhhHHHHH", 0xffffff14UL, x, y, r, options, h, m, s, ms);
aluqard 0:9c211972beb2 495 }
aluqard 0:9c211972beb2 496 void GDClass::cmd_coldstart(void) {
aluqard 0:9c211972beb2 497 cFFFFFF(0x32);
aluqard 0:9c211972beb2 498 }
aluqard 0:9c211972beb2 499 void GDClass::cmd_dial(int16_t x, int16_t y, int16_t r, uint16_t options, uint16_t val) {
aluqard 0:9c211972beb2 500 fmtcmd("IhhhHH", 0xffffff2dUL, x, y, r, options, val);
aluqard 0:9c211972beb2 501 }
aluqard 0:9c211972beb2 502 void GDClass::cmd_dlstart(void) {
aluqard 0:9c211972beb2 503 cFFFFFF(0x00);
aluqard 0:9c211972beb2 504 }
aluqard 0:9c211972beb2 505 void GDClass::cmd_fgcolor(uint32_t c) {
aluqard 0:9c211972beb2 506 fmtcmd("II", 0xffffff0aUL, c);
aluqard 0:9c211972beb2 507 }
aluqard 0:9c211972beb2 508 void GDClass::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) {
aluqard 0:9c211972beb2 509 fmtcmd("IhhhHHHHH", 0xffffff13UL, x, y, r, options, major, minor, val, range);
aluqard 0:9c211972beb2 510 }
aluqard 0:9c211972beb2 511 void GDClass::cmd_getmatrix(void) {
aluqard 0:9c211972beb2 512 fmtcmd("Iiiiiii", 0xffffff33UL, 0, 0, 0, 0, 0, 0);
aluqard 0:9c211972beb2 513 }
aluqard 0:9c211972beb2 514 void GDClass::cmd_getprops(uint32_t &ptr, uint32_t &w, uint32_t &h) {
aluqard 0:9c211972beb2 515 cFFFFFF(0x25);
aluqard 0:9c211972beb2 516 ptr = GDTR.getwp();
aluqard 0:9c211972beb2 517 cI(0);
aluqard 0:9c211972beb2 518 w = GDTR.getwp();
aluqard 0:9c211972beb2 519 cI(0);
aluqard 0:9c211972beb2 520 h = GDTR.getwp();
aluqard 0:9c211972beb2 521 cI(0);
aluqard 0:9c211972beb2 522 }
aluqard 0:9c211972beb2 523 void GDClass::cmd_getptr(void) {
aluqard 0:9c211972beb2 524 fmtcmd("II", 0xffffff23UL, 0);
aluqard 0:9c211972beb2 525 }
aluqard 0:9c211972beb2 526 void GDClass::cmd_gradcolor(uint32_t c) {
aluqard 0:9c211972beb2 527 fmtcmd("II", 0xffffff34UL, c);
aluqard 0:9c211972beb2 528 }
aluqard 0:9c211972beb2 529 void GDClass::cmd_gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1) {
aluqard 0:9c211972beb2 530 fmtcmd("IhhIhhI", 0xffffff0bUL, x0, y0, rgb0, x1, y1, rgb1);
aluqard 0:9c211972beb2 531 }
aluqard 0:9c211972beb2 532 void GDClass::cmd_inflate(uint32_t ptr) {
aluqard 0:9c211972beb2 533 cFFFFFF(0x22);
aluqard 0:9c211972beb2 534 cI(ptr);
aluqard 0:9c211972beb2 535 }
aluqard 0:9c211972beb2 536 void GDClass::cmd_interrupt(uint32_t ms) {
aluqard 0:9c211972beb2 537 fmtcmd("II", 0xffffff02UL, ms);
aluqard 0:9c211972beb2 538 }
aluqard 0:9c211972beb2 539 void GDClass::cmd_keys(int16_t x, int16_t y, int16_t w, int16_t h, byte font, uint16_t options, const char*s) {
aluqard 0:9c211972beb2 540 fmtcmd("IhhhhhHs", 0xffffff0eUL, x, y, w, h, font, options, s);
aluqard 0:9c211972beb2 541 }
aluqard 0:9c211972beb2 542 void GDClass::cmd_loadidentity(void) {
aluqard 0:9c211972beb2 543 cFFFFFF(0x26);
aluqard 0:9c211972beb2 544 }
aluqard 0:9c211972beb2 545 void GDClass::cmd_loadimage(uint32_t ptr, int32_t options) {
aluqard 0:9c211972beb2 546 fmtcmd("III", 0xffffff24UL, ptr, options);
aluqard 0:9c211972beb2 547 }
aluqard 0:9c211972beb2 548 void GDClass::cmd_memcpy(uint32_t dest, uint32_t src, uint32_t num) {
aluqard 0:9c211972beb2 549 fmtcmd("IIII", 0xffffff1dUL, dest, src, num);
aluqard 0:9c211972beb2 550 }
aluqard 0:9c211972beb2 551 void GDClass::cmd_memset(uint32_t ptr, byte value, uint32_t num) {
aluqard 0:9c211972beb2 552 cFFFFFF(0x1b);
aluqard 0:9c211972beb2 553 cI(ptr);
aluqard 0:9c211972beb2 554 cI((uint32_t)value);
aluqard 0:9c211972beb2 555 cI(num);
aluqard 0:9c211972beb2 556 }
aluqard 0:9c211972beb2 557 uint32_t GDClass::cmd_memcrc(uint32_t ptr, uint32_t num) {
aluqard 0:9c211972beb2 558 cFFFFFF(0x18);
aluqard 0:9c211972beb2 559 cI(ptr);
aluqard 0:9c211972beb2 560 cI(num);
aluqard 0:9c211972beb2 561 uint32_t r = GDTR.getwp();
aluqard 0:9c211972beb2 562 cI(0xFFFFFFFF);
aluqard 0:9c211972beb2 563 return r;
aluqard 0:9c211972beb2 564 }
aluqard 0:9c211972beb2 565 void GDClass::cmd_memwrite(uint32_t ptr, uint32_t num) {
aluqard 0:9c211972beb2 566 fmtcmd("III", 0xffffff1aUL, ptr, num);
aluqard 0:9c211972beb2 567 }
aluqard 0:9c211972beb2 568 void GDClass::cmd_regwrite(uint32_t ptr, uint32_t val) {
aluqard 0:9c211972beb2 569 cFFFFFF(0x1a);
aluqard 0:9c211972beb2 570 cI(ptr);
aluqard 0:9c211972beb2 571 cI(4UL);
aluqard 0:9c211972beb2 572 cI(val);
aluqard 0:9c211972beb2 573 }
aluqard 0:9c211972beb2 574 void GDClass::cmd_number(int16_t x, int16_t y, byte font, uint16_t options, uint32_t n) {
aluqard 0:9c211972beb2 575 // fmtcmd("IhhhHi", 0xffffff2eUL, x, y, font, options, n);
aluqard 0:9c211972beb2 576 cFFFFFF(0x2e);
aluqard 0:9c211972beb2 577 ch(x);
aluqard 0:9c211972beb2 578 ch(y);
aluqard 0:9c211972beb2 579 ch(font);
aluqard 0:9c211972beb2 580 cH(options);
aluqard 0:9c211972beb2 581 ci(n);
aluqard 0:9c211972beb2 582 }
aluqard 0:9c211972beb2 583 void GDClass::cmd_progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t options, uint16_t val, uint16_t range) {
aluqard 0:9c211972beb2 584 fmtcmd("IhhhhHHH", 0xffffff0fUL, x, y, w, h, options, val, range);
aluqard 0:9c211972beb2 585 }
aluqard 0:9c211972beb2 586 void GDClass::cmd_regread(uint32_t ptr) {
aluqard 0:9c211972beb2 587 fmtcmd("III", 0xffffff19UL, ptr, 0);
aluqard 0:9c211972beb2 588 }
aluqard 0:9c211972beb2 589 void GDClass::cmd_rotate(int32_t a) {
aluqard 0:9c211972beb2 590 cFFFFFF(0x29);
aluqard 0:9c211972beb2 591 ci(a);
aluqard 0:9c211972beb2 592 }
aluqard 0:9c211972beb2 593 void GDClass::cmd_scale(int32_t sx, int32_t sy) {
aluqard 0:9c211972beb2 594 cFFFFFF(0x28);
aluqard 0:9c211972beb2 595 ci(sx);
aluqard 0:9c211972beb2 596 ci(sy);
aluqard 0:9c211972beb2 597 }
aluqard 0:9c211972beb2 598 void GDClass::cmd_screensaver(void) {
aluqard 0:9c211972beb2 599 cFFFFFF(0x2f);
aluqard 0:9c211972beb2 600 }
aluqard 0:9c211972beb2 601 void GDClass::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) {
aluqard 0:9c211972beb2 602 fmtcmd("IhhhhHHHH", 0xffffff11UL, x, y, w, h, options, val, size, range);
aluqard 0:9c211972beb2 603 }
aluqard 0:9c211972beb2 604 void GDClass::cmd_setfont(byte font, uint32_t ptr) {
aluqard 0:9c211972beb2 605 fmtcmd("III", 0xffffff2bUL, font, ptr);
aluqard 0:9c211972beb2 606 }
aluqard 0:9c211972beb2 607 void GDClass::cmd_setmatrix(void) {
aluqard 0:9c211972beb2 608 cFFFFFF(0x2a);
aluqard 0:9c211972beb2 609 }
aluqard 0:9c211972beb2 610 void GDClass::cmd_sketch(int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format) {
aluqard 0:9c211972beb2 611 cFFFFFF(0x30);
aluqard 0:9c211972beb2 612 ch(x);
aluqard 0:9c211972beb2 613 ch(y);
aluqard 0:9c211972beb2 614 cH(w);
aluqard 0:9c211972beb2 615 cH(h);
aluqard 0:9c211972beb2 616 cI(ptr);
aluqard 0:9c211972beb2 617 cI(format);
aluqard 0:9c211972beb2 618 }
aluqard 0:9c211972beb2 619 void GDClass::cmd_slider(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t options, uint16_t val, uint16_t range) {
aluqard 0:9c211972beb2 620 fmtcmd("IhhhhHHH", 0xffffff10UL, x, y, w, h, options, val, range);
aluqard 0:9c211972beb2 621 }
aluqard 0:9c211972beb2 622 void GDClass::cmd_snapshot(uint32_t ptr) {
aluqard 0:9c211972beb2 623 fmtcmd("II", 0xffffff1fUL, ptr);
aluqard 0:9c211972beb2 624 }
aluqard 0:9c211972beb2 625 void GDClass::cmd_spinner(int16_t x, int16_t y, byte style, byte scale) {
aluqard 0:9c211972beb2 626 cFFFFFF(0x16);
aluqard 0:9c211972beb2 627 ch(x);
aluqard 0:9c211972beb2 628 ch(y);
aluqard 0:9c211972beb2 629 cH(style);
aluqard 0:9c211972beb2 630 cH(scale);
aluqard 0:9c211972beb2 631 }
aluqard 0:9c211972beb2 632 void GDClass::cmd_stop(void) {
aluqard 0:9c211972beb2 633 cFFFFFF(0x17);
aluqard 0:9c211972beb2 634 }
aluqard 0:9c211972beb2 635 void GDClass::cmd_swap(void) {
aluqard 0:9c211972beb2 636 cFFFFFF(0x01);
aluqard 0:9c211972beb2 637 }
aluqard 0:9c211972beb2 638 void GDClass::cmd_text(int16_t x, int16_t y, byte font, uint16_t options, const char *s) {
aluqard 0:9c211972beb2 639 // fmtcmd("IhhhHs", 0xffffff0cUL, x, y, font, options, s);
aluqard 0:9c211972beb2 640 cFFFFFF(0x0c);
aluqard 0:9c211972beb2 641 ch(x);
aluqard 0:9c211972beb2 642 ch(y);
aluqard 0:9c211972beb2 643 ch(font);
aluqard 0:9c211972beb2 644 cH(options);
aluqard 0:9c211972beb2 645 cs(s);
aluqard 0:9c211972beb2 646 align(strlen(s) + 1);
aluqard 0:9c211972beb2 647 }
aluqard 0:9c211972beb2 648 void GDClass::cmd_toggle(int16_t x, int16_t y, int16_t w, byte font, uint16_t options, uint16_t state, const char *s) {
aluqard 0:9c211972beb2 649 fmtcmd("IhhhhHHs", 0xffffff12UL, x, y, w, font, options, state, s);
aluqard 0:9c211972beb2 650 }
aluqard 0:9c211972beb2 651 void GDClass::cmd_track(int16_t x, int16_t y, uint16_t w, uint16_t h, byte tag) {
aluqard 0:9c211972beb2 652 fmtcmd("Ihhhhh", 0xffffff2cUL, x, y, w, h, tag);
aluqard 0:9c211972beb2 653 }
aluqard 0:9c211972beb2 654 void GDClass::cmd_translate(int32_t tx, int32_t ty) {
aluqard 0:9c211972beb2 655 cFFFFFF(0x27);
aluqard 0:9c211972beb2 656 ci(tx);
aluqard 0:9c211972beb2 657 ci(ty);
aluqard 0:9c211972beb2 658 }
aluqard 0:9c211972beb2 659
aluqard 0:9c211972beb2 660 byte GDClass::rd(uint32_t addr)
aluqard 0:9c211972beb2 661 {
aluqard 0:9c211972beb2 662 return GDTR.rd(addr);
aluqard 0:9c211972beb2 663 }
aluqard 0:9c211972beb2 664 void GDClass::wr(uint32_t addr, uint8_t v)
aluqard 0:9c211972beb2 665 {
aluqard 0:9c211972beb2 666 GDTR.wr(addr, v);
aluqard 0:9c211972beb2 667 }
aluqard 0:9c211972beb2 668 uint16_t GDClass::rd16(uint32_t addr)
aluqard 0:9c211972beb2 669 {
aluqard 0:9c211972beb2 670 return GDTR.rd16(addr);
aluqard 0:9c211972beb2 671 }
aluqard 0:9c211972beb2 672 void GDClass::wr16(uint32_t addr, uint16_t v)
aluqard 0:9c211972beb2 673 {
aluqard 0:9c211972beb2 674 GDTR.wr16(addr, v);
aluqard 0:9c211972beb2 675 }
aluqard 0:9c211972beb2 676 uint32_t GDClass::rd32(uint32_t addr)
aluqard 0:9c211972beb2 677 {
aluqard 0:9c211972beb2 678 return GDTR.rd32(addr);
aluqard 0:9c211972beb2 679 }
aluqard 0:9c211972beb2 680 void GDClass::wr32(uint32_t addr, uint32_t v)
aluqard 0:9c211972beb2 681 {
aluqard 0:9c211972beb2 682 GDTR.wr32(addr, v);
aluqard 0:9c211972beb2 683 }
aluqard 0:9c211972beb2 684 void GDClass::wr_n(uint32_t addr, byte *src, uint32_t n)
aluqard 0:9c211972beb2 685 {
aluqard 0:9c211972beb2 686 GDTR.wr_n(addr, src, n);
aluqard 0:9c211972beb2 687 }
aluqard 0:9c211972beb2 688
aluqard 0:9c211972beb2 689 void GDClass::cmdbyte(uint8_t b)
aluqard 0:9c211972beb2 690 {
aluqard 0:9c211972beb2 691 GDTR.cmdbyte(b);
aluqard 0:9c211972beb2 692 }
aluqard 0:9c211972beb2 693 void GDClass::cmd32(uint32_t b) {
aluqard 0:9c211972beb2 694 GDTR.cmd32(b);
aluqard 0:9c211972beb2 695 }
aluqard 0:9c211972beb2 696 void GDClass::finish(void) {
aluqard 0:9c211972beb2 697 GDTR.finish();
aluqard 0:9c211972beb2 698 }
aluqard 0:9c211972beb2 699 void GDClass::get_accel(int &x, int &y, int &z)
aluqard 0:9c211972beb2 700 {
aluqard 0:9c211972beb2 701 static float f[3];
aluqard 0:9c211972beb2 702
aluqard 0:9c211972beb2 703 for (byte i = 0; i < 3; i++)
aluqard 0:9c211972beb2 704 {
aluqard 0:9c211972beb2 705 float a = accel[i]->read() * 1000;
aluqard 0:9c211972beb2 706 // Serial.print(a, DEC); Serial.print(" ");
aluqard 0:9c211972beb2 707 int s = (-160 * ((int)(a - 376)) >> 6);
aluqard 0:9c211972beb2 708 f[i] = (((int)(3 * f[i])) >> 2) + (s >> 2);
aluqard 0:9c211972beb2 709 }
aluqard 0:9c211972beb2 710 // DEBUGOUT();
aluqard 0:9c211972beb2 711 x = f[2];
aluqard 0:9c211972beb2 712 y = f[1];
aluqard 0:9c211972beb2 713 z = f[0];
aluqard 0:9c211972beb2 714 }
aluqard 0:9c211972beb2 715 void GDClass::get_inputs(void) {
aluqard 0:9c211972beb2 716 GDTR.finish();
aluqard 0:9c211972beb2 717 byte *bi = (byte*)&inputs;
aluqard 0:9c211972beb2 718 GDTR.rd_n(bi, REG_TRACKER, 4);
aluqard 0:9c211972beb2 719 GDTR.rd_n(bi + 4, REG_TOUCH_RZ, 13);
aluqard 0:9c211972beb2 720 GDTR.rd_n(bi + 17, REG_TAG, 1);
aluqard 0:9c211972beb2 721 #if DUMP_INPUTS
aluqard 0:9c211972beb2 722 for (size_t i = 0; i < sizeof(inputs); i++) {
aluqard 0:9c211972beb2 723 DEBUGOUT("%x", bi[i]);
aluqard 0:9c211972beb2 724 DEBUGOUT(" ");
aluqard 0:9c211972beb2 725 }
aluqard 0:9c211972beb2 726 #endif
aluqard 0:9c211972beb2 727 }
aluqard 0:9c211972beb2 728 void GDClass::bulkrd(uint32_t a) {
aluqard 0:9c211972beb2 729 GDTR.bulk(a);
aluqard 0:9c211972beb2 730 }
aluqard 0:9c211972beb2 731 void GDClass::resume(void) {
aluqard 0:9c211972beb2 732 GDTR.resume();
aluqard 0:9c211972beb2 733 }
aluqard 0:9c211972beb2 734 void GDClass::__end(void) {
aluqard 0:9c211972beb2 735 GDTR.__end();
aluqard 0:9c211972beb2 736 }
aluqard 0:9c211972beb2 737 void GDClass::play(uint8_t instrument, uint8_t note) {
aluqard 0:9c211972beb2 738 wr16(REG_SOUND, (note << 8) | instrument);
aluqard 0:9c211972beb2 739 wr(REG_PLAY, 1);
aluqard 0:9c211972beb2 740 }
aluqard 0:9c211972beb2 741 void GDClass::sample(uint32_t start, uint32_t len, uint16_t freq, uint16_t format, int loop)
aluqard 0:9c211972beb2 742 {
aluqard 0:9c211972beb2 743 wr32(REG_PLAYBACK_START, start);
aluqard 0:9c211972beb2 744 wr32(REG_PLAYBACK_LENGTH, len);
aluqard 0:9c211972beb2 745 wr16(REG_PLAYBACK_FREQ, freq);
aluqard 0:9c211972beb2 746 wr(REG_PLAYBACK_FORMAT, format);
aluqard 0:9c211972beb2 747 wr(REG_PLAYBACK_LOOP, loop);
aluqard 0:9c211972beb2 748 wr(REG_PLAYBACK_PLAY, 1);
aluqard 0:9c211972beb2 749 }
aluqard 0:9c211972beb2 750 void GDClass::reset() {
aluqard 0:9c211972beb2 751 GDTR.__end();
aluqard 0:9c211972beb2 752 GDTR.wr(REG_CPURESET, 1);
aluqard 0:9c211972beb2 753 GDTR.wr(REG_CPURESET, 0);
aluqard 0:9c211972beb2 754 GDTR.resume();
aluqard 0:9c211972beb2 755 }
aluqard 0:9c211972beb2 756
aluqard 0:9c211972beb2 757
aluqard 0:9c211972beb2 758 // Load named file from storage
aluqard 0:9c211972beb2 759 // returns 0 on failure (e.g. file not found), 1 on success
aluqard 0:9c211972beb2 760
aluqard 0:9c211972beb2 761 byte GDClass::load(const char *filename, void (*progress)(long, long))
aluqard 0:9c211972beb2 762 {
aluqard 0:9c211972beb2 763 #if defined(RASPBERRY_PI)
aluqard 0:9c211972beb2 764 FILE *f = fopen(filename, "rb");
aluqard 0:9c211972beb2 765 if (!f) {
aluqard 0:9c211972beb2 766 perror(filename);
aluqard 0:9c211972beb2 767 exit(1);
aluqard 0:9c211972beb2 768 }
aluqard 0:9c211972beb2 769 byte buf[512];
aluqard 0:9c211972beb2 770 int n;
aluqard 0:9c211972beb2 771 while ((n = fread(buf, 1, 512, f)) > 0) {
aluqard 0:9c211972beb2 772 GDTR.cmd_n(buf, (n + 3) & ~3);
aluqard 0:9c211972beb2 773 }
aluqard 0:9c211972beb2 774 fclose(f);
aluqard 0:9c211972beb2 775 return 1;
aluqard 0:9c211972beb2 776 #else
aluqard 0:9c211972beb2 777 __end();
aluqard 0:9c211972beb2 778 Reader* r = new Reader(GDTR.SPI(), SD);
aluqard 0:9c211972beb2 779 if (r->openfile(filename))
aluqard 0:9c211972beb2 780 {
aluqard 0:9c211972beb2 781 byte buf[512];
aluqard 0:9c211972beb2 782 while (r->offset < r->size)
aluqard 0:9c211972beb2 783 {
aluqard 0:9c211972beb2 784 uint16_t n = min(512, r->size - r->offset);
aluqard 0:9c211972beb2 785 n = (n + 3) & ~3; // force 32-bit alignment
aluqard 0:9c211972beb2 786 r->readsector(buf);
aluqard 0:9c211972beb2 787
aluqard 0:9c211972beb2 788 resume();
aluqard 0:9c211972beb2 789 if (progress)
aluqard 0:9c211972beb2 790 (*progress)(r->offset, r->size);
aluqard 0:9c211972beb2 791 copyram(buf, n);
aluqard 0:9c211972beb2 792 GDTR.stop();
aluqard 0:9c211972beb2 793 }
aluqard 0:9c211972beb2 794 resume();
aluqard 0:9c211972beb2 795 delete r;
aluqard 0:9c211972beb2 796 return 1;
aluqard 0:9c211972beb2 797 }
aluqard 0:9c211972beb2 798 resume();
aluqard 0:9c211972beb2 799 delete r;
aluqard 0:9c211972beb2 800 return 0;
aluqard 0:9c211972beb2 801 #endif
aluqard 0:9c211972beb2 802 }
aluqard 0:9c211972beb2 803
aluqard 0:9c211972beb2 804 // Generated by mk_bsod.py. Blue screen with 'ERROR' text
aluqard 0:9c211972beb2 805 static const unsigned char __bsod[31] = {
aluqard 0:9c211972beb2 806 0, 255, 255, 255, 255, 0, 0, 2, 7, 0, 0, 38, 12, 255, 255, 255, 240,
aluqard 0:9c211972beb2 807 0, 120, 0, 28, 0, 0, 6, 69, 82, 82, 79, 82, 33, 0
aluqard 0:9c211972beb2 808 };
aluqard 0:9c211972beb2 809 // "Cannot open file" text
aluqard 0:9c211972beb2 810 static const unsigned char __bsod_badfile[31] = {
aluqard 0:9c211972beb2 811 12, 255, 255, 255, 240, 0, 148, 0, 28, 0, 0, 6, 67, 97, 110, 110, 111,
aluqard 0:9c211972beb2 812 116, 32, 111, 112, 101, 110, 32, 102, 105, 108, 101, 0, 0, 0
aluqard 0:9c211972beb2 813 };
aluqard 0:9c211972beb2 814
aluqard 0:9c211972beb2 815 void GDClass::safeload(const char *filename)
aluqard 0:9c211972beb2 816 {
aluqard 0:9c211972beb2 817 if (!load(filename)) {
aluqard 0:9c211972beb2 818 copy(__bsod, sizeof(__bsod));
aluqard 0:9c211972beb2 819 copy(__bsod_badfile, sizeof(__bsod_badfile));
aluqard 0:9c211972beb2 820 cmd_text(240, 176, 28, OPT_CENTER, filename);
aluqard 0:9c211972beb2 821 swap();
aluqard 0:9c211972beb2 822 for (;;)
aluqard 0:9c211972beb2 823 ;
aluqard 0:9c211972beb2 824 }
aluqard 0:9c211972beb2 825 }