Merge pull request #9059 from gemu2015/st7789-update

St7789 update
This commit is contained in:
Theo Arends 2020-08-09 15:13:35 +02:00 committed by GitHub
commit 37b1669afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 37 deletions

View File

@ -17,6 +17,9 @@ const uint16_t ST7789_colors[]={ST7789_BLACK,ST7789_WHITE,ST7789_RED,ST7789_GREE
ST7789_YELLOW,ST7789_NAVY,ST7789_DARKGREEN,ST7789_DARKCYAN,ST7789_MAROON,ST7789_PURPLE,ST7789_OLIVE,\
ST7789_LIGHTGREY,ST7789_DARKGREY,ST7789_ORANGE,ST7789_GREENYELLOW,ST7789_PINK};
#ifdef ESP32
#define ST7789_DIMMER
#endif
uint16_t Arduino_ST7789::GetColorFromIndex(uint8_t index) {
if (index>=sizeof(ST7789_colors)/2) index=0;
@ -87,7 +90,7 @@ Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t sid, int8_t sclk, i
// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t cs)
Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t cs, int8_t bp)
: Renderer(ST7789_TFTWIDTH, ST7789_TFTHEIGHT) {
_cs = cs;
_dc = dc;
@ -95,6 +98,7 @@ Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t cs)
_hwSPI = true;
_SPI9bit = false;
_sid = _sclk = -1;
_bp = bp;
}
@ -253,7 +257,15 @@ void Arduino_ST7789::commonInit(const uint8_t *cmdList) {
}
if (_bp>=0) {
#define ESP32_PWM_CHANNEL 1
#ifdef ST7789_DIMMER
ledcSetup(ESP32_PWM_CHANNEL,4000,8);
ledcAttachPin(_bp,ESP32_PWM_CHANNEL);
ledcWrite(ESP32_PWM_CHANNEL,128);
#else
pinMode(_bp, OUTPUT);
#endif
}
@ -348,13 +360,15 @@ void Arduino_ST7789::setRotation(uint8_t m) {
}
}
void Arduino_ST7789::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
uint16_t y1) {
void Arduino_ST7789::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
setAddrWindow_int(x0,y0,x1-1,y1-1);
}
void Arduino_ST7789::setAddrWindow_int(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
uint16_t x_start = x0 + _xstart, x_end = x1 + _xstart;
uint16_t y_start = y0 + _ystart, y_end = y1 + _ystart;
writecommand(ST7789_CASET); // Column addr set
writedata(x_start >> 8);
writedata(x_start & 0xFF); // XSTART
@ -370,23 +384,11 @@ void Arduino_ST7789::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
writecommand(ST7789_RAMWR); // write to RAM
}
void Arduino_ST7789::pushColor(uint16_t color) {
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
spiwrite(color >> 8);
spiwrite(color);
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
setAddrWindow(x,y,x+1,y+1);
setAddrWindow_int(x,y,x+1,y+1);
SPI_BEGIN_TRANSACTION();
DC_HIGH();
@ -405,7 +407,7 @@ void Arduino_ST7789::drawFastVLine(int16_t x, int16_t y, int16_t h,
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((y+h-1) >= _height) h = _height-y;
setAddrWindow(x, y, x, y+h-1);
setAddrWindow_int(x, y, x, y+h-1);
uint8_t hi = color >> 8, lo = color;
@ -428,7 +430,7 @@ void Arduino_ST7789::drawFastHLine(int16_t x, int16_t y, int16_t w,
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width) w = _width-x;
setAddrWindow(x, y, x+w-1, y);
setAddrWindow_int(x, y, x+w-1, y);
uint8_t hi = color >> 8, lo = color;
@ -458,7 +460,7 @@ void Arduino_ST7789::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
if((x + w - 1) >= _width) w = _width - x;
if((y + h - 1) >= _height) h = _height - y;
setAddrWindow(x, y, x+w-1, y+h-1);
setAddrWindow_int(x, y, x+w-1, y+h-1);
uint8_t hi = color >> 8, lo = color;
@ -543,12 +545,58 @@ void Arduino_ST7789::DisplayOnff(int8_t on) {
if (on) {
writecommand(ST7789_DISPON); //Display on
if (_bp>=0) {
#ifdef ST7789_DIMMER
ledcWrite(ESP32_PWM_CHANNEL,255);
#else
digitalWrite(_bp,HIGH);
#endif
}
} else {
writecommand(ST7789_DISPOFF);
if (_bp>=0) {
#ifdef ST7789_DIMMER
ledcWrite(ESP32_PWM_CHANNEL,0);
#else
digitalWrite(_bp,LOW);
#endif
}
}
}
// dimmer 0-100
void Arduino_ST7789::dim(uint8_t dimmer) {
if (dimmer>15) dimmer=15;
dimmer=((float)dimmer/15.0)*255.0;
#ifdef ESP32
ledcWrite(ESP32_PWM_CHANNEL,dimmer);
#endif
}
void Arduino_ST7789::pushColor(uint16_t color) {
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
spiwrite(color >> 8);
spiwrite(color);
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::pushColors(uint16_t *data, uint8_t len, boolean first) {
uint16_t color;
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
while (len--) {
color = *data++;
spiwrite(color >> 8);
spiwrite(color);
}
CS_HIGH();
SPI_END_TRANSACTION();
}

View File

@ -121,8 +121,8 @@ class Arduino_ST7789 : public Renderer {
public:
Arduino_ST7789(int8_t DC, int8_t RST, int8_t SID, int8_t SCLK, int8_t CS = -1,int8_t bp = -1);
Arduino_ST7789(int8_t DC, int8_t RST, int8_t CS = -1);
Arduino_ST7789(int8_t DC, int8_t RST, int8_t SID, int8_t SCLK, int8_t CS, int8_t bp);
Arduino_ST7789(int8_t DC, int8_t RST, int8_t CS, int8_t bp);
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),
pushColor(uint16_t color),
@ -134,12 +134,14 @@ class Arduino_ST7789 : public Renderer {
setRotation(uint8_t r),
invertDisplay(boolean i),
DisplayInit(int8_t p,int8_t size,int8_t rot,int8_t font),
init(uint16_t width, uint16_t height);
setAddrWindow_int(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),
init(uint16_t width, uint16_t height);
uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
uint16_t GetColorFromIndex(uint8_t index);
uint16_t color565(uint8_t r, uint8_t g, uint8_t b) { return Color565(r, g, b); }
void DisplayOnff(int8_t on);
void dim(uint8_t contrast);
void pushColors(uint16_t *data, uint8_t len, boolean first);
protected:
uint8_t _colstart, _rowstart, _xstart, _ystart; // some displays need this changed

View File

@ -505,7 +505,7 @@ void DisplayText(void)
cp += var;
linebuf[fill] = 0;
break;
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) && USE_SCRIPT_FATFS>=0
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
case 'P':
{ char *ep=strchr(cp,':');
if (ep) {
@ -1512,7 +1512,7 @@ void rgb888_to_565(uint8_t *in, uint16_t *out, uint32_t len);
#endif
#endif
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT) && USE_SCRIPT_FATFS>=0
#if defined(USE_SCRIPT_FATFS) && defined(USE_SCRIPT)
#ifdef ESP32
extern FS *fsp;
@ -1541,14 +1541,13 @@ void Draw_RGB_Bitmap(char *file,uint16_t xp, uint16_t yp) {
uint16_t ysize;
fp.read((uint8_t*)&ysize,2);
#if 1
uint16_t xdiv=xsize/XBUFF_LEN;
renderer->setAddrWindow(xp,yp,xp+xsize,yp+ysize);
for(int16_t j=0; j<ysize; j++) {
for(int16_t i=0; i<xsize; i+=XBUFF_LEN) {
uint16_t rgb[XBUFF_LEN];
uint16_t len=fp.read((uint8_t*)rgb,XBUFF_LEN*2);
if (len>=2) renderer->pushColors(rgb,len/2,true);
}
uint16_t rgb[xsize];
for (int16_t j=0; j<ysize; j++) {
// for(int16_t i=0; i<xsize; i+=XBUFF_LEN) {
fp.read((uint8_t*)rgb,xsize*2);
renderer->pushColors(rgb,xsize,true);
// }
OsWatchLoop();
}
renderer->setAddrWindow(0,0,0,0);

View File

@ -83,17 +83,17 @@ void ST7789_InitDriver()
fg_color = ST7789_WHITE;
bg_color = ST7789_BLACK;
uint8_t bppin=BACKPLANE_PIN;
int8_t bppin=BACKPLANE_PIN;
if (PinUsed(GPIO_BACKLIGHT)) {
bppin=Pin(GPIO_BACKLIGHT);
}
uint8_t reset = -1;
int8_t reset = -1;
if (PinUsed(GPIO_OLED_RESET)) {
reset=Pin(GPIO_OLED_RESET);
}
uint8_t cs = -1;
int8_t cs = -1;
if (PinUsed(GPIO_SSPI_CS)) {
cs=Pin(GPIO_SSPI_CS);
} else if (PinUsed(GPIO_SPI_CS)) {
@ -114,7 +114,7 @@ void ST7789_InitDriver()
// init renderer, may use hardware spi
if (PinUsed(GPIO_SPI_CS) && (Pin(GPIO_SPI_MOSI)==HW_SPI_MOSI) && (Pin(GPIO_SPI_CLK)==HW_SPI_CLK) && PinUsed(GPIO_SPI_DC)) {
st7789 = new Arduino_ST7789(Pin(GPIO_SPI_DC), reset, Pin(GPIO_SPI_MOSI), Pin(GPIO_SPI_CLK), cs, bppin);
st7789 = new Arduino_ST7789(Pin(GPIO_SPI_DC), reset, cs, bppin);
} else {
if ((PinUsed(GPIO_SSPI_CS) || PinUsed(GPIO_OLED_RESET)) && PinUsed(GPIO_SSPI_MOSI) && PinUsed(GPIO_SSPI_SCLK) && PinUsed(GPIO_SSPI_DC)) {
st7789 = new Arduino_ST7789(Pin(GPIO_SSPI_DC), reset, Pin(GPIO_SSPI_MOSI), Pin(GPIO_SSPI_SCLK), cs, bppin);