You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
8.0 KiB

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "st7735.h"
  6. /********************************** EASY PORT *********************************/
  7. /*
  8. * If you porting this code, you can change below headers and function pointers
  9. * in gpio structure.
  10. */
  11. #include <wiringPi.h>
  12. #include <wiringPiSPI.h>
  13. struct
  14. {
  15. void (* const delay)(unsigned int milliseconds);
  16. void (* const pinMode)(int pin, int mode);
  17. void (* const digitalWrite)(int pin, int value);
  18. int (* const spiSetup)(int channel, int speed);
  19. int (* const spiDataRW)(int channel, uint8 *data, int length);
  20. } static const gpio =
  21. {
  22. delay,
  23. pinMode,
  24. digitalWrite,
  25. wiringPiSPISetup,
  26. wiringPiSPIDataRW
  27. };
  28. /****************************** END EASY PORT END *****************************/
  29. static uint8 screen_buffer[SCREEN_HEIGHT * SCREEN_WIDTH * 3]; // TODO: make dynamic
  30. static uint16 screen_window_x1;
  31. static uint16 screen_window_x2;
  32. static uint16 screen_window_y1;
  33. static uint16 screen_window_y2;
  34. static uint16 screen_cursor_x;
  35. static uint16 screen_cursor_y;
  36. void advance_screen_cursor()
  37. {
  38. screen_cursor_x++;
  39. if (screen_cursor_x > screen_window_x2) {
  40. screen_cursor_x = screen_window_x1;
  41. screen_cursor_y++;
  42. if (screen_cursor_y > screen_window_y2) {
  43. screen_cursor_y = screen_window_y1;
  44. }
  45. }
  46. }
  47. static lcd_t *activeDisplay;
  48. /*
  49. * Safe allocation of the memory block.
  50. *
  51. * Parameters:
  52. * size - Size of memory block to allocate.
  53. *
  54. * Return:
  55. * Pointer to the memory block. If an error occurs, stop the program.
  56. */
  57. static inline void *safeMalloc(size_t size)
  58. {
  59. void *memoryBlock = (void*) malloc(size);
  60. /* Check the pointer */
  61. if(memoryBlock == NULL)
  62. {
  63. fprintf(stderr, "Out of RAM memory!\n");
  64. exit(EXIT_FAILURE);
  65. }
  66. return memoryBlock;
  67. } /* safeMalloc */
  68. uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2);
  69. void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
  70. void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);
  71. void lcd_setOrientation(lcd_t* lcd, uint16 orientation);
  72. void lcd_setGamma(lcd_t* lcd, uint16 state);
  73. void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b);
  74. void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count);
  75. /*
  76. * Write the command to the display driver.
  77. *
  78. * Parameters:
  79. * cmd - The command to write.
  80. */
  81. static inline void writeCommand(uint8 cmd)
  82. {
  83. gpio.digitalWrite(activeDisplay->a0, LOW);
  84. gpio.spiDataRW(activeDisplay->channel, &cmd, 1);
  85. } /* writeCommand */
  86. /*
  87. * Write the data to the display driver.
  88. *
  89. * Parameters:
  90. * data - The data to write.
  91. */
  92. static inline void writeData(uint8 data)
  93. {
  94. gpio.digitalWrite(activeDisplay->a0, HIGH);
  95. gpio.spiDataRW(activeDisplay->channel, &data, 1);
  96. } /* writeData */
  97. lcd_t *lcd_init(int spiSpeed, int channel, int cs, int a0, int rs)
  98. {
  99. /* Create the one instance of the lcdst_t structure and activate it */
  100. lcd_t *instance = (lcd_t *) safeMalloc(sizeof(lcd_t));
  101. activeDisplay = instance;
  102. instance->channel = channel;
  103. instance->cs = cs;
  104. instance->a0 = a0;
  105. instance->rs = rs;
  106. /*
  107. * instance->width; instance->height
  108. * The setting of this variables will take place
  109. * in the function lcdst_setOrientation() below.
  110. */
  111. /* Configure the a0 pin. The logic level is not significant now. */
  112. gpio.pinMode(instance->a0, OUTPUT);
  113. /* If the rs pin is connected then configure it */
  114. if(instance->rs != -1)
  115. {
  116. gpio.pinMode(instance->rs, OUTPUT);
  117. gpio.digitalWrite(instance->rs, LOW);
  118. gpio.delay(10);
  119. gpio.digitalWrite(instance->rs, HIGH);
  120. gpio.delay(10);
  121. }
  122. /* Configure the SPI interface */
  123. if(gpio.spiSetup(instance->channel, spiSpeed) == -1)
  124. {
  125. fprintf(stderr, "Failed to setup the SPI interface!\n");
  126. exit(EXIT_FAILURE);
  127. }
  128. /* Software reset; Wait minimum 120ms */
  129. writeCommand(0x01);
  130. gpio.delay(150);
  131. /* Sleep out; Wait minimum 120ms */
  132. writeCommand(0x11);
  133. gpio.delay(150);
  134. /* Set the orientation and the gamma */
  135. lcd_setOrientation(instance, 0);
  136. lcd_setGamma(instance, 2); /* Optional */
  137. /* Set the pixel format */
  138. writeCommand(0x3A);
  139. writeData(0x06);
  140. /* Display ON; Wait 100ms before start */
  141. writeCommand(0x29);
  142. gpio.delay(100);
  143. return instance;
  144. } /* lcd_init */
  145. void lcd_deinit(lcd_t *display)
  146. {
  147. if(display == NULL) return;
  148. free(display);
  149. } /* lcdst_uninit */
  150. void lcd_setOrientation(lcd_t* lcd, uint16 orientation)
  151. {
  152. writeCommand(0x36); /* Memory Data Access Control */
  153. int sw = SCREEN_WIDTH;
  154. int sh = SCREEN_HEIGHT;
  155. uint8 my = 1 << 7; // row address order bit
  156. uint8 mx = 1 << 6; // column address order bit
  157. uint8 mv = 1 << 5; // row/column exchange bit
  158. switch(orientation)
  159. {
  160. case 1:
  161. writeData(mx & mv);
  162. activeDisplay->width = sw;
  163. activeDisplay->height = sh;
  164. break;
  165. case 2:
  166. writeData(my & mx);
  167. activeDisplay->width = sh;
  168. activeDisplay->height = sw;
  169. break;
  170. case 3:
  171. writeData(my & mv);
  172. activeDisplay->width = sw;
  173. activeDisplay->height = sh;
  174. break;
  175. case 4:
  176. writeData(mx);
  177. activeDisplay->width = sh;
  178. activeDisplay->height = sw;
  179. break;
  180. default:
  181. writeData(0); /* None */
  182. activeDisplay->width = sh;
  183. activeDisplay->height = sw;
  184. break;
  185. }
  186. lcdhw_setWindow(lcd, 0, 0, activeDisplay->width - 1, activeDisplay->height - 1);
  187. } /* lcdst_setOrientation */
  188. void lcd_setGamma(lcd_t* lcd, uint16 state)
  189. {
  190. /* The status (0 or 1) of the GS pin can only be empirically tested */
  191. switch(state)
  192. {
  193. case 1: state = 2; break; /* GS_pin=1: 1.8; GS_pin=0: 2.5 */
  194. case 2: state = 4; break; /* GS_pin=1: 2.5; GS_pin=0: 2.2 */
  195. case 3: state = 8; break; /* GS_pin=1: 1.0; GS_pin=0: 1.8 */
  196. default: state = 1; break; /* GS_pin=1: 2.2; GS_pin=0: 1.0 */
  197. }
  198. /* Set built-in gamma */
  199. writeCommand(0x26);
  200. writeData(state);
  201. } /* lcdst_setGamma */
  202. void lcd_setInversion(lcd_t* lcd, uint16 state)
  203. {
  204. /* Display inversion ON/OFF */
  205. writeCommand(state ? 0x21 : 0x20);
  206. } /* lcdst_setInversion */
  207. uint16 lcdhw_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2)
  208. {
  209. /* Accept: 0 <= x1 <= x2 < activeDisplay->width */
  210. if(x2 < x1) return 1;
  211. if(x2 >= activeDisplay->width) return 1;
  212. /* Accept: 0 <= y1 <= y2 < activeDisplay->height */
  213. if(y2 < y1) return 1;
  214. if(y2 >= activeDisplay->height) return 1;
  215. /* Set column address */
  216. writeCommand(0x2A);
  217. writeData(x1 >> 8); writeData(x1 & 0xFF);
  218. writeData(x2 >> 8); writeData(x2 & 0XFF);
  219. /* Set row address */
  220. writeCommand(0x2B);
  221. writeData(y1 >> 8); writeData(y1 & 0xFF);
  222. writeData(y2 >> 8); writeData(y2 & 0xFF);
  223. /* Activate RAW write */
  224. writeCommand(0x2C);
  225. //gpio.delay(5);
  226. return 0;
  227. } /* lcdst_setWindow */
  228. void lcd_activateRamWrite(void)
  229. {
  230. writeCommand(0x2C);
  231. //gpio.delay(5);
  232. } /* lcdst_activateRamWrite */
  233. uint8 pixel[3];
  234. inline void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  235. {
  236. gpio.digitalWrite(activeDisplay->a0, HIGH);
  237. pixel[0] = r;
  238. pixel[1] = g;
  239. pixel[2] = b;
  240. gpio.spiDataRW(activeDisplay->channel, pixel, 3);
  241. }
  242. void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  243. {
  244. gpio.digitalWrite(activeDisplay->a0, HIGH);
  245. gpio.spiDataRW(activeDisplay->channel, pixels, count * 3);
  246. }
  247. uint8 line_buffer[SCREEN_WIDTH*3]; // lcd->width or lcd->height
  248. void lcd_redrawBuffer(lcd_t* lcd)
  249. {
  250. for (int i = 0; i < lcd->height; i++) {
  251. memcpy(line_buffer, &screen_buffer[i*lcd->width*3], lcd->width*3);
  252. lcdhw_setWindow(lcd, 0, i, lcd->width - 1, i);
  253. lcdhw_pushPixels(lcd, line_buffer, lcd->width);
  254. }
  255. }
  256. uint16 lcd_setWindow(lcd_t* lcd, uint16 x1, uint16 y1, uint16 x2, uint16 y2)
  257. {
  258. screen_window_x1 = x1;
  259. screen_window_x2 = x2;
  260. screen_window_y1 = y1;
  261. screen_window_y2 = y2;
  262. screen_cursor_x = x1;
  263. screen_cursor_y = y1;
  264. return 0;
  265. }
  266. void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  267. {
  268. int i = screen_cursor_x
  269. + screen_cursor_y * lcd->width;
  270. screen_buffer[i * 3 + 0] = r;
  271. screen_buffer[i * 3 + 1] = g;
  272. screen_buffer[i * 3 + 2] = b;
  273. advance_screen_cursor();
  274. }
  275. void lcd_pushPixelSkip(lcd_t* lcd)
  276. {
  277. advance_screen_cursor();
  278. }
  279. void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  280. {
  281. for (int i = 0; i < count; i++) {
  282. lcd_pushPixel(lcd,
  283. pixels[i * 3 + 0],
  284. pixels[i * 3 + 1],
  285. pixels[i * 3 + 2]);
  286. }
  287. }