add rotate and invert for screen

This commit is contained in:
Neucrack
2019-03-29 11:19:17 +08:00
parent d0bdb51f59
commit 33f02ae2db
5 changed files with 118 additions and 22 deletions

View File

@@ -1,11 +1,6 @@
#include "SPI.h"
SPIClass spi0(SPI0);
#ifdef K210
#error "11111111"
#else
#error "222222"
#endif
void setup()
{

View File

@@ -3,16 +3,51 @@
SPIClass spi_(SPI0);
Sipeed_ST7789 lcd(320, 240, spi_);
void func()
{
lcd.fillScreen(COLOR_RED);
lcd.drawRect(20, 20, 50, 50, COLOR_WHITE);
lcd.fillCircle(100, 100, 40, COLOR_WHITE);
lcd.fillTriangle(10, 200, 300, 200, 300, 150, COLOR_WHITE);
lcd.setTextSize(2);
lcd.setTextColor(COLOR_WHITE);
lcd.setCursor(100,30);
lcd.println("hello Sipeed Maix");
}
void func2()
{
lcd.fillScreen(COLOR_RED);
lcd.drawRect(20, 20, 50, 50, COLOR_WHITE);
lcd.fillCircle(180, 50, 40, COLOR_WHITE);
lcd.fillTriangle(10, 300, 200, 300, 200, 150, COLOR_WHITE);
lcd.setTextSize(2);
lcd.setTextColor(COLOR_WHITE);
lcd.setCursor(1,100);
lcd.println("hello Sipeed Maix");
}
void setup()
{
lcd.begin(15000000, COLOR_BLACK);
lcd.drawRect(20, 20, 50, 50, COLOR_WHITE);
lcd.drawCircle(100, 100, 40, COLOR_WHITE);
lcd.fillTriangle(10, 200, 300, 200, 300, 150, COLOR_WHITE);
lcd.begin(15000000, COLOR_RED);
}
void loop()
{
lcd.setRotation(0);
func();
delay(3000);
lcd.invertDisplay(true);
func();
delay(3000);
lcd.setRotation(1);
func2();
delay(3000);
lcd.setRotation(2);
func();
delay(3000);
lcd.setRotation(3);
func2();
delay(3000);
}

View File

@@ -7,7 +7,8 @@
Sipeed_ST7789::Sipeed_ST7789(uint16_t w, uint16_t h, SPIClass& spi, int8_t dc_pin, int8_t rst_pin, uint8_t dma_ch )
:Adafruit_GFX(w,h),
_spi(spi), _dcxPin(dc_pin), _rstPin(rst_pin),
_dmaCh(dma_ch)
_dmaCh(dma_ch),
_screenDir(DIR_YX_RLDU)
{
configASSERT(_spi.busId()==SPI0);
}
@@ -33,6 +34,7 @@ boolean Sipeed_ST7789::begin( uint32_t freq, uint16_t color )
sysctl_set_spi0_dvp_data(1);
_freq = freq;
lcd_init(spiNum, SIPEED_ST7789_SS, SIPEED_ST7789_RST_GPIONUM, SIPEED_ST7789_DCX_GPIONUM, _freq, _rstPin, _dcxPin, _dmaCh);
lcd_set_direction((lcd_dir_t)_screenDir);
lcd_clear(color);
return true;
}
@@ -87,11 +89,78 @@ void Sipeed_ST7789::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_
lcd_draw_rectangle(x, y, x+w-1, y+h-1, 1, color);
}
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth)
void Sipeed_ST7789::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth)
{
lcd_draw_rectangle(x, y, x+w-1, y+h-1, lineWidth, color);
}
uint16_t getValueByRotation(uint8_t rotation)
{
uint16_t v = DIR_YX_RLDU;
switch(rotation) {
case 0:
v = DIR_YX_RLDU;
break;
case 1:
v = DIR_XY_RLUD;
break;
case 2:
v = DIR_YX_LRUD;
break;
case 3:
v = DIR_XY_LRDU;
break;
}
return v;
}
/**************************************************************************/
/*!
@brief Set rotation setting for display
@param x 0 thru 3 corresponding to 4 cardinal rotations
*/
/**************************************************************************/
void Sipeed_ST7789::setRotation(uint8_t x) {
rotation = (x & 3);
configASSERT(rotation >= 0 && rotation <= 3);
switch(rotation) {
case 0:
case 2:
_width = WIDTH;
_height = HEIGHT;
break;
case 1:
case 3:
_width = HEIGHT;
_height = WIDTH;
break;
}
_screenDir = getValueByRotation(rotation);
lcd_set_direction((lcd_dir_t)_screenDir);
}
void Sipeed_ST7789::invertDisplay(boolean invert) {
uint16_t _screenDir = getValueByRotation(rotation);
if( invert )
{
switch(rotation) {
case 0:
_screenDir = DIR_YX_RLUD;
break;
case 1:
_screenDir = DIR_XY_LRUD;
break;
case 2:
_screenDir = DIR_YX_LRDU;
break;
case 3:
_screenDir = DIR_XY_RLDU;
break;
}
}
lcd_set_direction((lcd_dir_t)_screenDir);
}

View File

@@ -55,10 +55,7 @@ public:
~Sipeed_ST7789(void);
boolean begin( uint32_t freq = 15000000, uint16_t color = 0xffff );
// void display(void);
// void clearDisplay(uint16_t color);
// void invertDisplay(boolean i);
// void dim(boolean dim);
virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
virtual void writePixel(int16_t x, int16_t y, uint16_t color);
virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
@@ -74,9 +71,8 @@ public:
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth);
//TODO: direction
// virtual void setRotation(uint8_t r);
// virtual void invertDisplay(boolean i);
virtual void setRotation(uint8_t r);
virtual void invertDisplay(boolean invert);
private:
SPIClass& _spi;
@@ -84,7 +80,7 @@ private:
int8_t _rstPin;
uint8_t _dmaCh;
uint32_t _freq;
uint16_t _screenDir;
};

View File

@@ -61,7 +61,8 @@ typedef enum _lcd_dir
DIR_XY_LRDU = 0xC0,
DIR_YX_LRDU = 0xE0,
DIR_XY_MASK = 0x20,
DIR_MASK = 0xE0,
DIR_RL_MASK = 0x40,
DIR_UD_MASK = 0x80
} lcd_dir_t;
typedef struct _lcd_ctl