Browse Source

Add support for 320x240 display

master
Dejvino 3 years ago
parent
commit
1fc62b17a6
4 changed files with 67 additions and 40 deletions
  1. +2
    -2
      demo/base.c
  2. +6
    -2
      spilcd.h
  3. +46
    -36
      st7735.c
  4. +13
    -0
      st7735.h

+ 2
- 2
demo/base.c View File

@@ -20,9 +20,9 @@ lcd_t* demo_init()


wiringPiSetup(); wiringPiSetup();


lcd_t* lcd = lcd_init(40000000, 1, 15, 7, 8);
lcd_t* lcd = lcd_init(30000000, 1, 15, 7, 8);
lcd_setOrientation(lcd, 2);
lcd_setOrientation(lcd, 4);


return lcd; return lcd;
} }


+ 6
- 2
spilcd.h View File

@@ -2,13 +2,17 @@
#define uint8 unsigned char #define uint8 unsigned char
#endif #endif


#ifndef uint16
#define uint16 unsigned short
#endif

#ifndef __SPILCD_H__ #ifndef __SPILCD_H__
#define __SPILCD_H__ #define __SPILCD_H__
typedef struct typedef struct
{ {
int channel; int channel;
int cs, a0, rs; int cs, a0, rs;
uint8 width, height;
uint16 width, height;
} lcd_t; } lcd_t;


/* /*
@@ -49,7 +53,7 @@ void lcd_deinit(lcd_t *lcd);
* Return: 0 for success, 1 for error. * Return: 0 for success, 1 for error.
* *
*/ */
uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2);
uint16 lcd_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2);


/* /*
* Push a pixel into the previously set drawing area. * Push a pixel into the previously set drawing area.


+ 46
- 36
st7735.c View File

@@ -28,13 +28,13 @@ struct
}; };
/****************************** END EASY PORT END *****************************/ /****************************** END EASY PORT END *****************************/


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;
static uint8 screen_buffer[SCREEN_HEIGHT * SCREEN_WIDTH * 3]; // TODO: make dynamic
static uint16 screen_window_x1;
static uint16 screen_window_x2;
static uint16 screen_window_y1;
static uint16 screen_window_y2;
static uint16 screen_cursor_x;
static uint16 screen_cursor_y;


void advance_screen_cursor() void advance_screen_cursor()
{ {
@@ -74,12 +74,12 @@ static inline void *safeMalloc(size_t size)
} /* safeMalloc */ } /* safeMalloc */




uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2);
uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2);
void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b); void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count); void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);


void lcd_setOrientation(lcd_t* lcd, uint8 orientation);
void lcd_setGamma(lcd_t* lcd, uint8 state);
void lcd_setOrientation(lcd_t* lcd, uint16 orientation);
void lcd_setGamma(lcd_t* lcd, uint16 state);
void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b); void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count); void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);


@@ -174,43 +174,53 @@ void lcd_deinit(lcd_t *display)
free(display); free(display);
} /* lcdst_uninit */ } /* lcdst_uninit */


void lcd_setOrientation(lcd_t* lcd, uint8 orientation)
void lcd_setOrientation(lcd_t* lcd, uint16 orientation)
{ {
writeCommand(0x36); /* Memory Data Access Control */ writeCommand(0x36); /* Memory Data Access Control */


int sw = SCREEN_WIDTH;
int sh = SCREEN_HEIGHT;

uint8 my = 1 << 7; // row address order bit
uint8 mx = 1 << 6; // column address order bit
uint8 mv = 1 << 5; // row/column exchange bit
switch(orientation) switch(orientation)
{ {
case 1: case 1:
writeData(0x60); /* MX + MV */
activeDisplay->width = 160;
activeDisplay->height = 128;
lcdhw_setWindow(lcd, 0, 0, 159, 127);
writeData(mx & mv);
activeDisplay->width = sw;
activeDisplay->height = sh;
break; break;


case 2: case 2:
writeData(0xC0); /* MY + MX */
activeDisplay->width = 128;
activeDisplay->height = 160;
lcdhw_setWindow(lcd, 0, 0, 127, 159);
writeData(my & mx);
activeDisplay->width = sh;
activeDisplay->height = sw;
break; break;


case 3: case 3:
writeData(0xA0); /* MY + MV */
activeDisplay->width = 160;
activeDisplay->height = 128;
lcdhw_setWindow(lcd, 0, 0, 159, 127);
writeData(my & mv);
activeDisplay->width = sw;
activeDisplay->height = sh;
break;

case 4:
writeData(mx);
activeDisplay->width = sh;
activeDisplay->height = sw;
break; break;



default: default:
writeData(0x00); /* None */
activeDisplay->width = 128;
activeDisplay->height = 160;
lcdhw_setWindow(lcd, 0, 0, 127, 159);
writeData(0); /* None */
activeDisplay->width = sh;
activeDisplay->height = sw;
break; break;
} }
lcdhw_setWindow(lcd, 0, 0, activeDisplay->width - 1, activeDisplay->height - 1);
} /* lcdst_setOrientation */ } /* lcdst_setOrientation */


void lcd_setGamma(lcd_t* lcd, uint8 state)
void lcd_setGamma(lcd_t* lcd, uint16 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)
@@ -226,13 +236,13 @@ void lcd_setGamma(lcd_t* lcd, uint8 state)
writeData(state); writeData(state);
} /* lcdst_setGamma */ } /* lcdst_setGamma */


void lcd_setInversion(lcd_t* lcd, uint8 state)
void lcd_setInversion(lcd_t* lcd, uint16 state)
{ {
/* Display inversion ON/OFF */ /* Display inversion ON/OFF */
writeCommand(state ? 0x21 : 0x20); writeCommand(state ? 0x21 : 0x20);
} /* lcdst_setInversion */ } /* lcdst_setInversion */


uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2)
{ {
/* Accept: 0 <= x1 <= x2 < activeDisplay->width */ /* Accept: 0 <= x1 <= x2 < activeDisplay->width */
if(x2 < x1) return 1; if(x2 < x1) return 1;
@@ -244,13 +254,13 @@ uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
/* Set column address */ /* Set column address */
writeCommand(0x2A); writeCommand(0x2A);
writeData(0); writeData(x1);
writeData(0); writeData(x2);
writeData(x1 >> 8); writeData(x1 & 0xFF);
writeData(x2 >> 8); writeData(x2 & 0XFF);
/* Set row address */ /* Set row address */
writeCommand(0x2B); writeCommand(0x2B);
writeData(0); writeData(y1);
writeData(0); writeData(y2);
writeData(y1 >> 8); writeData(y1 & 0xFF);
writeData(y2 >> 8); writeData(y2 & 0xFF);
/* Activate RAW write */ /* Activate RAW write */
writeCommand(0x2C); writeCommand(0x2C);
@@ -282,7 +292,7 @@ void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
gpio.spiDataRW(activeDisplay->channel, pixels, count * 3); gpio.spiDataRW(activeDisplay->channel, pixels, count * 3);
} }


uint8 line_buffer[160*3]; // lcd->width or lcd->height
uint8 line_buffer[SCREEN_WIDTH*3]; // lcd->width or lcd->height


void lcd_redrawBuffer(lcd_t* lcd) void lcd_redrawBuffer(lcd_t* lcd)
{ {
@@ -293,7 +303,7 @@ void lcd_redrawBuffer(lcd_t* lcd)
} }
} }


uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
uint16 lcd_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2)
{ {
screen_window_x1 = x1; screen_window_x1 = x1;
screen_window_x2 = x2; screen_window_x2 = x2;


+ 13
- 0
st7735.h View File

@@ -1,4 +1,17 @@
#include "spilcd.h" #include "spilcd.h"


#define SCREEN_320x240
//#define SCREEN_160x128

#ifdef SCREEN_320x240
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#endif

#ifdef SCREEN_160x128
#define SCREEN_WIDTH 160
#define SCREEN_HEIGHT 128
#endif

// TODO: optional HW-specific functions // TODO: optional HW-specific functions



Loading…
Cancel
Save