UP 增加OLED模拟器

main
JiXieShi 2024-06-22 19:13:18 +08:00
parent 453538b610
commit eb2101c4d0
6 changed files with 196 additions and 34 deletions

View File

@ -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)

View File

@ -3,23 +3,27 @@
#include "log.h"
#include "tool.h"
#include "t_oled.h"
#include "sim_oled.h"
#include <windows.h>
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();
}

View File

@ -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

View File

@ -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;

54
sim/oled/oled.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "sim_oled.h"
#include "graphics.h"
#include <conio.h>
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
}
}
}
}

51
sim/oled/sim_oled.h Normal file
View File

@ -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