Retro Invaders a space invaders clone by Chris Favreau. Written for the RetroMbuino development board from outrageouscircuits.com for the game programming contest.

Dependencies:   mbed

This is a space invaders clone written for the Retro Mbuino from outrageous circuits.

Development board: http://www.outrageouscircuits.com/shop/product/15 ).

The game itself is basic space invaders. Shoot them before they get to the bottom of the screen. It has a UFO saucer which you can shoot for extra points. You get 4 shields and each shield can be hit up to 4 times before it is gone. Hmm... as each level increases the speed of the invaders shots goes up. The invaders only speed up when there is less of them. You complete the level when you shoot all the invaders. The game ends when a) you run out of lives (you start with 3) or the invaders get to the bottom.

The LEDs turned out to be a pretty cool addition to the game. I wrote a class that blinks them and turns them on for a specified amount of time. They add a nice extra to the game. I use them on the intro screen and when the UFO is present.

The sound turned out to be really difficult for a few reasons. The biggest was that I had never written a sound engine before. The interrupt service routine working off the timer was the easier part. I also had a lot of trouble because there is no filter to filter out the PWM frequency to the speaker... so I had to run the PWM frequency way up there 30 kHz.

The graphics turned out to be a bit of a bear too. Thanks to Chris Taylor for his really great LCD API. I picked up a couple of frames per second from that. I had modified the DisplayN18 class for blitting a single line buffer to the LCD panel however his is a little faster for some reason? I used a different approach to doing the graphics (as I have very little experience with anything other than double buffered displays). I have a tile map and a list of sprites. Each tile/sprite is 1 bit 8x8. They could be bigger. I ran out of time. That much is not special. What is different from what I can tell is that I use a 1 line buffer that is 160 shorts long. The render function first adds the tile map data into the line buffer first. Then the sprites are added over the existing data. You can have a great deal of different sprites and maps going to the screen and just have to rewrite the LCD memory once per frame. After each line is composited, the line is then drawn to the LCD. Kind of like an Atari 2600. Each sprite/tile has a foreground and background color and can be different from the other tiles/sprites. There is one color reserved for Transparency.

There are 16 colors to choose from. I chose a palette based on the Macintosh OS 4.1 palette I found on WikiPedia. It is a very nice mix of colors.

I found a sprite editor called SpriteX ( https://code.google.com/p/spritesx-ed/ )... it works nicely except that the 16x16 sprites are in a weird format. Time limited me to 8x8 sprites. Oh well.

I used nokring to make the music. It makes RTTTL formatted ring tones which my sound api can play. Here is a useful site that has lots of arcade/video game ring tones with a link to nokring in the utilities page. http://arcadetones.emuunlim.com/files.htm

Other than all that stuff I used state machines to do most of the game logic. Please excuse the horrible coding as I tried to comment a lot of it however it is not very nice to look at. Lots of long functions...

Committer:
cfavreau
Date:
Tue Mar 03 04:26:01 2015 +0000
Revision:
0:c79e1f29f029
Retro Invaders by Chris Favreau for the RetroMbuino Platform - outrageouscircuits.com game programming contest.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cfavreau 0:c79e1f29f029 1 #include "mbed.h"
cfavreau 0:c79e1f29f029 2 #include "LCD_ST7735.h"
cfavreau 0:c79e1f29f029 3
cfavreau 0:c79e1f29f029 4 LCD_ST7735::LCD_ST7735(
cfavreau 0:c79e1f29f029 5 PinName backlightPin,
cfavreau 0:c79e1f29f029 6 PinName resetPin,
cfavreau 0:c79e1f29f029 7 PinName dsPin,
cfavreau 0:c79e1f29f029 8 PinName mosiPin,
cfavreau 0:c79e1f29f029 9 PinName misoPin,
cfavreau 0:c79e1f29f029 10 PinName clkPin,
cfavreau 0:c79e1f29f029 11 PinName csPin,
cfavreau 0:c79e1f29f029 12 PanelColorFilter colorFilter
cfavreau 0:c79e1f29f029 13 ) :
cfavreau 0:c79e1f29f029 14 _colorFilter(colorFilter),
cfavreau 0:c79e1f29f029 15 _backlight(backlightPin, 0),
cfavreau 0:c79e1f29f029 16 _reset(resetPin, 1),
cfavreau 0:c79e1f29f029 17 _ds(dsPin, 0),
cfavreau 0:c79e1f29f029 18 _cs(csPin, 1),
cfavreau 0:c79e1f29f029 19 _spi(mosiPin, misoPin, clkPin)
cfavreau 0:c79e1f29f029 20 {
cfavreau 0:c79e1f29f029 21 _spi.format(8, 3);
cfavreau 0:c79e1f29f029 22 _spi.frequency(15000000);
cfavreau 0:c79e1f29f029 23
cfavreau 0:c79e1f29f029 24 initDisplay();
cfavreau 0:c79e1f29f029 25 setOrientation(Rotate270, false);
cfavreau 0:c79e1f29f029 26 clearScreen();
cfavreau 0:c79e1f29f029 27 setForegroundColor(0xffff);
cfavreau 0:c79e1f29f029 28 setBackgroundColor(0x0000);
cfavreau 0:c79e1f29f029 29 }
cfavreau 0:c79e1f29f029 30
cfavreau 0:c79e1f29f029 31 void LCD_ST7735::setOrientation(Orientation orientation, bool flip)
cfavreau 0:c79e1f29f029 32 {
cfavreau 0:c79e1f29f029 33 const static uint8_t my = 0x80;
cfavreau 0:c79e1f29f029 34 const static uint8_t mx = 0x40;
cfavreau 0:c79e1f29f029 35 const static uint8_t mv = 0x20;
cfavreau 0:c79e1f29f029 36
cfavreau 0:c79e1f29f029 37 uint8_t madctlData = _colorFilter;
cfavreau 0:c79e1f29f029 38 switch(orientation)
cfavreau 0:c79e1f29f029 39 {
cfavreau 0:c79e1f29f029 40 case Rotate0:
cfavreau 0:c79e1f29f029 41 _width = 128;
cfavreau 0:c79e1f29f029 42 _height = 160;
cfavreau 0:c79e1f29f029 43 madctlData |= flip ? mx : 0;
cfavreau 0:c79e1f29f029 44 break;
cfavreau 0:c79e1f29f029 45
cfavreau 0:c79e1f29f029 46 case Rotate90:
cfavreau 0:c79e1f29f029 47 _width = 160;
cfavreau 0:c79e1f29f029 48 _height = 128;
cfavreau 0:c79e1f29f029 49 madctlData |= flip ? my | mv | mx : mv | mx;
cfavreau 0:c79e1f29f029 50 break;
cfavreau 0:c79e1f29f029 51
cfavreau 0:c79e1f29f029 52 case Rotate180:
cfavreau 0:c79e1f29f029 53 _width = 128;
cfavreau 0:c79e1f29f029 54 _height = 160;
cfavreau 0:c79e1f29f029 55 madctlData |= flip ? my : mx | my;
cfavreau 0:c79e1f29f029 56 break;
cfavreau 0:c79e1f29f029 57
cfavreau 0:c79e1f29f029 58 case Rotate270:
cfavreau 0:c79e1f29f029 59 _width = 160;
cfavreau 0:c79e1f29f029 60 _height = 128;
cfavreau 0:c79e1f29f029 61 madctlData |= flip ? mv : mv | my;
cfavreau 0:c79e1f29f029 62 break;
cfavreau 0:c79e1f29f029 63 }
cfavreau 0:c79e1f29f029 64 write(CMD_MADCTL, (uint8_t[]){madctlData}, 1);
cfavreau 0:c79e1f29f029 65 }
cfavreau 0:c79e1f29f029 66
cfavreau 0:c79e1f29f029 67 int LCD_ST7735::getWidth()
cfavreau 0:c79e1f29f029 68 {
cfavreau 0:c79e1f29f029 69 return _width;
cfavreau 0:c79e1f29f029 70 }
cfavreau 0:c79e1f29f029 71
cfavreau 0:c79e1f29f029 72 int LCD_ST7735::getHeight()
cfavreau 0:c79e1f29f029 73 {
cfavreau 0:c79e1f29f029 74 return _height;
cfavreau 0:c79e1f29f029 75 }
cfavreau 0:c79e1f29f029 76
cfavreau 0:c79e1f29f029 77 void LCD_ST7735::setBacklight(bool state)
cfavreau 0:c79e1f29f029 78 {
cfavreau 0:c79e1f29f029 79 _backlight = state ? 1 : 0;
cfavreau 0:c79e1f29f029 80 }
cfavreau 0:c79e1f29f029 81
cfavreau 0:c79e1f29f029 82 void LCD_ST7735::clearScreen(uint16_t color)
cfavreau 0:c79e1f29f029 83 {
cfavreau 0:c79e1f29f029 84 clipRect(0, 0, _width - 1, _height - 1);
cfavreau 0:c79e1f29f029 85 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 86 uint8_t colorHigh = color >> 8;
cfavreau 0:c79e1f29f029 87 uint8_t colorLow = color;
cfavreau 0:c79e1f29f029 88 for(int i = 0; i < 128 * 160 * 2; ++i)
cfavreau 0:c79e1f29f029 89 {
cfavreau 0:c79e1f29f029 90 writeBatchData(colorHigh, colorLow);
cfavreau 0:c79e1f29f029 91 }
cfavreau 0:c79e1f29f029 92 endBatchCommand();
cfavreau 0:c79e1f29f029 93 }
cfavreau 0:c79e1f29f029 94
cfavreau 0:c79e1f29f029 95 void LCD_ST7735::setPixel(int x, int y, uint16_t color)
cfavreau 0:c79e1f29f029 96 {
cfavreau 0:c79e1f29f029 97 write(CMD_CASET, (uint8_t[]){0, x, 0, x}, 4);
cfavreau 0:c79e1f29f029 98 write(CMD_RASET, (uint8_t[]){0, y, 0, y}, 4);
cfavreau 0:c79e1f29f029 99 write(CMD_RAMWR, color);
cfavreau 0:c79e1f29f029 100 }
cfavreau 0:c79e1f29f029 101
cfavreau 0:c79e1f29f029 102 void LCD_ST7735::drawLine(int x1, int y1, int x2, int y2, uint16_t color)
cfavreau 0:c79e1f29f029 103 {
cfavreau 0:c79e1f29f029 104 int dx = abs(x2 - x1);
cfavreau 0:c79e1f29f029 105 int dy = abs(y2 - y1);
cfavreau 0:c79e1f29f029 106
cfavreau 0:c79e1f29f029 107 if (dx == 0)
cfavreau 0:c79e1f29f029 108 {
cfavreau 0:c79e1f29f029 109 drawVertLine(x1, y1, y2, color);
cfavreau 0:c79e1f29f029 110 return;
cfavreau 0:c79e1f29f029 111 }
cfavreau 0:c79e1f29f029 112 else if(dy == 0)
cfavreau 0:c79e1f29f029 113 {
cfavreau 0:c79e1f29f029 114 drawHorizLine(x1, y1, x2, color);
cfavreau 0:c79e1f29f029 115 return;
cfavreau 0:c79e1f29f029 116 }
cfavreau 0:c79e1f29f029 117
cfavreau 0:c79e1f29f029 118 int sx = (x1 < x2) ? 1 : -1;
cfavreau 0:c79e1f29f029 119 int sy = (y1 < y2) ? 1 : -1;
cfavreau 0:c79e1f29f029 120 int err = dx - dy;
cfavreau 0:c79e1f29f029 121 while(x1 != x2 || y1 != y2)
cfavreau 0:c79e1f29f029 122 {
cfavreau 0:c79e1f29f029 123 setPixel(x1, y1, color);
cfavreau 0:c79e1f29f029 124 int e2 = err << 1;
cfavreau 0:c79e1f29f029 125 if (e2 > -dy)
cfavreau 0:c79e1f29f029 126 {
cfavreau 0:c79e1f29f029 127 err -= dy;
cfavreau 0:c79e1f29f029 128 x1 += sx;
cfavreau 0:c79e1f29f029 129 }
cfavreau 0:c79e1f29f029 130 if (e2 < dx)
cfavreau 0:c79e1f29f029 131 {
cfavreau 0:c79e1f29f029 132 err += dx;
cfavreau 0:c79e1f29f029 133 y1 += sy;
cfavreau 0:c79e1f29f029 134 }
cfavreau 0:c79e1f29f029 135 }
cfavreau 0:c79e1f29f029 136 setPixel(x2, y2, color);
cfavreau 0:c79e1f29f029 137 }
cfavreau 0:c79e1f29f029 138
cfavreau 0:c79e1f29f029 139 void LCD_ST7735::swap(int &a, int &b)
cfavreau 0:c79e1f29f029 140 {
cfavreau 0:c79e1f29f029 141 int t = a;
cfavreau 0:c79e1f29f029 142 a = b;
cfavreau 0:c79e1f29f029 143 b = t;
cfavreau 0:c79e1f29f029 144 }
cfavreau 0:c79e1f29f029 145
cfavreau 0:c79e1f29f029 146 void LCD_ST7735::drawRect(int x1, int y1, int x2, int y2, uint16_t color)
cfavreau 0:c79e1f29f029 147 {
cfavreau 0:c79e1f29f029 148 if (x1 > x2) swap(x1, x2);
cfavreau 0:c79e1f29f029 149 if (y1 > y2) swap(y1, y2);
cfavreau 0:c79e1f29f029 150
cfavreau 0:c79e1f29f029 151 drawHorizLine(x1, y1, x2, color);
cfavreau 0:c79e1f29f029 152 drawHorizLine(x1, y2, x2, color);
cfavreau 0:c79e1f29f029 153 drawVertLine(x1, y1, y2, color);
cfavreau 0:c79e1f29f029 154 drawVertLine(x2, y1, y2, color);
cfavreau 0:c79e1f29f029 155 }
cfavreau 0:c79e1f29f029 156
cfavreau 0:c79e1f29f029 157 void LCD_ST7735::drawCircle(int x, int y, int r, uint16_t color)
cfavreau 0:c79e1f29f029 158 {
cfavreau 0:c79e1f29f029 159 int ix = r;
cfavreau 0:c79e1f29f029 160 int iy = 0;
cfavreau 0:c79e1f29f029 161 int err = 1 - r;
cfavreau 0:c79e1f29f029 162
cfavreau 0:c79e1f29f029 163 while(ix >= iy)
cfavreau 0:c79e1f29f029 164 {
cfavreau 0:c79e1f29f029 165 setPixel(x + ix, y + iy, color);
cfavreau 0:c79e1f29f029 166 setPixel(x + iy, y + ix, color);
cfavreau 0:c79e1f29f029 167 setPixel(x - ix, y + iy, color);
cfavreau 0:c79e1f29f029 168 setPixel(x - iy, y + ix, color);
cfavreau 0:c79e1f29f029 169 setPixel(x - ix, y - iy, color);
cfavreau 0:c79e1f29f029 170 setPixel(x - iy, y - ix, color);
cfavreau 0:c79e1f29f029 171 setPixel(x + ix, y - iy, color);
cfavreau 0:c79e1f29f029 172 setPixel(x + iy, y - ix, color);
cfavreau 0:c79e1f29f029 173 iy++;
cfavreau 0:c79e1f29f029 174 if (err < 0)
cfavreau 0:c79e1f29f029 175 {
cfavreau 0:c79e1f29f029 176 err += 2 * iy + 1;
cfavreau 0:c79e1f29f029 177 }
cfavreau 0:c79e1f29f029 178 else
cfavreau 0:c79e1f29f029 179 {
cfavreau 0:c79e1f29f029 180 ix--;
cfavreau 0:c79e1f29f029 181 err += 2 * (iy - ix + 1);
cfavreau 0:c79e1f29f029 182 }
cfavreau 0:c79e1f29f029 183 }
cfavreau 0:c79e1f29f029 184 }
cfavreau 0:c79e1f29f029 185
cfavreau 0:c79e1f29f029 186 void LCD_ST7735::drawEllipse(int x, int y, int rx, int ry, uint16_t color)
cfavreau 0:c79e1f29f029 187 {
cfavreau 0:c79e1f29f029 188 int a2 = rx * rx;
cfavreau 0:c79e1f29f029 189 int b2 = ry * ry;
cfavreau 0:c79e1f29f029 190 int fa2 = 4 * a2;
cfavreau 0:c79e1f29f029 191 int fb2 = 4 * b2;
cfavreau 0:c79e1f29f029 192
cfavreau 0:c79e1f29f029 193 int ix, iy, sigma;
cfavreau 0:c79e1f29f029 194 for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
cfavreau 0:c79e1f29f029 195 {
cfavreau 0:c79e1f29f029 196 setPixel(x + ix, y + iy, color);
cfavreau 0:c79e1f29f029 197 setPixel(x - ix, y + iy, color);
cfavreau 0:c79e1f29f029 198 setPixel(x + ix, y - iy, color);
cfavreau 0:c79e1f29f029 199 setPixel(x - ix, y - iy, color);
cfavreau 0:c79e1f29f029 200 if (sigma >= 0)
cfavreau 0:c79e1f29f029 201 {
cfavreau 0:c79e1f29f029 202 sigma+= fa2 * (1 - iy);
cfavreau 0:c79e1f29f029 203 iy--;
cfavreau 0:c79e1f29f029 204 }
cfavreau 0:c79e1f29f029 205 sigma += b2 * ((4 * ix) + 6);
cfavreau 0:c79e1f29f029 206 }
cfavreau 0:c79e1f29f029 207
cfavreau 0:c79e1f29f029 208 for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
cfavreau 0:c79e1f29f029 209 {
cfavreau 0:c79e1f29f029 210 setPixel(x + ix, y + iy, color);
cfavreau 0:c79e1f29f029 211 setPixel(x - ix, y + iy, color);
cfavreau 0:c79e1f29f029 212 setPixel(x + ix, y - iy, color);
cfavreau 0:c79e1f29f029 213 setPixel(x - ix, y - iy, color);
cfavreau 0:c79e1f29f029 214 if (sigma >= 0)
cfavreau 0:c79e1f29f029 215 {
cfavreau 0:c79e1f29f029 216 sigma+= fb2 * (1 - ix);
cfavreau 0:c79e1f29f029 217 ix--;
cfavreau 0:c79e1f29f029 218 }
cfavreau 0:c79e1f29f029 219 sigma += a2 * ((4 * iy) + 6);
cfavreau 0:c79e1f29f029 220 }
cfavreau 0:c79e1f29f029 221 }
cfavreau 0:c79e1f29f029 222 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t fillColor)
cfavreau 0:c79e1f29f029 223 {
cfavreau 0:c79e1f29f029 224 if (x1 > x2) swap(x1, x2);
cfavreau 0:c79e1f29f029 225 if (y1 > y2) swap(y1, y2);
cfavreau 0:c79e1f29f029 226
cfavreau 0:c79e1f29f029 227 clipRect(x1, y1, x2, y2);
cfavreau 0:c79e1f29f029 228 int c = ((x2-x1) * (y2-y1)) << 1;
cfavreau 0:c79e1f29f029 229 uint8_t colorHigh = fillColor >> 8;
cfavreau 0:c79e1f29f029 230 uint8_t colorLow = fillColor;
cfavreau 0:c79e1f29f029 231 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 232 while(c--)
cfavreau 0:c79e1f29f029 233 {
cfavreau 0:c79e1f29f029 234 writeBatchData(colorHigh, colorLow);
cfavreau 0:c79e1f29f029 235 }
cfavreau 0:c79e1f29f029 236 endBatchCommand();
cfavreau 0:c79e1f29f029 237 }
cfavreau 0:c79e1f29f029 238
cfavreau 0:c79e1f29f029 239 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor)
cfavreau 0:c79e1f29f029 240 {
cfavreau 0:c79e1f29f029 241 if (x1 > x2) swap(x1, x2);
cfavreau 0:c79e1f29f029 242 if (y1 > y2) swap(y1, y2);
cfavreau 0:c79e1f29f029 243
cfavreau 0:c79e1f29f029 244 drawRect(x1, y1, x2, y2, borderColor);
cfavreau 0:c79e1f29f029 245 ++x1; ++y1; --x2; --y2;
cfavreau 0:c79e1f29f029 246 if (x2 >= x1 && y2 >= y1)
cfavreau 0:c79e1f29f029 247 {
cfavreau 0:c79e1f29f029 248 int c = ((x2 + 1 - x1) * (y2 + 1 - y1)) << 1;
cfavreau 0:c79e1f29f029 249
cfavreau 0:c79e1f29f029 250 clipRect(x1, y1, x2, y2);
cfavreau 0:c79e1f29f029 251 uint8_t colorHigh = fillColor >> 8;
cfavreau 0:c79e1f29f029 252 uint8_t colorLow = fillColor;
cfavreau 0:c79e1f29f029 253 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 254 while(c--)
cfavreau 0:c79e1f29f029 255 {
cfavreau 0:c79e1f29f029 256 writeBatchData(colorHigh, colorLow);
cfavreau 0:c79e1f29f029 257 }
cfavreau 0:c79e1f29f029 258 endBatchCommand();
cfavreau 0:c79e1f29f029 259 }
cfavreau 0:c79e1f29f029 260 }
cfavreau 0:c79e1f29f029 261
cfavreau 0:c79e1f29f029 262 void LCD_ST7735::fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor)
cfavreau 0:c79e1f29f029 263 {
cfavreau 0:c79e1f29f029 264 int ix = r;
cfavreau 0:c79e1f29f029 265 int iy = 0;
cfavreau 0:c79e1f29f029 266 int err = 1 - r;
cfavreau 0:c79e1f29f029 267
cfavreau 0:c79e1f29f029 268 while(ix >= iy)
cfavreau 0:c79e1f29f029 269 {
cfavreau 0:c79e1f29f029 270 setPixel(x - ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 271 setPixel(x + ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 272 drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 273
cfavreau 0:c79e1f29f029 274 setPixel(x - iy, y + ix, borderColor);
cfavreau 0:c79e1f29f029 275 setPixel(x + iy, y + ix, borderColor);
cfavreau 0:c79e1f29f029 276 drawHorizLine(x - iy + 1, y + ix, x + iy - 1, fillColor);
cfavreau 0:c79e1f29f029 277
cfavreau 0:c79e1f29f029 278 setPixel(x - ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 279 setPixel(x + ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 280 drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 281
cfavreau 0:c79e1f29f029 282 setPixel(x - iy, y - ix, borderColor);
cfavreau 0:c79e1f29f029 283 setPixel(x + iy, y - ix, borderColor);
cfavreau 0:c79e1f29f029 284 drawHorizLine(x - iy + 1, y - ix, x + iy - 1, fillColor);
cfavreau 0:c79e1f29f029 285 iy++;
cfavreau 0:c79e1f29f029 286 if (err < 0)
cfavreau 0:c79e1f29f029 287 {
cfavreau 0:c79e1f29f029 288 err += 2 * iy + 1;
cfavreau 0:c79e1f29f029 289 }
cfavreau 0:c79e1f29f029 290 else
cfavreau 0:c79e1f29f029 291 {
cfavreau 0:c79e1f29f029 292 ix--;
cfavreau 0:c79e1f29f029 293 err += 2 * (iy - ix + 1);
cfavreau 0:c79e1f29f029 294 }
cfavreau 0:c79e1f29f029 295 }
cfavreau 0:c79e1f29f029 296 }
cfavreau 0:c79e1f29f029 297
cfavreau 0:c79e1f29f029 298 void LCD_ST7735::fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor)
cfavreau 0:c79e1f29f029 299 {
cfavreau 0:c79e1f29f029 300 int a2 = rx * rx;
cfavreau 0:c79e1f29f029 301 int b2 = ry * ry;
cfavreau 0:c79e1f29f029 302 int fa2 = 4 * a2;
cfavreau 0:c79e1f29f029 303 int fb2 = 4 * b2;
cfavreau 0:c79e1f29f029 304
cfavreau 0:c79e1f29f029 305 int ix, iy, sigma;
cfavreau 0:c79e1f29f029 306 for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
cfavreau 0:c79e1f29f029 307 {
cfavreau 0:c79e1f29f029 308 setPixel(x + ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 309 setPixel(x - ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 310 drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 311
cfavreau 0:c79e1f29f029 312 setPixel(x + ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 313 setPixel(x - ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 314 drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 315
cfavreau 0:c79e1f29f029 316 if (sigma >= 0)
cfavreau 0:c79e1f29f029 317 {
cfavreau 0:c79e1f29f029 318 sigma+= fa2 * (1 - iy);
cfavreau 0:c79e1f29f029 319 iy--;
cfavreau 0:c79e1f29f029 320 }
cfavreau 0:c79e1f29f029 321 sigma += b2 * ((4 * ix) + 6);
cfavreau 0:c79e1f29f029 322 }
cfavreau 0:c79e1f29f029 323
cfavreau 0:c79e1f29f029 324 for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
cfavreau 0:c79e1f29f029 325 {
cfavreau 0:c79e1f29f029 326 setPixel(x + ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 327 setPixel(x - ix, y + iy, borderColor);
cfavreau 0:c79e1f29f029 328 drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 329
cfavreau 0:c79e1f29f029 330 setPixel(x + ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 331 setPixel(x - ix, y - iy, borderColor);
cfavreau 0:c79e1f29f029 332 drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
cfavreau 0:c79e1f29f029 333 if (sigma >= 0)
cfavreau 0:c79e1f29f029 334 {
cfavreau 0:c79e1f29f029 335 sigma+= fb2 * (1 - ix);
cfavreau 0:c79e1f29f029 336 ix--;
cfavreau 0:c79e1f29f029 337 }
cfavreau 0:c79e1f29f029 338 sigma += a2 * ((4 * iy) + 6);
cfavreau 0:c79e1f29f029 339 }
cfavreau 0:c79e1f29f029 340 }
cfavreau 0:c79e1f29f029 341
cfavreau 0:c79e1f29f029 342 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp)
cfavreau 0:c79e1f29f029 343 {
cfavreau 0:c79e1f29f029 344 int w = *pbmp++;
cfavreau 0:c79e1f29f029 345 int h = *pbmp++;
cfavreau 0:c79e1f29f029 346
cfavreau 0:c79e1f29f029 347 clip(x, y, w, h);
cfavreau 0:c79e1f29f029 348 int c = w * h;
cfavreau 0:c79e1f29f029 349 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 350 while(c--)
cfavreau 0:c79e1f29f029 351 {
cfavreau 0:c79e1f29f029 352 writeBatchData(*pbmp++);
cfavreau 0:c79e1f29f029 353 }
cfavreau 0:c79e1f29f029 354 endBatchCommand();
cfavreau 0:c79e1f29f029 355 }
cfavreau 0:c79e1f29f029 356
cfavreau 0:c79e1f29f029 357 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight)
cfavreau 0:c79e1f29f029 358 {
cfavreau 0:c79e1f29f029 359 int w = *pbmp++;
cfavreau 0:c79e1f29f029 360 int h = *pbmp++;
cfavreau 0:c79e1f29f029 361
cfavreau 0:c79e1f29f029 362 clip(x, y, srcWidth, srcHeight);
cfavreau 0:c79e1f29f029 363 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 364 const uint16_t *p = pbmp + srcX + (srcY * w);
cfavreau 0:c79e1f29f029 365 for(int iy = 0; iy < srcHeight; ++iy)
cfavreau 0:c79e1f29f029 366 {
cfavreau 0:c79e1f29f029 367 for(int ix = 0; ix < srcWidth; ++ix)
cfavreau 0:c79e1f29f029 368 {
cfavreau 0:c79e1f29f029 369 writeBatchData(*(p + ix));
cfavreau 0:c79e1f29f029 370 }
cfavreau 0:c79e1f29f029 371 p += w;
cfavreau 0:c79e1f29f029 372 }
cfavreau 0:c79e1f29f029 373 endBatchCommand();
cfavreau 0:c79e1f29f029 374 }
cfavreau 0:c79e1f29f029 375
cfavreau 0:c79e1f29f029 376 void LCD_ST7735::setForegroundColor(uint16_t color)
cfavreau 0:c79e1f29f029 377 {
cfavreau 0:c79e1f29f029 378 _foregroundColorHigh = color >> 8;
cfavreau 0:c79e1f29f029 379 _foregroundColorLow = color;
cfavreau 0:c79e1f29f029 380 }
cfavreau 0:c79e1f29f029 381
cfavreau 0:c79e1f29f029 382 void LCD_ST7735::setBackgroundColor(uint16_t color)
cfavreau 0:c79e1f29f029 383 {
cfavreau 0:c79e1f29f029 384 _backgroundColorHigh = color >> 8;
cfavreau 0:c79e1f29f029 385 _backgroundColorLow = color;
cfavreau 0:c79e1f29f029 386 }
cfavreau 0:c79e1f29f029 387
cfavreau 0:c79e1f29f029 388 void LCD_ST7735::drawString(const uint8_t *pFont, int x, int y, const char *pString)
cfavreau 0:c79e1f29f029 389 {
cfavreau 0:c79e1f29f029 390 uint8_t w = *pFont;
cfavreau 0:c79e1f29f029 391 uint8_t h = *(pFont + 1);
cfavreau 0:c79e1f29f029 392 uint8_t offset = *(pFont + 2);
cfavreau 0:c79e1f29f029 393 uint8_t leftPad = *(pFont + 3);
cfavreau 0:c79e1f29f029 394 uint8_t rightPad = *(pFont + 4);
cfavreau 0:c79e1f29f029 395 uint8_t topPad = *(pFont + 5);
cfavreau 0:c79e1f29f029 396 uint8_t bottomPad = *(pFont + 6);
cfavreau 0:c79e1f29f029 397
cfavreau 0:c79e1f29f029 398 if (y + topPad + h + bottomPad < 0) return;
cfavreau 0:c79e1f29f029 399 if (y >= _height) return;
cfavreau 0:c79e1f29f029 400 if (x + leftPad + w + rightPad < 0) return;
cfavreau 0:c79e1f29f029 401
cfavreau 0:c79e1f29f029 402 char *p = (char*)pString;
cfavreau 0:c79e1f29f029 403 while(*p != 0)
cfavreau 0:c79e1f29f029 404 {
cfavreau 0:c79e1f29f029 405 if (x >= _width) return;
cfavreau 0:c79e1f29f029 406 drawChar(pFont, x, y, *p++, w, h, offset, leftPad, rightPad, topPad, bottomPad);
cfavreau 0:c79e1f29f029 407 x += (w + leftPad + rightPad);
cfavreau 0:c79e1f29f029 408 }
cfavreau 0:c79e1f29f029 409 }
cfavreau 0:c79e1f29f029 410
cfavreau 0:c79e1f29f029 411 void LCD_ST7735::measureString(const uint8_t *pFont, const char *pString, uint8_t &width, uint8_t &height)
cfavreau 0:c79e1f29f029 412 {
cfavreau 0:c79e1f29f029 413 uint8_t w = *pFont;
cfavreau 0:c79e1f29f029 414 uint8_t h = *(pFont + 1);
cfavreau 0:c79e1f29f029 415 uint8_t leftPad = *(pFont + 3);
cfavreau 0:c79e1f29f029 416 uint8_t rightPad = *(pFont + 4);
cfavreau 0:c79e1f29f029 417 uint8_t topPad = *(pFont + 5);
cfavreau 0:c79e1f29f029 418 uint8_t bottomPad = *(pFont + 6);
cfavreau 0:c79e1f29f029 419
cfavreau 0:c79e1f29f029 420 width = (w + leftPad + rightPad) * strlen(pString);
cfavreau 0:c79e1f29f029 421 height = (h + topPad + bottomPad);
cfavreau 0:c79e1f29f029 422 }
cfavreau 0:c79e1f29f029 423
cfavreau 0:c79e1f29f029 424 void LCD_ST7735::selectDevice()
cfavreau 0:c79e1f29f029 425 {
cfavreau 0:c79e1f29f029 426 _spi.prepareFastSPI();
cfavreau 0:c79e1f29f029 427 }
cfavreau 0:c79e1f29f029 428
cfavreau 0:c79e1f29f029 429 void LCD_ST7735::drawVertLine(int x1, int y1, int y2, uint16_t color)
cfavreau 0:c79e1f29f029 430 {
cfavreau 0:c79e1f29f029 431 clipRect(x1, y1, x1, y2);
cfavreau 0:c79e1f29f029 432 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 433 int c = (y2 - y1) << 1;
cfavreau 0:c79e1f29f029 434 uint8_t colorHigh = color >> 8;
cfavreau 0:c79e1f29f029 435 uint8_t colorLow = color;
cfavreau 0:c79e1f29f029 436 for (int i = 0; i < c; ++i)
cfavreau 0:c79e1f29f029 437 {
cfavreau 0:c79e1f29f029 438 writeBatchData(colorHigh, colorLow);
cfavreau 0:c79e1f29f029 439 }
cfavreau 0:c79e1f29f029 440 endBatchCommand();
cfavreau 0:c79e1f29f029 441 }
cfavreau 0:c79e1f29f029 442
cfavreau 0:c79e1f29f029 443 void LCD_ST7735::drawHorizLine(int x1, int y1, int x2, uint16_t color)
cfavreau 0:c79e1f29f029 444 {
cfavreau 0:c79e1f29f029 445 clipRect(x1, y1, x2, y1);
cfavreau 0:c79e1f29f029 446 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 447 int c = (x2 - x1) << 1;
cfavreau 0:c79e1f29f029 448 uint8_t colorHigh = color >> 8;
cfavreau 0:c79e1f29f029 449 uint8_t colorLow = color;
cfavreau 0:c79e1f29f029 450 for (int i = 0; i < c; ++i)
cfavreau 0:c79e1f29f029 451 {
cfavreau 0:c79e1f29f029 452 writeBatchData(colorHigh, colorLow);
cfavreau 0:c79e1f29f029 453 }
cfavreau 0:c79e1f29f029 454 endBatchCommand();
cfavreau 0:c79e1f29f029 455 }
cfavreau 0:c79e1f29f029 456
cfavreau 0:c79e1f29f029 457 void LCD_ST7735::drawChar(const uint8_t *pFont, int x, int y, char c, uint8_t w, uint8_t h, uint8_t offset, uint8_t leftPad, uint8_t rightPad, uint8_t topPad, uint8_t bottomPad)
cfavreau 0:c79e1f29f029 458 {
cfavreau 0:c79e1f29f029 459 const uint8_t *pChar = (pFont + 7) + ((c - offset) * h);
cfavreau 0:c79e1f29f029 460
cfavreau 0:c79e1f29f029 461 clip(x, y, w + leftPad + rightPad, h + topPad + bottomPad);
cfavreau 0:c79e1f29f029 462
cfavreau 0:c79e1f29f029 463 beginBatchCommand(CMD_RAMWR);
cfavreau 0:c79e1f29f029 464
cfavreau 0:c79e1f29f029 465 // Render top spacing
cfavreau 0:c79e1f29f029 466 for (int r = 0; r < topPad; ++r)
cfavreau 0:c79e1f29f029 467 {
cfavreau 0:c79e1f29f029 468 for (int c = 0; c < w + leftPad + rightPad; ++c)
cfavreau 0:c79e1f29f029 469 {
cfavreau 0:c79e1f29f029 470 writeBatchData(_backgroundColorHigh);
cfavreau 0:c79e1f29f029 471 writeBatchData(_backgroundColorLow);
cfavreau 0:c79e1f29f029 472 }
cfavreau 0:c79e1f29f029 473 }
cfavreau 0:c79e1f29f029 474
cfavreau 0:c79e1f29f029 475 // Render character
cfavreau 0:c79e1f29f029 476 for(int r = 0; r < h; ++r)
cfavreau 0:c79e1f29f029 477 {
cfavreau 0:c79e1f29f029 478 uint8_t b = pChar[r];
cfavreau 0:c79e1f29f029 479
cfavreau 0:c79e1f29f029 480 // Render left spacing
cfavreau 0:c79e1f29f029 481 for (int c = 0; c < leftPad; ++c)
cfavreau 0:c79e1f29f029 482 {
cfavreau 0:c79e1f29f029 483 writeBatchData(_backgroundColorHigh);
cfavreau 0:c79e1f29f029 484 writeBatchData(_backgroundColorLow);
cfavreau 0:c79e1f29f029 485 }
cfavreau 0:c79e1f29f029 486 for(int c = 0; c < w; ++c)
cfavreau 0:c79e1f29f029 487 {
cfavreau 0:c79e1f29f029 488 if (b & 0x80)
cfavreau 0:c79e1f29f029 489 {
cfavreau 0:c79e1f29f029 490 writeBatchData(_foregroundColorHigh);
cfavreau 0:c79e1f29f029 491 writeBatchData(_foregroundColorLow);
cfavreau 0:c79e1f29f029 492 }
cfavreau 0:c79e1f29f029 493 else
cfavreau 0:c79e1f29f029 494 {
cfavreau 0:c79e1f29f029 495 writeBatchData(_backgroundColorHigh);
cfavreau 0:c79e1f29f029 496 writeBatchData(_backgroundColorLow);
cfavreau 0:c79e1f29f029 497 }
cfavreau 0:c79e1f29f029 498
cfavreau 0:c79e1f29f029 499 b <<= 1;
cfavreau 0:c79e1f29f029 500 }
cfavreau 0:c79e1f29f029 501
cfavreau 0:c79e1f29f029 502 for (int c = 0; c < rightPad; ++c)
cfavreau 0:c79e1f29f029 503 {
cfavreau 0:c79e1f29f029 504 writeBatchData(_backgroundColorHigh);
cfavreau 0:c79e1f29f029 505 writeBatchData(_backgroundColorLow);
cfavreau 0:c79e1f29f029 506 }
cfavreau 0:c79e1f29f029 507 }
cfavreau 0:c79e1f29f029 508
cfavreau 0:c79e1f29f029 509 // Render bottom spacing
cfavreau 0:c79e1f29f029 510 for (int r = 0; r < bottomPad; ++r)
cfavreau 0:c79e1f29f029 511 {
cfavreau 0:c79e1f29f029 512 for (int c = 0; c < w + leftPad + rightPad; ++c)
cfavreau 0:c79e1f29f029 513 {
cfavreau 0:c79e1f29f029 514 writeBatchData(_backgroundColorHigh);
cfavreau 0:c79e1f29f029 515 writeBatchData(_backgroundColorLow);
cfavreau 0:c79e1f29f029 516 }
cfavreau 0:c79e1f29f029 517 }
cfavreau 0:c79e1f29f029 518 endBatchCommand();
cfavreau 0:c79e1f29f029 519 }
cfavreau 0:c79e1f29f029 520
cfavreau 0:c79e1f29f029 521 void LCD_ST7735::initDisplay()
cfavreau 0:c79e1f29f029 522 {
cfavreau 0:c79e1f29f029 523 selectDevice();
cfavreau 0:c79e1f29f029 524 reset();
cfavreau 0:c79e1f29f029 525
cfavreau 0:c79e1f29f029 526 writeCommand(CMD_SLPOUT);
cfavreau 0:c79e1f29f029 527
cfavreau 0:c79e1f29f029 528 write(CMD_FRMCTR1, (uint8_t[]){0x01, 0x2c, 0x2d}, 3);
cfavreau 0:c79e1f29f029 529 write(CMD_FRMCTR2, (uint8_t[]){0x01, 0x2c, 0x2d}, 3);
cfavreau 0:c79e1f29f029 530 write(CMD_FRMCTR3, (uint8_t[]){0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d}, 6);
cfavreau 0:c79e1f29f029 531
cfavreau 0:c79e1f29f029 532 write(CMD_INVCTR, (uint8_t[]){0x07}, 1);
cfavreau 0:c79e1f29f029 533
cfavreau 0:c79e1f29f029 534 write(CMD_PWCTR1, (uint8_t[]){0xa2, 0x02, 0x84}, 3);
cfavreau 0:c79e1f29f029 535 write(CMD_PWCTR2, (uint8_t[]){0xc5}, 1);
cfavreau 0:c79e1f29f029 536 write(CMD_PWCTR3, (uint8_t[]){0x0a, 0x00}, 2);
cfavreau 0:c79e1f29f029 537 write(CMD_PWCTR4, (uint8_t[]){0x8a, 0x2a}, 2);
cfavreau 0:c79e1f29f029 538 write(CMD_PWCTR5, (uint8_t[]){0x8a, 0xee}, 2);
cfavreau 0:c79e1f29f029 539
cfavreau 0:c79e1f29f029 540 write(CMD_VMCTR1, (uint8_t[]){0x0e}, 1);
cfavreau 0:c79e1f29f029 541
cfavreau 0:c79e1f29f029 542 write(CMD_MADCTL, (uint8_t[]){0xc0 | _colorFilter}, 1);
cfavreau 0:c79e1f29f029 543
cfavreau 0:c79e1f29f029 544 // Gama sequence
cfavreau 0:c79e1f29f029 545 write(CMD_GAMCTRP1, (uint8_t[])
cfavreau 0:c79e1f29f029 546 {
cfavreau 0:c79e1f29f029 547 0x0f, 0x1a,
cfavreau 0:c79e1f29f029 548 0x0f, 0x18,
cfavreau 0:c79e1f29f029 549 0x2f, 0x28,
cfavreau 0:c79e1f29f029 550 0x20, 0x22,
cfavreau 0:c79e1f29f029 551 0x1f, 0x1b,
cfavreau 0:c79e1f29f029 552 0x23, 0x37,
cfavreau 0:c79e1f29f029 553 0x00, 0x07,
cfavreau 0:c79e1f29f029 554 0x02, 0x10
cfavreau 0:c79e1f29f029 555 }, 16);
cfavreau 0:c79e1f29f029 556
cfavreau 0:c79e1f29f029 557 write(CMD_GAMCTRN1, (uint8_t[])
cfavreau 0:c79e1f29f029 558 {
cfavreau 0:c79e1f29f029 559 0x0f, 0x1b,
cfavreau 0:c79e1f29f029 560 0x0f, 0x17,
cfavreau 0:c79e1f29f029 561 0x33, 0x2c,
cfavreau 0:c79e1f29f029 562 0x29, 0x2e,
cfavreau 0:c79e1f29f029 563 0x30, 0x30,
cfavreau 0:c79e1f29f029 564 0x39, 0x3f,
cfavreau 0:c79e1f29f029 565 0x00, 0x07,
cfavreau 0:c79e1f29f029 566 0x03, 0x10
cfavreau 0:c79e1f29f029 567 }, 16);
cfavreau 0:c79e1f29f029 568
cfavreau 0:c79e1f29f029 569 write(CMD_CASET, (uint8_t[]){0x00, 0x00, 0x00, 0x7f}, 4);
cfavreau 0:c79e1f29f029 570 write(CMD_RASET, (uint8_t[]){0x00, 0x00, 0x00, 0x9f}, 4);
cfavreau 0:c79e1f29f029 571
cfavreau 0:c79e1f29f029 572 write(CMD_EXTCTRL, (uint8_t[]){0x01}, 1);
cfavreau 0:c79e1f29f029 573
cfavreau 0:c79e1f29f029 574 // Disable RAM power save
cfavreau 0:c79e1f29f029 575 write(0xf6, (uint8_t[]){0x00}, 1);
cfavreau 0:c79e1f29f029 576
cfavreau 0:c79e1f29f029 577 // 65k color mode
cfavreau 0:c79e1f29f029 578 write(CMD_COLMOD, (uint8_t[]){0x05}, 1);
cfavreau 0:c79e1f29f029 579
cfavreau 0:c79e1f29f029 580 // Enable display
cfavreau 0:c79e1f29f029 581 writeCommand(CMD_DISPON);
cfavreau 0:c79e1f29f029 582
cfavreau 0:c79e1f29f029 583 setBacklight(true);
cfavreau 0:c79e1f29f029 584 }
cfavreau 0:c79e1f29f029 585
cfavreau 0:c79e1f29f029 586 void LCD_ST7735::reset()
cfavreau 0:c79e1f29f029 587 {
cfavreau 0:c79e1f29f029 588 _reset = 0;
cfavreau 0:c79e1f29f029 589 wait_us(100);
cfavreau 0:c79e1f29f029 590 _reset = 1;
cfavreau 0:c79e1f29f029 591 wait_us(100);
cfavreau 0:c79e1f29f029 592 }
cfavreau 0:c79e1f29f029 593
cfavreau 0:c79e1f29f029 594 void LCD_ST7735::clip(int x, int y, int w, int h)
cfavreau 0:c79e1f29f029 595 {
cfavreau 0:c79e1f29f029 596 clipRect(x, y, (x + w) - 1, (y + h) - 1);
cfavreau 0:c79e1f29f029 597 }
cfavreau 0:c79e1f29f029 598
cfavreau 0:c79e1f29f029 599 void LCD_ST7735::clipRect(int x1, int y1, int x2, int y2)
cfavreau 0:c79e1f29f029 600 {
cfavreau 0:c79e1f29f029 601 uint8_t x1l = (uint8_t)x1;
cfavreau 0:c79e1f29f029 602 uint8_t x1h = (uint8_t)(x1 >> 8);
cfavreau 0:c79e1f29f029 603 uint8_t x2l = (uint8_t)x2;
cfavreau 0:c79e1f29f029 604 uint8_t x2h = (uint8_t)(x2 >> 8);
cfavreau 0:c79e1f29f029 605 write(CMD_CASET, (uint8_t[]){x1h, x1l, x2h, x2l}, 4);
cfavreau 0:c79e1f29f029 606
cfavreau 0:c79e1f29f029 607 uint8_t y1l = (uint8_t)y1;
cfavreau 0:c79e1f29f029 608 uint8_t y1h = (uint8_t)(y1 >> 8);
cfavreau 0:c79e1f29f029 609 uint8_t y2l = (uint8_t)y2;
cfavreau 0:c79e1f29f029 610 uint8_t y2h = (uint8_t)(y2 >> 8);
cfavreau 0:c79e1f29f029 611 write(CMD_RASET, (uint8_t[]){y1h, y1l, y2h, y2l}, 4);
cfavreau 0:c79e1f29f029 612 }
cfavreau 0:c79e1f29f029 613
cfavreau 0:c79e1f29f029 614 void LCD_ST7735::writeCommand(uint8_t cmd)
cfavreau 0:c79e1f29f029 615 {
cfavreau 0:c79e1f29f029 616 _cs = 0;
cfavreau 0:c79e1f29f029 617 _ds = 0;
cfavreau 0:c79e1f29f029 618 _spi.fastWrite(cmd);
cfavreau 0:c79e1f29f029 619 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 620 _spi.clearRx();
cfavreau 0:c79e1f29f029 621 _cs = 1;
cfavreau 0:c79e1f29f029 622 }
cfavreau 0:c79e1f29f029 623
cfavreau 0:c79e1f29f029 624 void LCD_ST7735::write(uint8_t cmd, uint8_t data[], int dataLen)
cfavreau 0:c79e1f29f029 625 {
cfavreau 0:c79e1f29f029 626 _cs = 0;
cfavreau 0:c79e1f29f029 627 _ds = 0;
cfavreau 0:c79e1f29f029 628 _spi.fastWrite(cmd);
cfavreau 0:c79e1f29f029 629 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 630 if (data != NULL & dataLen > 0)
cfavreau 0:c79e1f29f029 631 {
cfavreau 0:c79e1f29f029 632 _ds = 1;
cfavreau 0:c79e1f29f029 633 for(int i = 0; i < dataLen; ++i)
cfavreau 0:c79e1f29f029 634 {
cfavreau 0:c79e1f29f029 635 _spi.fastWrite(data[i]);
cfavreau 0:c79e1f29f029 636 }
cfavreau 0:c79e1f29f029 637 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 638 _ds = 0;
cfavreau 0:c79e1f29f029 639 }
cfavreau 0:c79e1f29f029 640 _spi.clearRx();
cfavreau 0:c79e1f29f029 641 _cs = 1;
cfavreau 0:c79e1f29f029 642 }
cfavreau 0:c79e1f29f029 643
cfavreau 0:c79e1f29f029 644 void LCD_ST7735::write(uint8_t cmd, uint16_t data)
cfavreau 0:c79e1f29f029 645 {
cfavreau 0:c79e1f29f029 646 _cs = 0;
cfavreau 0:c79e1f29f029 647 _ds = 0;
cfavreau 0:c79e1f29f029 648 _spi.fastWrite(cmd);
cfavreau 0:c79e1f29f029 649 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 650 _ds = 1;
cfavreau 0:c79e1f29f029 651 _spi.fastWrite(data >> 8);
cfavreau 0:c79e1f29f029 652 _spi.fastWrite(data);
cfavreau 0:c79e1f29f029 653 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 654 _spi.clearRx();
cfavreau 0:c79e1f29f029 655 _ds = 0;
cfavreau 0:c79e1f29f029 656 _cs = 1;
cfavreau 0:c79e1f29f029 657 }
cfavreau 0:c79e1f29f029 658
cfavreau 0:c79e1f29f029 659 void LCD_ST7735::beginBatchCommand(uint8_t cmd)
cfavreau 0:c79e1f29f029 660 {
cfavreau 0:c79e1f29f029 661 _cs = 0;
cfavreau 0:c79e1f29f029 662 _ds = 0;
cfavreau 0:c79e1f29f029 663 _spi.fastWrite(cmd);
cfavreau 0:c79e1f29f029 664 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 665 _ds = 1;
cfavreau 0:c79e1f29f029 666 }
cfavreau 0:c79e1f29f029 667
cfavreau 0:c79e1f29f029 668 void LCD_ST7735::writeBatchData(uint8_t data)
cfavreau 0:c79e1f29f029 669 {
cfavreau 0:c79e1f29f029 670 _spi.fastWrite(data);
cfavreau 0:c79e1f29f029 671 }
cfavreau 0:c79e1f29f029 672
cfavreau 0:c79e1f29f029 673 void LCD_ST7735::writeBatchData(uint8_t dataHigh, uint8_t dataLow)
cfavreau 0:c79e1f29f029 674 {
cfavreau 0:c79e1f29f029 675 _spi.fastWrite(dataHigh);
cfavreau 0:c79e1f29f029 676 _spi.fastWrite(dataLow);
cfavreau 0:c79e1f29f029 677 }
cfavreau 0:c79e1f29f029 678
cfavreau 0:c79e1f29f029 679
cfavreau 0:c79e1f29f029 680 void LCD_ST7735::writeBatchData(uint16_t data)
cfavreau 0:c79e1f29f029 681 {
cfavreau 0:c79e1f29f029 682 _spi.fastWrite(data >> 8);
cfavreau 0:c79e1f29f029 683 _spi.fastWrite(data);
cfavreau 0:c79e1f29f029 684 }
cfavreau 0:c79e1f29f029 685
cfavreau 0:c79e1f29f029 686 void LCD_ST7735::endBatchCommand()
cfavreau 0:c79e1f29f029 687 {
cfavreau 0:c79e1f29f029 688 _spi.waitWhileBusy();
cfavreau 0:c79e1f29f029 689 _spi.clearRx();
cfavreau 0:c79e1f29f029 690 _ds = 0;
cfavreau 0:c79e1f29f029 691 _cs = 1;
cfavreau 0:c79e1f29f029 692 }