oled_common.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. #include "GUI_Paint.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. #elif defined(SPI_BRIDGE)
  31. spi_bridge_init();
  32. spi_bridge = spi_get_bridge();
  33. #endif
  34. oled_init();
  35. oled_clear(BLACK);
  36. test_oled();
  37. while(1) {}
  38. oled_display();
  39. }
  40. //
  41. void init_gpio_oled(void)
  42. {
  43. GPIO_InitTypeDef GPIO_InitStruct = {0};
  44. __HAL_RCC_GPIOA_CLK_ENABLE();
  45. // GPIOA_5 - reset
  46. GPIO_InitStruct.Pin = OLED_RST_PIN;
  47. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  48. GPIO_InitStruct.Pull = GPIO_NOPULL;
  49. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  50. HAL_GPIO_Init(OLED_RST_PORT, &GPIO_InitStruct);
  51. // GPIOA_6 - DC
  52. GPIO_InitStruct.Pin = OLED_DC_PIN;
  53. HAL_GPIO_Init(OLED_DC_PORT, &GPIO_InitStruct);
  54. HAL_GPIO_WritePin(OLED_RST_PORT, OLED_RST_PIN, GPIO_PIN_RESET);
  55. #if defined(I2C_BRIDGE)
  56. HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_SET);
  57. #elif defined(SPI_BRIDGE)
  58. HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_RESET);
  59. #endif
  60. }
  61. //
  62. void oled_reset(void)
  63. {
  64. OLED_RST_1;
  65. oled_delay_ms(100);
  66. OLED_RST_0;
  67. oled_delay_ms(100);
  68. OLED_RST_1;
  69. oled_delay_ms(100);
  70. }
  71. //
  72. void oled_delay_ms(uint32_t ms)
  73. {
  74. osDelay(ms);
  75. }
  76. // TODO убрать sizeof
  77. void oled_command(uint8_t com)
  78. {
  79. #ifdef I2C_BRIDGE
  80. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x00, 1, &com, sizeof(com), 100);
  81. #endif
  82. #ifdef SPI_BRIDGE
  83. OLED_DC_0;
  84. OLED_CS_0;
  85. HAL_SPI_Transmit(spi_bridge, &com, 1, 10);
  86. OLED_CS_1;
  87. #endif
  88. }
  89. // TODO убрать sizeof
  90. void oled_data(uint8_t dat)
  91. {
  92. #ifdef I2C_BRIDGE
  93. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, &dat, sizeof(dat), 100);
  94. #endif
  95. #ifdef SPI_BRIDGE
  96. OLED_DC_1;
  97. OLED_CS_0;
  98. HAL_SPI_Transmit(spi_bridge, &dat, 1, 10);
  99. OLED_CS_1;
  100. #endif
  101. }
  102. //
  103. void oled_display(void)
  104. {
  105. oled_command(SSD1327_SETCOLUMNADDRESS);
  106. oled_command(0x00);
  107. oled_command(0x3F);
  108. oled_command(SSD1327_SETROWADDRESS);
  109. oled_command(0x00);
  110. oled_command(0x7F);
  111. #ifdef I2C_BRIDGE
  112. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 1000);
  113. #endif
  114. #ifdef SPI_BRIDGE
  115. OLED_DC_1;
  116. OLED_CS_0;
  117. HAL_SPI_Transmit(spi_bridge, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 100);
  118. OLED_CS_1;
  119. #endif
  120. }
  121. //
  122. void oled_bitmap(uint8_t *bitmap)
  123. {
  124. oled_command(0x22);
  125. oled_command(0x00);
  126. oled_command(0x07);
  127. #ifdef I2C_BRIDGE
  128. HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  129. #endif
  130. #ifdef SPI_BRIDGE
  131. OLED_DC_1;
  132. OLED_CS_0;
  133. HAL_SPI_Transmit(spi_bridge, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
  134. OLED_CS_1;
  135. #endif
  136. }
  137. //
  138. void oled_invert_colors(uint8_t invert)
  139. {
  140. oled_command(invert ? SSD1327_INVERTDISPLAY : SSD1327_NORMALDISPLAY);
  141. }
  142. //
  143. void oled_rotate_display(uint8_t rotate)
  144. {
  145. if (rotate > 1) rotate = 1;
  146. oled_command(0xA0 | (0x01 & rotate)); // Set Segment Re-Map Default
  147. // 0xA0 (0x00) => column Address 0 mapped to 127
  148. // 0xA1 (0x01) => Column Address 127 mapped to 0
  149. oled_command(0xC0 | (0x08 & (rotate<<3))); // Set COM Output Scan Direction
  150. // 0xC0 (0x00) => normal mode (RESET) Scan from COM0 to COM[N-1];Where N is the Multiplex ratio.
  151. // 0xC8 (0xC8) => remapped mode. Scan from COM[N-1] to COM0;;Where N is the Multiplex ratio.
  152. }
  153. //
  154. void oled_display_on(uint8_t on)
  155. {
  156. oled_command(on ? SSD1327_DISPLAYON : SSD1327_DISPLAYOFF);
  157. }
  158. //
  159. void oled_set_contrast(uint8_t contrast)
  160. {
  161. oled_command(SSD1327_SETCONTRASTCURRENT); // Set Contrast Control
  162. oled_command(contrast);
  163. }
  164. //
  165. void oled_start_scroll_right(uint8_t start_page, uint8_t end_page, uint8_t speed)
  166. {
  167. oled_command(SSD1327_RIGHT_HORIZONTAL_SCROLL);
  168. oled_command(0x00);
  169. oled_command(start_page);
  170. oled_command(speed);
  171. oled_command(end_page);
  172. oled_command(SSD1327_ACTIVATE_SCROLL);
  173. }
  174. //
  175. void oled_start_scroll_left(uint8_t start_page, uint8_t end_page, uint8_t speed)
  176. {
  177. oled_command(SSD1327_LEFT_HORIZONTAL_SCROLL);
  178. oled_command(0x00);
  179. oled_command(start_page);
  180. oled_command(speed);
  181. oled_command(end_page);
  182. oled_command(SSD1327_ACTIVATE_SCROLL);
  183. }
  184. //
  185. void oled_stop_scroll(void)
  186. {
  187. oled_command(SSD1327_DEACTIVATE_SCROLL);
  188. }
  189. //
  190. void oled_init(void)
  191. {
  192. oled_command(0xae);//--turn off oled panel
  193. oled_command(0x15); // set column address
  194. oled_command(0x00); // start column 0
  195. oled_command(0x7f); // end column 127
  196. oled_command(0x75); // set row address
  197. oled_command(0x00); // start row 0
  198. oled_command(0x7f); // end row 127
  199. oled_command(0x81); // set contrast control
  200. oled_command(0x80);
  201. oled_command(0xa0); // gment remap
  202. oled_command(0x51); //51
  203. oled_command(0xa1); // start line
  204. oled_command(0x00);
  205. oled_command(0xa2); // display offset
  206. oled_command(0x00);
  207. oled_command(0xa4); // rmal display
  208. oled_command(0xa8); // set multiplex ratio
  209. oled_command(0x7f);
  210. oled_command(0xb1); // set phase leghth
  211. oled_command(0xf1);
  212. oled_command(0xb3); // set dclk
  213. oled_command(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
  214. oled_command(0xab); //
  215. oled_command(0x01); //
  216. oled_command(0xb6); // set phase leghth
  217. oled_command(0x0f);
  218. oled_command(0xbe);
  219. oled_command(0x0f);
  220. oled_command(0xbc);
  221. oled_command(0x08);
  222. oled_command(0xd5);
  223. oled_command(0x62);
  224. oled_command(0xfd);
  225. oled_command(0x12);
  226. oled_display_on(1);
  227. #if 0
  228. // Рабочий вариант
  229. oled_command(0xae);//--turn off oled panel
  230. oled_command(0x15); // set column address
  231. oled_command(0x00); // start column 0
  232. oled_command(0x7f); // end column 127
  233. oled_command(0x75); // set row address
  234. oled_command(0x00); // start row 0
  235. oled_command(0x7f); // end row 127
  236. oled_command(0x81); // set contrast control
  237. oled_command(0x80);
  238. oled_command(0xa0); // gment remap
  239. oled_command(0x51); //51
  240. oled_command(0xa1); // start line
  241. oled_command(0x00);
  242. oled_command(0xa2); // display offset
  243. oled_command(0x00);
  244. oled_command(0xa4); // rmal display
  245. oled_command(0xa8); // set multiplex ratio
  246. oled_command(0x7f);
  247. oled_command(0xb1); // set phase leghth
  248. oled_command(0xf1);
  249. oled_command(0xb3); // set dclk
  250. oled_command(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
  251. oled_command(0xab); //
  252. oled_command(0x01); //
  253. oled_command(0xb6); // set phase leghth
  254. oled_command(0x0f);
  255. oled_command(0xbe);
  256. oled_command(0x0f);
  257. oled_command(0xbc);
  258. oled_command(0x08);
  259. oled_command(0xd5);
  260. oled_command(0x62);
  261. oled_command(0xfd);
  262. oled_command(0x12);
  263. oled_display_on(1);
  264. #endif
  265. #if 0
  266. oled_command(SSD1327_DISPLAYOFF); // Display Off
  267. oled_command(SSD1327_SETCOLUMNADDRESS);
  268. oled_command(0x00);
  269. oled_command(0x7F);
  270. oled_command(SSD1327_SETROWADDRESS);
  271. oled_command(0x00);
  272. oled_command(0x7F);
  273. oled_set_contrast(0x7F);
  274. oled_command(SSD1327_SEGREMAP);
  275. oled_command(0x51);
  276. oled_command(SSD1327_SETDISPLAYSTARTLINE);
  277. oled_command(0x00);
  278. oled_command(SSD1327_SETDISPLAYOFFSET);
  279. oled_command(0x0);
  280. oled_command(SSD1327_DISPLAYALLON_RESUME); // Entire Display ON
  281. oled_command(SSD1327_SETMULTIPLEX);
  282. oled_command(0x7F);
  283. oled_command(SSD1327_SETPHASELENGTH);
  284. oled_command(0xF1); // !
  285. oled_command(SSD1327_SETFRONTCLOCKDIVIDER_OSCILLATORFREQUENCY);
  286. oled_command(0x00);
  287. oled_command(SSD1327_FUNCTIONSELECTIONA); // !
  288. oled_command(0x01); // !
  289. oled_command(SSD1327_SETSECONDPRECHARGEPERTIOD);
  290. oled_command(0x0F); // !
  291. oled_command(SSD1327_SETSETVCOMVOLTAGE);
  292. oled_command(0x0F); // !
  293. oled_command(SSD1327_SETPRECHARGEVOLTAGE);
  294. oled_command(0x08); // !
  295. oled_command(SSD1327_FUNCTIONSELECTIONB);
  296. oled_command(0x62);
  297. oled_command(SSD1327_SETCOMMANDLOCK); // !
  298. oled_command(0x12); // !
  299. oled_display_on(1);
  300. #endif
  301. }
  302. //
  303. void oled_draw_pixel(int16_t x, int16_t y, uint8_t color)
  304. {
  305. if ((x < 0) || (x >= OLED_WIDTH) || (y < 0) || (y >= OLED_HEIGHT))
  306. return;
  307. uint8_t SelectedCell = oled_buf[x/2 + y*(OLED_WIDTH/2)];
  308. if(x % 2)
  309. {
  310. SelectedCell &= ~(0x0F);
  311. SelectedCell |= (0x0F & color);
  312. }
  313. else
  314. {
  315. SelectedCell &= ~(0xF0);
  316. SelectedCell |= (0xF0 & (color<<4));
  317. }
  318. oled_buf[x/2 + y*(OLED_WIDTH/2)] = SelectedCell;
  319. }
  320. //
  321. void oled_clear(uint8_t color)
  322. {
  323. if (color > WHITE) color = WHITE;
  324. memset(oled_buf, (color << 4 | color), OLED_BUF_SIZE);
  325. }
  326. //
  327. void oled_dma_end_callback(SPI_HandleTypeDef *hspi)
  328. {
  329. #if defined(SPI_BRIDGE)
  330. if (hspi == spi_bridge) {
  331. OLED_CS_1;
  332. }
  333. #endif
  334. }
  335. // ------------------------------------------------------------------------------- //
  336. //
  337. void oled_draw_rec(int x_start, int y_start, int x_end, int y_end, uint8_t color)
  338. {
  339. for (int i = 0; i < y_end; i++) {
  340. GFX_DrawLine(x_start, y_start + i, x_end, y_start + i, color);
  341. }
  342. GFX_DrawLine(x_start, y_end, x_end, y_end, color);
  343. }
  344. #define STR_H 20
  345. #define STR_Y_S 36
  346. #define STR_FRAME_START_Y 8
  347. #define STR_X_S 4
  348. #define STR_X_E 123
  349. //
  350. void oled_draw_string_frame(uint8_t str_num)
  351. {
  352. for (int i = 0; i < 2; i++) {
  353. // нижняя
  354. 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);
  355. // верхняя
  356. 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);
  357. }
  358. // Правая
  359. 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);
  360. // Левая
  361. 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);
  362. }
  363. //
  364. void test_oled_old(void)
  365. {
  366. #if 0
  367. for (int i = 0; i < 16; i++) {
  368. SSD1327_DrawPixel(10, 20 + i, i);
  369. }
  370. SSD1327_DrawPixel(20, 20, 15);
  371. SSD1327_DrawPixel(30, 20, 15);
  372. SSD1327_DrawPixel(40, 20, 5);
  373. #endif
  374. //void GFX_DrawLine(int x_start, int y_start, int x_end, int y_end, uint8_t color);
  375. // Верхняя
  376. GFX_DrawLine(0, 0, 127, 0, 15);
  377. // Левая
  378. GFX_DrawLine(0, 0, 0, 127, 15);
  379. // Правая
  380. GFX_DrawLine(127, 0, 127, 127, 15);
  381. // Нижняя
  382. GFX_DrawLine(0, 127, 127, 127, 15);
  383. GFX_SetFont(font_8x5);
  384. GFX_SetFontSize(1);
  385. GFX_DrawString(4, 10, (char*)"BbAa12345", 15, 0);
  386. GFX_SetFontSize(2);
  387. GFX_DrawString(4, 30, (char*)"BbAa12345", 15, 0);
  388. GFX_SetFontSize(3);
  389. GFX_DrawString(4, 60, (char*)"BbAa12345", 15, 0);
  390. oled_display();
  391. }
  392. sFONT Font8 = {
  393. Font8_Table,
  394. 5, /* Width */
  395. 8, /* Height */
  396. };
  397. //
  398. void test_oled(void)
  399. {
  400. //void Paint_DrawPoint(UWORD Xpoint, UWORD Ypoint, UWORD Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_FillWay);
  401. #if 1
  402. Paint_NewImage(oled_buf, OLED_WIDTH, OLED_HEIGHT, 0, BLACK);
  403. Paint_SetScale(16);
  404. // 1.Select Image
  405. Paint_SelectImage(oled_buf);
  406. Paint_Clear(BLACK);
  407. Paint_DrawPoint(40, 40, WHITE, DOT_PIXEL_8X8, DOT_FILL_AROUND);
  408. //void Paint_DrawLine(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend,
  409. // UWORD Color, DOT_PIXEL Line_width, LINE_STYLE Line_Style)
  410. Paint_DrawLine(10, 10, 10, 20, WHITE, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
  411. Paint_DrawString_EN(10, 17, "hello world", &Font8, 0x2, 0xc);
  412. #endif
  413. #if 0
  414. GFX_SetFont(Font8_Table);
  415. GFX_SetFontSize(1);
  416. //GFX_SetFont(font_8x13);
  417. ///GFX_SetFontSize(1);
  418. GFX_DrawString(8, 20, (char*)"Hellow world", 15, 0);
  419. #endif
  420. oled_display();
  421. }