oled_common.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "GFX_SSD1327.h"
  14. }
  15. #if defined(I2C_BRIDGE)
  16. I2C_HandleTypeDef *i2c_bridge;
  17. #elif defined(SPI_BRIDGE)
  18. SPI_HandleTypeDef *spi_bridge;
  19. #endif
  20. uint8_t oled_buf[OLED_BUF_SIZE];
  21. //
  22. void init_oled(void)
  23. {
  24. init_gpio_oled();
  25. oled_reset();
  26. #if defined(I2C_BRIDGE)
  27. i2c_bridge_init();
  28. i2c_bridge = i2c_get_bridge();
  29. //SSD1327_I2cInit(i2c_bridge);
  30. #elif defined(SPI_BRIDGE)
  31. spi_bridge_init();
  32. spi_bridge = spi_get_bridge();
  33. //SSD1327_SpiInit(spi_bridge);
  34. #endif
  35. oled_init();
  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. HAL_GPIO_WritePin(OLED_RST_PORT, OLED_RST_PIN, GPIO_PIN_RESET);
  75. HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_RESET);
  76. }
  77. //
  78. void oled_reset(void)
  79. {
  80. OLED_RST_1;
  81. oled_delay_ms(100);
  82. OLED_RST_0;
  83. oled_delay_ms(100);
  84. OLED_RST_1;
  85. oled_delay_ms(100);
  86. }
  87. //
  88. void oled_delay_ms(uint32_t ms)
  89. {
  90. osDelay(ms);
  91. }
  92. // TODO убрать sizeof
  93. void oled_command(uint8_t com)
  94. {
  95. #ifdef I2C_BRIDGE
  96. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x00, 1, &com, sizeof(com), 100);
  97. #endif
  98. #ifdef SPI_BRIDGE
  99. OLED_DC_0;
  100. OLED_CS_0;
  101. HAL_SPI_Transmit(spi_bridge, &com, 1, 10);
  102. OLED_CS_1;
  103. #endif
  104. }
  105. // TODO убрать sizeof
  106. void oled_data(uint8_t dat)
  107. {
  108. #ifdef I2C_BRIDGE
  109. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, &dat, sizeof(dat), 100);
  110. #endif
  111. #ifdef SPI_BRIDGE
  112. OLED_DC_1;
  113. OLED_CS_0;
  114. HAL_SPI_Transmit(spi_bridge, &dat, 1, 10);
  115. OLED_CS_1;
  116. #endif
  117. }
  118. //
  119. void oled_display(void)
  120. {
  121. oled_command(SSD1327_SETCOLUMNADDRESS);
  122. oled_command(0x00);
  123. oled_command(0x3F);
  124. oled_command(SSD1327_SETROWADDRESS);
  125. oled_command(0x00);
  126. oled_command(0x7F);
  127. #ifdef I2C_BRIDGE
  128. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 1000);
  129. #endif
  130. #ifdef SPI_BRIDGE
  131. OLED_DC_1;
  132. OLED_CS_0;
  133. HAL_SPI_Transmit(spi_bridge, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 100);
  134. OLED_CS_1;
  135. #endif
  136. }
  137. //
  138. void oled_bitmap(uint8_t *bitmap)
  139. {
  140. oled_command(0x22);
  141. oled_command(0x00);
  142. oled_command(0x07);
  143. #ifdef I2C_BRIDGE
  144. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  145. #endif
  146. #ifdef SPI_BRIDGE
  147. OLED_DC_1;
  148. OLED_CS_0;
  149. HAL_SPI_Transmit(spi_bridge, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  150. OLED_CS_1;
  151. #endif
  152. }
  153. //
  154. void oled_invert_colors(uint8_t invert)
  155. {
  156. oled_command(invert ? SSD1327_INVERTDISPLAY : SSD1327_NORMALDISPLAY);
  157. }
  158. //
  159. void oled_rotate_display(uint8_t rotate)
  160. {
  161. if (rotate > 1) rotate = 1;
  162. oled_command(0xA0 | (0x01 & rotate)); // Set Segment Re-Map Default
  163. // 0xA0 (0x00) => column Address 0 mapped to 127
  164. // 0xA1 (0x01) => Column Address 127 mapped to 0
  165. oled_command(0xC0 | (0x08 & (rotate<<3))); // Set COM Output Scan Direction
  166. // 0xC0 (0x00) => normal mode (RESET) Scan from COM0 to COM[N-1];Where N is the Multiplex ratio.
  167. // 0xC8 (0xC8) => remapped mode. Scan from COM[N-1] to COM0;;Where N is the Multiplex ratio.
  168. }
  169. //
  170. void oled_display_on(uint8_t on)
  171. {
  172. oled_command(on ? SSD1327_DISPLAYON : SSD1327_DISPLAYOFF);
  173. }
  174. //
  175. void oled_set_contrast(uint8_t contrast)
  176. {
  177. oled_command(SSD1327_SETCONTRASTCURRENT); // Set Contrast Control
  178. oled_command(contrast);
  179. }
  180. //
  181. void oled_start_scroll_right(uint8_t start_page, uint8_t end_page, uint8_t speed)
  182. {
  183. oled_command(SSD1327_RIGHT_HORIZONTAL_SCROLL);
  184. oled_command(0x00);
  185. oled_command(start_page);
  186. oled_command(speed);
  187. oled_command(end_page);
  188. oled_command(SSD1327_ACTIVATE_SCROLL);
  189. }
  190. //
  191. void oled_start_scroll_left(uint8_t start_page, uint8_t end_page, uint8_t speed)
  192. {
  193. oled_command(SSD1327_LEFT_HORIZONTAL_SCROLL);
  194. oled_command(0x00);
  195. oled_command(start_page);
  196. oled_command(speed);
  197. oled_command(end_page);
  198. oled_command(SSD1327_ACTIVATE_SCROLL);
  199. }
  200. //
  201. void oled_stop_scroll(void)
  202. {
  203. oled_command(SSD1327_DEACTIVATE_SCROLL);
  204. }
  205. //
  206. void oled_init(void)
  207. {
  208. oled_command(0xae);//--turn off oled panel
  209. oled_command(0x15); // set column address
  210. oled_command(0x00); // start column 0
  211. oled_command(0x7f); // end column 127
  212. oled_command(0x75); // set row address
  213. oled_command(0x00); // start row 0
  214. oled_command(0x7f); // end row 127
  215. oled_command(0x81); // set contrast control
  216. oled_command(0x80);
  217. oled_command(0xa0); // gment remap
  218. oled_command(0x51); //51
  219. oled_command(0xa1); // start line
  220. oled_command(0x00);
  221. oled_command(0xa2); // display offset
  222. oled_command(0x00);
  223. oled_command(0xa4); // rmal display
  224. oled_command(0xa8); // set multiplex ratio
  225. oled_command(0x7f);
  226. oled_command(0xb1); // set phase leghth
  227. oled_command(0xf1);
  228. oled_command(0xb3); // set dclk
  229. oled_command(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
  230. oled_command(0xab); //
  231. oled_command(0x01); //
  232. oled_command(0xb6); // set phase leghth
  233. oled_command(0x0f);
  234. oled_command(0xbe);
  235. oled_command(0x0f);
  236. oled_command(0xbc);
  237. oled_command(0x08);
  238. oled_command(0xd5);
  239. oled_command(0x62);
  240. oled_command(0xfd);
  241. oled_command(0x12);
  242. oled_display_on(1);
  243. #if 0
  244. oled_command(SSD1327_DISPLAYOFF); // Display Off
  245. oled_command(SSD1327_SETCOLUMNADDRESS);
  246. oled_command(0x00);
  247. oled_command(0x7F);
  248. oled_command(SSD1327_SETROWADDRESS);
  249. oled_command(0x00);
  250. oled_command(0x7F);
  251. oled_set_contrast(0x7F);
  252. oled_command(SSD1327_SEGREMAP);
  253. oled_command(0x51);
  254. oled_command(SSD1327_SETDISPLAYSTARTLINE);
  255. oled_command(0x00);
  256. oled_command(SSD1327_SETDISPLAYOFFSET);
  257. oled_command(0x0);
  258. oled_command(SSD1327_DISPLAYALLON_RESUME); // Entire Display ON
  259. oled_command(SSD1327_SETMULTIPLEX);
  260. oled_command(0x7F);
  261. oled_command(SSD1327_SETPHASELENGTH);
  262. oled_command(0xF1); // !
  263. oled_command(SSD1327_SETFRONTCLOCKDIVIDER_OSCILLATORFREQUENCY);
  264. oled_command(0x00);
  265. oled_command(SSD1327_FUNCTIONSELECTIONA); // !
  266. oled_command(0x01); // !
  267. oled_command(SSD1327_SETSECONDPRECHARGEPERTIOD);
  268. oled_command(0x0F); // !
  269. oled_command(SSD1327_SETSETVCOMVOLTAGE);
  270. oled_command(0x0F); // !
  271. oled_command(SSD1327_SETPRECHARGEVOLTAGE);
  272. oled_command(0x08); // !
  273. oled_command(SSD1327_FUNCTIONSELECTIONB);
  274. oled_command(0x62);
  275. oled_command(SSD1327_SETCOMMANDLOCK); // !
  276. oled_command(0x12); // !
  277. oled_display_on(1);
  278. #endif
  279. }
  280. //
  281. void oled_draw_pixel(int16_t x, int16_t y, uint8_t color)
  282. {
  283. if ((x < 0) || (x >= OLED_WIDTH) || (y < 0) || (y >= OLED_HEIGHT))
  284. return;
  285. uint8_t SelectedCell = oled_buf[x/2 + y*(OLED_WIDTH/2)];
  286. if(x % 2)
  287. {
  288. SelectedCell &= ~(0x0F);
  289. SelectedCell |= (0x0F & color);
  290. }
  291. else
  292. {
  293. SelectedCell &= ~(0xF0);
  294. SelectedCell |= (0xF0 & (color<<4));
  295. }
  296. oled_buf[x/2 + y*(OLED_WIDTH/2)] = SelectedCell;
  297. }
  298. //
  299. void oled_clear(uint8_t color)
  300. {
  301. if (color > WHITE) color = WHITE;
  302. memset(oled_buf, (color << 4 | color), OLED_BUF_SIZE);
  303. }
  304. //
  305. void oled_dma_end_callback(SPI_HandleTypeDef *hspi)
  306. {
  307. if (hspi == spi_bridge) {
  308. OLED_CS_1;
  309. }
  310. }
  311. // ------------------------------------------------------------------------------- //
  312. //
  313. void oled_draw_rec(int x_start, int y_start, int x_end, int y_end, uint8_t color)
  314. {
  315. for (int i = 0; i < y_end; i++) {
  316. GFX_DrawLine(x_start, y_start + i, x_end, y_start + i, color);
  317. }
  318. GFX_DrawLine(x_start, y_end, x_end, y_end, color);
  319. }
  320. #define STR_H 20
  321. #define STR_Y_S 36
  322. #define STR_FRAME_START_Y 8
  323. #define STR_X_S 4
  324. #define STR_X_E 123
  325. //
  326. void oled_draw_string_frame(uint8_t str_num)
  327. {
  328. for (int i = 0; i < 2; i++) {
  329. // нижняя
  330. 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);
  331. // верхняя
  332. 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);
  333. }
  334. // Правая
  335. 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);
  336. // Левая
  337. 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);
  338. }
  339. //
  340. void test_oled(void)
  341. {
  342. #if 0
  343. for (int i = 0; i < 16; i++) {
  344. SSD1327_DrawPixel(10, 20 + i, i);
  345. }
  346. SSD1327_DrawPixel(20, 20, 15);
  347. SSD1327_DrawPixel(30, 20, 15);
  348. SSD1327_DrawPixel(40, 20, 5);
  349. #endif
  350. //void GFX_DrawLine(int x_start, int y_start, int x_end, int y_end, uint8_t color);
  351. // Верхняя
  352. GFX_DrawLine(0, 0, 127, 0, 15);
  353. // Левая
  354. GFX_DrawLine(0, 0, 0, 127, 15);
  355. // Правая
  356. GFX_DrawLine(127, 0, 127, 127, 15);
  357. // Нижняя
  358. GFX_DrawLine(0, 127, 127, 127, 15);
  359. GFX_SetFont(font_8x5);
  360. GFX_SetFontSize(1);
  361. GFX_DrawString(4, 10, (char*)"BbAa12345", 15, 0);
  362. GFX_SetFontSize(2);
  363. GFX_DrawString(4, 30, (char*)"BbAa12345", 15, 0);
  364. GFX_SetFontSize(3);
  365. GFX_DrawString(4, 60, (char*)"BbAa12345", 15, 0);
  366. oled_display();
  367. }