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.

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