491 lines
12 KiB
C
491 lines
12 KiB
C
#include "oled.h"
|
||
#include "main.h"
|
||
#include "i2c.h"
|
||
#include "app.h"
|
||
#include <stdio.h>
|
||
#include "string.h"
|
||
#include "MyLib.h"
|
||
|
||
#define OLED_ADDRESS 0x78 //电阻焊在了0x3C,左移一位得到0x78
|
||
// OLED参数
|
||
#define OLED_PAGE 8 // OLED页数
|
||
#define OLED_ROW 8 * OLED_PAGE // OLED行数
|
||
#define OLED_COLUMN 128 // OLED列数
|
||
|
||
// 显存
|
||
uint8_t OLED_GRAM[OLED_PAGE][OLED_COLUMN];
|
||
|
||
void OLED_Send(uint8_t *data, uint8_t len)//发送数据 8bit * len
|
||
{
|
||
HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDRESS, data, len, 20);//1000
|
||
}
|
||
|
||
void OLED_SendCmd(uint8_t cmd)//发送命令 [0x00;cmd]
|
||
{
|
||
static uint8_t sendBuffer[2] = {0};
|
||
sendBuffer[1] = cmd;
|
||
OLED_Send(sendBuffer, 2);
|
||
}
|
||
|
||
|
||
//OLED初始化
|
||
void OLED_Init(void) {
|
||
|
||
OLED_SendCmd(0xAE); /*关闭显示 display off*/
|
||
|
||
OLED_SendCmd(0x20);//设置显示模式
|
||
OLED_SendCmd(0x10);//设置为页显示模式
|
||
OLED_SendCmd(0xB0);//设置起始页的地址模式0-7
|
||
|
||
OLED_SendCmd(0xC0);// 0xc0上下反置 0xc8正常
|
||
|
||
OLED_SendCmd(0x00);
|
||
OLED_SendCmd(0x10);
|
||
|
||
OLED_SendCmd(0x40);//--set start line address,从RAM中哪一行起读取显示内容
|
||
|
||
OLED_SendCmd(0x81);//设置对比度
|
||
|
||
OLED_SendCmd(0xFF);//亮度调节 0x00~0xff
|
||
OLED_SendCmd(0xA0);// 0xa0左右反置 0xa1正常
|
||
|
||
OLED_SendCmd(0xA6);//设置显示方式;bit0:1,反相显示;0,正常显示
|
||
OLED_SendCmd(0xA8);
|
||
|
||
OLED_SendCmd(0x3F);
|
||
|
||
OLED_SendCmd(0xA4);//全局显示开启;0xa4正常,0xa5无视命令点亮全屏
|
||
|
||
OLED_SendCmd(0xD3);
|
||
OLED_SendCmd(0x00);
|
||
|
||
OLED_SendCmd(0xD5);
|
||
OLED_SendCmd(0xF0);
|
||
|
||
OLED_SendCmd(0xD9);
|
||
OLED_SendCmd(0x22);
|
||
|
||
OLED_SendCmd(0xDA);
|
||
OLED_SendCmd(0x12);
|
||
|
||
OLED_SendCmd(0xDB);
|
||
OLED_SendCmd(0x20);
|
||
|
||
OLED_SendCmd(0x8D);//设置电荷泵开关
|
||
OLED_SendCmd(0x14);//开
|
||
|
||
OLED_NewFrame();
|
||
OLED_ShowFrame();
|
||
|
||
OLED_SendCmd(0xAF); /*开启显示 display ON*/
|
||
|
||
}
|
||
|
||
|
||
|
||
//开启OLED显示
|
||
void OLED_DisPlay_On()
|
||
{
|
||
OLED_SendCmd(0x8D); // 电荷泵使能
|
||
OLED_SendCmd(0x14); // 开启电荷泵
|
||
OLED_SendCmd(0xAF); // 点亮屏幕
|
||
}
|
||
|
||
|
||
//关闭OLED显示
|
||
void OLED_DisPlay_Off()
|
||
{
|
||
OLED_SendCmd(0x8D); // 电荷泵使能
|
||
OLED_SendCmd(0x10); // 关闭电荷泵
|
||
OLED_SendCmd(0xAE); // 关闭屏幕
|
||
}
|
||
|
||
|
||
//设置颜色模式 黑底白字或白底黑字
|
||
//颜色模式COLOR_NORMAL(黑底白字) / COLOR_REVERSED(白底黑字)
|
||
void OLED_SetColorMode(OLED_ColorMode mode)
|
||
{
|
||
if (mode == OLED_COLOR_NORMAL)
|
||
{
|
||
OLED_SendCmd(0xA6); // 正常显示
|
||
}
|
||
if (mode == OLED_COLOR_REVERSED)
|
||
{
|
||
OLED_SendCmd(0xA7); // 反色显示
|
||
}
|
||
}
|
||
|
||
|
||
/**********************显存操作函数begin*************************/
|
||
//清空显存 绘制新的一帧
|
||
void OLED_NewFrame()
|
||
{
|
||
memset(OLED_GRAM, 0, sizeof(OLED_GRAM));
|
||
}
|
||
|
||
//将当前显存显示到屏幕上
|
||
void OLED_ShowFrame()
|
||
{
|
||
static uint8_t sendBuffer[OLED_COLUMN + 1];
|
||
sendBuffer[0] = 0x40;
|
||
for (uint8_t i = 0; i < OLED_PAGE; i++)
|
||
{
|
||
OLED_SendCmd(0xB0 + i); // 设置页地址
|
||
OLED_SendCmd(0x00); // 设置列地址低4位
|
||
OLED_SendCmd(0x10); // 设置列地址高4位
|
||
memcpy(sendBuffer + 1, OLED_GRAM[i], OLED_COLUMN);
|
||
OLED_Send(sendBuffer, OLED_COLUMN + 1);
|
||
}
|
||
}
|
||
|
||
//将当前显存显示到屏幕上
|
||
void OLED_ShowPageFrame(uint8_t page)
|
||
{
|
||
static uint8_t sendBuffer[OLED_COLUMN + 1];
|
||
sendBuffer[0] = 0x40;
|
||
{
|
||
OLED_SendCmd(0xB0 + page); // 设置页地址
|
||
OLED_SendCmd(0x00); // 设置列地址低4位
|
||
OLED_SendCmd(0x10); // 设置列地址高4位
|
||
memcpy(sendBuffer + 1, OLED_GRAM[page], OLED_COLUMN);
|
||
OLED_Send(sendBuffer, OLED_COLUMN + 1);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 设置一个像素点
|
||
* @param x 横坐标
|
||
* @param y 纵坐标
|
||
* @param color 颜色
|
||
*/
|
||
void OLED_SetPixel(uint8_t x, uint8_t y, OLED_ColorMode color)
|
||
{
|
||
if (x >= OLED_COLUMN || y >= OLED_ROW)
|
||
return;
|
||
if (!color)
|
||
{
|
||
OLED_GRAM[y / 8][x] |= 1 << (y % 8);
|
||
}
|
||
else
|
||
{
|
||
OLED_GRAM[y / 8][x] &= ~(1 << (y % 8));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 设置显存中一字节数据的某几位
|
||
* @param page 页地址
|
||
* @param column 列地址
|
||
* @param data 数据
|
||
* @param start 起始位
|
||
* @param end 结束位
|
||
* @param color 颜色
|
||
* @note 此函数将显存中的某一字节的第start位到第end位设置为与data相同
|
||
* @note start和end的范围为0-7, start必须小于等于end
|
||
* @note 此函数与OLED_SetByte_Fine的区别在于此函数只能设置显存中的某一真实字节
|
||
*/
|
||
void OLED_SetByte_Fine(uint8_t page, uint8_t column, uint8_t data, uint8_t start, uint8_t end, OLED_ColorMode color)
|
||
{
|
||
static uint8_t temp;
|
||
if (page >= OLED_PAGE || column >= OLED_COLUMN)
|
||
return;
|
||
if (color)
|
||
data = ~data;
|
||
|
||
temp = data | (0xff << (end + 1)) | (0xff >> (8 - start));
|
||
OLED_GRAM[page][column] &= temp;
|
||
temp = data & ~(0xff << (end + 1)) & ~(0xff >> (8 - start));
|
||
OLED_GRAM[page][column] |= temp;
|
||
// 使用OLED_SetPixel实现
|
||
// for (uint8_t i = start; i <= end; i++) {
|
||
// OLED_SetPixel(column, page * 8 + i, !((data >> i) & 0x01));
|
||
// }
|
||
}
|
||
|
||
/**
|
||
* @brief 设置显存中的一字节数据
|
||
* @param page 页地址
|
||
* @param column 列地址
|
||
* @param data 数据
|
||
* @param color 颜色
|
||
* @note 此函数将显存中的某一字节设置为data的值
|
||
*/
|
||
void OLED_SetByte(uint8_t page, uint8_t column, uint8_t data, OLED_ColorMode color)
|
||
{
|
||
if (page >= OLED_PAGE || column >= OLED_COLUMN)
|
||
return;
|
||
if (color)
|
||
data = ~data;
|
||
OLED_GRAM[page][column] = data;
|
||
}
|
||
|
||
/**
|
||
* @brief 设置显存中的一字节数据的某几位
|
||
* @param x 横坐标
|
||
* @param y 纵坐标
|
||
* @param data 数据
|
||
* @param len 位数
|
||
* @param color 颜色
|
||
* @note 此函数将显存中从(x,y)开始向下数len位设置为与data相同
|
||
* @note len的范围为1-8
|
||
* @note 此函数与OLED_SetByte_Fine的区别在于此函数的横坐标和纵坐标是以像素为单位的, 可能出现跨两个真实字节的情况(跨页)
|
||
*/
|
||
void OLED_SetBits_Fine(uint8_t x, uint8_t y, uint8_t data, uint8_t len, OLED_ColorMode color)
|
||
{
|
||
uint8_t page = y / 8;
|
||
uint8_t bit = y % 8;
|
||
if (bit + len > 8)
|
||
{
|
||
OLED_SetByte_Fine(page, x, data << bit, bit, 7, color);
|
||
OLED_SetByte_Fine(page + 1, x, data >> (8 - bit), 0, len + bit - 1 - 8, color);
|
||
}
|
||
else
|
||
{
|
||
OLED_SetByte_Fine(page, x, data << bit, bit, bit + len - 1, color);
|
||
}
|
||
// 使用OLED_SetPixel实现
|
||
// for (uint8_t i = 0; i < len; i++) {
|
||
// OLED_SetPixel(x, y + i, !((data >> i) & 0x01));
|
||
// }
|
||
}
|
||
|
||
/**
|
||
* @brief 设置显存中一字节长度的数据
|
||
* @param x 横坐标
|
||
* @param y 纵坐标
|
||
* @param data 数据
|
||
* @param color 颜色
|
||
* @note 此函数将显存中从(x,y)开始向下数8位设置为与data相同
|
||
* @note 此函数与OLED_SetByte的区别在于此函数的横坐标和纵坐标是以像素为单位的, 可能出现跨两个真实字节的情况(跨页)
|
||
*/
|
||
void OLED_SetBits(uint8_t x, uint8_t y, uint8_t data, OLED_ColorMode color)
|
||
{
|
||
uint8_t page = y / 8;
|
||
uint8_t bit = y % 8;
|
||
OLED_SetByte_Fine(page, x, data << bit, bit, 7, color);
|
||
if (bit)
|
||
{
|
||
OLED_SetByte_Fine(page + 1, x, data >> (8 - bit), 0, bit - 1, color);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 设置一块显存区域
|
||
* @param x 起始横坐标
|
||
* @param y 起始纵坐标
|
||
* @param data 数据的起始地址
|
||
* @param w 宽度
|
||
* @param h 高度
|
||
* @param color 颜色
|
||
* @note 此函数将显存中从(x,y)开始的w*h个像素设置为data中的数据
|
||
* @note data的数据应该采用列行式排列
|
||
*/
|
||
void OLED_SetBlock(uint8_t x, uint8_t y, const uint8_t *data, uint8_t w, uint8_t h, OLED_ColorMode color)
|
||
{
|
||
uint8_t fullRow = h / 8; // 完整的行数
|
||
uint8_t partBit = h % 8; // 不完整的字节中的有效位数
|
||
for (uint8_t i = 0; i < w; i++)
|
||
{
|
||
for (uint8_t j = 0; j < fullRow; j++)
|
||
{
|
||
OLED_SetBits(x + i, y + j * 8, data[i + j * w], color);
|
||
}
|
||
}
|
||
if (partBit)
|
||
{
|
||
uint16_t fullNum = w * fullRow; // 完整的字节数
|
||
for (uint8_t i = 0; i < w; i++)
|
||
{
|
||
OLED_SetBits_Fine(x + i, y + (fullRow * 8), data[fullNum + i], partBit, color);
|
||
}
|
||
}
|
||
// 使用OLED_SetPixel实现
|
||
// for (uint8_t i = 0; i < w; i++) {
|
||
// for (uint8_t j = 0; j < h; j++) {
|
||
// for (uint8_t k = 0; k < 8; k++) {
|
||
// if (j * 8 + k >= h) break; // 防止越界(不完整的字节
|
||
// OLED_SetPixel(x + i, y + j * 8 + k, !((data[i + j * w] >> k) & 0x01));
|
||
// }
|
||
// }
|
||
// }
|
||
}
|
||
/**********************显存操作函数end*************************/
|
||
|
||
// ================================ 文字绘制 ================================
|
||
|
||
/**
|
||
* @brief 绘制一个ASCII字符
|
||
* @param x 起始点横坐标
|
||
* @param y 起始点纵坐标
|
||
* @param ch 字符
|
||
* @param font 字体
|
||
* @param color 颜色
|
||
*/
|
||
void OLED_PrintASCIIChar(uint8_t x, uint8_t y, char ch, const ASCIIFont *font, OLED_ColorMode color)
|
||
{
|
||
OLED_SetBlock(x, y, font->chars + (ch - ' ') * (((font->h + 7) / 8) * font->w), font->w, font->h, color);
|
||
}
|
||
|
||
/**
|
||
* @brief 绘制一个ASCII字符串
|
||
* @param x 起始点横坐标
|
||
* @param y 起始点纵坐标
|
||
* @param str 字符串
|
||
* @param font 字体
|
||
* @param color 颜色
|
||
*/
|
||
void OLED_PrintASCIIString(uint8_t x, uint8_t y, char *str, const ASCIIFont *font, OLED_ColorMode color)
|
||
{
|
||
uint8_t x0 = x;
|
||
while (*str)
|
||
{
|
||
OLED_PrintASCIIChar(x0, y, *str, font, color);
|
||
x0 += font->w;
|
||
str++;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 获取UTF-8编码的字符长度
|
||
*/
|
||
uint8_t _OLED_GetUTF8Len(char *string)
|
||
{
|
||
if ((string[0] & 0x80) == 0x00)
|
||
{
|
||
return 1;
|
||
}
|
||
else if ((string[0] & 0xE0) == 0xC0)
|
||
{
|
||
return 2;
|
||
}
|
||
else if ((string[0] & 0xF0) == 0xE0)
|
||
{
|
||
return 3;
|
||
}
|
||
else if ((string[0] & 0xF8) == 0xF0)
|
||
{
|
||
return 4;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* @brief 绘制字符串
|
||
* @param x 起始点横坐标
|
||
* @param y 起始点纵坐标
|
||
* @param str 字符串
|
||
* @param font 字体
|
||
* @param color 颜色
|
||
*
|
||
* @note 为保证字符串中的中文会被自动识别并绘制, 需:
|
||
* 1. 编译器字符集设置为UTF-8
|
||
* 2. 使用波特律动LED取模工具生成字模(https://led.baud-dance.com)
|
||
*/
|
||
/**
|
||
* @brief 绘制字符串
|
||
* @param x 起始点横坐标
|
||
* @param y 起始点纵坐标
|
||
* @param str 字符串
|
||
* @param font 字体
|
||
* @param color 颜色
|
||
*
|
||
* @note 为保证字符串中的中文会被自动识别并绘制, 需:
|
||
* 1. 编译器字符集设置为UTF-8
|
||
* 2. 使用波特律动LED取模工具生成字模(https://led.baud-dance.com)
|
||
*/
|
||
void OLED_PrintString(uint8_t x, uint8_t y, char *str, const Font *font, OLED_ColorMode color)
|
||
{
|
||
uint16_t i = 0; // 字符串索引
|
||
uint8_t oneLen = (((font->h + 7) / 8) * font->w) + 4; // 一个字模占多少字节
|
||
uint8_t found; // 是否找到字模
|
||
uint8_t utf8Len; // UTF-8编码长度
|
||
uint8_t *head; // 字模头指针
|
||
while (str[i])
|
||
{
|
||
found = 0;
|
||
utf8Len = _OLED_GetUTF8Len(str + i);
|
||
if (utf8Len == 0)
|
||
break; // 有问题的UTF-8编码
|
||
|
||
// 寻找字符 TODO 优化查找算法, 二分查找或者hash
|
||
for (uint8_t j = 0; j < font->len; j++)
|
||
{
|
||
head = (uint8_t *)(font->chars) + (j * oneLen);
|
||
if (memcmp(str + i, head, utf8Len) == 0)
|
||
{
|
||
OLED_SetBlock(x, y, head + 4, font->w, font->h, color);
|
||
// 移动光标
|
||
x += font->w;
|
||
i += utf8Len;
|
||
found = 1;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 若未找到字模,且为ASCII字符, 则缺省显示ASCII字符
|
||
if (found == 0)
|
||
{
|
||
if (utf8Len == 1)
|
||
{
|
||
OLED_PrintASCIIChar(x, y, str[i], font->ascii, color);
|
||
// 移动光标
|
||
x += font->ascii->w;
|
||
i += utf8Len;
|
||
}
|
||
else
|
||
{
|
||
OLED_PrintASCIIChar(x, y, ' ', font->ascii, color);
|
||
x += font->ascii->w;
|
||
i += utf8Len;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void OLED_Act(void)
|
||
{
|
||
float v = 0;
|
||
// if(it_1000ms_flag == 1)
|
||
// {
|
||
// it_1000ms_flag = 0;
|
||
// HAL_GPIO_TogglePin(LED_NOR_GPIO_Port,LED_NOR_Pin);//功能指示灯
|
||
// }
|
||
|
||
//OLED_NewFrame();//清除显存
|
||
|
||
//OLED_PrintASCIIChar(64, 32, '1', &afont16x8, OLED_COLOR_NORMAL); //ASCII单字符示例
|
||
//OLED_PrintASCIIString(64, 32, "123456", &afont16x8, OLED_COLOR_NORMAL); //ASCII字符串示例
|
||
//OLED_PrintString(20,20 , , &afont16x8, OLED_COLOR_NORMAL);
|
||
|
||
OLED_PrintASCIIString(30, 0 , "Vol", &afont24x12, OLED_COLOR_NORMAL);
|
||
// OLED_ShowPageFrame(0);
|
||
// OLED_ShowPageFrame(1);
|
||
// OLED_ShowPageFrame(2);
|
||
|
||
char str[20]; //
|
||
// for(i = 0;i < 15;i++)str[i] = 0x20;
|
||
// str[i] = 0x00;
|
||
// OLED_PrintASCIIString(30, 32, str, &afont24x12, OLED_COLOR_NORMAL);
|
||
|
||
v = tempA;
|
||
v /= 10;
|
||
sprintf(str, "%.1f",v ); // 将数转换为字符串
|
||
OLED_PrintASCIIString(30, 32, str, &afont24x12, OLED_COLOR_NORMAL);
|
||
|
||
// OLED_ShowPageFrame(0);
|
||
// OLED_ShowPageFrame(1);
|
||
// OLED_ShowPageFrame(2);
|
||
//
|
||
// OLED_ShowPageFrame(4);
|
||
// OLED_ShowPageFrame(5);
|
||
// OLED_ShowPageFrame(6);
|
||
|
||
|
||
OLED_ShowFrame();//显示当前显存内容
|
||
}
|
||
|
||
|
||
|
||
|