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.

332 lines
7.7 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. 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. uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 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, uint8 orientation);
  72. void lcd_setGamma(lcd_t* lcd, uint8 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, HIGH); /* Reset OFF */
  118. gpio.delay(10);
  119. }
  120. /* Configure the SPI interface */
  121. if(gpio.spiSetup(instance->channel, spiSpeed) == -1)
  122. {
  123. fprintf(stderr, "Failed to setup the SPI interface!\n");
  124. exit(EXIT_FAILURE);
  125. }
  126. /* Software reset; Wait minimum 120ms */
  127. writeCommand(0x01);
  128. gpio.delay(150);
  129. /* Sleep out; Wait minimum 120ms */
  130. writeCommand(0x11);
  131. gpio.delay(150);
  132. /* Set the orientation and the gamma */
  133. lcd_setOrientation(instance, 0);
  134. lcd_setGamma(instance, 2); /* Optional */
  135. /* Set the pixel format */
  136. writeCommand(0x3A);
  137. writeData(0x06);
  138. /* Display ON; Wait 100ms before start */
  139. writeCommand(0x29);
  140. gpio.delay(100);
  141. return instance;
  142. } /* lcd_init */
  143. void lcd_deinit(lcd_t *display)
  144. {
  145. if(display == NULL) return;
  146. free(display);
  147. } /* lcdst_uninit */
  148. void lcd_setOrientation(lcd_t* lcd, uint8 orientation)
  149. {
  150. writeCommand(0x36); /* Memory Data Access Control */
  151. switch(orientation)
  152. {
  153. case 1:
  154. writeData(0x60); /* MX + MV */
  155. activeDisplay->width = 160;
  156. activeDisplay->height = 128;
  157. lcdhw_setWindow(lcd, 0, 0, 159, 127);
  158. break;
  159. case 2:
  160. writeData(0xC0); /* MY + MX */
  161. activeDisplay->width = 128;
  162. activeDisplay->height = 160;
  163. lcdhw_setWindow(lcd, 0, 0, 127, 159);
  164. break;
  165. case 3:
  166. writeData(0xA0); /* MY + MV */
  167. activeDisplay->width = 160;
  168. activeDisplay->height = 128;
  169. lcdhw_setWindow(lcd, 0, 0, 159, 127);
  170. break;
  171. default:
  172. writeData(0x00); /* None */
  173. activeDisplay->width = 128;
  174. activeDisplay->height = 160;
  175. lcdhw_setWindow(lcd, 0, 0, 127, 159);
  176. break;
  177. }
  178. } /* lcdst_setOrientation */
  179. void lcd_setGamma(lcd_t* lcd, uint8 state)
  180. {
  181. /* The status (0 or 1) of the GS pin can only be empirically tested */
  182. switch(state)
  183. {
  184. case 1: state = 2; break; /* GS_pin=1: 1.8; GS_pin=0: 2.5 */
  185. case 2: state = 4; break; /* GS_pin=1: 2.5; GS_pin=0: 2.2 */
  186. case 3: state = 8; break; /* GS_pin=1: 1.0; GS_pin=0: 1.8 */
  187. default: state = 1; break; /* GS_pin=1: 2.2; GS_pin=0: 1.0 */
  188. }
  189. /* Set built-in gamma */
  190. writeCommand(0x26);
  191. writeData(state);
  192. } /* lcdst_setGamma */
  193. void lcd_setInversion(lcd_t* lcd, uint8 state)
  194. {
  195. /* Display inversion ON/OFF */
  196. writeCommand(state ? 0x21 : 0x20);
  197. } /* lcdst_setInversion */
  198. uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
  199. {
  200. /* Accept: 0 <= x1 <= x2 < activeDisplay->width */
  201. if(x2 < x1) return 1;
  202. if(x2 >= activeDisplay->width) return 1;
  203. /* Accept: 0 <= y1 <= y2 < activeDisplay->height */
  204. if(y2 < y1) return 1;
  205. if(y2 >= activeDisplay->height) return 1;
  206. /* Set column address */
  207. writeCommand(0x2A);
  208. writeData(0); writeData(x1);
  209. writeData(0); writeData(x2);
  210. /* Set row address */
  211. writeCommand(0x2B);
  212. writeData(0); writeData(y1);
  213. writeData(0); writeData(y2);
  214. /* Activate RAW write */
  215. writeCommand(0x2C);
  216. //gpio.delay(5);
  217. return 0;
  218. } /* lcdst_setWindow */
  219. void lcd_activateRamWrite(void)
  220. {
  221. writeCommand(0x2C);
  222. //gpio.delay(5);
  223. } /* lcdst_activateRamWrite */
  224. uint8 pixel[3];
  225. inline void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  226. {
  227. gpio.digitalWrite(activeDisplay->a0, HIGH);
  228. pixel[0] = r;
  229. pixel[1] = g;
  230. pixel[2] = b;
  231. gpio.spiDataRW(activeDisplay->channel, pixel, 3);
  232. }
  233. void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  234. {
  235. gpio.digitalWrite(activeDisplay->a0, HIGH);
  236. gpio.spiDataRW(activeDisplay->channel, pixels, count * 3);
  237. }
  238. uint8 line_buffer[160*3]; // lcd->width or lcd->height
  239. void lcd_redrawBuffer(lcd_t* lcd)
  240. {
  241. for (int i = 0; i < lcd->height; i++) {
  242. memcpy(line_buffer, &screen_buffer[i*lcd->width*3], lcd->width*3);
  243. lcdhw_setWindow(lcd, 0, i, lcd->width - 1, i);
  244. lcdhw_pushPixels(lcd, line_buffer, lcd->width);
  245. }
  246. }
  247. uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
  248. {
  249. screen_window_x1 = x1;
  250. screen_window_x2 = x2;
  251. screen_window_y1 = y1;
  252. screen_window_y2 = y2;
  253. screen_cursor_x = x1;
  254. screen_cursor_y = y1;
  255. return 0;
  256. }
  257. void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  258. {
  259. int i = screen_cursor_x
  260. + screen_cursor_y * lcd->width;
  261. screen_buffer[i * 3 + 0] = r;
  262. screen_buffer[i * 3 + 1] = g;
  263. screen_buffer[i * 3 + 2] = b;
  264. advance_screen_cursor();
  265. }
  266. void lcd_pushPixelSkip(lcd_t* lcd)
  267. {
  268. advance_screen_cursor();
  269. }
  270. void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  271. {
  272. for (int i = 0; i < count; i++) {
  273. lcd_pushPixel(lcd,
  274. pixels[i * 3 + 0],
  275. pixels[i * 3 + 1],
  276. pixels[i * 3 + 2]);
  277. }
  278. }