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.

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