Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

334 wiersze
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, 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, uint8 orientation)
  151. {
  152. writeCommand(0x36); /* Memory Data Access Control */
  153. switch(orientation)
  154. {
  155. case 1:
  156. writeData(0x60); /* MX + MV */
  157. activeDisplay->width = 160;
  158. activeDisplay->height = 128;
  159. lcdhw_setWindow(lcd, 0, 0, 159, 127);
  160. break;
  161. case 2:
  162. writeData(0xC0); /* MY + MX */
  163. activeDisplay->width = 128;
  164. activeDisplay->height = 160;
  165. lcdhw_setWindow(lcd, 0, 0, 127, 159);
  166. break;
  167. case 3:
  168. writeData(0xA0); /* MY + MV */
  169. activeDisplay->width = 160;
  170. activeDisplay->height = 128;
  171. lcdhw_setWindow(lcd, 0, 0, 159, 127);
  172. break;
  173. default:
  174. writeData(0x00); /* None */
  175. activeDisplay->width = 128;
  176. activeDisplay->height = 160;
  177. lcdhw_setWindow(lcd, 0, 0, 127, 159);
  178. break;
  179. }
  180. } /* lcdst_setOrientation */
  181. void lcd_setGamma(lcd_t* lcd, uint8 state)
  182. {
  183. /* The status (0 or 1) of the GS pin can only be empirically tested */
  184. switch(state)
  185. {
  186. case 1: state = 2; break; /* GS_pin=1: 1.8; GS_pin=0: 2.5 */
  187. case 2: state = 4; break; /* GS_pin=1: 2.5; GS_pin=0: 2.2 */
  188. case 3: state = 8; break; /* GS_pin=1: 1.0; GS_pin=0: 1.8 */
  189. default: state = 1; break; /* GS_pin=1: 2.2; GS_pin=0: 1.0 */
  190. }
  191. /* Set built-in gamma */
  192. writeCommand(0x26);
  193. writeData(state);
  194. } /* lcdst_setGamma */
  195. void lcd_setInversion(lcd_t* lcd, uint8 state)
  196. {
  197. /* Display inversion ON/OFF */
  198. writeCommand(state ? 0x21 : 0x20);
  199. } /* lcdst_setInversion */
  200. uint8 lcdhw_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
  201. {
  202. /* Accept: 0 <= x1 <= x2 < activeDisplay->width */
  203. if(x2 < x1) return 1;
  204. if(x2 >= activeDisplay->width) return 1;
  205. /* Accept: 0 <= y1 <= y2 < activeDisplay->height */
  206. if(y2 < y1) return 1;
  207. if(y2 >= activeDisplay->height) return 1;
  208. /* Set column address */
  209. writeCommand(0x2A);
  210. writeData(0); writeData(x1);
  211. writeData(0); writeData(x2);
  212. /* Set row address */
  213. writeCommand(0x2B);
  214. writeData(0); writeData(y1);
  215. writeData(0); writeData(y2);
  216. /* Activate RAW write */
  217. writeCommand(0x2C);
  218. //gpio.delay(5);
  219. return 0;
  220. } /* lcdst_setWindow */
  221. void lcd_activateRamWrite(void)
  222. {
  223. writeCommand(0x2C);
  224. //gpio.delay(5);
  225. } /* lcdst_activateRamWrite */
  226. uint8 pixel[3];
  227. inline void lcdhw_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  228. {
  229. gpio.digitalWrite(activeDisplay->a0, HIGH);
  230. pixel[0] = r;
  231. pixel[1] = g;
  232. pixel[2] = b;
  233. gpio.spiDataRW(activeDisplay->channel, pixel, 3);
  234. }
  235. void lcdhw_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  236. {
  237. gpio.digitalWrite(activeDisplay->a0, HIGH);
  238. gpio.spiDataRW(activeDisplay->channel, pixels, count * 3);
  239. }
  240. uint8 line_buffer[160*3]; // lcd->width or lcd->height
  241. void lcd_redrawBuffer(lcd_t* lcd)
  242. {
  243. for (int i = 0; i < lcd->height; i++) {
  244. memcpy(line_buffer, &screen_buffer[i*lcd->width*3], lcd->width*3);
  245. lcdhw_setWindow(lcd, 0, i, lcd->width - 1, i);
  246. lcdhw_pushPixels(lcd, line_buffer, lcd->width);
  247. }
  248. }
  249. uint8 lcd_setWindow(lcd_t* lcd, uint8 x1, uint8 y1, uint8 x2, uint8 y2)
  250. {
  251. screen_window_x1 = x1;
  252. screen_window_x2 = x2;
  253. screen_window_y1 = y1;
  254. screen_window_y2 = y2;
  255. screen_cursor_x = x1;
  256. screen_cursor_y = y1;
  257. return 0;
  258. }
  259. void lcd_pushPixel(lcd_t* lcd, uint8 r, uint8 g, uint8 b)
  260. {
  261. int i = screen_cursor_x
  262. + screen_cursor_y * lcd->width;
  263. screen_buffer[i * 3 + 0] = r;
  264. screen_buffer[i * 3 + 1] = g;
  265. screen_buffer[i * 3 + 2] = b;
  266. advance_screen_cursor();
  267. }
  268. void lcd_pushPixelSkip(lcd_t* lcd)
  269. {
  270. advance_screen_cursor();
  271. }
  272. void lcd_pushPixels(lcd_t* lcd, uint8* pixels, size_t count)
  273. {
  274. for (int i = 0; i < count; i++) {
  275. lcd_pushPixel(lcd,
  276. pixels[i * 3 + 0],
  277. pixels[i * 3 + 1],
  278. pixels[i * 3 + 2]);
  279. }
  280. }