#include #include #include #include "st7735.h" #include "font8x8_basic.h" /********************************** EASY PORT *********************************/ /* * If you porting this code, you can change below headers and function pointers * in gpio structure. */ #include #include 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 *****************************/ /* The global variable that stores the pointer to the structure, * with the current active display. */ 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 */ 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); /* * Write the command to the display driver. * * Parameters: * cmd - The command to write. */ static inline void writeCommand(uint8 cmd) { gpio.digitalWrite(activeDisplay->a0, LOW); gpio.spiDataRW(activeDisplay->cs, &cmd, 1); } /* 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); gpio.spiDataRW(activeDisplay->cs, &data, 1); } /* writeData */ lcd_t *lcd_init(int spiSpeed, int cs, int a0, int rs) { /* Create the one instance of the lcdst_t structure and activate it */ lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t)); activeDisplay = instance; /* Assign specific pins */ 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 */ if(gpio.spiSetup(instance->cs, spiSpeed) == -1) { 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 */ lcd_setOrientation(0); lcd_setGamma(2); /* Optional */ /* 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 */ void lcd_setOrientation(uint8 orientation) { writeCommand(0x36); /* Memory Data Access Control */ switch(orientation) { case 1: writeData(0x60); /* MX + MV */ activeDisplay->width = 160; activeDisplay->height = 128; lcd_setWindow(0, 0, 159, 127); break; case 2: writeData(0xC0); /* MY + MX */ activeDisplay->width = 128; activeDisplay->height = 160; lcd_setWindow(0, 0, 127, 159); break; case 3: writeData(0xA0); /* MY + MV */ activeDisplay->width = 160; activeDisplay->height = 128; lcd_setWindow(0, 0, 159, 127); break; default: writeData(0x00); /* None */ activeDisplay->width = 128; activeDisplay->height = 160; lcd_setWindow(0, 0, 127, 159); break; } } /* lcdst_setOrientation */ void lcd_setGamma(uint8 state) { /* 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 */ void lcd_setInversion(uint8 state) { /* Display inversion ON/OFF */ writeCommand(state ? 0x21 : 0x20); } /* lcdst_setInversion */ uint8 lcd_setWindow(uint8 x1, uint8 y1, uint8 x2, uint8 y2) { /* 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]; inline void lcd_pushPx(uint8 r, uint8 g, uint8 b) { gpio.digitalWrite(activeDisplay->a0, HIGH); pixel[0] = r; pixel[1] = g; pixel[2] = b; gpio.spiDataRW(activeDisplay->cs, pixel, 3); } /* lcdst_pushPx */ void lcd_pushPixels(uint8* pixels, size_t count) { 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]); } }