SPI based library for the ST7735 LCD controller.
Dependents: RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more
Revision 3:451148656b76, committed 2014-09-20
- Comitter:
- taylorza
- Date:
- Sat Sep 20 19:09:16 2014 +0000
- Parent:
- 2:43f08d03a7e2
- Child:
- 4:88d22437119c
- Commit message:
- Added support for setting the screen orientation and determining the screen width and height based on the orientation.
Changed in this revision
LCD_ST7735.cpp | Show annotated file Show diff for this revision Revisions of this file |
LCD_ST7735.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/LCD_ST7735.cpp Sat Sep 20 05:43:17 2014 +0000 +++ b/LCD_ST7735.cpp Sat Sep 20 19:09:16 2014 +0000 @@ -24,6 +24,52 @@ setBackgroundColor(0x0000); } +void LCD_ST7735::setOrientation(Orientation orientation, bool flip) +{ + const static uint8_t my = 0x80; + const static uint8_t mx = 0x40; + const static uint8_t mv = 0x20; + + uint8_t madctlData = 0x08; + switch(orientation) + { + case Rotate0: + _width = 128; + _height = 160; + madctlData |= flip ? mx : 0; + break; + + case Rotate90: + _width = 160; + _height = 128; + madctlData |= flip ? my | mv | mx : mv | mx; + break; + + case Rotate180: + _width = 128; + _height = 160; + madctlData |= flip ? my : mx | my; + break; + + case Rotate270: + _width = 160; + _height = 128; + madctlData |= flip ? mv : mv | my; + break; + } + write(CMD_MADCTL, (uint8_t[]){madctlData}, 1); +} + +int LCD_ST7735::getWidth() +{ + return _width; +} + +int LCD_ST7735::getHeight() +{ + return _height; +} + void LCD_ST7735::setBacklight(bool state) { _backlight = state ? 1 : 0; @@ -31,7 +77,7 @@ void LCD_ST7735::clearScreen(uint16_t color) { - clipRect(0, 0, 127, 159); + clipRect(0, 0, _width - 1, _height - 1); beginBatchCommand(CMD_RAMWR); for(int i = 0; i < 128 * 160 * 2; ++i) {
--- a/LCD_ST7735.h Sat Sep 20 05:43:17 2014 +0000 +++ b/LCD_ST7735.h Sat Sep 20 19:09:16 2014 +0000 @@ -10,6 +10,20 @@ class LCD_ST7735 { public: + /** Orientation of the display */ + enum Orientation + { + /** No rotation of the display image*/ + Rotate0, + /** Rotate the display image 90 degrees */ + Rotate90, + /** Rotate the display image 180 degrees */ + Rotate180, + /** Rotate the display image 270 degrees */ + Rotate270 + }; + + public: /**Creates an instance of the LCD_ST7735 driver * @param backlightPin pin used to control the backlight * @param resetPin pin used to reset the display controller @@ -28,6 +42,18 @@ PinName clkPin, PinName csPin ); + + /** Set the orientation of the display + * @param orientation Orientation of the display. + * @param flip Flips the display direction + */ + void setOrientation(Orientation orientation, bool flip); + + /** Get the width of the display given the current orientation */ + int getWidth(); + + /** Get the height of the display given the current orientation */ + int getHeight(); /** Control the display's backlight * @param state true to turn the backlight on, false to turn it off @@ -152,7 +178,7 @@ * @param pString ASCIIZ string to draw to the display. * @note The font is currently limited to an 8x8 font. See the font_IBM.h file for an example font. */ - void drawString(const uint8_t *pFont, int x, int y, const char *pString); + void drawString(const uint8_t *pFont, int x, int y, const char *pString); private: void drawVertLine(int x1, int y1, int y2, uint16_t color); @@ -178,6 +204,10 @@ void clipRect(int x1, int y1, int x2, int y2); private: + int _width; + int _height; + Orientation _orientation; + bool _flip; uint16_t _foregroundColor; uint16_t _backgroundColor;