接着需要实现以下2个函数。等下初始化oled接口需要用到。 uint8_t u8x8_byte_hc32_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){ static uint8_t buffer[32]; /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ static uint8_t buf_idx; uint8_t *data; switch(msg) { case U8X8_MSG_BYTE_SEND: data = (uint8_t *)arg_ptr; while( arg_int > 0 ) { buffer[buf_idx++] = *data; data++; arg_int--; } break; case U8X8_MSG_BYTE_INIT: /* add your custom code to init i2c subsystem */ break; case U8X8_MSG_BYTE_SET_DC: /* ignored for i2c */ break; case U8X8_MSG_BYTE_START_TRANSFER: buf_idx = 0; break; case U8X8_MSG_BYTE_END_TRANSFER: HAL_I2C_Master_Transmit(&SSD1306_I2C_PORT, SSD1306_I2C_ADDR, buffer, buf_idx, HAL_MAX_DELAY); break; default: return 0; } return 1;}uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){ switch(msg) { case U8X8_MSG_GPIO_AND_DELAY_INIT: /* Insert codes for initialization */ break; case U8X8_MSG_DELAY_MILLI: /* ms Delay */ SysTick_DelayTicks(arg_int); break;// case U8X8_MSG_GPIO_MENU_SELECT:// u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);// break;// case U8X8_MSG_GPIO_MENU_NEXT:// u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);// break;// case U8X8_MSG_GPIO_MENU_PREV:// u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);// break;// case U8X8_MSG_GPIO_MENU_HOME:// u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);// break; default: u8x8_SetGPIOResult(u8x8, 1); // default return value break; } return 1;}下面就是初始化函数 u8g2_t g_u8g2;void u8g2Init(u8g2_t *u8g2){ u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hc32_hw_i2c, u8x8_gpio_and_delay); u8g2_InitDisplay(u8g2); u8g2_SetPowerSave(u8g2, 0); u8g2_ClearBuffer(u8g2);}然后在main函数中初始化: u8g2Init(&g_u8g2);下面是测试绘图函数: void draw(u8g2_t *u8g2){ u8g2_ClearDisplay(u8g2); u8g2_SetFontMode(u8g2, 1); /*字体模式选择*/ u8g2_SetFontDirection(u8g2, 0); /*字体方向选择*/ u8g2_SetFont(u8g2, u8g2_font_inb24_mf); /*字库选择*/ u8g2_DrawStr(u8g2, 0, 20, "U"); u8g2_SetFontDirection(u8g2, 1); u8g2_SetFont(u8g2, u8g2_font_inb30_mn); u8g2_DrawStr(u8g2, 21,8,"8"); u8g2_SetFontDirection(u8g2, 0); u8g2_SetFont(u8g2, u8g2_font_inb24_mf); u8g2_DrawStr(u8g2, 51,30,"g"); u8g2_DrawStr(u8g2, 67,30,"\xb2"); u8g2_DrawHLine(u8g2, 2, 35, 47); u8g2_DrawHLine(u8g2, 3, 36, 47); u8g2_DrawVLine(u8g2, 45, 32, 12); u8g2_DrawVLine(u8g2, 46, 33, 12); u8g2_SetFont(u8g2, u8g2_font_6x12_tr); u8g2_DrawStr(u8g2, 1, 54,"u8g2 test by"); u8g2_DrawStr(u8g2, 60,64,"stmwooddata"); u8g2_SendBuffer(u8g2);}最后看看实际效果。
|