#include #include #include #include #include "st7735.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 *****************************/ 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(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. * * Parameters: * cmd - The command to write. */ static inline void writeCommand(uint8 cmd) { gpio.digitalWrite(activeDisplay->a0, LOW); gpio.spiDataRW(activeDisplay->channel, &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->channel, &data, 1); } /* writeData */ 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 */ lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t)); activeDisplay = instance; instance->channel = channel; 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->channel, 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(instance, 0); lcd_setGamma(instance, 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(lcd_t* lcd, uint8 orientation) { writeCommand(0x36); /* Memory Data Access Control */ switch(orientation) { case 1: writeData(0x60); /* MX + MV */ activeDisplay->width = 160; activeDisplay->height = 128; lcd_setWindow(lcd, 0, 0, 159, 127); break; case 2: writeData(0xC0); /* MY + MX */ activeDisplay->width = 128; activeDisplay->height = 160; lcd_setWindow(lcd, 0, 0, 127, 159); break; case 3: writeData(0xA0); /* MY + MV */ activeDisplay->width = 160; activeDisplay->height = 128; lcd_setWindow(lcd, 0, 0, 159, 127); break; default: writeData(0x00); /* None */ activeDisplay->width = 128; activeDisplay->height = 160; lcd_setWindow(lcd, 0, 0, 127, 159); break; } } /* lcdst_setOrientation */ void lcd_setGamma(lcd_t* lcd, 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(lcd_t* lcd, uint8 state) { /* Display inversion ON/OFF */ writeCommand(state ? 0x21 : 0x20); } /* lcdst_setInversion */ uint8 lcd_setWindow(lcd_t* lcd, 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_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b) { gpio.digitalWrite(activeDisplay->a0, HIGH); pixel[0] = r; pixel[1] = g; pixel[2] = b; gpio.spiDataRW(activeDisplay->channel, pixel, 3); } /* lcdst_pushPx */ void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count) { gpio.digitalWrite(activeDisplay->a0, HIGH); gpio.spiDataRW(activeDisplay->channel, pixels, count * 3); }