2020-08-01 19:49:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2020-08-02 13:51:47 +00:00
|
|
|
#include <string.h>
|
2020-08-01 19:49:42 +00:00
|
|
|
#include "st7735.h"
|
|
|
|
|
|
|
|
/********************************** EASY PORT *********************************/
|
|
|
|
/*
|
|
|
|
* If you porting this code, you can change below headers and function pointers
|
|
|
|
* in gpio structure.
|
|
|
|
*/
|
|
|
|
#include <wiringPi.h>
|
|
|
|
#include <wiringPiSPI.h>
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
void (* const delay)(unsigned int milliseconds);
|
|
|
|
void (* const pinMode)(int pin, int mode);
|
|
|
|
void (* const digitalWrite)(int pin, int value);
|
|
|
|
int (* const spiSetup)(int channel, int speed);
|
|
|
|
int (* const spiDataRW)(int channel, uint8 *data, int length);
|
|
|
|
} static const gpio =
|
|
|
|
{
|
|
|
|
delay,
|
|
|
|
pinMode,
|
|
|
|
digitalWrite,
|
|
|
|
wiringPiSPISetup,
|
|
|
|
wiringPiSPIDataRW
|
|
|
|
};
|
|
|
|
/****************************** END EASY PORT END *****************************/
|
|
|
|
|
|
|
|
static lcd_t *activeDisplay;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Safe allocation of the memory block.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* size - Size of memory block to allocate.
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* Pointer to the memory block. If an error occurs, stop the program.
|
|
|
|
*/
|
|
|
|
static inline void *safeMalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *memoryBlock = (void*) malloc(size);
|
|
|
|
|
|
|
|
/* Check the pointer */
|
|
|
|
if(memoryBlock == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Out of RAM memory!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return memoryBlock;
|
|
|
|
} /* safeMalloc */
|
|
|
|
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
void lcd_setOrientation(lcd_t* lcd, uint8 orientation);
|
|
|
|
void lcd_setGamma(lcd_t* lcd, uint8 state);
|
|
|
|
void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
|
|
|
|
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);
|
2020-08-01 19:49:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the command to the display driver.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* cmd - The command to write.
|
|
|
|
*/
|
|
|
|
static inline void writeCommand(uint8 cmd)
|
|
|
|
{
|
|
|
|
gpio.digitalWrite(activeDisplay->a0, LOW);
|
2020-08-02 13:51:47 +00:00
|
|
|
gpio.spiDataRW(activeDisplay->channel, &cmd, 1);
|
2020-08-01 19:49:42 +00:00
|
|
|
} /* writeCommand */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the data to the display driver.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* data - The data to write.
|
|
|
|
*/
|
|
|
|
static inline void writeData(uint8 data)
|
|
|
|
{
|
|
|
|
gpio.digitalWrite(activeDisplay->a0, HIGH);
|
2020-08-02 13:51:47 +00:00
|
|
|
gpio.spiDataRW(activeDisplay->channel, &data, 1);
|
2020-08-01 19:49:42 +00:00
|
|
|
} /* writeData */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_t *lcd_init(int spiSpeed, int channel, int cs, int a0, int rs)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
/* Create the one instance of the lcdst_t structure and activate it */
|
|
|
|
lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t));
|
|
|
|
|
|
|
|
activeDisplay = instance;
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
instance->channel = channel;
|
2020-08-01 19:49:42 +00:00
|
|
|
instance->cs = cs;
|
|
|
|
instance->a0 = a0;
|
|
|
|
instance->rs = rs;
|
|
|
|
/*
|
|
|
|
* instance->width; instance->height
|
|
|
|
* The setting of this variables will take place
|
|
|
|
* in the function lcdst_setOrientation() below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Configure the a0 pin. The logic level is not significant now. */
|
|
|
|
gpio.pinMode(instance->a0, OUTPUT);
|
|
|
|
|
|
|
|
/* If the rs pin is connected then configure it */
|
|
|
|
if(instance->rs != -1)
|
|
|
|
{
|
|
|
|
gpio.pinMode(instance->rs, OUTPUT);
|
|
|
|
gpio.digitalWrite(instance->rs, HIGH); /* Reset OFF */
|
|
|
|
gpio.delay(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the SPI interface */
|
2020-08-02 13:51:47 +00:00
|
|
|
if(gpio.spiSetup(instance->channel, spiSpeed) == -1)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to setup the SPI interface!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Software reset; Wait minimum 120ms */
|
|
|
|
writeCommand(0x01);
|
|
|
|
gpio.delay(150);
|
|
|
|
|
|
|
|
/* Sleep out; Wait minimum 120ms */
|
|
|
|
writeCommand(0x11);
|
|
|
|
gpio.delay(150);
|
|
|
|
|
|
|
|
/* Set the orientation and the gamma */
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_setOrientation(instance, 0);
|
|
|
|
lcd_setGamma(instance, 2); /* Optional */
|
2020-08-01 19:49:42 +00:00
|
|
|
|
|
|
|
/* Set the pixel format */
|
|
|
|
writeCommand(0x3A);
|
|
|
|
writeData(0x06);
|
|
|
|
|
|
|
|
/* Display ON; Wait 100ms before start */
|
|
|
|
writeCommand(0x29);
|
|
|
|
gpio.delay(100);
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
} /* lcd_init */
|
|
|
|
|
|
|
|
void lcd_deinit(lcd_t *display)
|
|
|
|
{
|
|
|
|
if(display == NULL) return;
|
|
|
|
|
|
|
|
free(display);
|
|
|
|
} /* lcdst_uninit */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
void lcd_setOrientation(lcd_t* lcd, uint8 orientation)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
writeCommand(0x36); /* Memory Data Access Control */
|
|
|
|
|
|
|
|
switch(orientation)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
writeData(0x60); /* MX + MV */
|
|
|
|
activeDisplay->width = 160;
|
|
|
|
activeDisplay->height = 128;
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_setWindow(lcd, 0, 0, 159, 127);
|
2020-08-01 19:49:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
writeData(0xC0); /* MY + MX */
|
|
|
|
activeDisplay->width = 128;
|
|
|
|
activeDisplay->height = 160;
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_setWindow(lcd, 0, 0, 127, 159);
|
2020-08-01 19:49:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
writeData(0xA0); /* MY + MV */
|
|
|
|
activeDisplay->width = 160;
|
|
|
|
activeDisplay->height = 128;
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_setWindow(lcd, 0, 0, 159, 127);
|
2020-08-01 19:49:42 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
writeData(0x00); /* None */
|
|
|
|
activeDisplay->width = 128;
|
|
|
|
activeDisplay->height = 160;
|
2020-08-02 13:51:47 +00:00
|
|
|
lcd_setWindow(lcd, 0, 0, 127, 159);
|
2020-08-01 19:49:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} /* lcdst_setOrientation */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
void lcd_setGamma(lcd_t* lcd, uint8 state)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
/* The status (0 or 1) of the GS pin can only be empirically tested */
|
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case 1: state = 2; break; /* GS_pin=1: 1.8; GS_pin=0: 2.5 */
|
|
|
|
case 2: state = 4; break; /* GS_pin=1: 2.5; GS_pin=0: 2.2 */
|
|
|
|
case 3: state = 8; break; /* GS_pin=1: 1.0; GS_pin=0: 1.8 */
|
|
|
|
default: state = 1; break; /* GS_pin=1: 2.2; GS_pin=0: 1.0 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set built-in gamma */
|
|
|
|
writeCommand(0x26);
|
|
|
|
writeData(state);
|
|
|
|
} /* lcdst_setGamma */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
void lcd_setInversion(lcd_t* lcd, uint8 state)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
/* Display inversion ON/OFF */
|
|
|
|
writeCommand(state ? 0x21 : 0x20);
|
|
|
|
} /* lcdst_setInversion */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
/* Accept: 0 <= x1 <= x2 < activeDisplay->width */
|
|
|
|
if(x2 < x1) return 1;
|
|
|
|
if(x2 >= activeDisplay->width) return 1;
|
|
|
|
|
|
|
|
/* Accept: 0 <= y1 <= y2 < activeDisplay->height */
|
|
|
|
if(y2 < y1) return 1;
|
|
|
|
if(y2 >= activeDisplay->height) return 1;
|
|
|
|
|
|
|
|
/* Set column address */
|
|
|
|
writeCommand(0x2A);
|
|
|
|
writeData(0); writeData(x1);
|
|
|
|
writeData(0); writeData(x2);
|
|
|
|
|
|
|
|
/* Set row address */
|
|
|
|
writeCommand(0x2B);
|
|
|
|
writeData(0); writeData(y1);
|
|
|
|
writeData(0); writeData(y2);
|
|
|
|
|
|
|
|
/* Activate RAW write */
|
|
|
|
writeCommand(0x2C);
|
|
|
|
//gpio.delay(5);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} /* lcdst_setWindow */
|
|
|
|
|
|
|
|
void lcd_activateRamWrite(void)
|
|
|
|
{
|
|
|
|
writeCommand(0x2C);
|
|
|
|
//gpio.delay(5);
|
|
|
|
} /* lcdst_activateRamWrite */
|
|
|
|
|
|
|
|
uint8 pixel[3];
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
inline void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
gpio.digitalWrite(activeDisplay->a0, HIGH);
|
|
|
|
pixel[0] = r;
|
|
|
|
pixel[1] = g;
|
|
|
|
pixel[2] = b;
|
2020-08-02 13:51:47 +00:00
|
|
|
gpio.spiDataRW(activeDisplay->channel, pixel, 3);
|
2020-08-01 19:49:42 +00:00
|
|
|
} /* lcdst_pushPx */
|
|
|
|
|
2020-08-02 13:51:47 +00:00
|
|
|
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
|
2020-08-01 19:49:42 +00:00
|
|
|
{
|
|
|
|
gpio.digitalWrite(activeDisplay->a0, HIGH);
|
2020-08-02 13:51:47 +00:00
|
|
|
gpio.spiDataRW(activeDisplay->channel, pixels, count * 3);
|
2020-08-01 19:49:42 +00:00
|
|
|
}
|
|
|
|
|