diff --git a/CMakeLists.txt b/CMakeLists.txt index a62971c..0e907a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,14 +10,17 @@ foreach (DEMO_SUBDIR ${DEMO_SUBDIRS}) include_directories(${DEMO_SUBDIR}) endforeach () -file(GLOB_RECURSE SOURCES "demo/*/*.*") - - -#add_library(HW_LIB_List HW_LIB_Task HW_LIB_Printf HW_LIB_Utils HW_LIB_Iic HW_LIB_Spi HW_LIB_Key) +file(GLOB SIM_SUBDIRS "sim/*") +foreach (SIM_SUBDIR ${SIM_SUBDIRS}) + include_directories(${SIM_SUBDIR}) +endforeach () +include_directories(easyx/include) +link_directories(easyx/lib64) +file(GLOB_RECURSE SOURCES "demo/*/*.*" "sim/*/*.*") +link_libraries(libeasyx.a libgdi32.a libole32.a) 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 HW_LIB_Oled) +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) \ No newline at end of file diff --git a/demo/oled/test.c b/demo/oled/test.c index 483d244..36b1f05 100644 --- a/demo/oled/test.c +++ b/demo/oled/test.c @@ -3,23 +3,27 @@ #include "log.h" #include "tool.h" #include "t_oled.h" +#include "sim_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, 128); + Buf_Print("Data", data, l, 16); } void Refresh_Call(OLED_T *dev) { LOGT("OLED", "CALL"); - BufPrint("Buf", dev->buf, T_U8, dev->width * (dev->height / 8), 16); + Buf_Print("Buf", dev->buf, dev->width * (dev->height / 8), 128); + SIM_OLED_DrawFromBuffer(dev->buf, dev->width, dev->height / 8); } uint8_t oledbuf[8][128] = {0}; void Test_OLED() { + SIM_OLED_INIT(0xFFFFFF, 0x0, 10, true); OLED_T oled = { .height=64, .width=128, @@ -31,6 +35,17 @@ void Test_OLED() { }; OLED_Init(&oled); OLED_CLS(&oled); - OLED_ShowString(&oled, 0, 0, "Hello", 12); - OLED_Refresh(&oled); + OLED_DrawRect(&oled, 0, 0, 127, 63); + OLED_ShowString(&oled, 12, 24, "Hello JXS", 16); + SIM_OLED_START(); + int s = 0; + char buf[30]; + while (s < 500) { + sprintf(buf, "DATA:%d", s); + OLED_ShowString(&oled, 2, 2, buf, 12); + OLED_Refresh(&oled); + s++; + Sleep(500); + } + SIM_OLED_STOP(); } \ No newline at end of file diff --git a/lib/inc/oled/oled.h b/lib/inc/oled/oled.h index b346dd2..303c562 100644 --- a/lib/inc/oled/oled.h +++ b/lib/inc/oled/oled.h @@ -32,10 +32,12 @@ typedef uint8_t (*OLED_CMD_t)(uint8_t *data, size_t len); typedef uint8_t (*OLED_DATA_t)(uint8_t *data, size_t len); #if REFRESH_CALL_ENABLE + /** * @brief OLED刷新函数指针类型 */ typedef void (*OLED_REFRESH_t)(OLED_T *dev); + #endif /** @@ -63,22 +65,39 @@ struct OLED_Dev { }; // OLED初始化指令数组 +//const uint8_t initCmd[] = { +// 1, 0xAE, // 关闭显示 +// 2, 0xD5, 0x80, // 设置显示时钟分频比/振荡器频率 +// 2, 0xA8, 0x3F, // 设置多路复用比率 +// 2, 0xD3, 0x00, // 设置显示偏移 +// 2, 0x40, 0x00, // 设置显示起始行 +// 2, 0x8D, 0x14, // 电荷泵设置 +// 2, 0x20, 0x00, // 设置内存寻址模式 +// 1, 0xA0, // 设置段重映射 +// 1, 0xC0, // 设置COM输出扫描方向 +// 2, 0xDA, 0x12, // 设置COM引脚硬件配置 +// 2, 0x81, 0xCF, // 设置对比度控制 +// 2, 0xD9, 0xF1, // 设置预充电周期 +// 2, 0xDB, 0x20, // 设置VCOMH取消电平 +// 2, 0xA4, 0xA6, // 整个显示打开 +// 1, 0xAF // 打开显示 +//}; const uint8_t initCmd[] = { - 1, 0xAE, // 关闭显示 - 2, 0xD5, 0x80, // 设置显示时钟分频比/振荡器频率 - 2, 0xA8, 0x3F, // 设置多路复用比率 - 2, 0xD3, 0x00, // 设置显示偏移 - 2, 0x40, 0x00, // 设置显示起始行 - 2, 0x8D, 0x14, // 电荷泵设置 - 2, 0x20, 0x00, // 设置内存寻址模式 - 1, 0xA0, // 设置段重映射 - 1, 0xC0, // 设置COM输出扫描方向 - 2, 0xDA, 0x12, // 设置COM引脚硬件配置 - 2, 0x81, 0xCF, // 设置对比度控制 - 2, 0xD9, 0xF1, // 设置预充电周期 - 2, 0xDB, 0x20, // 设置VCOMH取消电平 - 2, 0xA4, 0xA6, // 整个显示打开 - 1, 0xAF // 打开显示 + 0xAE, // 关闭显示 + 0xD5, 0x80, // 设置显示时钟分频比/振荡器频率 + 0xA8, 0x3F, // 设置多路复用比率 + 0xD3, 0x00, // 设置显示偏移 + 0x40, 0x00, // 设置显示起始行 + 0x8D, 0x14, // 电荷泵设置 + 0x20, 0x00, // 设置内存寻址模式 + 0xA0, // 设置段重映射 + 0xC0, // 设置COM输出扫描方向 + 0xDA, 0x12, // 设置COM引脚硬件配置 + 0x81, 0xCF, // 设置对比度控制 + 0xD9, 0xF1, // 设置预充电周期 + 0xDB, 0x20, // 设置VCOMH取消电平 + 0xA4, 0xA6, // 整个显示打开 + 0xAF // 打开显示 }; /** @@ -163,6 +182,18 @@ void OLED_RSet(OLED_T *dev, uint8_t x, uint8_t y); */ void OLED_DrawLine(OLED_T *dev, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2); +/** + * @brief 在 OLED 上绘制矩形 + * @param dev: [输入] OLED 设备指针 + * @param x1: [输入] 矩形左上角 x 坐标 + * @param y1: [输入] 矩形左上角 y 坐标 + * @param x2: [输入] 矩形右下角 x 坐标 + * @param y2: [输入] 矩形右下角 y 坐标 + * @return void + * @example OLED_DrawRect(&oledDevice, 10, 10, 50, 30); + **/ +void OLED_DrawRect(OLED_T *dev, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2); + /** * @brief 绘制圆 * @param dev: [输入] OLED设备指针 diff --git a/lib/src/oled/oled.cpp b/lib/src/oled/oled.cpp index 1b1edf0..93f830e 100644 --- a/lib/src/oled/oled.cpp +++ b/lib/src/oled/oled.cpp @@ -1,18 +1,19 @@ #include "oled.h" #include "oled_font.h" -#define BUFPOINT(x, i) (*(dev->buf + x + (i * dev->width))) +#define BUFPOINT(x, y) (*(dev->buf + x + (y * dev->width))) void OLED_Init(OLED_T *dev) { uint8_t *cmdIndex = (uint8_t *) initCmd; - uint8_t count, temp; - while (*cmdIndex) { - temp = *cmdIndex++; - count = temp & 0x7F; - - dev->cmd(cmdIndex, count); - cmdIndex += count; - } +// uint8_t count, temp; +// while (*cmdIndex) { +// temp = *cmdIndex++; +// count = temp & 0x7F; +// +// dev->cmd(cmdIndex, count); +// cmdIndex += count; +// } + dev->cmd(cmdIndex, sizeof(initCmd)); dev->state = IDLE; } @@ -119,6 +120,13 @@ void OLED_DrawLine(OLED_T *dev, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) } } +void OLED_DrawRect(OLED_T *dev, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) { + OLED_DrawLine(dev, x1, y1, x2, y1); // Top side + OLED_DrawLine(dev, x1, y1, x1, y2); // Left side + OLED_DrawLine(dev, x2, y1, x2, y2); // Right side + OLED_DrawLine(dev, x1, y2, x2, y2); // Bottom side +} + void OLED_DrawCircle(OLED_T *dev, uint8_t x, uint8_t y, uint8_t r) { int a, b, num; a = 0; diff --git a/sim/oled/oled.cpp b/sim/oled/oled.cpp new file mode 100644 index 0000000..60524af --- /dev/null +++ b/sim/oled/oled.cpp @@ -0,0 +1,54 @@ +#include "sim_oled.h" +#include "graphics.h" +#include + +static uint32_t pixelColor, backgroundColor; +static int scaleFactor; +static bool border; + +void SIM_OLED_INIT(uint32_t pixcolor, uint32_t backcolor, int scale, bool b) { + pixelColor = pixcolor; + backgroundColor = backcolor; + scaleFactor = scale; + border = b; +} + +void SIM_OLED_START() { + initgraph(WIDTH, HEIGHT); + setbkcolor(backgroundColor); // 设置背景色为黑色 + setfillcolor(pixelColor); + cleardevice(); // 清空屏幕 +} + +void SIM_OLED_STOP() { + getch(); + closegraph(); +} + +void drawOledPixel(int oledX, int oledY, int blockSize) { + int startX = oledX * blockSize; + int startY = oledY * blockSize; + if (border) + fillrectangle(startX, startY, startX + blockSize, startY + blockSize); + if (!border) + solidrectangle(startX, startY, startX + blockSize, startY + blockSize); +} + +#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit) + +void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height) { + cleardevice(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + uint8_t byteData = buf[y * width + x]; + for (int i = 0; i < 8; i++) { + uint8_t bit = GET_BIT(byteData, i); + if (bit) + drawOledPixel(x, y * 8 + (i), scaleFactor); // 这里假设 OLED 像素点大小为一个bit,对应的 EasyX 像素块大小为 10x10 + } + } + } +} + + + diff --git a/sim/oled/sim_oled.h b/sim/oled/sim_oled.h new file mode 100644 index 0000000..e01cb16 --- /dev/null +++ b/sim/oled/sim_oled.h @@ -0,0 +1,51 @@ +#ifndef HW_LIB_SIM_OLED_H +#define HW_LIB_SIM_OLED_H +#ifdef __cplusplus +extern "C" { +#endif + +#include "stdint.h" + +#define WIDTH 1280 +#define HEIGHT 640 + +/** + * @brief 初始化模拟 OLED 显示 + * @param pixcolor: [输入] 像素颜色 + * @param backcolor: [输入] 背景颜色 + * @param scale: [输入] 显示缩放比例 + * @param border: [输入] 是否显示边框 + * @return void + * @example SIM_OLED_INIT(0xFFFF00, 0x000000, 2, true); + **/ +void SIM_OLED_INIT(uint32_t pixcolor, uint32_t backcolor, int scale, bool border); + +/** + * @brief 开始模拟 OLED 显示 + * @return void + * @example SIM_OLED_START(); + **/ +void SIM_OLED_START(); + +/** + * @brief 停止模拟 OLED 显示 + * @return void + * @example SIM_OLED_STOP(); + **/ +void SIM_OLED_STOP(); + +/** + * @brief 从缓冲区绘制到模拟 OLED 显示 + * @param buf: [输入] 缓冲区指针 + * @param width: [输入] 图像宽度 + * @param height: [输入] 图像高度 + * @return void + * @example SIM_OLED_DrawFromBuffer(buffer, 128, 64); + **/ +void SIM_OLED_DrawFromBuffer(uint8_t *buf, uint8_t width, uint8_t height); + +#ifdef __cplusplus +} +#endif + +#endif //HW_LIB_SIM_OLED_H