DEV Community

jay jordan
jay jordan

Posted on

portable computer smart watch

ifndef ARDUINO_STM32_FEATHER

#include "pins_arduino.h"
#include "wiring_private.h"

endif

include

include

// If the SPI library has transaction support, these functions
// establish settings and protect from interference from other
// libraries. Otherwise, they simply do nothing.

ifdef SPI_HAS_TRANSACTION

static inline void spi_begin(void) attribute((always_inline));
static inline void spi_begin(void) {

if defined (ARDUINO_ARCH_ARC32)

// max speed!
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));

else

// max speed!
Enter fullscreen mode Exit fullscreen mode

SPI.beginTransaction(SPISettings(24000000, MSBFIRST, SPI_MODE0));

endif

}
static inline void spi_end(void) attribute((always_inline));
static inline void spi_end(void) {
SPI.endTransaction();
}

else

define spi_begin()

define spi_end()

endif

// Constructor when using software SPI. All output pins are configurable.
ILI9488::ILI9488(int8_t cs, int8_t dc, int8_t mosi,
int8_t sclk, int8_t rst, int8_t miso) : Adafruit_GFX(ILI9488_TFTWIDTH, ILI9488_TFTHEIGHT) {
_cs = cs;
_dc = dc;
_mosi = mosi;
_miso = miso;
_sclk = sclk;
_rst = rst;
hwSPI = false;
}

// 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.)
ILI9488::ILI9488(int8_t cs, int8_t dc, int8_t rst) : Adafruit_GFX(ILI9488_TFTWIDTH, ILI9488_TFTHEIGHT) {
_cs = cs;
_dc = dc;
_rst = rst;
hwSPI = true;
_mosi = _sclk = 0;
}

void ILI9488::spiwrite(uint8_t c) {

//Serial.print("0x"); Serial.print(c, HEX); Serial.print(", ");

if (hwSPI) {

if defined (AVR)

#ifndef SPI_HAS_TRANSACTION
uint8_t backupSPCR = SPCR;
SPCR = mySPCR;
#endif
SPDR = c;
while(!(SPSR & _BV(SPIF)));
#ifndef SPI_HAS_TRANSACTION
SPCR = backupSPCR;
#endif

else

SPI.transfer(c);
Enter fullscreen mode Exit fullscreen mode

endif

} else {

if defined(ESP8266) || defined (ARDUINO_ARCH_ARC32) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_ESP32)

for(uint8_t bit = 0x80; bit; bit >>= 1) {
  if(c & bit) {
digitalWrite(_mosi, HIGH);
  } else {
digitalWrite(_mosi, LOW);
  }
  digitalWrite(_sclk, HIGH);
  digitalWrite(_sclk, LOW);
}
Enter fullscreen mode Exit fullscreen mode

else

// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
  if(c & bit) {
//digitalWrite(_mosi, HIGH);
*mosiport |=  mosipinmask;
  } else {
//digitalWrite(_mosi, LOW);
*mosiport &= ~mosipinmask;
  }
  //digitalWrite(_sclk, HIGH);
  *clkport |=  clkpinmask;
  //digitalWrite(_sclk, LOW);
  *clkport &= ~clkpinmask;
}
Enter fullscreen mode Exit fullscreen mode

endif

}
}

void ILI9488::writecommand(uint8_t c) {

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport &= ~dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, LOW);
digitalWrite(_sclk, LOW);
digitalWrite(_cs, LOW);

endif

spiwrite(c);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

}

void ILI9488::writedata(uint8_t c) {

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

spiwrite(c);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

}

// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in PROGMEM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.

define DELAY 0x80

// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void ILI9488::commandList(uint8_t *addr) {

uint8_t numCommands, numArgs;
uint16_t ms;

numCommands = pgm_read_byte(addr++); // Number of commands to follow
while(numCommands--) { // For each command...
writecommand(pgm_read_byte(addr++)); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & DELAY; // If hibit set, delay follows args
numArgs &= ~DELAY; // Mask out delay bit
while(numArgs--) { // For each argument...
writedata(pgm_read_byte(addr++)); // Read, issue argument
}

if(ms) {
  ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
  if(ms == 255) ms = 500;     // If 255, delay for 500 ms
  delay(ms);
}
Enter fullscreen mode Exit fullscreen mode

}
}

void ILI9488::begin(void) {
if (_rst > 0) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, LOW);
}

pinMode(_dc, OUTPUT);
pinMode(_cs, OUTPUT);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
dcport = portOutputRegister(digitalPinToPort(_dc));
dcpinmask = digitalPinToBitMask(_dc);

endif

if(hwSPI) { // Using hardware SPI
SPI.begin();

ifndef SPI_HAS_TRANSACTION

SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
Enter fullscreen mode Exit fullscreen mode

#if defined (AVR)
SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (full! speed!)
mySPCR = SPCR;
#elif defined(TEENSYDUINO) || defined (
STM32F1)
SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (full! speed!)
#elif defined (
arm_)
SPI.setClockDivider(11); // 8-ish MHz (full! speed!)
#endif

endif

} else {
pinMode(_sclk, OUTPUT);
pinMode(_mosi, OUTPUT);
pinMode(_miso, INPUT);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

clkport     = portOutputRegister(digitalPinToPort(_sclk));
clkpinmask  = digitalPinToBitMask(_sclk);
mosiport    = portOutputRegister(digitalPinToPort(_mosi));
mosipinmask = digitalPinToBitMask(_mosi);
*clkport   &= ~clkpinmask;
*mosiport  &= ~mosipinmask;
Enter fullscreen mode Exit fullscreen mode

endif

}

// toggle RST low to reset
if (_rst > 0) {
digitalWrite(_rst, HIGH);
delay(5);
digitalWrite(_rst, LOW);
delay(20);
digitalWrite(_rst, HIGH);
delay(150);
}

/*
uint8_t x = readcommand8(ILI9488_RDMODE);
Serial.print("\nDisplay Power Mode: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9488_RDMADCTL);
Serial.print("\nMADCTL Mode: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9488_RDPIXFMT);
Serial.print("\nPixel Format: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9488_RDIMGFMT);
Serial.print("\nImage Format: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9488_RDSELFDIAG);
Serial.print("\nSelf Diagnostic: 0x"); Serial.println(x, HEX);
*/
//if(cmdList) commandList(cmdList);

if (hwSPI) spi_begin();
// writecommand(0xEF);
// writedata(0x03);
// writedata(0x80);
// writedata(0x02);
//
// writecommand(0xCF);
// writedata(0x00);
// writedata(0XC1);
// writedata(0X30);
//
// writecommand(0xED);
// writedata(0x64);
// writedata(0x03);
// writedata(0X12);
// writedata(0X81);
//
// writecommand(0xE8);
// writedata(0x85);
// writedata(0x00);
// writedata(0x78);
//
// writecommand(0xCB);
// writedata(0x39);
// writedata(0x2C);
// writedata(0x00);
// writedata(0x34);
// writedata(0x02);
//
// writecommand(0xF7);
// writedata(0x20);
//
// writecommand(0xEA);
// writedata(0x00);
// writedata(0x00);
//
// writecommand(ILI9488_PWCTR1); //Power control
// writedata(0x23); //VRH[5:0]
//
// writecommand(ILI9488_PWCTR2); //Power control
// writedata(0x10); //SAP[2:0];BT[3:0]
//
// writecommand(ILI9488_VMCTR1); //VCM control
// writedata(0x3e); //¶Ô±È¶Èµ÷½Ú
// writedata(0x28);
//
// writecommand(ILI9488_VMCTR2); //VCM control2
// writedata(0x86); //--
//
// writecommand(ILI9488_MADCTL); // Memory Access Control
// writedata(0x48);
//
// writecommand(ILI9488_PIXFMT);
// writedata(0x55);
//
// writecommand(ILI9488_FRMCTR1);
// writedata(0x00);
// writedata(0x18);
//
// writecommand(ILI9488_DFUNCTR); // Display Function Control
// writedata(0x08);
// writedata(0x82);
// writedata(0x27);
//
// writecommand(0xF2); // 3Gamma Function Disable
// writedata(0x00);
//
// writecommand(ILI9488_GAMMASET); //Gamma curve selected
// writedata(0x01);
//
// writecommand(ILI9488_GMCTRP1); //Set Gamma
// writedata(0x0F);
// writedata(0x31);
// writedata(0x2B);
// writedata(0x0C);
// writedata(0x0E);
// writedata(0x08);
// writedata(0x4E);
// writedata(0xF1);
// writedata(0x37);
// writedata(0x07);
// writedata(0x10);
// writedata(0x03);
// writedata(0x0E);
// writedata(0x09);
// writedata(0x00);
//
// writecommand(ILI9488_GMCTRN1); //Set Gamma
// writedata(0x00);
// writedata(0x0E);
// writedata(0x14);
// writedata(0x03);
// writedata(0x11);
// writedata(0x07);
// writedata(0x31);
// writedata(0xC1);
// writedata(0x48);
// writedata(0x08);
// writedata(0x0F);
// writedata(0x0C);
// writedata(0x31);
// writedata(0x36);
// writedata(0x0F);

writecommand(0xE0);
writedata(0x00);
writedata(0x03);
writedata(0x09);
writedata(0x08);
writedata(0x16);
writedata(0x0A);
writedata(0x3F);
writedata(0x78);
writedata(0x4C);
writedata(0x09);
writedata(0x0A);
writedata(0x08);
writedata(0x16);
writedata(0x1A);
writedata(0x0F);

writecommand(0XE1);
writedata(0x00);
writedata(0x16);
writedata(0x19);
writedata(0x03);
writedata(0x0F);
writedata(0x05);
writedata(0x32);
writedata(0x45);
writedata(0x46);
writedata(0x04);
writedata(0x0E);
writedata(0x0D);
writedata(0x35);
writedata(0x37);
writedata(0x0F);



writecommand(0XC0);      //Power Control 1
writedata(0x17);    //Vreg1out
writedata(0x15);    //Verg2out

writecommand(0xC1);      //Power Control 2
writedata(0x41);    //VGH,VGL

writecommand(0xC5);      //Power Control 3
writedata(0x00);
writedata(0x12);    //Vcom
writedata(0x80);

writecommand(0x36);      //Memory Access
writedata(0x48);

writecommand(0x3A);      // Interface Pixel Format
writedata(0x66);      //18 bit

writecommand(0XB0);      // Interface Mode Control
writedata(0x80);                 //SDO NOT USE

writecommand(0xB1);      //Frame rate
writedata(0xA0);    //60Hz

writecommand(0xB4);      //Display Inversion Control
writedata(0x02);    //2-dot

writecommand(0XB6);      //Display Function Control  RGB/MCU Interface Control

writedata(0x02);    //MCU
writedata(0x02);    //Source,Gate scan dieection

writecommand(0XE9);      // Set Image Functio
writedata(0x00);    // Disable 24 bit data

writecommand(0xF7);      // Adjust Control
writedata(0xA9);
writedata(0x51);
writedata(0x2C);
writedata(0x82);    // D7 stream, loose
Enter fullscreen mode Exit fullscreen mode

writecommand(ILI9488_SLPOUT); //Exit Sleep
if (hwSPI) spi_end();
delay(120);
if (hwSPI) spi_begin();
writecommand(ILI9488_DISPON); //Display on
if (hwSPI) spi_end();

}

void ILI9488::setScrollArea(uint16_t topFixedArea, uint16_t bottomFixedArea){
if (hwSPI) spi_begin();
writecommand(0x33); // Vertical scroll definition
writedata(topFixedArea >> 8);
writedata(topFixedArea);
writedata((_height - topFixedArea - bottomFixedArea) >> 8);
writedata(_height - topFixedArea - bottomFixedArea);
writedata(bottomFixedArea >> 8);
writedata(bottomFixedArea);
if (hwSPI) spi_end();
}
void ILI9488::scroll(uint16_t pixels){
if (hwSPI) spi_begin();
writecommand(0x37); // Vertical scrolling start address
writedata(pixels >> 8);
writedata(pixels);
if (hwSPI) spi_end();
}

void ILI9488::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
uint16_t y1) {

writecommand(ILI9488_CASET); // Column addr set
writedata(x0 >> 8);
writedata(x0 & 0xFF); // XSTART
writedata(x1 >> 8);
writedata(x1 & 0xFF); // XEND

writecommand(ILI9488_PASET); // Row addr set
writedata(y0>>8);
writedata(y0 &0xff); // YSTART
writedata(y1>>8);
writedata(y1 &0xff); // YEND

writecommand(ILI9488_RAMWR); // write to RAM

}

void ILI9488::drawImage(const uint8_t* img, uint16_t x, uint16_t y, uint16_t w, uint16_t h){

// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width)  w = _width  - x;
if((y + h - 1) >= _height) h = _height - y;

if (hwSPI) spi_begin();
setAddrWindow(x, y, x+w-1, y+h-1);

// uint8_t hi = color >> 8, lo = color;
Enter fullscreen mode Exit fullscreen mode

#if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)
dcport |= dcpinmask;
*csport &= ~cspinmask;
#else
digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);
#endif
uint8_t linebuff[w*3+1];
uint16_t pixels = w*h;
// uint16_t count = 0;
uint32_t count = 0;
for (uint16_t i = 0; i < h; i++) {
uint16_t pixcount = 0;
for (uint16_t o = 0; o < w; o++) {
uint8_t b1 = img[count];
count++;
uint8_t b2 = img[count];
count++;
uint16_t color = b1 << 8 | b2;
linebuff[pixcount] = (((color & 0xF800) >> 11)
255) / 31;
pixcount++;
linebuff[pixcount] = (((color & 0x07E0) >> 5) * 255) / 63;
pixcount++;
linebuff[pixcount] = ((color & 0x001F)* 255) / 31;
pixcount++;
} // for row
#if defined (STM32F1)
SPI.dmaSend(linebuff, w*3);
#else
for(uint16_t b = 0; b < w*3; b++){
spiwrite(linebuff[b]);
}
#endif

}// for col
#if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)
*csport |= cspinmask;
#else
digitalWrite(_cs, HIGH);
#endif

if (hwSPI) spi_end();
Enter fullscreen mode Exit fullscreen mode

}

void ILI9488::pushColor(uint16_t color) {
if (hwSPI) spi_begin();

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

// spiwrite(color >> 8);
// spiwrite(color);
// spiwrite(0); // added for 24 bit
write16BitColor(color);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

if (hwSPI) spi_end();
}

void ILI9488::pushColors(uint16_t data, uint8_t len, boolean first) {
uint16_t color;
uint8_t buff[len*3+1];
uint16_t count = 0;
uint8_t lencount = len;
if (hwSPI) spi_begin();
#if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)
*csport &= ~cspinmask;
#else
digitalWrite(cs, LOW);
#endif
if(first == true) { // Issue GRAM write command only on first call
#if defined(USE_FAST_PINIO) && !defined (_VARIANT_ARDUINO_STM32
)
*dcport |= dcpinmask;
#else
digitalWrite(_dc, HIGH);
#endif
}
while(lencount--) {
color = *data++;
buff[count] = (((color & 0xF800) >> 11)
255) / 31;
count++;
buff[count] = (((color & 0x07E0) >> 5) * 255) / 63;
count++;
buff[count] = ((color & 0x001F)* 255) / 31;
count++;
}
#if defined (STM32F1)
SPI.dmaSend(buff, len*3);
#else
for(uint16_t b = 0; b < len*3; b++){
spiwrite(buff[b]);
}
#endif
#if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)
*csport |= cspinmask;
#else
digitalWrite(_cs, HIGH);
#endif

if (hwSPI) spi_end();
Enter fullscreen mode Exit fullscreen mode

}

void ILI9488::write16BitColor(uint16_t color){
// #if (STM32F1)
// uint8_t buff[4] = {
// (((color & 0xF800) >> 11)* 255) / 31,
// (((color & 0x07E0) >> 5) * 255) / 63,
// ((color & 0x001F)* 255) / 31
// };
// SPI.dmaSend(buff, 3);
// #else
uint8_t r = (color & 0xF800) >> 11;
uint8_t g = (color & 0x07E0) >> 5;
uint8_t b = color & 0x001F;

r = (r * 255) / 31;
g = (g * 255) / 63;
b = (b * 255) / 31;

spiwrite(r);
spiwrite(g);
spiwrite(b);
yield();
// #endif
}

void ILI9488::drawPixel(int16_t x, int16_t y, uint16_t color) {

if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;

if (hwSPI) spi_begin();
setAddrWindow(x,y,x+1,y+1);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

// spiwrite(color >> 8);
// spiwrite(color);
// spiwrite(0); // added for 24 bit
write16BitColor(color);

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

if (hwSPI) spi_end();
}

void ILI9488::drawFastVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {

// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;

if((y+h-1) >= _height)
h = _height-y;

if (hwSPI) spi_begin();
setAddrWindow(x, y, x, y+h-1);

// uint8_t hi = color >> 8, lo = color;

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

while (h--) {
// spiwrite(hi);
// spiwrite(lo);
// spiwrite(0); // added for 24 bit
write16BitColor(color);

}

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

if (hwSPI) spi_end();
}

void ILI9488::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {

// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width) w = _width-x;
if (hwSPI) spi_begin();
setAddrWindow(x, y, x+w-1, y);

// uint8_t hi = color >> 8, lo = color;

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

while (w--) {
// spiwrite(hi);
// spiwrite(lo);
// spiwrite(0); // added for 24 bit
write16BitColor(color);
}

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

if (hwSPI) spi_end();
}

void ILI9488::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}

// fill a rectangle
void ILI9488::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {

// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width) w = _width - x;
if((y + h - 1) >= _height) h = _height - y;

if (hwSPI) spi_begin();
setAddrWindow(x, y, x+w-1, y+h-1);

// uint8_t hi = color >> 8, lo = color;

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*dcport |= dcpinmask;
*csport &= ~cspinmask;

else

digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);

endif

if (STM32F1)

//use dma fast fills
uint8_t buff[4] = {
(((color & 0xF800) >> 11)* 255) / 31,
(((color & 0x07E0) >> 5) * 255) / 63,
((color & 0x001F)* 255) / 31
};
uint8_t linebuff[w*3+1];
int cnt = 0;
for(int i = 0; i < w; i++){
linebuff[cnt]= buff[0];
cnt++;
linebuff[cnt]= buff[1];
cnt++;
linebuff[cnt]= buff[2];
cnt++;
}
for(y=h; y>0; y--) {
SPI.dmaSend(linebuff, w*3);
}

else

for(y=h; y>0; y--) {
for(x=w; x>0; x--) {
// spiwrite(hi);
// spiwrite(lo);
// spiwrite(0); // added for 24 bit
write16BitColor(color);
}
}

endif

if defined(USE_FAST_PINIO) && !defined (VARIANT_ARDUINO_STM32)

*csport |= cspinmask;

else

digitalWrite(_cs, HIGH);

endif

if (hwSPI) spi_end();
}

// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t ILI9488::color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}

define MADCTL_MY 0x80

define MADCTL_MX 0x40

define MADCTL_MV 0x20

define MADCTL_ML 0x10

define MADCTL_RGB 0x00

define MADCTL_BGR 0x08

define MADCTL_MH 0x04

void ILI9488::setRotation(uint8_t m) {

if (hwSPI) spi_begin();
writecommand(ILI9488_MADCTL);
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
writedata(MADCTL_MX | MADCTL_BGR);
_width = ILI9488_TFTWIDTH;
_height = ILI9488_TFTHEIGHT;
break;
case 1:
writedata(MADCTL_MV | MADCTL_BGR);
_width = ILI9488_TFTHEIGHT;
_height = ILI9488_TFTWIDTH;
break;
case 2:
writedata(MADCTL_MY | MADCTL_BGR);
_width = ILI9488_TFTWIDTH;
_height = ILI9488_TFTHEIGHT;
break;
case 3:
writedata(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
_width = ILI9488_TFTHEIGHT;
_height = ILI9488_TFTWIDTH;
break;
}
if (hwSPI) spi_end();
}

void ILI9488::invertDisplay(boolean i) {
if (hwSPI) spi_begin();
writecommand(i ? ILI9488_INVON : ILI9488_INVOFF);
if (hwSPI) spi_end();
}

////////// stuff not actively being used, but kept for posterity

uint8_t ILI9488::spiread(void) {
uint8_t r = 0;

if (hwSPI) {

if defined (AVR)

#ifndef SPI_HAS_TRANSACTION
uint8_t backupSPCR = SPCR;
SPCR = mySPCR;
#endif
SPDR = 0x00;
while(!(SPSR & _BV(SPIF)));
r = SPDR;

#ifndef SPI_HAS_TRANSACTION
SPCR = backupSPCR;
#endif

else

r = SPI.transfer(0x00);
Enter fullscreen mode Exit fullscreen mode

endif

} else {

for (uint8_t i=0; i<8; i++) {
  digitalWrite(_sclk, LOW);
  digitalWrite(_sclk, HIGH);
  r <<= 1;
  if (digitalRead(_miso))
r |= 0x1;
}
Enter fullscreen mode Exit fullscreen mode

}
//Serial.print("read: 0x"); Serial.print(r, HEX);

return r;
}

uint8_t ILI9488::readdata(void) {
digitalWrite(_dc, HIGH);
digitalWrite(_cs, LOW);
uint8_t r = spiread();
digitalWrite(_cs, HIGH);

return r;
}

uint8_t ILI9488::readcommand8(uint8_t c, uint8_t index) {
if (hwSPI) spi_begin();
digitalWrite(_dc, LOW); // command
digitalWrite(_cs, LOW);
spiwrite(0xD9); // woo sekret command?
digitalWrite(_dc, HIGH); // data
spiwrite(0x10 + index);
digitalWrite(_cs, HIGH);

digitalWrite(_dc, LOW);
digitalWrite(_sclk, LOW);
digitalWrite(_cs, LOW);
spiwrite(c);

digitalWrite(_dc, HIGH);
uint8_t r = spiread();
digitalWrite(_cs, HIGH);
if (hwSPI) spi_end();
return r;
}

/*

uint16_t ILI9488::readcommand16(uint8_t c) {
digitalWrite(_dc, LOW);
if (_cs)
digitalWrite(_cs, LOW);

spiwrite(c);
pinMode(_sid, INPUT); // input!
uint16_t r = spiread();
r <<= 8;
r |= spiread();
if (_cs)
digitalWrite(_cs, HIGH);

pinMode(_sid, OUTPUT); // back to output
return r;
}

uint32_t ILI9488::readcommand32(uint8_t c) {
digitalWrite(_dc, LOW);
if (_cs)
digitalWrite(_cs, LOW);
spiwrite(c);
pinMode(_sid, INPUT); // input!

dummyclock();
dummyclock();

uint32_t r = spiread();
r <<= 8;
r |= spiread();
r <<= 8;
r |= spiread();
r <<= 8;
r |= spiread();
if (_cs)
digitalWrite(_cs, HIGH);

pinMode(_sid, OUTPUT); // back to output
return r;
}

/
/
**************************************************
This is our GFX example for the Adafruit ILI9488 Breakout and Shield
----> http://www.adafruit.com/products/1651

Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/

include "SPI.h"

include

include

define TFT_CS PA1

define TFT_DC PB3

define TFT_LED PB0

define TFT_RST PB4

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
ILI9488 tft = ILI9488(TFT_CS, TFT_DC, TFT_RST);
// If using the breakout, change pins as desired
//Adafruit_ILI9488 tft = Adafruit_ILI9488(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

void setup() {
Serial.begin(9600);
Serial.println("ILI9488 Test!");

tft.begin();

// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9488_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9488_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9488_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9488_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9488_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);

Serial.println(F("Benchmark Time (microseconds)"));

Serial.print(F("Screen fill "));
Serial.println(testFillScreen());
delay(500);

Serial.print(F("Text "));
Serial.println(testText());
delay(3000);

Serial.print(F("Lines "));
Serial.println(testLines(ILI9488_CYAN));
delay(500);

Serial.print(F("Horiz/Vert Lines "));
Serial.println(testFastLines(ILI9488_RED, ILI9488_BLUE));
delay(500);

Serial.print(F("Rectangles (outline) "));
Serial.println(testRects(ILI9488_GREEN));
delay(500);

Serial.print(F("Rectangles (filled) "));
Serial.println(testFilledRects(ILI9488_YELLOW, ILI9488_MAGENTA));
delay(500);

Serial.print(F("Circles (filled) "));
Serial.println(testFilledCircles(10, ILI9488_MAGENTA));

Serial.print(F("Circles (outline) "));
Serial.println(testCircles(10, ILI9488_WHITE));
delay(500);

Serial.print(F("Triangles (outline) "));
Serial.println(testTriangles());
delay(500);

Serial.print(F("Triangles (filled) "));
Serial.println(testFilledTriangles());
delay(500);

Serial.print(F("Rounded rects (outline) "));
Serial.println(testRoundRects());
delay(500);

Serial.print(F("Rounded rects (filled) "));
Serial.println(testFilledRoundRects());
delay(500);

Serial.println(F("Done!"));

}

void loop(void) {
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
testText();
delay(1000);
}
}

unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(ILI9488_BLACK);
tft.fillScreen(ILI9488_RED);
tft.fillScreen(ILI9488_GREEN);
tft.fillScreen(ILI9488_BLUE);
tft.fillScreen(ILI9488_BLACK);
return micros() - start;
}

unsigned long testText() {
tft.fillScreen(ILI9488_BLACK);
unsigned long start = micros();
tft.setCursor(0, 0);
tft.setTextColor(ILI9488_WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(ILI9488_YELLOW); tft.setTextSize(2);
tft.println(1234.56);
tft.setTextColor(ILI9488_RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(ILI9488_GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tft.setTextSize(1);
tft.println("my foonting turlingdromes.");
tft.println("And hooptiously drangle me");
tft.println("with crinkly bindlewurdles,");
tft.println("Or I will rend thee");
tft.println("in the gobberwarts");
tft.println("with my blurglecruncheon,");
tft.println("see if I don't!");
return micros() - start;
}

unsigned long testLines(uint16_t color) {
unsigned long start, t;
int x1, y1, x2, y2,
w = tft.width(),
h = tft.height();

tft.fillScreen(ILI9488_BLACK);

x1 = y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t = micros() - start; // fillScreen doesn't count against timing

tft.fillScreen(ILI9488_BLACK);

x1 = w - 1;
y1 = 0;
y2 = h - 1;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;

tft.fillScreen(ILI9488_BLACK);

x1 = 0;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = w - 1;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);
t += micros() - start;

tft.fillScreen(ILI9488_BLACK);

x1 = w - 1;
y1 = h - 1;
y2 = 0;
start = micros();
for(x2=0; x2<w; x2+=6) tft.drawLine(x1, y1, x2, y2, color);
x2 = 0;
for(y2=0; y2<h; y2+=6) tft.drawLine(x1, y1, x2, y2, color);

return micros() - start;
}

unsigned long testFastLines(uint16_t color1, uint16_t color2) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height();

tft.fillScreen(ILI9488_BLACK);
start = micros();
for(y=0; y<h; y+=5) tft.drawFastHLine(0, y, w, color1);
for(x=0; x<w; x+=5) tft.drawFastVLine(x, 0, h, color2);

return micros() - start;
}

unsigned long testRects(uint16_t color) {
unsigned long start;
int n, i, i2,
cx = tft.width() / 2,
cy = tft.height() / 2;

tft.fillScreen(ILI9488_BLACK);
n = min(tft.width(), tft.height());
start = micros();
for(i=2; i<n; i+=6) {
i2 = i / 2;
tft.drawRect(cx-i2, cy-i2, i, i, color);
}

return micros() - start;
}

unsigned long testFilledRects(uint16_t color1, uint16_t color2) {
unsigned long start, t = 0;
int n, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(ILI9488_BLACK);
n = min(tft.width(), tft.height());
for(i=n; i>0; i-=6) {
i2 = i / 2;
start = micros();
tft.fillRect(cx-i2, cy-i2, i, i, color1);
t += micros() - start;
// Outlines are not included in timing results
tft.drawRect(cx-i2, cy-i2, i, i, color2);
}

return t;
}

unsigned long testFilledCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, w = tft.width(), h = tft.height(), r2 = radius * 2;

tft.fillScreen(ILI9488_BLACK);
start = micros();
for(x=radius; x<w; x+=r2) {
for(y=radius; y<h; y+=r2) {
tft.fillCircle(x, y, radius, color);
}
}

return micros() - start;
}

unsigned long testCircles(uint8_t radius, uint16_t color) {
unsigned long start;
int x, y, r2 = radius * 2,
w = tft.width() + radius,
h = tft.height() + radius;

// Screen is not cleared for this one -- this is
// intentional and does not affect the reported time.
start = micros();
for(x=0; x<w; x+=r2) {
for(y=0; y<h; y+=r2) {
tft.drawCircle(x, y, radius, color);
}
}

return micros() - start;
}

unsigned long testTriangles() {
unsigned long start;
int n, i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(ILI9488_BLACK);
n = min(cx, cy);
start = micros();
for(i=0; i<n; i+=5) {
tft.drawTriangle(
cx , cy - i, // peak
cx - i, cy + i, // bottom left
cx + i, cy + i, // bottom right
tft.color565(0, 0, i));
}

return micros() - start;
}

unsigned long testFilledTriangles() {
unsigned long start, t = 0;
int i, cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(ILI9488_BLACK);
start = micros();
for(i=min(cx,cy); i>10; i-=5) {
start = micros();
tft.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(0, i, i));
t += micros() - start;
tft.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
tft.color565(i, i, 0));
}

return t;
}

unsigned long testRoundRects() {
unsigned long start;
int w, i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(ILI9488_BLACK);
w = min(tft.width(), tft.height());
start = micros();
for(i=0; i<w; i+=6) {
i2 = i / 2;
tft.drawRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(i, 0, 0));
}

return micros() - start;
}

unsigned long testFilledRoundRects() {
unsigned long start;
int i, i2,
cx = tft.width() / 2 - 1,
cy = tft.height() / 2 - 1;

tft.fillScreen(ILI9488_BLACK);
start = micros();
for(i=min(tft.width(), tft.height()); i>20; i-=6) {
i2 = i / 2;
tft.fillRoundRect(cx-i2, cy-i2, i, i, i/8, tft.color565(0, i, 0));
}

return micros() - start;
}
/***************************************************
STM32 Support added by Jaret Burkett at OSHlab.com

This is our library for the Adafruit ILI9488 Breakout and Shield
----> http://www.adafruit.com/products/1651

Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/

ifndef ILI9488H

define ILI9488H

if ARDUINO >= 100

#include "Arduino.h"
#include "Print.h"

else

#include "WProgram.h"

endif

include

ifdef __AVR

#include

elif defined(ESP8266)

#include

endif

if defined(ARDUINO_STM32_FEATHER) || defined(ARDUINO_ARCH_RP2040)

typedef volatile uint32_t RwReg;

endif

if defined (AVR) || defined(TEENSYDUINO) || defined (arm) || defined (STM32F1)

define USE_FAST_PINIO

endif

if defined(ARDUINO_ARCH_RP2040)

undef USE_FAST_PINIO

endif

define ILI9488_TFTWIDTH 320

define ILI9488_TFTHEIGHT 480

define ILI9488_NOP 0x00

define ILI9488_SWRESET 0x01

define ILI9488_RDDID 0x04

define ILI9488_RDDST 0x09

define ILI9488_SLPIN 0x10

define ILI9488_SLPOUT 0x11

define ILI9488_PTLON 0x12

define ILI9488_NORON 0x13

define ILI9488_RDMODE 0x0A

define ILI9488_RDMADCTL 0x0B

define ILI9488_RDPIXFMT 0x0C

define ILI9488_RDIMGFMT 0x0D

define ILI9488_RDSELFDIAG 0x0F

define ILI9488_INVOFF 0x20

define ILI9488_INVON 0x21

define ILI9488_GAMMASET 0x26

define ILI9488_DISPOFF 0x28

define ILI9488_DISPON 0x29

define ILI9488_CASET 0x2A

define ILI9488_PASET 0x2B

define ILI9488_RAMWR 0x2C

define ILI9488_RAMRD 0x2E

define ILI9488_PTLAR 0x30

define ILI9488_MADCTL 0x36

define ILI9488_PIXFMT 0x3A

define ILI9488_FRMCTR1 0xB1

define ILI9488_FRMCTR2 0xB2

define ILI9488_FRMCTR3 0xB3

define ILI9488_INVCTR 0xB4

define ILI9488_DFUNCTR 0xB6

define ILI9488_PWCTR1 0xC0

define ILI9488_PWCTR2 0xC1

define ILI9488_PWCTR3 0xC2

define ILI9488_PWCTR4 0xC3

define ILI9488_PWCTR5 0xC4

define ILI9488_VMCTR1 0xC5

define ILI9488_VMCTR2 0xC7

define ILI9488_RDID1 0xDA

define ILI9488_RDID2 0xDB

define ILI9488_RDID3 0xDC

define ILI9488_RDID4 0xDD

define ILI9488_GMCTRP1 0xE0

define ILI9488_GMCTRN1 0xE1

/*

define ILI9488_PWCTR6 0xFC

*/

// Color definitions

define ILI9488_BLACK 0x0000 /* 0, 0, 0 */

define ILI9488_NAVY 0x000F /* 0, 0, 128 */

define ILI9488_DARKGREEN 0x03E0 /* 0, 128, 0 */

define ILI9488_DARKCYAN 0x03EF /* 0, 128, 128 */

define ILI9488_MAROON 0x7800 /* 128, 0, 0 */

define ILI9488_PURPLE 0x780F /* 128, 0, 128 */

define ILI9488_OLIVE 0x7BE0 /* 128, 128, 0 */

define ILI9488_LIGHTGREY 0xC618 /* 192, 192, 192 */

define ILI9488_DARKGREY 0x7BEF /* 128, 128, 128 */

define ILI9488_BLUE 0x001F /* 0, 0, 255 */

define ILI9488_GREEN 0x07E0 /* 0, 255, 0 */

define ILI9488_CYAN 0x07FF /* 0, 255, 255 */

define ILI9488_RED 0xF800 /* 255, 0, 0 */

define ILI9488_MAGENTA 0xF81F /* 255, 0, 255 */

define ILI9488_YELLOW 0xFFE0 /* 255, 255, 0 */

define ILI9488_WHITE 0xFFFF /* 255, 255, 255 */

define ILI9488_ORANGE 0xFD20 /* 255, 165, 0 */

define ILI9488_GREENYELLOW 0xAFE5 /* 173, 255, 47 */

define ILI9488_PINK 0xF81F

class ILI9488 : public Adafruit_GFX {

public:

ILI9488(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,
int8_t _RST, int8_t _MISO);
ILI9488(int8_t _CS, int8_t _DC, int8_t _RST = -1);

void begin(void),
setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),
setScrollArea(uint16_t topFixedArea, uint16_t bottomFixedArea),
scroll(uint16_t pixels),
pushColor(uint16_t color),
pushColors(uint16_t data, uint8_t len, boolean first),
drawImage(const uint8_t
img, uint16_t x, uint16_t y, uint16_t w, uint16_t h),
fillScreen(uint16_t color),
drawPixel(int16_t x, int16_t y, uint16_t color),
drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color),
setRotation(uint8_t r),
invertDisplay(boolean i);
uint16_t color565(uint8_t r, uint8_t g, uint8_t b);

/* These are not for current use, 8-bit protocol only! /
uint8_t readdata(void),
readcommand8(uint8_t reg, uint8_t index = 0);
/

uint16_t readcommand16(uint8_t);
uint32_t readcommand32(uint8_t);
void dummyclock(void);
*/

void spiwrite(uint8_t),
writecommand(uint8_t c),
write16BitColor(uint16_t color),
writedata(uint8_t d),
commandList(uint8_t *addr);
uint8_t spiread(void);

private:
uint8_t tabcolor;

boolean hwSPI;

if defined (AVR) || defined(TEENSYDUINO)

uint8_t mySPCR;
volatile uint8_t *mosiport, *clkport, *dcport, *rsport, *csport;
int8_t _cs, _dc, _rst, _mosi, _miso, _sclk;
uint8_t mosipinmask, clkpinmask, cspinmask, dcpinmask;
////This def is for the Arduino.ORG M0!!!
//#elif defined(ARDUINO_SAM_ZERO)
// volatile PORT_OUT_Type *mosiport, *clkport, *dcport, *rsport, *csport;
// int32_t _cs, _dc, _rst, _mosi, _miso, _sclk;
// PORT_OUT_Type mosipinmask, clkpinmask, cspinmask, dcpinmask;

elif defined (STM32F1) || defined (VARIANT_ARDUINO_STM32) || defined (STM32F100xE) || defined (STM32F101xE) || defined (STM32F101xG) || defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC)

uint8_t mySPCR;
volatile uint32_t *mosiport, *clkport, *dcport, *rsport, *csport;
int32_t _cs, _dc, _rst, _mosi, _miso, _sclk;
uint32_t mosipinmask, clkpinmask, cspinmask, dcpinmask;
Enter fullscreen mode Exit fullscreen mode




elif defined (arm)


volatile RwReg *mosiport, *clkport, *dcport, *rsport, *csport;
int32_t _cs, _dc, _rst, _mosi, _miso, _sclk;
uint32_t mosipinmask, clkpinmask, cspinmask, dcpinmask;
Enter fullscreen mode Exit fullscreen mode




elif defined (ESP8266)


int32_t  _cs, _dc, _rst, _mosi, _miso, _sclk;
Enter fullscreen mode Exit fullscreen mode




else


int8_t  _cs, _dc, _rst, _mosi, _miso, _sclk;
Enter fullscreen mode Exit fullscreen mode




endif

};

endif

Top comments (0)