yet another AkiSpiLcd library example

Dependencies:   AkiSpiLcd mbed

Fork of LCDRAM by masahiro wakayama

Committer:
m_wakayama
Date:
Tue Nov 25 10:28:12 2014 +0000
Revision:
0:e705efefad3f
Child:
1:72320e85a5e2
AkiSpiLcd sample to show moving cube

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m_wakayama 0:e705efefad3f 1
m_wakayama 0:e705efefad3f 2 #include "mbed.h"
m_wakayama 0:e705efefad3f 3 #include "AkiSpiLcd.h"
m_wakayama 0:e705efefad3f 4
m_wakayama 0:e705efefad3f 5 //AkiSpiLcd LCD(MISO, MOSI, SCK, CSL, CSR);
m_wakayama 0:e705efefad3f 6 AkiSpiLcd LCDRAM(p11, p12, p13, p9, p10);
m_wakayama 0:e705efefad3f 7 uint8_t lineBuffer[400 / 8];
m_wakayama 0:e705efefad3f 8
m_wakayama 0:e705efefad3f 9 // SYSTEM --------------------------------------------------
m_wakayama 0:e705efefad3f 10 uint8_t mainCounter;
m_wakayama 0:e705efefad3f 11 // GAME ----------------------------------------------------
m_wakayama 0:e705efefad3f 12 int scrollX,scrollY;
m_wakayama 0:e705efefad3f 13 // BOX -----------------------------------------------------
m_wakayama 0:e705efefad3f 14 int8_t boxPosi[8][3] = {
m_wakayama 0:e705efefad3f 15 {-78,-78,-78},{-78,+78,-78},{-78,+78,+78},{-78,-78,+78},
m_wakayama 0:e705efefad3f 16 {+78,-78,-78},{+78,+78,-78},{+78,+78,+78},{+78,-78,+78}
m_wakayama 0:e705efefad3f 17 };
m_wakayama 0:e705efefad3f 18 uint8_t boxLink[12][2] = {
m_wakayama 0:e705efefad3f 19 {0,1},{1,2},{2,3},{3,0},
m_wakayama 0:e705efefad3f 20 {4,5},{5,6},{6,7},{7,4},
m_wakayama 0:e705efefad3f 21 {0,4},{1,5},{2,6},{3,7}
m_wakayama 0:e705efefad3f 22 };
m_wakayama 0:e705efefad3f 23 int boxOffsetX;
m_wakayama 0:e705efefad3f 24 int boxOffsetY;
m_wakayama 0:e705efefad3f 25 int boxAngleX;
m_wakayama 0:e705efefad3f 26 int boxAngleZ;
m_wakayama 0:e705efefad3f 27 float buff3D[8][3];
m_wakayama 0:e705efefad3f 28
m_wakayama 0:e705efefad3f 29 // ASCII ---------------------------------
m_wakayama 0:e705efefad3f 30 int8_t asciiPosi[9][2] = {
m_wakayama 0:e705efefad3f 31 {-6,-9},{ 0,-9},{+6,-9},
m_wakayama 0:e705efefad3f 32 {-6, 0},{ 0, 0},{+6, 0},
m_wakayama 0:e705efefad3f 33 {-6,+9},{ 0,+9},{+6,+9}
m_wakayama 0:e705efefad3f 34 };
m_wakayama 0:e705efefad3f 35 uint8_t asciiLink[10 + 26][6][2] = {
m_wakayama 0:e705efefad3f 36 {{0,2},{2,8},{8,6},{6,0},{0,0},{0,0}}, // 0
m_wakayama 0:e705efefad3f 37 {{1,7},{1,7},{0,0},{0,0},{0,0},{0,0}}, // 1
m_wakayama 0:e705efefad3f 38 {{0,2},{2,5},{3,5},{3,6},{6,8},{0,0}}, // 2
m_wakayama 0:e705efefad3f 39 {{0,2},{3,5},{6,8},{2,8},{0,0},{0,0}}, // 3
m_wakayama 0:e705efefad3f 40 {{0,3},{3,5},{2,8},{0,0},{0,0},{0,0}}, // 4
m_wakayama 0:e705efefad3f 41 {{0,2},{0,3},{3,5},{5,8},{6,8},{0,0}}, // 5
m_wakayama 0:e705efefad3f 42 {{0,2},{0,6},{3,5},{5,8},{6,8},{0,0}}, // 6
m_wakayama 0:e705efefad3f 43 {{0,2},{2,7},{0,0},{0,0},{0,0},{0,0}}, // 7
m_wakayama 0:e705efefad3f 44 {{0,2},{0,6},{2,8},{3,5},{6,8},{0,0}}, // 8
m_wakayama 0:e705efefad3f 45 {{0,2},{0,3},{3,5},{2,5},{5,7},{0,0}}, // 9
m_wakayama 0:e705efefad3f 46 {{1,3},{3,6},{1,5},{5,8},{0,0},{0,0}}, // A
m_wakayama 0:e705efefad3f 47 {{0,2},{2,3},{3,8},{6,8},{0,6},{0,0}}, // B
m_wakayama 0:e705efefad3f 48 {{0,2},{0,6},{6,8},{0,0},{0,0},{0,0}}, // C
m_wakayama 0:e705efefad3f 49 {{0,5},{6,5},{0,6},{0,0},{0,0},{0,0}}, // D
m_wakayama 0:e705efefad3f 50 {{0,2},{0,6},{3,5},{6,8},{0,0},{0,0}}, // E
m_wakayama 0:e705efefad3f 51 {{0,2},{0,6},{3,5},{0,0},{0,0},{0,0}}, // F
m_wakayama 0:e705efefad3f 52 {{0,2},{0,6},{6,8},{5,8},{4,5},{0,0}}, // G
m_wakayama 0:e705efefad3f 53 {{0,6},{3,5},{2,8},{0,0},{0,0},{0,0}}, // H
m_wakayama 0:e705efefad3f 54 {{1,7},{0,0},{0,0},{0,0},{0,0},{0,0}}, // I
m_wakayama 0:e705efefad3f 55 {{2,8},{6,8},{3,6},{0,0},{0,0},{0,0}}, // J
m_wakayama 0:e705efefad3f 56 {{0,6},{2,3},{3,8},{0,0},{0,0},{0,0}}, // K
m_wakayama 0:e705efefad3f 57 {{0,6},{6,8},{0,0},{0,0},{0,0},{0,0}}, // L
m_wakayama 0:e705efefad3f 58 {{0,6},{0,4},{2,4},{2,8},{0,0},{0,0}}, // M
m_wakayama 0:e705efefad3f 59 {{0,6},{0,8},{2,8},{0,0},{0,0},{0,0}}, // N
m_wakayama 0:e705efefad3f 60 {{0,6},{0,2},{2,8},{6,8},{0,0},{0,0}}, // O
m_wakayama 0:e705efefad3f 61 {{0,2},{2,5},{3,5},{0,6},{0,0},{0,0}}, // P
m_wakayama 0:e705efefad3f 62 {{0,2},{0,6},{2,5},{5,7},{6,7},{4,8}}, // Q
m_wakayama 0:e705efefad3f 63 {{0,2},{2,5},{3,5},{0,6},{3,8},{0,0}}, // R
m_wakayama 0:e705efefad3f 64 {{0,2},{0,3},{3,5},{5,8},{6,8},{0,0}}, // S
m_wakayama 0:e705efefad3f 65 {{0,2},{1,7},{0,0},{0,0},{0,0},{0,0}}, // T
m_wakayama 0:e705efefad3f 66 {{0,6},{6,8},{2,8},{0,0},{0,0},{0,0}}, // U
m_wakayama 0:e705efefad3f 67 {{0,7},{2,7},{0,0},{0,0},{0,0},{0,0}}, // V
m_wakayama 0:e705efefad3f 68 {{0,6},{4,6},{4,8},{2,8},{0,0},{0,0}}, // W
m_wakayama 0:e705efefad3f 69 {{0,8},{2,6},{0,0},{0,0},{0,0},{0,0}}, // X
m_wakayama 0:e705efefad3f 70 {{0,4},{2,4},{4,7},{0,0},{0,0},{0,0}}, // Y
m_wakayama 0:e705efefad3f 71 {{0,2},{2,6},{6,8},{0,0},{0,0},{0,0}}, // Z
m_wakayama 0:e705efefad3f 72 };
m_wakayama 0:e705efefad3f 73 int buff2D[9][2];
m_wakayama 0:e705efefad3f 74 int asciiAngle;
m_wakayama 0:e705efefad3f 75
m_wakayama 0:e705efefad3f 76 void dotSet(int posX, int posY) {
m_wakayama 0:e705efefad3f 77 int ramAddress;
m_wakayama 0:e705efefad3f 78 uint8_t data;
m_wakayama 0:e705efefad3f 79 if ((posX >= 0) && (posX < 400) && (posY >= 0) && (posY < 240)) {
m_wakayama 0:e705efefad3f 80 ramAddress = (posX / 8) + (posY * 400 / 8);
m_wakayama 0:e705efefad3f 81 data = LCDRAM.ram_read(ramAddress);
m_wakayama 0:e705efefad3f 82 data |= 0x80 >> (posX % 8);
m_wakayama 0:e705efefad3f 83 LCDRAM.ram_write(ramAddress, data);
m_wakayama 0:e705efefad3f 84 }
m_wakayama 0:e705efefad3f 85 }
m_wakayama 0:e705efefad3f 86
m_wakayama 0:e705efefad3f 87 void drawLine(int posX1, int posY1, int posX2, int posY2) {
m_wakayama 0:e705efefad3f 88 float moveX = (float)(posX1 - posX2);
m_wakayama 0:e705efefad3f 89 float moveY = (float)(posY1 - posY2);
m_wakayama 0:e705efefad3f 90 int absX = abs((int)(moveX));
m_wakayama 0:e705efefad3f 91 int absY = abs((int)(moveY));
m_wakayama 0:e705efefad3f 92 int tmp;
m_wakayama 0:e705efefad3f 93 if (absX > absY) {
m_wakayama 0:e705efefad3f 94 tmp = absX;
m_wakayama 0:e705efefad3f 95 } else {
m_wakayama 0:e705efefad3f 96 tmp = absY;
m_wakayama 0:e705efefad3f 97 }
m_wakayama 0:e705efefad3f 98 moveX = moveX / (float)(tmp);
m_wakayama 0:e705efefad3f 99 moveY = moveY / (float)(tmp);
m_wakayama 0:e705efefad3f 100 for (int i = 0;i < tmp;i++) {
m_wakayama 0:e705efefad3f 101 dotSet(posX2 + moveX * i, posY2 + moveY * i);
m_wakayama 0:e705efefad3f 102 }
m_wakayama 0:e705efefad3f 103 }
m_wakayama 0:e705efefad3f 104
m_wakayama 0:e705efefad3f 105 void ram2lcdAll() {
m_wakayama 0:e705efefad3f 106 for (int y = 0;y < 240;y++) {
m_wakayama 0:e705efefad3f 107 LCDRAM.ram_read(y * 400 / 8,lineBuffer,400 / 8);
m_wakayama 0:e705efefad3f 108 LCDRAM.directUpdateSingle(y + 1,lineBuffer);
m_wakayama 0:e705efefad3f 109 }
m_wakayama 0:e705efefad3f 110 }
m_wakayama 0:e705efefad3f 111
m_wakayama 0:e705efefad3f 112 void calcBox() {
m_wakayama 0:e705efefad3f 113 float tmpX = boxAngleX * 3.141592653f / 180.0f;
m_wakayama 0:e705efefad3f 114 float tmpZ = boxAngleZ * 3.141592653f / 180.0f;
m_wakayama 0:e705efefad3f 115 float sinX = sin(tmpX);
m_wakayama 0:e705efefad3f 116 float cosX = cos(tmpX);
m_wakayama 0:e705efefad3f 117 float sinZ = sin(tmpZ);
m_wakayama 0:e705efefad3f 118 float cosZ = cos(tmpZ);
m_wakayama 0:e705efefad3f 119 for (int i = 0;i < 8;i++) {
m_wakayama 0:e705efefad3f 120 buff3D[i][0] = cosZ * boxPosi[i][0] - sinZ * boxPosi[i][1];
m_wakayama 0:e705efefad3f 121 buff3D[i][1] = sinZ * boxPosi[i][0] + cosZ * boxPosi[i][1];
m_wakayama 0:e705efefad3f 122 buff3D[i][2] = sinX * boxPosi[i][1] + cosX * boxPosi[i][2];
m_wakayama 0:e705efefad3f 123 buff3D[i][1] = cosX * boxPosi[i][1] - sinX * boxPosi[i][2];
m_wakayama 0:e705efefad3f 124 buff3D[i][0] = cosZ * boxPosi[i][0] - sinZ * buff3D[i][2];
m_wakayama 0:e705efefad3f 125 buff3D[i][2] = sinZ * boxPosi[i][0] + cosZ * buff3D[i][2];
m_wakayama 0:e705efefad3f 126 }
m_wakayama 0:e705efefad3f 127 }
m_wakayama 0:e705efefad3f 128
m_wakayama 0:e705efefad3f 129 void drawBox() {
m_wakayama 0:e705efefad3f 130 for (int i = 0;i < 12;i++) {
m_wakayama 0:e705efefad3f 131 drawLine((int)(buff3D[boxLink[i][0]][0]) + boxOffsetX, (int)(buff3D[boxLink[i][0]][1]) + boxOffsetY, (int)(buff3D[boxLink[i][1]][0]) + boxOffsetX, (int)(buff3D[boxLink[i][1]][1]) + boxOffsetY);
m_wakayama 0:e705efefad3f 132 }
m_wakayama 0:e705efefad3f 133 }
m_wakayama 0:e705efefad3f 134
m_wakayama 0:e705efefad3f 135 void drawAscii(uint8_t num,int posX,int posY,int angle) {
m_wakayama 0:e705efefad3f 136 float tmpA = angle * 3.141592653f / 180.0f;
m_wakayama 0:e705efefad3f 137 float tmpS = sin(tmpA);
m_wakayama 0:e705efefad3f 138 float tmpC = cos(tmpA);
m_wakayama 0:e705efefad3f 139 for (int i = 0;i < 9;i++) {
m_wakayama 0:e705efefad3f 140 buff2D[i][0] = (int)(tmpC * asciiPosi[i][0] - tmpS * asciiPosi[i][1]);
m_wakayama 0:e705efefad3f 141 buff2D[i][1] = (int)(tmpS * asciiPosi[i][0] + tmpC * asciiPosi[i][1]);
m_wakayama 0:e705efefad3f 142 }
m_wakayama 0:e705efefad3f 143 for (int i = 0;i < 6;i++) {
m_wakayama 0:e705efefad3f 144 if ((asciiLink[num][i][0] != 0) || (asciiLink[num][i][1] != 0)) {
m_wakayama 0:e705efefad3f 145 drawLine(buff2D[asciiLink[num][i][0]][0] + posX,buff2D[asciiLink[num][i][0]][1] + posY,buff2D[asciiLink[num][i][1]][0] + posX,buff2D[asciiLink[num][i][1]][1] + posY);
m_wakayama 0:e705efefad3f 146 }
m_wakayama 0:e705efefad3f 147 }
m_wakayama 0:e705efefad3f 148 }
m_wakayama 0:e705efefad3f 149
m_wakayama 0:e705efefad3f 150 void rollAscii() {
m_wakayama 0:e705efefad3f 151 for (int i = 0;i < (10 + 26);i++) {
m_wakayama 0:e705efefad3f 152 int angle = (asciiAngle + i * 8) % 360;
m_wakayama 0:e705efefad3f 153 float tmpA = angle * 3.141592653f / 180.0f;
m_wakayama 0:e705efefad3f 154 float tmpS = sin(tmpA);
m_wakayama 0:e705efefad3f 155 float tmpC = cos(tmpA);
m_wakayama 0:e705efefad3f 156 drawAscii(i,(int)(tmpC * 400.0f / 2.9f - tmpS * 400.0f / 2.9f + 400.0f / 2.0f),(int)(tmpS * 240.0f / 2.9f + tmpC * 240.0f / 2.9f + 240.0f / 2.0f),(angle + 135) % 360);
m_wakayama 0:e705efefad3f 157 }
m_wakayama 0:e705efefad3f 158 }
m_wakayama 0:e705efefad3f 159 // --------------------------------------------------------------------
m_wakayama 0:e705efefad3f 160 // --------------------------------------------------------------------
m_wakayama 0:e705efefad3f 161 uint16_t vaders[5];
m_wakayama 0:e705efefad3f 162 uint8_t vaderLine[5] = {0,1,1,2,2};
m_wakayama 0:e705efefad3f 163 uint8_t cgBuffer[3][2][2 * 8] = {
m_wakayama 0:e705efefad3f 164 {{0x03,0x00,0x07,0x80,0x0f,0xc0,0x1b,0x60,0x1f,0xe0,0x04,0x80,0x0b,0x40,0x14,0xa0},
m_wakayama 0:e705efefad3f 165 {0x00,0x30,0x00,0x78,0x00,0xfc,0x01,0xb6,0x01,0xfe,0x00,0x84,0x01,0x02,0x00,0x84}},
m_wakayama 0:e705efefad3f 166 {{0x08,0x20,0x04,0x40,0x0f,0xe0,0x1b,0xb0,0x3f,0xf8,0x2f,0xe8,0x28,0x28,0x06,0xc0},
m_wakayama 0:e705efefad3f 167 {0x01,0x04,0x04,0x89,0x05,0xfd,0x0b,0xb7,0x07,0xff,0x01,0xfc,0x01,0x04,0x00,0xd8}},
m_wakayama 0:e705efefad3f 168 {{0x0f,0x00,0x7f,0xe0,0xff,0xf0,0xe6,0x70,0xff,0xf0,0x19,0x80,0x36,0xc0,0xc0,0x30},
m_wakayama 0:e705efefad3f 169 {
m_wakayama 0:e705efefad3f 170 0x00,0xf0,
m_wakayama 0:e705efefad3f 171 0x07,0xfe,
m_wakayama 0:e705efefad3f 172 0x0f,0xff,
m_wakayama 0:e705efefad3f 173 0x0e,0x67,
m_wakayama 0:e705efefad3f 174 0x0f,0xff,
m_wakayama 0:e705efefad3f 175 0x01,0x98,
m_wakayama 0:e705efefad3f 176 0x06,0x66,
m_wakayama 0:e705efefad3f 177 0x01,0x98,
m_wakayama 0:e705efefad3f 178 }},
m_wakayama 0:e705efefad3f 179 };
m_wakayama 0:e705efefad3f 180 void draw_vaders() {
m_wakayama 0:e705efefad3f 181 uint8_t counter,tmp;
m_wakayama 0:e705efefad3f 182 for (int y = 0;y < 5;y++) {
m_wakayama 0:e705efefad3f 183 for (int x = 0;x < 11;x++) {
m_wakayama 0:e705efefad3f 184 if ((vaders[y] & (0x400 >> x)) != 0) {
m_wakayama 0:e705efefad3f 185 counter = 0;
m_wakayama 0:e705efefad3f 186 for (int i = 0;i < 8;i++) {
m_wakayama 0:e705efefad3f 187 tmp = cgBuffer[vaderLine[y]][(mainCounter + scrollY) & 1][counter++];
m_wakayama 0:e705efefad3f 188 if (tmp != 0) {
m_wakayama 0:e705efefad3f 189 LCDRAM.ram_write((((y + scrollY) * 12 + i) * 400 / 8) + (x * 2 + scrollX + 0),tmp);
m_wakayama 0:e705efefad3f 190 }
m_wakayama 0:e705efefad3f 191 tmp = cgBuffer[vaderLine[y]][(mainCounter + scrollY) & 1][counter++];
m_wakayama 0:e705efefad3f 192 if (tmp != 0) {
m_wakayama 0:e705efefad3f 193 LCDRAM.ram_write((((y + scrollY) * 12 + i) * 400 / 8) + (x * 2 + scrollX + 1),tmp);
m_wakayama 0:e705efefad3f 194 }
m_wakayama 0:e705efefad3f 195 }
m_wakayama 0:e705efefad3f 196 }
m_wakayama 0:e705efefad3f 197 }
m_wakayama 0:e705efefad3f 198 }
m_wakayama 0:e705efefad3f 199 }
m_wakayama 0:e705efefad3f 200 void init_vaders() {
m_wakayama 0:e705efefad3f 201 for (int i = 0;i < 5;i++) {
m_wakayama 0:e705efefad3f 202 vaders[i] = 0x7ff;
m_wakayama 0:e705efefad3f 203 }
m_wakayama 0:e705efefad3f 204 }
m_wakayama 0:e705efefad3f 205 void gameControl() {
m_wakayama 0:e705efefad3f 206 if ((mainCounter & 1) == 0) {
m_wakayama 0:e705efefad3f 207 if ((scrollY & 1) == 0) {
m_wakayama 0:e705efefad3f 208 if (++scrollX > 28) {
m_wakayama 0:e705efefad3f 209 scrollX--;
m_wakayama 0:e705efefad3f 210 scrollY++;
m_wakayama 0:e705efefad3f 211 }
m_wakayama 0:e705efefad3f 212 } else {
m_wakayama 0:e705efefad3f 213 if (--scrollX < 0) {
m_wakayama 0:e705efefad3f 214 scrollX++;
m_wakayama 0:e705efefad3f 215 scrollY++;
m_wakayama 0:e705efefad3f 216 }
m_wakayama 0:e705efefad3f 217 }
m_wakayama 0:e705efefad3f 218 }
m_wakayama 0:e705efefad3f 219 }
m_wakayama 0:e705efefad3f 220 // --------------------------------------------------------------------
m_wakayama 0:e705efefad3f 221 // --------------------------------------------------------------------
m_wakayama 0:e705efefad3f 222
m_wakayama 0:e705efefad3f 223 int main() {
m_wakayama 0:e705efefad3f 224 wait_ms(1);
m_wakayama 0:e705efefad3f 225 LCDRAM.cls();
m_wakayama 0:e705efefad3f 226 boxOffsetX = 400 / 2;
m_wakayama 0:e705efefad3f 227 boxOffsetY = 240 / 2;
m_wakayama 0:e705efefad3f 228 boxAngleX = 0;
m_wakayama 0:e705efefad3f 229 boxAngleZ = 0;
m_wakayama 0:e705efefad3f 230 init_vaders();
m_wakayama 0:e705efefad3f 231 while(1) {
m_wakayama 0:e705efefad3f 232 mainCounter++;
m_wakayama 0:e705efefad3f 233 gameControl();
m_wakayama 0:e705efefad3f 234 LCDRAM.cls_ram(SCREEN0);
m_wakayama 0:e705efefad3f 235 draw_vaders();
m_wakayama 0:e705efefad3f 236 calcBox();
m_wakayama 0:e705efefad3f 237 drawBox();
m_wakayama 0:e705efefad3f 238 rollAscii();
m_wakayama 0:e705efefad3f 239 ram2lcdAll();
m_wakayama 0:e705efefad3f 240 boxAngleX = (boxAngleX + 2) % 360;
m_wakayama 0:e705efefad3f 241 boxAngleZ = (boxAngleZ + 3) % 360;
m_wakayama 0:e705efefad3f 242 asciiAngle = (asciiAngle + 358) % 360;
m_wakayama 0:e705efefad3f 243 }
m_wakayama 0:e705efefad3f 244 }