linux-st7735/st7735.c

334 lines
7.7 KiB
C
Raw Normal View History

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 *****************************/
2020-08-02 17:38:03 +00:00
static uint8 screen_buffer[128 * 160 * 3]; // TODO: make dynamic
static uint8 screen_window_x1;
static uint8 screen_window_x2;
static uint8 screen_window_y1;
static uint8 screen_window_y2;
static uint8 screen_cursor_x;
static uint8 screen_cursor_y;
2020-08-02 19:38:10 +00:00
void advance_screen_cursor()
{
screen_cursor_x++;
if (screen_cursor_x > screen_window_x2) {
screen_cursor_x = screen_window_x1;
screen_cursor_y++;
if (screen_cursor_y > screen_window_y2) {
screen_cursor_y = screen_window_y1;
}
}
}
2020-08-01 19:49:42 +00:00
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 17:38:03 +00:00
uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2);
void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);
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);
2020-08-04 13:52:54 +00:00
gpio.digitalWrite(instance->rs, LOW);
gpio.delay(10);
gpio.digitalWrite(instance->rs, HIGH);
2020-08-01 19:49:42 +00:00
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 17:38:03 +00:00
lcdhw_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 17:38:03 +00:00
lcdhw_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 17:38:03 +00:00
lcdhw_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 17:38:03 +00:00
lcdhw_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 17:38:03 +00:00
uint8 lcdhw_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 17:38:03 +00:00
inline void lcdhw_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-02 17:38:03 +00:00
}
2020-08-01 19:49:42 +00:00
2020-08-02 17:38:03 +00:00
void lcdhw_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
}
2020-08-02 17:38:03 +00:00
uint8 line_buffer[160*3]; // lcd->width or lcd->height
void lcd_redrawBuffer(lcd_t* lcd)
{
for (int i = 0; i < lcd->height; i++) {
memcpy(line_buffer, &screen_buffer[i*lcd->width*3], lcd->width*3);
lcdhw_setWindow(lcd, 0, i, lcd->width - 1, i);
lcdhw_pushPixels(lcd, line_buffer, lcd->width);
}
}
uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
{
screen_window_x1 = x1;
screen_window_x2 = x2;
screen_window_y1 = y1;
screen_window_y2 = y2;
screen_cursor_x = x1;
screen_cursor_y = y1;
return 0;
}
void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
{
int i = screen_cursor_x
+ screen_cursor_y * lcd->width;
screen_buffer[i * 3 + 0] = r;
screen_buffer[i * 3 + 1] = g;
screen_buffer[i * 3 + 2] = b;
2020-08-02 19:38:10 +00:00
advance_screen_cursor();
}
void lcd_pushPixelSkip(lcd_t* lcd)
{
advance_screen_cursor();
2020-08-02 17:38:03 +00:00
}
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
{
for (int i = 0; i < count; i++) {
lcd_pushPixel(lcd,
pixels[i * 3 + 0],
pixels[i * 3 + 1],
pixels[i * 3 + 2]);
}
}