@@ -0,0 +1,7 @@ | |||||
# ST7735 SPI Display Toolkit | |||||
Built and tested with OrangePi Zero and ST7735 128x160 v1.1 SPI display. | |||||
## Resources | |||||
* [https://github.com/orangepi-xunlong/WiringOP](orangepi-xunlong/WiringOP) - GPIO controlling library compatible with Raspberry Pi's WiringPi | |||||
* [https://github.com/dhepper/font8x8](dhepper/font8x8) - 8x8 pixels monospaced font |
@@ -1,74 +1,106 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include "st7735.h" | |||||
#include <stdlib.h> | |||||
#include <time.h> | |||||
#include <unistd.h> | |||||
#include <wiringPi.h> | #include <wiringPi.h> | ||||
#include "spilcd.h" | |||||
#include "spilcd_gfx.h" | |||||
#include "spilcd_font.h" | |||||
uint8 random_color_r(int seed); | |||||
uint8 random_color_g(int seed); | |||||
uint8 random_color_b(int seed); | |||||
inline uint8 random_color_r(int seed) { return seed % (256 / 7) * 7; } | |||||
inline uint8 random_color_g(int seed) { return seed % (256 / 13) * 13; } | |||||
inline uint8 random_color_b(int seed) { return seed % (256 / 23) * 23; } | |||||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||||
{ | { | ||||
setbuf(stdout, NULL); | setbuf(stdout, NULL); | ||||
srand(time(NULL)); | |||||
wiringPiSetup(); | wiringPiSetup(); | ||||
lcd_t* lcd = lcd_init(40000000, 10, 2, 0); | |||||
lcd_t* lcd = lcd_init(40000000, 1, 10, 7, 8); | |||||
printf("Fill display..."); | printf("Fill display..."); | ||||
printf("blue..."); | printf("blue..."); | ||||
lcd_fillScreen(0, 70, 160); | |||||
delay(1000); | |||||
lcd_fillScreen(lcd, 0, 70, 160); | |||||
printf("...waiting 1 second..."); | |||||
sleep(1); | |||||
printf("black..."); | printf("black..."); | ||||
lcd_fillScreen(0, 0, 0); | |||||
lcd_fillScreen(lcd, 0, 0, 0); | |||||
printf("DONE\n"); | printf("DONE\n"); | ||||
delay(1000); | |||||
/* Draw the lines */ | |||||
/*printf("Lines..."); | |||||
lcdst_drawHLine(0, 149, 128, 0, 255, 255); | |||||
lcdst_drawHLine(0, 139, 128, 255, 255, 0); | |||||
lcdst_drawVLine(117, 0, 160, 0, 255, 255); | |||||
lcdst_drawVLine(107, 0, 160, 255, 255, 0); | |||||
printf("DONE\n");*/ | |||||
/*printf("Points..."); | |||||
for (int i = 1; i < 20; i++) { | |||||
lcdst_drawPx(5 - 1, 70 + i, 100, 100, 100); | |||||
lcdst_drawPx(5 + i + 1, 70 + i, 100, 100, 100); | |||||
printf("...waiting 1 second...\n"); | |||||
sleep(1); | |||||
printf("Points..."); | |||||
for (int i = 1; i < 2000; i++) { | |||||
int r = rand(); | |||||
lcd_drawPixel(lcd, r % 128, i % 160, | |||||
random_color_r(i), | |||||
random_color_g(i), | |||||
random_color_b(i)); | |||||
} | } | ||||
printf("DONE\n"); | printf("DONE\n"); | ||||
printf("Triangle..."); | |||||
for (int i = 1; i < 20; i++) { | |||||
lcdst_drawHLine(5, 70 + i, i, 255, 0, 0); | |||||
printf("...waiting 1 second...\n"); | |||||
sleep(1); | |||||
printf("Regions..."); | |||||
int w = 15; | |||||
int h = 20; | |||||
for (int i = 1; i < 200; i++) { | |||||
int x = rand() % (128 - w); | |||||
int y = rand() % (160 - h); | |||||
lcd_setWindow(lcd, x, y, x + w - 1, y + h - 1); | |||||
uint8 r = rand(); | |||||
uint8 g = rand(); | |||||
uint8 b = rand(); | |||||
for (int p = 0; p < w*h; p++) { | |||||
lcd_pushPixel(lcd, r * p / (w*h), g * (w*h-p) / (w*h), b); | |||||
} | |||||
} | } | ||||
printf("DONE\n");*/ | |||||
printf("DONE\n"); | |||||
printf("...waiting 1 second...\n"); | |||||
sleep(1); | |||||
printf("Rectangles..."); | printf("Rectangles..."); | ||||
//printf("outline..."); | |||||
//lcd_drawRect(10, 10, 10, 10, 0, 255, 255); | |||||
//lcd_drawRect(10, 30, 10, 10, 255, 255, 0); | |||||
printf("filled..."); | |||||
lcd_fillRect(30, 10, 10, 10, 0, 255, 255); | |||||
lcd_fillRect(30, 30, 10, 10, 255, 255, 0); | |||||
printf("bunch..."); | |||||
for (int i = 0; i < 40; i++) { | |||||
lcd_fillRect(40 + (i%13) * 4, 40 + (i%19) * 4, 20, 20, i % 5 * 15, i % 7 * 17, i % 3 * 23); | |||||
} | |||||
lcd_fillScreen(lcd, 0, 0, 0); | |||||
lcd_fillRect(lcd, 30, 10, 10, 10, 0, 255, 255); | |||||
lcd_fillRect(lcd, 30, 30, 10, 10, 255, 255, 0); | |||||
printf("DONE\n"); | printf("DONE\n"); | ||||
printf("...waiting 1 second...\n"); | |||||
sleep(1); | |||||
printf("Text..."); | printf("Text..."); | ||||
lcd_printChar(10, 90, 'A'); | |||||
lcd_printText(10, 50, "Ahoj!"); | |||||
lcd_printChar(10, 100, 'Z'); | |||||
lcd_fillScreen(lcd, 0, 0, 0); | |||||
lcd_printChar(lcd, 10, 90, 'A'); | |||||
lcd_printText(lcd, 10, 50, "Hello, world!"); | |||||
lcd_printChar(lcd, 10, 100, 'Z'); | |||||
printf("DONE\n"); | printf("DONE\n"); | ||||
/* Send the raw data */ | |||||
/* printf("Raw..."); | |||||
lcdst_setWindow(20, 20, 29, 29); | |||||
for(uint8 i=0; i<100; i++) lcdst_pushPx(255, 0, 255); | |||||
lcdst_setWindow(0, 0, 127, 159); | |||||
printf("DONE\n");*/ | |||||
printf("...waiting 2 seconds...\n"); | |||||
sleep(2); | |||||
/* Uninitialize the display */ | |||||
//lcd_deinit(lcd); | |||||
printf("Pulsing color..."); | |||||
for (int i = 0; i < 256; i++) { | |||||
lcd_fillScreen(lcd, | |||||
random_color_r(i), | |||||
random_color_g(i), | |||||
random_color_b(i)); | |||||
} | |||||
printf("DONE\n"); | |||||
printf("...waiting 2 seconds before shutdown...\n"); | |||||
sleep(2); | |||||
printf("Terminating...\n"); | |||||
lcd_fillScreen(lcd, 0, 0, 0); | |||||
lcd_deinit(lcd); | |||||
printf("DONE\n"); | |||||
return 0; | return 0; | ||||
} | } | ||||
@@ -1,3 +1,3 @@ | |||||
project('dejvino-ST7735', 'c') | project('dejvino-ST7735', 'c') | ||||
src = ['main.c', 'st7735.c'] | |||||
src = ['main.c', 'spilcd_gfx.c', 'spilcd_font.c', 'st7735.c'] | |||||
executable('st7735', src, link_args: '-lwiringPi') | executable('st7735', src, link_args: '-lwiringPi') |
@@ -0,0 +1,80 @@ | |||||
#ifndef uint8 | |||||
#define uint8 unsigned char | |||||
#endif | |||||
#ifndef __SPILCD_H__ | |||||
#define __SPILCD_H__ | |||||
typedef struct | |||||
{ | |||||
int channel; | |||||
int cs, a0, rs; | |||||
uint8 width, height; | |||||
} lcd_t; | |||||
/* | |||||
* Initialize the display. | |||||
* | |||||
* Parameters: | |||||
* spiSpeed - Speed of the SPI interface. | |||||
* channel - SPI channel | |||||
* cs - Chip selection pin. | |||||
* a0 - Data/Command pin. | |||||
* rs - Optional reset pin. Use -1 when not connected. | |||||
* | |||||
* Return: Pointer to the structure with display data. | |||||
* | |||||
*/ | |||||
lcd_t *lcd_init(int spiSpeed, int channel, int cs, int a0, int rs); | |||||
/* | |||||
* Reset the specified display and clear the previously assigned memory. | |||||
* | |||||
* Parameters: | |||||
* lcd - display to use | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_deinit(lcd_t *lcd); | |||||
/* | |||||
* Set the drawing area. | |||||
* | |||||
* Parameters: | |||||
* lcd - display to use | |||||
* x1 - The X parameter of the starting point. | |||||
* y1 - The Y parameter of the starting point. | |||||
* x2 - The X parameter of the ending point. | |||||
* y2 - The Y parameter of the ending point. | |||||
* | |||||
* Return: 0 for success, 1 for error. | |||||
* | |||||
*/ | |||||
uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2); | |||||
/* | |||||
* Push a pixel into the previously set drawing area. | |||||
* | |||||
* Parameters: | |||||
* lcd - display to use | |||||
* r - red color (0 to 255) | |||||
* g - green color (0 to 255) | |||||
* b - blue color (0 to 255) | |||||
*/ | |||||
void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b); | |||||
/* | |||||
* Push an array of pixels into the previously set drawing area. | |||||
* Significantly faster than pushing pixels individually. | |||||
* | |||||
* Parameters: | |||||
* lcd - display to use | |||||
* pixels - array of pixels where: | |||||
* pixels[i * 3 + 0] = i-th red color (0 to 255) | |||||
* pixels[i * 3 + 1] = i-th green color (0 to 255) | |||||
* pixels[i * 3 + 2] = i-th blue color (0 to 255) | |||||
* count - number of pixels in the array | |||||
*/ | |||||
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count); | |||||
#endif // __SPILCD_H__ | |||||
@@ -0,0 +1,37 @@ | |||||
#include <stdio.h> | |||||
#include <stdint.h> | |||||
#include <stdlib.h> | |||||
#include <string.h> | |||||
#include "spilcd_font.h" | |||||
#include "font8x8_basic.h" | |||||
static void lcd_pushChar(lcd_t* lcd, char c) | |||||
{ | |||||
char* bitmap = font8x8_basic[(unsigned int) c]; | |||||
int x,y; | |||||
int set; | |||||
for (x=0; x < 8; x++) { | |||||
for (y=0; y < 8; y++) { | |||||
set = bitmap[x] & 1 << y; | |||||
if (set) { | |||||
lcd_pushPixel(lcd, 200, 0, 0); | |||||
} else { | |||||
lcd_pushPixel(lcd, 0, 0, 0); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
void lcd_printChar(lcd_t* lcd, uint8 x, uint8 y, char c) | |||||
{ | |||||
lcd_setWindow(lcd, x, y, x+8 - 1, y+8 - 1); | |||||
lcd_pushChar(lcd, c); | |||||
} | |||||
void lcd_printText(lcd_t* lcd, uint8 x, uint8 y, char* text) | |||||
{ | |||||
for (int i = 0; i < strlen(text); i++) { | |||||
lcd_printChar(lcd, x + i * 8, y, text[i]); | |||||
} | |||||
} | |||||
@@ -0,0 +1,5 @@ | |||||
#include "spilcd.h" | |||||
void lcd_printChar(lcd_t* lcd, uint8 x, uint8 y, char c); | |||||
void lcd_printText(lcd_t* lcd, uint8 x, uint8 y, char* text); | |||||
@@ -0,0 +1,41 @@ | |||||
#include <stdio.h> | |||||
#include <stdint.h> | |||||
#include <stdlib.h> | |||||
#include "spilcd_gfx.h" | |||||
void lcd_drawPixel(lcd_t* lcd, uint8 x, uint8 y, uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
if(lcd_setWindow(lcd, x, y, x, y)) return; | |||||
lcd_pushPixel(lcd, r, g, b); | |||||
} | |||||
void lcd_fillRect(lcd_t* lcd, uint8 x, uint8 y, uint8 w, uint8 h, | |||||
uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
if((w == 0) || (h == 0)) return; | |||||
if((x+w-1) >= lcd->width) w = lcd->width - x; | |||||
if((y+h-1) >= lcd->height) h = lcd->height - y; | |||||
if(lcd_setWindow(lcd, x, y, x+w-1, y+h-1)) return; | |||||
#define BUFFER_PIXELS 64 | |||||
int wh = w*h; | |||||
uint8 buffer[BUFFER_PIXELS * sizeof(uint8) * 3]; | |||||
for (int p = 0; p < wh; p += BUFFER_PIXELS) { | |||||
for(int pb = 0; pb < BUFFER_PIXELS; pb++) { | |||||
buffer[pb * 3 + 0] = r; | |||||
buffer[pb * 3 + 1] = g; | |||||
buffer[pb * 3 + 2] = b; | |||||
} | |||||
int rem = wh - p; | |||||
lcd_pushPixels(lcd, buffer, ((rem < BUFFER_PIXELS) ? rem : BUFFER_PIXELS)); | |||||
} | |||||
} | |||||
void lcd_fillScreen(lcd_t* lcd, uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
lcd_fillRect(lcd, 0, 0, | |||||
lcd->width, lcd->height, | |||||
r, g, b); | |||||
} | |||||
@@ -0,0 +1,50 @@ | |||||
#include "spilcd.h" | |||||
/* | |||||
* Draw one pixel on the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* x - The X parameter of the pixel. | |||||
* y - The Y parameter of the pixel. | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_drawPixel(lcd_t* lcd, uint8 x, uint8 y, uint8 r, uint8 g, uint8 b); | |||||
/* | |||||
* Draw a filled rectangle on the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* x - Parameter X of the upper left corner of the rectangle. | |||||
* y - Parameter Y of the upper left corner of the rectangle. | |||||
* w - The width of the rectangle. | |||||
* h - The height of the rectangle. | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_fillRect(lcd_t* lcd, uint8 x, uint8 y, uint8 w, uint8 h, uint8 r, uint8 g, uint8 b); | |||||
/* | |||||
* Fill the entire screen with one color of the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_fillScreen(lcd_t* lcd, uint8 r, uint8 g, uint8 b); | |||||
@@ -1,8 +1,8 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | |||||
#include "st7735.h" | #include "st7735.h" | ||||
#include "font8x8_basic.h" | |||||
/********************************** EASY PORT *********************************/ | /********************************** EASY PORT *********************************/ | ||||
/* | /* | ||||
@@ -28,9 +28,6 @@ struct | |||||
}; | }; | ||||
/****************************** END EASY PORT END *****************************/ | /****************************** END EASY PORT END *****************************/ | ||||
/* The global variable that stores the pointer to the structure, | |||||
* with the current active display. | |||||
*/ | |||||
static lcd_t *activeDisplay; | static lcd_t *activeDisplay; | ||||
/* | /* | ||||
@@ -57,11 +54,10 @@ static inline void *safeMalloc(size_t size) | |||||
} /* safeMalloc */ | } /* safeMalloc */ | ||||
void lcd_setOrientation(uint8 orientation); | |||||
void lcd_setGamma(uint8 state); | |||||
void lcd_pushPx(uint8 r, uint8 g, uint8 b); | |||||
void lcd_pushPixels(uint8* pixels, size_t count); | |||||
void lcd_pushChar(char c); | |||||
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); | |||||
/* | /* | ||||
* Write the command to the display driver. | * Write the command to the display driver. | ||||
@@ -72,7 +68,7 @@ void lcd_pushChar(char c); | |||||
static inline void writeCommand(uint8 cmd) | static inline void writeCommand(uint8 cmd) | ||||
{ | { | ||||
gpio.digitalWrite(activeDisplay->a0, LOW); | gpio.digitalWrite(activeDisplay->a0, LOW); | ||||
gpio.spiDataRW(activeDisplay->cs, &cmd, 1); | |||||
gpio.spiDataRW(activeDisplay->channel, &cmd, 1); | |||||
} /* writeCommand */ | } /* writeCommand */ | ||||
/* | /* | ||||
@@ -84,17 +80,17 @@ static inline void writeCommand(uint8 cmd) | |||||
static inline void writeData(uint8 data) | static inline void writeData(uint8 data) | ||||
{ | { | ||||
gpio.digitalWrite(activeDisplay->a0, HIGH); | gpio.digitalWrite(activeDisplay->a0, HIGH); | ||||
gpio.spiDataRW(activeDisplay->cs, &data, 1); | |||||
gpio.spiDataRW(activeDisplay->channel, &data, 1); | |||||
} /* writeData */ | } /* writeData */ | ||||
lcd_t *lcd_init(int spiSpeed, int cs, int a0, int rs) | |||||
lcd_t *lcd_init(int spiSpeed, int channel, int cs, int a0, int rs) | |||||
{ | { | ||||
/* Create the one instance of the lcdst_t structure and activate it */ | /* Create the one instance of the lcdst_t structure and activate it */ | ||||
lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t)); | lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t)); | ||||
activeDisplay = instance; | activeDisplay = instance; | ||||
/* Assign specific pins */ | |||||
instance->channel = channel; | |||||
instance->cs = cs; | instance->cs = cs; | ||||
instance->a0 = a0; | instance->a0 = a0; | ||||
instance->rs = rs; | instance->rs = rs; | ||||
@@ -116,7 +112,7 @@ lcd_t *lcd_init(int spiSpeed, int cs, int a0, int rs) | |||||
} | } | ||||
/* Configure the SPI interface */ | /* Configure the SPI interface */ | ||||
if(gpio.spiSetup(instance->cs, spiSpeed) == -1) | |||||
if(gpio.spiSetup(instance->channel, spiSpeed) == -1) | |||||
{ | { | ||||
fprintf(stderr, "Failed to setup the SPI interface!\n"); | fprintf(stderr, "Failed to setup the SPI interface!\n"); | ||||
exit(EXIT_FAILURE); | exit(EXIT_FAILURE); | ||||
@@ -131,8 +127,8 @@ lcd_t *lcd_init(int spiSpeed, int cs, int a0, int rs) | |||||
gpio.delay(150); | gpio.delay(150); | ||||
/* Set the orientation and the gamma */ | /* Set the orientation and the gamma */ | ||||
lcd_setOrientation(0); | |||||
lcd_setGamma(2); /* Optional */ | |||||
lcd_setOrientation(instance, 0); | |||||
lcd_setGamma(instance, 2); /* Optional */ | |||||
/* Set the pixel format */ | /* Set the pixel format */ | ||||
writeCommand(0x3A); | writeCommand(0x3A); | ||||
@@ -152,7 +148,7 @@ void lcd_deinit(lcd_t *display) | |||||
free(display); | free(display); | ||||
} /* lcdst_uninit */ | } /* lcdst_uninit */ | ||||
void lcd_setOrientation(uint8 orientation) | |||||
void lcd_setOrientation(lcd_t* lcd, uint8 orientation) | |||||
{ | { | ||||
writeCommand(0x36); /* Memory Data Access Control */ | writeCommand(0x36); /* Memory Data Access Control */ | ||||
@@ -162,33 +158,33 @@ void lcd_setOrientation(uint8 orientation) | |||||
writeData(0x60); /* MX + MV */ | writeData(0x60); /* MX + MV */ | ||||
activeDisplay->width = 160; | activeDisplay->width = 160; | ||||
activeDisplay->height = 128; | activeDisplay->height = 128; | ||||
lcd_setWindow(0, 0, 159, 127); | |||||
lcd_setWindow(lcd, 0, 0, 159, 127); | |||||
break; | break; | ||||
case 2: | case 2: | ||||
writeData(0xC0); /* MY + MX */ | writeData(0xC0); /* MY + MX */ | ||||
activeDisplay->width = 128; | activeDisplay->width = 128; | ||||
activeDisplay->height = 160; | activeDisplay->height = 160; | ||||
lcd_setWindow(0, 0, 127, 159); | |||||
lcd_setWindow(lcd, 0, 0, 127, 159); | |||||
break; | break; | ||||
case 3: | case 3: | ||||
writeData(0xA0); /* MY + MV */ | writeData(0xA0); /* MY + MV */ | ||||
activeDisplay->width = 160; | activeDisplay->width = 160; | ||||
activeDisplay->height = 128; | activeDisplay->height = 128; | ||||
lcd_setWindow(0, 0, 159, 127); | |||||
lcd_setWindow(lcd, 0, 0, 159, 127); | |||||
break; | break; | ||||
default: | default: | ||||
writeData(0x00); /* None */ | writeData(0x00); /* None */ | ||||
activeDisplay->width = 128; | activeDisplay->width = 128; | ||||
activeDisplay->height = 160; | activeDisplay->height = 160; | ||||
lcd_setWindow(0, 0, 127, 159); | |||||
lcd_setWindow(lcd, 0, 0, 127, 159); | |||||
break; | break; | ||||
} | } | ||||
} /* lcdst_setOrientation */ | } /* lcdst_setOrientation */ | ||||
void lcd_setGamma(uint8 state) | |||||
void lcd_setGamma(lcd_t* lcd, uint8 state) | |||||
{ | { | ||||
/* The status (0 or 1) of the GS pin can only be empirically tested */ | /* The status (0 or 1) of the GS pin can only be empirically tested */ | ||||
switch(state) | switch(state) | ||||
@@ -204,13 +200,13 @@ void lcd_setGamma(uint8 state) | |||||
writeData(state); | writeData(state); | ||||
} /* lcdst_setGamma */ | } /* lcdst_setGamma */ | ||||
void lcd_setInversion(uint8 state) | |||||
void lcd_setInversion(lcd_t* lcd, uint8 state) | |||||
{ | { | ||||
/* Display inversion ON/OFF */ | /* Display inversion ON/OFF */ | ||||
writeCommand(state ? 0x21 : 0x20); | writeCommand(state ? 0x21 : 0x20); | ||||
} /* lcdst_setInversion */ | } /* lcdst_setInversion */ | ||||
uint8 lcd_setWindow(uint8 x1, uint8 y1, uint8 x2, uint8 y2) | |||||
uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2) | |||||
{ | { | ||||
/* Accept: 0 <= x1 <= x2 < activeDisplay->width */ | /* Accept: 0 <= x1 <= x2 < activeDisplay->width */ | ||||
if(x2 < x1) return 1; | if(x2 < x1) return 1; | ||||
@@ -245,88 +241,18 @@ void lcd_activateRamWrite(void) | |||||
uint8 pixel[3]; | uint8 pixel[3]; | ||||
inline void lcd_pushPx(uint8 r, uint8 g, uint8 b) | |||||
inline void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b) | |||||
{ | { | ||||
gpio.digitalWrite(activeDisplay->a0, HIGH); | gpio.digitalWrite(activeDisplay->a0, HIGH); | ||||
pixel[0] = r; | pixel[0] = r; | ||||
pixel[1] = g; | pixel[1] = g; | ||||
pixel[2] = b; | pixel[2] = b; | ||||
gpio.spiDataRW(activeDisplay->cs, pixel, 3); | |||||
gpio.spiDataRW(activeDisplay->channel, pixel, 3); | |||||
} /* lcdst_pushPx */ | } /* lcdst_pushPx */ | ||||
void lcd_pushPixels(uint8* pixels, size_t count) | |||||
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count) | |||||
{ | { | ||||
gpio.digitalWrite(activeDisplay->a0, HIGH); | gpio.digitalWrite(activeDisplay->a0, HIGH); | ||||
gpio.spiDataRW(activeDisplay->cs, pixels, count * 3); | |||||
} | |||||
void lcd_drawPx(uint8 x, uint8 y, uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
if(lcd_setWindow(x, y, x, y)) return; | |||||
lcd_pushPx(r, g, b); | |||||
} /* lcdst_drawPx */ | |||||
void lcd_fillRect(uint8 x, uint8 y, uint8 w, uint8 h, | |||||
uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
/* Draw only in the display space */ | |||||
if((w == 0) || (h == 0)) return; | |||||
if((x+w-1) >= activeDisplay->width) w = activeDisplay->width - x; | |||||
if((y+h-1) >= activeDisplay->height) h = activeDisplay->height - y; | |||||
/* Draw the filled rectangle */ | |||||
if(lcd_setWindow(x, y, x+w-1, y+h-1)) return; | |||||
#define BUFFER_PIXELS 64 | |||||
int wh = w*h; | |||||
uint8 buffer[BUFFER_PIXELS * sizeof(uint8) * 3]; | |||||
for (int p = 0; p < wh; p += BUFFER_PIXELS) { | |||||
for(int pb = 0; pb < BUFFER_PIXELS; pb++) { | |||||
buffer[pb * 3 + 0] = r; | |||||
buffer[pb * 3 + 1] = g; | |||||
buffer[pb * 3 + 2] = b; | |||||
} | |||||
int rem = wh - p; | |||||
lcd_pushPixels(buffer, ((rem < BUFFER_PIXELS) ? rem : BUFFER_PIXELS)); | |||||
} | |||||
} | |||||
void lcd_fillScreen(uint8 r, uint8 g, uint8 b) | |||||
{ | |||||
/* Fill the whole screen with one color */ | |||||
lcd_fillRect(0, 0, activeDisplay->width, activeDisplay->height, r, g, b); | |||||
} /* lcdst_drawScreen */ | |||||
void lcd_pushChar(char c) | |||||
{ | |||||
char* bitmap = font8x8_basic[(unsigned int) c]; | |||||
int x,y; | |||||
int set; | |||||
int mask; | |||||
for (x=0; x < 8; x++) { | |||||
for (y=0; y < 8; y++) { | |||||
set = bitmap[x] & 1 << y; | |||||
printf("%c", set ? 'X' : ' '); | |||||
if (set) { | |||||
lcd_pushPx(200, 0, 0); | |||||
} else { | |||||
lcd_pushPx(0, 0, 0); | |||||
} | |||||
} | |||||
printf("\n"); | |||||
} | |||||
} | |||||
void lcd_printChar(uint8 x, uint8 y, char c) | |||||
{ | |||||
lcd_setWindow(x, y, x+8 - 1, y+8 - 1); | |||||
lcd_pushChar(c); | |||||
} | |||||
void lcd_printText(uint8 x, uint8 y, char* text) | |||||
{ | |||||
for (int i = 0; i < strlen(text); i++) { | |||||
lcd_printChar(x + i * 8, y, text[i]); | |||||
} | |||||
gpio.spiDataRW(activeDisplay->channel, pixels, count * 3); | |||||
} | } | ||||
@@ -1,100 +1,4 @@ | |||||
#ifndef uint8 | |||||
#define uint8 unsigned char | |||||
#endif | |||||
typedef struct | |||||
{ | |||||
int cs, a0, rs; | |||||
uint8 width, height; | |||||
} lcd_t; | |||||
#include "spilcd.h" | |||||
/* | |||||
* Initialize the display and create a data structure for it. | |||||
* The last initialized display is active. | |||||
* | |||||
* Parameters: | |||||
* spiSpeed - Speed of the SPI interface. | |||||
* cs - Chip selection pin. | |||||
* a0 - Data/Command pin. | |||||
* rs - Optional reset pin. If you do not use it, enter -1. | |||||
* | |||||
* Return: Pointer to the structure with display data. | |||||
* | |||||
*/ | |||||
lcd_t *lcd_init(int spiSpeed, int cs, int a0, int rs); | |||||
// TODO: optional HW-specific functions | |||||
/* | |||||
* Reset the specified display and clear the previously assigned memory. | |||||
* | |||||
* Parameters: | |||||
* display - Pointer to the structure with display data. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_deinit(lcd_t *display); | |||||
/* | |||||
* Set the drawing area on the currently active display. | |||||
* | |||||
* Parameters: | |||||
* x1 - The X parameter of the first point. | |||||
* y1 - The Y parameter of the first point. | |||||
* x2 - The X parameter of the second point. | |||||
* y2 - The Y parameter of the second point. | |||||
* | |||||
* Return: Confirmation of the occurrence or non-occurrence of an error. | |||||
* 0 - The error did not occur; 1 - The error occurred. | |||||
* | |||||
*/ | |||||
uint8 lcd_setWindow(uint8 x1, uint8 y1, uint8 x2, uint8 y2); | |||||
/* | |||||
* Draw one pixel on the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* x - The X parameter of the pixel. | |||||
* y - The Y parameter of the pixel. | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_drawPx(uint8 x, uint8 y, uint8 r, uint8 g, uint8 b); | |||||
/* | |||||
* Draw a filled rectangle on the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* x - Parameter X of the upper left corner of the rectangle. | |||||
* y - Parameter Y of the upper left corner of the rectangle. | |||||
* w - The width of the rectangle. | |||||
* h - The height of the rectangle. | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_fillRect(uint8 x, uint8 y, uint8 w, uint8 h, uint8 r, uint8 g, uint8 b); | |||||
/* | |||||
* Fill the entire screen with one color of the currently active display. | |||||
* The color intensity scale for a normal pixel is from 0 to 255. | |||||
* The color intensity scale for the reduced pixel is from 0 to 15. | |||||
* | |||||
* Parameters: | |||||
* r - The intensity of the red color. | |||||
* g - The intensity of the green color. | |||||
* b - The intensity of the blue color. | |||||
* | |||||
* Return: void | |||||
*/ | |||||
void lcd_fillScreen(uint8 r, uint8 g, uint8 b); | |||||
void lcd_printChar(uint8 x, uint8 y, char c); | |||||
void lcd_printText(uint8 x, uint8 y, char* text); |