oled_common.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "stm32g4xx_hal.h"
  2. #include "oled_common.h"
  3. #include "oled_config.h"
  4. #include "config.h"
  5. #include "i2c_bridge.h"
  6. #include "spi_bridge.h"
  7. #include "logo_grayscale.h"
  8. #include "fonts.h"
  9. #include "SSD1327.h"
  10. #include "cmsis_os.h"
  11. #include <string.h>
  12. extern "C" {
  13. #include "OLED_SSD1327.h"
  14. #include "GFX_SSD1327.h"
  15. }
  16. #if defined(I2C_BRIDGE)
  17. I2C_HandleTypeDef *i2c_bridge;
  18. #elif defined(SPI_BRIDGE)
  19. SPI_HandleTypeDef *spi_bridge;
  20. #endif
  21. uint8_t oled_buf[OLED_BUF_SIZE];
  22. //
  23. void init_oled(void)
  24. {
  25. init_gpio_oled();
  26. oled_reset();
  27. #if defined(I2C_BRIDGE)
  28. i2c_bridge_init();
  29. i2c_bridge = i2c_get_bridge();
  30. SSD1327_I2cInit(i2c_bridge);
  31. #elif defined(SPI_BRIDGE)
  32. spi_bridge_init();
  33. spi_bridge = spi_get_bridge();
  34. SSD1327_SpiInit(spi_bridge);
  35. #endif
  36. oled_clear(BLACK);
  37. #if 0
  38. ssd1327.clearBuffer();
  39. ssd1327.drawString(16, 16, (char*)"Hello", 0xF, 32);
  40. ssd1327.drawString(16, 48, (char*)"World!", 0xF, 32);
  41. ssd1327.writeFullBuffer();
  42. #endif
  43. #if 0
  44. oled_draw_pixel(20, 20, 15);
  45. oled_draw_pixel(21, 20, 15);
  46. oled_draw_pixel(22, 20, 15);
  47. oled_draw_pixel(20, 21, 15);
  48. oled_draw_pixel(21, 21, 15);
  49. oled_draw_pixel(22, 21, 15);
  50. oled_draw_pixel(20, 22, 15);
  51. oled_draw_pixel(21, 22, 15);
  52. oled_draw_pixel(22, 22, 15);
  53. oled_draw_pixel(20, 23, 15);
  54. oled_draw_pixel(21, 23, 15);
  55. oled_draw_pixel(22, 23, 15);
  56. #endif
  57. //GFX_Image(15, 0, (uint8_t*)logo_grayscale, 96, 96);
  58. oled_display();
  59. }
  60. //
  61. void init_gpio_oled(void)
  62. {
  63. GPIO_InitTypeDef GPIO_InitStruct = {0};
  64. __HAL_RCC_GPIOA_CLK_ENABLE();
  65. // GPIOA_5 - reset
  66. GPIO_InitStruct.Pin = OLED_RST_PIN;
  67. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  68. GPIO_InitStruct.Pull = GPIO_NOPULL;
  69. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  70. HAL_GPIO_Init(OLED_RST_PORT, &GPIO_InitStruct);
  71. // GPIOA_6 - DC
  72. GPIO_InitStruct.Pin = OLED_DC_PIN;
  73. HAL_GPIO_Init(OLED_DC_PORT, &GPIO_InitStruct);
  74. // GPIOA_7 - CS
  75. GPIO_InitStruct.Pin = OLED_CS_PIN;
  76. HAL_GPIO_Init(OLED_CS_PORT, &GPIO_InitStruct);
  77. HAL_GPIO_WritePin(OLED_RST_PORT, OLED_RST_PIN, GPIO_PIN_RESET);
  78. HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_RESET);
  79. HAL_GPIO_WritePin(OLED_CS_PORT, OLED_CS_PIN, GPIO_PIN_RESET);
  80. }
  81. //
  82. void oled_reset(void)
  83. {
  84. OLED_RST_1;
  85. oled_delay_ms(100);
  86. OLED_RST_0;
  87. oled_delay_ms(100);
  88. OLED_RST_1;
  89. oled_delay_ms(100);
  90. }
  91. //
  92. void oled_delay_ms(uint32_t ms)
  93. {
  94. osDelay(ms);
  95. }
  96. // TODO убрать sizeof
  97. void oled_command(uint8_t com)
  98. {
  99. #ifdef I2C_BRIDGE
  100. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x00, 1, &com, sizeof(com), 100);
  101. #endif
  102. #ifdef SPI_BRIDGE
  103. OLED_DC_0;
  104. OLED_CS_0;
  105. HAL_SPI_Transmit(spi_bridge, &com, 1, 10);
  106. OLED_CS_1;
  107. #endif
  108. }
  109. // TODO убрать sizeof
  110. void oled_data(uint8_t dat)
  111. {
  112. #ifdef I2C_BRIDGE
  113. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, &dat, sizeof(dat), 100);
  114. #endif
  115. #ifdef SPI_BRIDGE
  116. OLED_DC_1;
  117. OLED_CS_0;
  118. HAL_SPI_Transmit(spi_bridge, &dat, 1, 10);
  119. OLED_CS_1;
  120. #endif
  121. }
  122. //
  123. void oled_display(void)
  124. {
  125. oled_command(SSD1327_SETCOLUMNADDRESS);
  126. oled_command(0x00);
  127. oled_command(0x3F);
  128. oled_command(SSD1327_SETROWADDRESS);
  129. oled_command(0x00);
  130. oled_command(0x7F);
  131. #ifdef I2C_BRIDGE
  132. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 1000);
  133. #endif
  134. #ifdef SPI_BRIDGE
  135. OLED_DC_1;
  136. OLED_CS_0;
  137. HAL_SPI_Transmit(spi_bridge, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 100);
  138. OLED_CS_1;
  139. #endif
  140. }
  141. //
  142. void oled_bitmap(uint8_t *bitmap)
  143. {
  144. oled_command(0x22);
  145. oled_command(0x00);
  146. oled_command(0x07);
  147. #ifdef I2C_BRIDGE
  148. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  149. #endif
  150. #ifdef SPI_BRIDGE
  151. OLED_DC_1;
  152. OLED_CS_0;
  153. HAL_SPI_Transmit(spi_bridge, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  154. OLED_CS_1;
  155. #endif
  156. }
  157. //
  158. void oled_invert_colors(uint8_t invert)
  159. {
  160. oled_command(invert ? SSD1327_INVERTDISPLAY : SSD1327_NORMALDISPLAY);
  161. }
  162. //
  163. void oled_rotate_display(uint8_t rotate)
  164. {
  165. if (rotate > 1) rotate = 1;
  166. oled_command(0xA0 | (0x01 & rotate)); // Set Segment Re-Map Default
  167. // 0xA0 (0x00) => column Address 0 mapped to 127
  168. // 0xA1 (0x01) => Column Address 127 mapped to 0
  169. oled_command(0xC0 | (0x08 & (rotate<<3))); // Set COM Output Scan Direction
  170. // 0xC0 (0x00) => normal mode (RESET) Scan from COM0 to COM[N-1];Where N is the Multiplex ratio.
  171. // 0xC8 (0xC8) => remapped mode. Scan from COM[N-1] to COM0;;Where N is the Multiplex ratio.
  172. }
  173. //
  174. void oled_display_on(uint8_t on)
  175. {
  176. oled_command(on ? SSD1327_DISPLAYON : SSD1327_DISPLAYOFF);
  177. }
  178. //
  179. void oled_set_contrast(uint8_t contrast)
  180. {
  181. oled_command(SSD1327_SETCONTRASTCURRENT); // Set Contrast Control
  182. oled_command(contrast);
  183. }
  184. //
  185. void oled_start_scroll_right(uint8_t start_page, uint8_t end_page, uint8_t speed)
  186. {
  187. oled_command(SSD1327_RIGHT_HORIZONTAL_SCROLL);
  188. oled_command(0x00);
  189. oled_command(start_page);
  190. oled_command(speed);
  191. oled_command(end_page);
  192. oled_command(SSD1327_ACTIVATE_SCROLL);
  193. }
  194. //
  195. void oled_start_scroll_left(uint8_t start_page, uint8_t end_page, uint8_t speed)
  196. {
  197. oled_command(SSD1327_LEFT_HORIZONTAL_SCROLL);
  198. oled_command(0x00);
  199. oled_command(start_page);
  200. oled_command(speed);
  201. oled_command(end_page);
  202. oled_command(SSD1327_ACTIVATE_SCROLL);
  203. }
  204. //
  205. void oled_stop_scroll(void)
  206. {
  207. oled_command(SSD1327_DEACTIVATE_SCROLL);
  208. }
  209. void oled_init(void)
  210. {
  211. oled_command(SSD1327_DISPLAYOFF); // Display Off
  212. oled_command(SSD1327_SETCOLUMNADDRESS);
  213. oled_command(0x00);
  214. oled_command(0x7F);
  215. oled_command(SSD1327_SETROWADDRESS);
  216. oled_command(0x00);
  217. oled_command(0x7F);
  218. oled_set_contrast(0x7F);
  219. oled_command(SSD1327_SEGREMAP);
  220. oled_command(0x51);
  221. oled_command(SSD1327_SETDISPLAYSTARTLINE);
  222. oled_command(0x00);
  223. oled_command(SSD1327_SETDISPLAYOFFSET);
  224. oled_command(0x0);
  225. oled_command(SSD1327_DISPLAYALLON_RESUME); // Entire Display ON
  226. oled_command(SSD1327_SETMULTIPLEX);
  227. oled_command(0x7F);
  228. oled_command(SSD1327_SETPHASELENGTH);
  229. oled_command(0xF1); // !
  230. oled_command(SSD1327_SETFRONTCLOCKDIVIDER_OSCILLATORFREQUENCY);
  231. oled_command(0x00);
  232. oled_command(SSD1327_FUNCTIONSELECTIONA); // !
  233. oled_command(0x01); // !
  234. oled_command(SSD1327_SETSECONDPRECHARGEPERTIOD);
  235. oled_command(0x0F); // !
  236. oled_command(SSD1327_SETSETVCOMVOLTAGE);
  237. oled_command(0x0F); // !
  238. oled_command(SSD1327_SETPRECHARGEVOLTAGE);
  239. oled_command(0x08); // !
  240. oled_command(SSD1327_FUNCTIONSELECTIONB);
  241. oled_command(0x62);
  242. oled_command(SSD1327_SETCOMMANDLOCK); // !
  243. oled_command(0x12); // !
  244. oled_display_on(1);
  245. }
  246. //
  247. void oled_draw_pixel(int16_t x, int16_t y, uint8_t color)
  248. {
  249. if ((x < 0) || (x >= SSD1327_LCDWIDTH) || (y < 0) || (y >= SSD1327_LCDHEIGHT))
  250. return;
  251. uint8_t SelectedCell = oled_buf[x/2 + y*(SSD1327_LCDWIDTH/2)];
  252. if(x % 2)
  253. {
  254. SelectedCell &= ~(0x0F);
  255. SelectedCell |= (0x0F & color);
  256. }
  257. else
  258. {
  259. SelectedCell &= ~(0xF0);
  260. SelectedCell |= (0xF0 & (color<<4));
  261. }
  262. oled_buf[x/2 + y*(SSD1327_LCDWIDTH/2)] = SelectedCell;
  263. }
  264. //
  265. void oled_clear(uint8_t color)
  266. {
  267. if(color > WHITE) color = WHITE;
  268. memset(oled_buf, (color << 4 | color), OLED_BUF_SIZE);
  269. }
  270. // ------------------------------------------------------------------------------- //
  271. //
  272. void oled_draw_rec(int x_start, int y_start, int x_end, int y_end, uint8_t color)
  273. {
  274. for (int i = 0; i < y_end; i++) {
  275. GFX_DrawLine(x_start, y_start + i, x_end, y_start + i, color);
  276. }
  277. GFX_DrawLine(x_start, y_end, x_end, y_end, color);
  278. }
  279. #define STR_H 20
  280. #define STR_Y_S 36
  281. #define STR_FRAME_START_Y 8
  282. #define STR_X_S 4
  283. #define STR_X_E 123
  284. //
  285. void oled_draw_string_frame(uint8_t str_num)
  286. {
  287. for (int i = 0; i < 2; i++) {
  288. // нижняя
  289. GFX_DrawLine(STR_X_S, str_num*STR_H + STR_Y_S + i, STR_X_E, str_num*STR_H + STR_Y_S + i, 15);
  290. // верхняя
  291. GFX_DrawLine(STR_X_S, str_num*STR_H + STR_Y_S - STR_H + i, STR_X_E, str_num*STR_H + STR_Y_S - STR_H + i, 15);
  292. }
  293. // Правая
  294. GFX_DrawLine(STR_X_E, str_num*STR_H + STR_Y_S, STR_X_E, str_num*STR_H + STR_Y_S - STR_H, 15);
  295. // Левая
  296. GFX_DrawLine(STR_X_S, str_num*STR_H + STR_Y_S, STR_X_S, str_num*STR_H + STR_Y_S - STR_H, 15);
  297. }
  298. //
  299. void test_oled(void)
  300. {
  301. #if 0
  302. for (int i = 0; i < 16; i++) {
  303. SSD1327_DrawPixel(10, 20 + i, i);
  304. }
  305. SSD1327_DrawPixel(20, 20, 15);
  306. SSD1327_DrawPixel(30, 20, 15);
  307. SSD1327_DrawPixel(40, 20, 5);
  308. #endif
  309. //void GFX_DrawLine(int x_start, int y_start, int x_end, int y_end, uint8_t color);
  310. // Верхняя
  311. GFX_DrawLine(0, 0, 127, 0, 15);
  312. // Левая
  313. GFX_DrawLine(0, 0, 0, 127, 15);
  314. // Правая
  315. GFX_DrawLine(127, 0, 127, 127, 15);
  316. // Нижняя
  317. GFX_DrawLine(0, 127, 127, 127, 15);
  318. GFX_SetFont(font_8x5);
  319. GFX_SetFontSize(1);
  320. GFX_DrawString(4, 10, (char*)"BbAa12345", 15, 0);
  321. GFX_SetFontSize(2);
  322. GFX_DrawString(4, 30, (char*)"BbAa12345", 15, 0);
  323. GFX_SetFontSize(3);
  324. GFX_DrawString(4, 60, (char*)"BbAa12345", 15, 0);
  325. SSD1327_Display();
  326. }