From a1624f0682612117f01b55486260b119fda8a413 Mon Sep 17 00:00:00 2001 From: JiXieShi Date: Sat, 22 Jun 2024 15:21:15 +0800 Subject: [PATCH] =?UTF-8?q?UP=20=E9=80=9A=E7=94=A8OLED?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- demo/oled/t_oled.h | 6 ++++++ demo/oled/test.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/inc/oled/oled.h | 27 ++++++++++++++++++++++----- lib/src/oled/oled.cpp | 5 +++-- main.c | 4 +++- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 demo/oled/t_oled.h create mode 100644 demo/oled/test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fafeb5..a62971c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,4 +20,4 @@ add_executable(HW_Lib main.c ${SOURCES}) #导入库 add_subdirectory(lib) -target_link_libraries(HW_Lib HW_LIB_List HW_LIB_Task HW_LIB_Printf HW_LIB_Utils HW_LIB_Iic HW_LIB_Spi HW_LIB_Key) +target_link_libraries(HW_Lib HW_LIB_List HW_LIB_Task HW_LIB_Printf HW_LIB_Utils HW_LIB_Iic HW_LIB_Spi HW_LIB_Key HW_LIB_Oled) diff --git a/demo/oled/t_oled.h b/demo/oled/t_oled.h new file mode 100644 index 0000000..25844a8 --- /dev/null +++ b/demo/oled/t_oled.h @@ -0,0 +1,6 @@ +#ifndef HW_LIB_T_OLED_H +#define HW_LIB_T_OLED_H + +void Test_OLED(); + +#endif //HW_LIB_T_OLED_H diff --git a/demo/oled/test.c b/demo/oled/test.c new file mode 100644 index 0000000..6948eb4 --- /dev/null +++ b/demo/oled/test.c @@ -0,0 +1,42 @@ +#include +#include "oled.h" +#include "log.h" +#include "tool.h" +#include "t_oled.h" +#include + +uint8_t Cmd(uint8_t *data, size_t l) { + Buf_Print("Cmd", data, l, 16); +} + +uint8_t Data(uint8_t *data, size_t l) { + Buf_Print("Data", data, l, 16); +} + +void Refresh_Call(OLED_T *dev) { + LOGT("OLED", "CALL"); + Buf_Print("Buf", dev->buf, dev->width * (dev->height / 8), 16); +} + +uint8_t oledbuf[8][128]; + +void Test_OLED() { + OLED_T oled = { + .height=64, + .width=128, + .state=IDLE, + .buf=oledbuf, + .call=Refresh_Call, + .cmd=Cmd, + .data=Data + }; + OLED_Init(&oled); + Sleep(150); + OLED_CLS(&oled); + OLED_ShowString(&oled, 0, 0, "sss", 4); + while (1) { + // 每5ms调用一次key_ticks函数 + OLED_Refresh(&oled); + Sleep(5); // 使用Windows平台的Sleep函数进行5ms延时 + } +} \ No newline at end of file diff --git a/lib/inc/oled/oled.h b/lib/inc/oled/oled.h index 10189df..37c0b75 100644 --- a/lib/inc/oled/oled.h +++ b/lib/inc/oled/oled.h @@ -7,7 +7,7 @@ extern "C" { #include "stdint.h" -#define REFRESH_CALL_ENABLE 1 //使用DMA或者需要收到刷新完毕通知的回调函数 +#define REFRESH_CALL_ENABLE 1 //使用DMA或者整体刷新函数 /** @@ -33,9 +33,9 @@ typedef uint8_t (*OLED_DATA_t)(uint8_t *data, size_t len); #if REFRESH_CALL_ENABLE /** - * @brief OLED刷新完毕回调指针类型 + * @brief OLED刷新函数指针类型 */ -typedef void (*OLED_REFRESH_t)(); +typedef void (*OLED_REFRESH_t)(OLED_T *dev); #endif /** @@ -58,7 +58,7 @@ struct OLED_Dev { OLED_CMD_t cmd; /**< OLED命令处理函数指针 */ OLED_DATA_t data; /**< OLED数据处理函数指针 */ #if REFRESH_CALL_ENABLE - OLED_REFRESH_t call; /**< OLED刷新完毕回调函数指针 */ + OLED_REFRESH_t call; /**< OLED刷新函数指针 */ #endif }; @@ -76,7 +76,7 @@ const uint8_t initCmd[] = { 2, 0xDA, 0x12, // 设置COM引脚硬件配置 2, 0x81, 0xCF, // 设置对比度控制 2, 0xD9, 0xF1, // 设置预充电周期 - 2, 0xDB, 0x40, // 设置VCOMH取消电平 + 2, 0xDB, 0x20, // 设置VCOMH取消电平 2, 0xA4, 0xA6, // 整个显示打开 1, 0xAF // 打开显示 }; @@ -208,6 +208,23 @@ void OLED_ShowString(OLED_T *dev, uint8_t x, uint8_t y, uint8_t *chr, uint8_t si */ void OLED_SPos(OLED_T *dev, uint8_t x, uint8_t y); +/** + * @brief 填充OLED显示缓冲区 + * @param dev: [输入] OLED设备指针 + * @param data: [输入] 填充数据 + * @return void + * @example OLED_Fill(&oled_dev, 0xFF); + */ +void OLED_Fill(OLED_T *dev, uint8_t data); + +/** + * @brief 清空OLED显示缓冲区 + * @param dev: [输入] OLED设备指针 + * @return void + * @example OLED_CLS(&oled_dev); + */ +void OLED_CLS(OLED_T *dev); + #ifdef __cplusplus } #endif diff --git a/lib/src/oled/oled.cpp b/lib/src/oled/oled.cpp index e2077b0..7bbc77f 100644 --- a/lib/src/oled/oled.cpp +++ b/lib/src/oled/oled.cpp @@ -44,10 +44,11 @@ void OLED_Turn(OLED_T *dev, bool e) { } void OLED_Refresh(OLED_T *dev) { - uint8_t i, cmd[3] = {0xb0, 0x00, 0x10}; + #if REFRESH_CALL_ENABLE - dev->call(); + dev->call(dev); #else + uint8_t i, cmd[3] = {0xb0, 0x00, 0x10}; if (dev->state == IDLE) { dev->state = REFRESH; for (i = 0; i < (dev->height >> 3); i++) { diff --git a/main.c b/main.c index d7b3c6d..347923b 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include "t_arg.h" #include "t_list.h" #include "t_key.h" +#include "t_oled.h" #include "tool.h" #include @@ -25,6 +26,7 @@ int main() { Test_RunTime("List", Test_List); Test_RunTime("Queue", Test_Queue); // Test_RunTime("Task", Test_task); - Test_RunTime("Key", Test_Key); + Test_RunTime("OLED", Test_OLED); +// Test_RunTime("Key", Test_Key); return 0; }