| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- #include "stm32g4xx_hal.h"
- #include "oled_common.h"
- #include "oled_config.h"
- #include "config.h"
- #include "i2c_bridge.h"
- #include "spi_bridge.h"
- #include "logo_grayscale.h"
- #include "fonts.h"
- #include "SSD1327.h"
- #include "cmsis_os.h"
- #include <string.h>
- extern "C" {
- #include "GFX_SSD1327.h"
- #include "GUI_Paint.h"
- #include "fonts.h"
- }
-
- #if defined(I2C_BRIDGE)
- I2C_HandleTypeDef *i2c_bridge;
- #elif defined(SPI_BRIDGE)
- SPI_HandleTypeDef *spi_bridge;
- #endif
-
- uint8_t oled_buf[OLED_BUF_SIZE];
- //
- void init_oled(void)
- {
- init_gpio_oled();
-
- oled_reset();
-
- #if defined(I2C_BRIDGE)
- i2c_bridge_init();
- i2c_bridge = i2c_get_bridge();
-
- #elif defined(SPI_BRIDGE)
- spi_bridge_init();
- spi_bridge = spi_get_bridge();
- #endif
-
- oled_init();
-
- oled_clear(BLACK);
-
- test_oled();
-
- while(1) {}
-
- oled_display();
- }
- //
- void init_gpio_oled(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- __HAL_RCC_GPIOA_CLK_ENABLE();
- // GPIOA_5 - reset
- GPIO_InitStruct.Pin = OLED_RST_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- HAL_GPIO_Init(OLED_RST_PORT, &GPIO_InitStruct);
-
- // GPIOA_6 - DC
- GPIO_InitStruct.Pin = OLED_DC_PIN;
- HAL_GPIO_Init(OLED_DC_PORT, &GPIO_InitStruct);
-
- HAL_GPIO_WritePin(OLED_RST_PORT, OLED_RST_PIN, GPIO_PIN_RESET);
-
- #if defined(I2C_BRIDGE)
- HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_SET);
- #elif defined(SPI_BRIDGE)
- HAL_GPIO_WritePin(OLED_DC_PORT, OLED_DC_PIN, GPIO_PIN_RESET);
- #endif
- }
- //
- void oled_reset(void)
- {
- OLED_RST_1;
- oled_delay_ms(100);
- OLED_RST_0;
- oled_delay_ms(100);
- OLED_RST_1;
- oled_delay_ms(100);
- }
- //
- void oled_delay_ms(uint32_t ms)
- {
- osDelay(ms);
- }
- // TODO убрать sizeof
- void oled_command(uint8_t com)
- {
- #ifdef I2C_BRIDGE
- HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x00, 1, &com, sizeof(com), 100);
- #endif
-
- #ifdef SPI_BRIDGE
- OLED_DC_0;
- OLED_CS_0;
- HAL_SPI_Transmit(spi_bridge, &com, 1, 10);
- OLED_CS_1;
- #endif
- }
- // TODO убрать sizeof
- void oled_data(uint8_t dat)
- {
- #ifdef I2C_BRIDGE
- HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, &dat, sizeof(dat), 100);
- #endif
-
- #ifdef SPI_BRIDGE
- OLED_DC_1;
- OLED_CS_0;
- HAL_SPI_Transmit(spi_bridge, &dat, 1, 10);
- OLED_CS_1;
- #endif
- }
- //
- void oled_display(void)
- {
- oled_command(SSD1327_SETCOLUMNADDRESS);
- oled_command(0x00);
- oled_command(0x3F);
- oled_command(SSD1327_SETROWADDRESS);
- oled_command(0x00);
- oled_command(0x7F);
- #ifdef I2C_BRIDGE
- HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 1000);
- #endif
-
- #ifdef SPI_BRIDGE
- OLED_DC_1;
- OLED_CS_0;
- HAL_SPI_Transmit(spi_bridge, (uint8_t*)&oled_buf, OLED_BUF_SIZE, 100);
- OLED_CS_1;
- #endif
- }
- //
- void oled_bitmap(uint8_t *bitmap)
- {
- oled_command(0x22);
- oled_command(0x00);
- oled_command(0x07);
-
- #ifdef I2C_BRIDGE
- HAL_I2C_Mem_Write(i2c_bridge, OLED_I2C_ADDR, 0x40, 1, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
- #endif
-
- #ifdef SPI_BRIDGE
- OLED_DC_1;
- OLED_CS_0;
- HAL_SPI_Transmit(spi_bridge, bitmap, (OLED_HEIGHT * OLED_WIDTH / 8), 100);
- OLED_CS_1;
- #endif
- }
- //
- void oled_invert_colors(uint8_t invert)
- {
- oled_command(invert ? SSD1327_INVERTDISPLAY : SSD1327_NORMALDISPLAY);
- }
- //
- void oled_rotate_display(uint8_t rotate)
- {
- if (rotate > 1) rotate = 1;
- oled_command(0xA0 | (0x01 & rotate)); // Set Segment Re-Map Default
- // 0xA0 (0x00) => column Address 0 mapped to 127
- // 0xA1 (0x01) => Column Address 127 mapped to 0
- oled_command(0xC0 | (0x08 & (rotate<<3))); // Set COM Output Scan Direction
- // 0xC0 (0x00) => normal mode (RESET) Scan from COM0 to COM[N-1];Where N is the Multiplex ratio.
- // 0xC8 (0xC8) => remapped mode. Scan from COM[N-1] to COM0;;Where N is the Multiplex ratio.
- }
- //
- void oled_display_on(uint8_t on)
- {
- oled_command(on ? SSD1327_DISPLAYON : SSD1327_DISPLAYOFF);
- }
- //
- void oled_set_contrast(uint8_t contrast)
- {
- oled_command(SSD1327_SETCONTRASTCURRENT); // Set Contrast Control
- oled_command(contrast);
- }
- //
- void oled_start_scroll_right(uint8_t start_page, uint8_t end_page, uint8_t speed)
- {
- oled_command(SSD1327_RIGHT_HORIZONTAL_SCROLL);
- oled_command(0x00);
- oled_command(start_page);
- oled_command(speed);
- oled_command(end_page);
- oled_command(SSD1327_ACTIVATE_SCROLL);
- }
- //
- void oled_start_scroll_left(uint8_t start_page, uint8_t end_page, uint8_t speed)
- {
- oled_command(SSD1327_LEFT_HORIZONTAL_SCROLL);
- oled_command(0x00);
- oled_command(start_page);
- oled_command(speed);
- oled_command(end_page);
- oled_command(SSD1327_ACTIVATE_SCROLL);
- }
- //
- void oled_stop_scroll(void)
- {
- oled_command(SSD1327_DEACTIVATE_SCROLL);
- }
- //
- void oled_init(void)
- {
- oled_command(0xae);//--turn off oled panel
- oled_command(0x15); // set column address
- oled_command(0x00); // start column 0
- oled_command(0x7f); // end column 127
- oled_command(0x75); // set row address
- oled_command(0x00); // start row 0
- oled_command(0x7f); // end row 127
- oled_command(0x81); // set contrast control
- oled_command(0x80);
- oled_command(0xa0); // gment remap
- oled_command(0x51); //51
- oled_command(0xa1); // start line
- oled_command(0x00);
- oled_command(0xa2); // display offset
- oled_command(0x00);
- oled_command(0xa4); // rmal display
- oled_command(0xa8); // set multiplex ratio
- oled_command(0x7f);
- oled_command(0xb1); // set phase leghth
- oled_command(0xf1);
- oled_command(0xb3); // set dclk
- oled_command(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
- oled_command(0xab); //
- oled_command(0x01); //
- oled_command(0xb6); // set phase leghth
- oled_command(0x0f);
- oled_command(0xbe);
- oled_command(0x0f);
- oled_command(0xbc);
- oled_command(0x08);
- oled_command(0xd5);
- oled_command(0x62);
- oled_command(0xfd);
- oled_command(0x12);
-
- oled_display_on(1);
-
- #if 0
- // Рабочий вариант
- oled_command(0xae);//--turn off oled panel
- oled_command(0x15); // set column address
- oled_command(0x00); // start column 0
- oled_command(0x7f); // end column 127
- oled_command(0x75); // set row address
- oled_command(0x00); // start row 0
- oled_command(0x7f); // end row 127
- oled_command(0x81); // set contrast control
- oled_command(0x80);
- oled_command(0xa0); // gment remap
- oled_command(0x51); //51
- oled_command(0xa1); // start line
- oled_command(0x00);
- oled_command(0xa2); // display offset
- oled_command(0x00);
- oled_command(0xa4); // rmal display
- oled_command(0xa8); // set multiplex ratio
- oled_command(0x7f);
- oled_command(0xb1); // set phase leghth
- oled_command(0xf1);
- oled_command(0xb3); // set dclk
- oled_command(0x00); //80Hz:0xc1 90Hz:0xe1 100Hz:0x00 110Hz:0x30 120Hz:0x50 130Hz:0x70 01
- oled_command(0xab); //
- oled_command(0x01); //
- oled_command(0xb6); // set phase leghth
- oled_command(0x0f);
- oled_command(0xbe);
- oled_command(0x0f);
- oled_command(0xbc);
- oled_command(0x08);
- oled_command(0xd5);
- oled_command(0x62);
- oled_command(0xfd);
- oled_command(0x12);
-
- oled_display_on(1);
- #endif
-
-
- #if 0
- oled_command(SSD1327_DISPLAYOFF); // Display Off
-
- oled_command(SSD1327_SETCOLUMNADDRESS);
- oled_command(0x00);
- oled_command(0x7F);
- oled_command(SSD1327_SETROWADDRESS);
- oled_command(0x00);
- oled_command(0x7F);
-
- oled_set_contrast(0x7F);
-
- oled_command(SSD1327_SEGREMAP);
- oled_command(0x51);
-
- oled_command(SSD1327_SETDISPLAYSTARTLINE);
- oled_command(0x00);
-
- oled_command(SSD1327_SETDISPLAYOFFSET);
- oled_command(0x0);
-
- oled_command(SSD1327_DISPLAYALLON_RESUME); // Entire Display ON
- oled_command(SSD1327_SETMULTIPLEX);
- oled_command(0x7F);
-
- oled_command(SSD1327_SETPHASELENGTH);
- oled_command(0xF1); // !
-
- oled_command(SSD1327_SETFRONTCLOCKDIVIDER_OSCILLATORFREQUENCY);
- oled_command(0x00);
-
- oled_command(SSD1327_FUNCTIONSELECTIONA); // !
- oled_command(0x01); // !
-
- oled_command(SSD1327_SETSECONDPRECHARGEPERTIOD);
- oled_command(0x0F); // !
-
- oled_command(SSD1327_SETSETVCOMVOLTAGE);
- oled_command(0x0F); // !
-
- oled_command(SSD1327_SETPRECHARGEVOLTAGE);
- oled_command(0x08); // !
-
- oled_command(SSD1327_FUNCTIONSELECTIONB);
- oled_command(0x62);
-
- oled_command(SSD1327_SETCOMMANDLOCK); // !
- oled_command(0x12); // !
-
- oled_display_on(1);
- #endif
- }
- //
- void oled_draw_pixel(int16_t x, int16_t y, uint8_t color)
- {
- if ((x < 0) || (x >= OLED_WIDTH) || (y < 0) || (y >= OLED_HEIGHT))
- return;
- uint8_t SelectedCell = oled_buf[x/2 + y*(OLED_WIDTH/2)];
- if(x % 2)
- {
- SelectedCell &= ~(0x0F);
- SelectedCell |= (0x0F & color);
- }
- else
- {
- SelectedCell &= ~(0xF0);
- SelectedCell |= (0xF0 & (color<<4));
- }
- oled_buf[x/2 + y*(OLED_WIDTH/2)] = SelectedCell;
- }
- //
- void oled_clear(uint8_t color)
- {
- if (color > WHITE) color = WHITE;
- memset(oled_buf, (color << 4 | color), OLED_BUF_SIZE);
- }
- //
- void oled_dma_end_callback(SPI_HandleTypeDef *hspi)
- {
- #if defined(SPI_BRIDGE)
- if (hspi == spi_bridge) {
- OLED_CS_1;
- }
- #endif
- }
- // ------------------------------------------------------------------------------- //
- //
- void oled_draw_rec(int x_start, int y_start, int x_end, int y_end, uint8_t color)
- {
- for (int i = 0; i < y_end; i++) {
- GFX_DrawLine(x_start, y_start + i, x_end, y_start + i, color);
- }
- GFX_DrawLine(x_start, y_end, x_end, y_end, color);
- }
- #define STR_H 20
- #define STR_Y_S 36
- #define STR_FRAME_START_Y 8
- #define STR_X_S 4
- #define STR_X_E 123
- //
- void oled_draw_string_frame(uint8_t str_num)
- {
- for (int i = 0; i < 2; i++) {
- // нижняя
- 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);
-
- // верхняя
- 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);
- }
-
- // Правая
- 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);
-
- // Левая
- 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);
- }
- //
- void test_oled_old(void)
- {
- #if 0
- for (int i = 0; i < 16; i++) {
- SSD1327_DrawPixel(10, 20 + i, i);
- }
-
- SSD1327_DrawPixel(20, 20, 15);
- SSD1327_DrawPixel(30, 20, 15);
- SSD1327_DrawPixel(40, 20, 5);
- #endif
- //void GFX_DrawLine(int x_start, int y_start, int x_end, int y_end, uint8_t color);
- // Верхняя
- GFX_DrawLine(0, 0, 127, 0, 15);
-
- // Левая
- GFX_DrawLine(0, 0, 0, 127, 15);
-
- // Правая
- GFX_DrawLine(127, 0, 127, 127, 15);
-
- // Нижняя
- GFX_DrawLine(0, 127, 127, 127, 15);
-
- //GFX_SetFont(font_8x5);
-
- GFX_SetFontSize(1);
- GFX_DrawString(4, 10, (char*)"BbAa12345", 15, 0);
-
- GFX_SetFontSize(2);
- GFX_DrawString(4, 30, (char*)"BbAa12345", 15, 0);
-
- GFX_SetFontSize(3);
- GFX_DrawString(4, 60, (char*)"BbAa12345", 15, 0);
- oled_display();
- }
- //
- void test_oled(void)
- {
- //void Paint_DrawPoint(UWORD Xpoint, UWORD Ypoint, UWORD Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_FillWay);
- #if 1
- //OLED_1in5_B_Clear();
-
- Paint_NewImage(oled_buf, OLED_WIDTH, OLED_HEIGHT, 0, BLACK);
- Paint_SetScale(16);
- // 1.Select Image
- Paint_SelectImage(oled_buf);
- Paint_Clear(BLACK);
-
- while (1)
- {
- /*
- Paint_DrawPoint(20, 10, WHITE, DOT_PIXEL_1X1, DOT_STYLE_DFT);
- Paint_DrawPoint(30, 10, WHITE, DOT_PIXEL_2X2, DOT_STYLE_DFT);
- Paint_DrawPoint(40, 10, WHITE, DOT_PIXEL_3X3, DOT_STYLE_DFT);
- Paint_DrawLine(10, 10, 10, 20, WHITE, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
- Paint_DrawLine(20, 20, 20, 30, WHITE, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
- Paint_DrawLine(30, 30, 30, 40, WHITE, DOT_PIXEL_1X1, LINE_STYLE_DOTTED);
- Paint_DrawLine(40, 40, 40, 50, WHITE, DOT_PIXEL_1X1, LINE_STYLE_DOTTED);
- Paint_DrawCircle(60, 30, 15, WHITE, DOT_PIXEL_1X1, DRAW_FILL_EMPTY);
- Paint_DrawCircle(100, 40, 20, WHITE, DOT_PIXEL_1X1, DRAW_FILL_FULL);
- Paint_DrawRectangle(50, 30, 60, 40, WHITE, DOT_PIXEL_1X1, DRAW_FILL_EMPTY);
- Paint_DrawRectangle(90, 30, 110, 50, BLACK, DOT_PIXEL_1X1, DRAW_FILL_FULL);
-
- oled_display();
- osDelay(2000);
- //Paint_Clear(BLACK);
- oled_clear(BLACK);
- oled_display();
- osDelay(2000);
- */
- #if 0
- Paint_DrawString_EN(10, 0, "waveshare", &Font16, 0x1, 0xb);
- Paint_DrawString_EN(10, 17, "hello world", &Font8, 0x2, 0xc);
- Paint_DrawNum(10, 30, 123.456789, &Font8, 4, 0x3, 0xd);
- Paint_DrawNum(10, 43, 987654, &Font12, 5, 0x4, 0xe);
- #endif
-
- Paint_DrawString_EN(10, 0, "waveshare", &Font16, 0xf, 0x0);
- Paint_DrawString_EN(10, 17, "hello world", &Font8, 0xf, 0x0);
- Paint_DrawNum(10, 30, 123.456789, &Font8, 4, 0xf, 0x0);
- Paint_DrawNum(10, 43, 987654, &Font12, 5, 0xf, 0x0);
- Paint_DrawString_EN(10, 60, "hello world", &Font8, 0xf, 0x0);
-
- oled_display();
- }
-
- //Paint_DrawPoint(40, 40, WHITE, DOT_PIXEL_8X8, DOT_FILL_AROUND);
-
- //void Paint_DrawLine(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend,
- // UWORD Color, DOT_PIXEL Line_width, LINE_STYLE Line_Style)
-
- Paint_DrawLine(10, 10, 10, 20, WHITE, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
-
- Paint_DrawString_EN(10, 17, "hello world", &Font8, 0x2, 0xc);
- #endif
- #if 0
- GFX_SetFont(Font8_Table);
- GFX_SetFontSize(1);
-
- //GFX_SetFont(font_8x13);
- ///GFX_SetFontSize(1);
-
- GFX_DrawString(8, 20, (char*)"Hellow world", 15, 0);
- #endif
-
- oled_display();
- }
|