Skip to content

Commit 33f02ae

Browse files
committed
add rotate and invert for screen
1 parent d0bdb51 commit 33f02ae

File tree

5 files changed

+116
-20
lines changed

5 files changed

+116
-20
lines changed

libraries/SPI/examples/basic/basic.ino

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#include "SPI.h"
22

33
SPIClass spi0(SPI0);
4-
#ifdef K210
5-
#error "11111111"
6-
#else
7-
#error "222222"
8-
#endif
94

105
void setup()
116
{

libraries/Sipeed_ST7789/examples/basic_display/basic_display.ino

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,51 @@
33
SPIClass spi_(SPI0);
44
Sipeed_ST7789 lcd(320, 240, spi_);
55

6-
void setup()
6+
7+
void func()
78
{
8-
lcd.begin(15000000, COLOR_BLACK);
9+
lcd.fillScreen(COLOR_RED);
910
lcd.drawRect(20, 20, 50, 50, COLOR_WHITE);
10-
lcd.drawCircle(100, 100, 40, COLOR_WHITE);
11+
lcd.fillCircle(100, 100, 40, COLOR_WHITE);
1112
lcd.fillTriangle(10, 200, 300, 200, 300, 150, COLOR_WHITE);
13+
lcd.setTextSize(2);
14+
lcd.setTextColor(COLOR_WHITE);
15+
lcd.setCursor(100,30);
16+
lcd.println("hello Sipeed Maix");
1217
}
1318

14-
void loop()
19+
void func2()
1520
{
21+
lcd.fillScreen(COLOR_RED);
22+
lcd.drawRect(20, 20, 50, 50, COLOR_WHITE);
23+
lcd.fillCircle(180, 50, 40, COLOR_WHITE);
24+
lcd.fillTriangle(10, 300, 200, 300, 200, 150, COLOR_WHITE);
25+
lcd.setTextSize(2);
26+
lcd.setTextColor(COLOR_WHITE);
27+
lcd.setCursor(1,100);
28+
lcd.println("hello Sipeed Maix");
29+
}
1630

31+
void setup()
32+
{
33+
lcd.begin(15000000, COLOR_RED);
1734
}
1835

36+
void loop()
37+
{
38+
lcd.setRotation(0);
39+
func();
40+
delay(3000);
41+
lcd.invertDisplay(true);
42+
func();
43+
delay(3000);
44+
lcd.setRotation(1);
45+
func2();
46+
delay(3000);
47+
lcd.setRotation(2);
48+
func();
49+
delay(3000);
50+
lcd.setRotation(3);
51+
func2();
52+
delay(3000);
53+
}

libraries/Sipeed_ST7789/src/Sipeed_ST7789.cpp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
Sipeed_ST7789::Sipeed_ST7789(uint16_t w, uint16_t h, SPIClass& spi, int8_t dc_pin, int8_t rst_pin, uint8_t dma_ch )
88
:Adafruit_GFX(w,h),
99
_spi(spi), _dcxPin(dc_pin), _rstPin(rst_pin),
10-
_dmaCh(dma_ch)
10+
_dmaCh(dma_ch),
11+
_screenDir(DIR_YX_RLDU)
1112
{
1213
configASSERT(_spi.busId()==SPI0);
1314
}
@@ -33,6 +34,7 @@ boolean Sipeed_ST7789::begin( uint32_t freq, uint16_t color )
3334
sysctl_set_spi0_dvp_data(1);
3435
_freq = freq;
3536
lcd_init(spiNum, SIPEED_ST7789_SS, SIPEED_ST7789_RST_GPIONUM, SIPEED_ST7789_DCX_GPIONUM, _freq, _rstPin, _dcxPin, _dmaCh);
37+
lcd_set_direction((lcd_dir_t)_screenDir);
3638
lcd_clear(color);
3739
return true;
3840
}
@@ -87,11 +89,78 @@ void Sipeed_ST7789::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_
8789
lcd_draw_rectangle(x, y, x+w-1, y+h-1, 1, color);
8890
}
8991

90-
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth)
92+
void Sipeed_ST7789::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth)
9193
{
9294
lcd_draw_rectangle(x, y, x+w-1, y+h-1, lineWidth, color);
9395
}
9496

9597

98+
uint16_t getValueByRotation(uint8_t rotation)
99+
{
100+
uint16_t v = DIR_YX_RLDU;
101+
switch(rotation) {
102+
case 0:
103+
v = DIR_YX_RLDU;
104+
break;
105+
case 1:
106+
v = DIR_XY_RLUD;
107+
break;
108+
case 2:
109+
v = DIR_YX_LRUD;
110+
break;
111+
case 3:
112+
v = DIR_XY_LRDU;
113+
break;
114+
}
115+
return v;
116+
}
117+
118+
/**************************************************************************/
119+
/*!
120+
@brief Set rotation setting for display
121+
@param x 0 thru 3 corresponding to 4 cardinal rotations
122+
*/
123+
/**************************************************************************/
124+
void Sipeed_ST7789::setRotation(uint8_t x) {
125+
rotation = (x & 3);
126+
configASSERT(rotation >= 0 && rotation <= 3);
127+
128+
switch(rotation) {
129+
case 0:
130+
case 2:
131+
_width = WIDTH;
132+
_height = HEIGHT;
133+
break;
134+
case 1:
135+
case 3:
136+
_width = HEIGHT;
137+
_height = WIDTH;
138+
break;
139+
}
140+
_screenDir = getValueByRotation(rotation);
141+
lcd_set_direction((lcd_dir_t)_screenDir);
142+
}
143+
144+
void Sipeed_ST7789::invertDisplay(boolean invert) {
145+
uint16_t _screenDir = getValueByRotation(rotation);
146+
if( invert )
147+
{
148+
switch(rotation) {
149+
case 0:
150+
_screenDir = DIR_YX_RLUD;
151+
break;
152+
case 1:
153+
_screenDir = DIR_XY_LRUD;
154+
break;
155+
case 2:
156+
_screenDir = DIR_YX_LRDU;
157+
break;
158+
case 3:
159+
_screenDir = DIR_XY_RLDU;
160+
break;
161+
}
162+
}
163+
lcd_set_direction((lcd_dir_t)_screenDir);
164+
}
96165

97166

libraries/Sipeed_ST7789/src/Sipeed_ST7789.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ class Sipeed_ST7789 : public Adafruit_GFX{
5555
~Sipeed_ST7789(void);
5656

5757
boolean begin( uint32_t freq = 15000000, uint16_t color = 0xffff );
58-
// void display(void);
59-
// void clearDisplay(uint16_t color);
60-
// void invertDisplay(boolean i);
61-
// void dim(boolean dim);
58+
6259
virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
6360
virtual void writePixel(int16_t x, int16_t y, uint16_t color);
6461
virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
@@ -74,17 +71,16 @@ class Sipeed_ST7789 : public Adafruit_GFX{
7471

7572
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color, uint16_t lineWidth);
7673

77-
//TODO: direction
78-
// virtual void setRotation(uint8_t r);
79-
// virtual void invertDisplay(boolean i);
74+
virtual void setRotation(uint8_t r);
75+
virtual void invertDisplay(boolean invert);
8076

8177
private:
8278
SPIClass& _spi;
8379
int8_t _dcxPin;
8480
int8_t _rstPin;
8581
uint8_t _dmaCh;
8682
uint32_t _freq;
87-
83+
uint16_t _screenDir;
8884

8985

9086
};

libraries/Sipeed_ST7789/src/lcd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ typedef enum _lcd_dir
6161
DIR_XY_LRDU = 0xC0,
6262
DIR_YX_LRDU = 0xE0,
6363
DIR_XY_MASK = 0x20,
64-
DIR_MASK = 0xE0,
64+
DIR_RL_MASK = 0x40,
65+
DIR_UD_MASK = 0x80
6566
} lcd_dir_t;
6667

6768
typedef struct _lcd_ctl

0 commit comments

Comments
 (0)