oled_common.cpp 11 KB

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