UP 通用OLED
This commit is contained in:
193
lib/src/oled/oled.cpp
Normal file
193
lib/src/oled/oled.cpp
Normal file
@@ -0,0 +1,193 @@
|
||||
#include "oled.h"
|
||||
#include "oled_font.h"
|
||||
|
||||
#define BUFPOINT(x, i) (*(dev->buf + x + (i * 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;
|
||||
}
|
||||
dev->state = IDLE;
|
||||
}
|
||||
|
||||
void OLED_ON(OLED_T *dev) {
|
||||
uint8_t cmd[3] = {0x8D, 0x14, 0xAF};
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = WRITE;
|
||||
dev->cmd(cmd, 3);
|
||||
dev->state = IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_OFF(OLED_T *dev) {
|
||||
uint8_t cmd[3] = {0x8D, 0x10, 0xAE};
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = WRITE;
|
||||
dev->cmd(cmd, 3);
|
||||
dev->state = IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_Turn(OLED_T *dev, bool e) {
|
||||
uint8_t cmd = 0xA6 + e;
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = WRITE;
|
||||
dev->cmd(&cmd, 1);
|
||||
dev->state = IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_Refresh(OLED_T *dev) {
|
||||
uint8_t i, cmd[3] = {0xb0, 0x00, 0x10};
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = REFRESH;
|
||||
for (i = 0; i < (dev->height >> 3); i++) {
|
||||
cmd[0] = 0xb0 + i;
|
||||
dev->cmd(cmd, 3);
|
||||
dev->data(dev->buf + (i * dev->width), dev->width);
|
||||
}
|
||||
#if REFRESH_CALL_ENABLE
|
||||
dev->call();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_DisplayTurn(OLED_T *dev, bool e) {
|
||||
uint8_t cmd[2];
|
||||
if (e) {
|
||||
cmd[0] = 0xC8;
|
||||
cmd[1] = 0xA1;
|
||||
|
||||
} else {
|
||||
cmd[0] = 0xC0;
|
||||
cmd[1] = 0xA0;
|
||||
}
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = WRITE;
|
||||
dev->cmd(cmd, 1);
|
||||
dev->state = IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_Set(OLED_T *dev, uint8_t x, uint8_t y) {
|
||||
uint8_t i, m, n;
|
||||
i = y / 8;
|
||||
m = y % 8;
|
||||
n = 1 << m;
|
||||
BUFPOINT(x, i) |= n;
|
||||
}
|
||||
|
||||
void OLED_RSet(OLED_T *dev, uint8_t x, uint8_t y) {
|
||||
uint8_t i, m, n;
|
||||
i = y / 8;
|
||||
m = y % 8;
|
||||
n = 1 << m;
|
||||
BUFPOINT(x, i) = ~BUFPOINT(x, i);
|
||||
BUFPOINT(x, i) |= n;
|
||||
BUFPOINT(x, i) = ~BUFPOINT(x, i);
|
||||
}
|
||||
|
||||
void OLED_DrawLine(OLED_T *dev, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) {
|
||||
uint8_t i, k, k1, k2;
|
||||
if ((x1 < 0) || (x2 > dev->width) || (y1 < 0) || (y2 > dev->height) || (x1 > x2) || (y1 > y2))return;
|
||||
if (x1 == x2) //画竖线
|
||||
{
|
||||
for (i = 0; i < (y2 - y1); i++) {
|
||||
OLED_Set(dev, x1, y1 + i);
|
||||
}
|
||||
} else if (y1 == y2) //画横线
|
||||
{
|
||||
for (i = 0; i < (x2 - x1); i++) {
|
||||
OLED_Set(dev, x1 + i, y1);
|
||||
}
|
||||
} else //画斜线
|
||||
{
|
||||
k1 = y2 - y1;
|
||||
k2 = x2 - x1;
|
||||
k = k1 * 10 / k2;
|
||||
for (i = 0; i < (x2 - x1); i++) {
|
||||
OLED_Set(dev, x1 + i, y1 + i * k / 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_DrawCircle(OLED_T *dev, uint8_t x, uint8_t y, uint8_t r) {
|
||||
int a, b, num;
|
||||
a = 0;
|
||||
b = r;
|
||||
while (2 * b * b >= r * r) {
|
||||
OLED_Set(dev, x + a, y - b);
|
||||
OLED_Set(dev, x - a, y - b);
|
||||
OLED_Set(dev, x - a, y + b);
|
||||
OLED_Set(dev, x + a, y + b);
|
||||
|
||||
OLED_Set(dev, x + b, y + a);
|
||||
OLED_Set(dev, x + b, y - a);
|
||||
OLED_Set(dev, x - b, y - a);
|
||||
OLED_Set(dev, x - b, y + a);
|
||||
|
||||
a++;
|
||||
num = (a * a + b * b) - r * r;//计算画的点离圆心的距离
|
||||
if (num > 0) {
|
||||
b--;
|
||||
a--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_ShowChar(OLED_T *dev, uint8_t x, uint8_t y, uint8_t chr, uint8_t size1) {
|
||||
uint8_t i, m, temp, size2, chr1;
|
||||
uint8_t y0 = y;
|
||||
size2 = (size1 / 8 + ((size1 % 8) ? 1 : 0)) * (size1 / 2); //得到字体一个字符对应点阵集所占的字节数
|
||||
chr1 = chr - ' '; //计算偏移后的值
|
||||
for (i = 0; i < size2; i++) {
|
||||
if (size1 == 12) { temp = asc2_1206[chr1][i]; } //调用1206字体
|
||||
else if (size1 == 16) { temp = asc2_1608[chr1][i]; } //调用1608字体
|
||||
else if (size1 == 24) { temp = asc2_2412[chr1][i]; } //调用2412字体
|
||||
else return;
|
||||
for (m = 0; m < 8; m++) //写入数据
|
||||
{
|
||||
if (temp & 0x80)OLED_Set(dev, x, y);
|
||||
else OLED_RSet(dev, x, y);
|
||||
temp <<= 1;
|
||||
y++;
|
||||
if ((y - y0) == size1) {
|
||||
y = y0;
|
||||
x++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_ShowString(OLED_T *dev, uint8_t x, uint8_t y, uint8_t *chr, uint8_t size1) {
|
||||
while ((*chr >= ' ') && (*chr <= '~'))//判断是不是非法字符!
|
||||
{
|
||||
OLED_ShowChar(dev, x, y, *chr, size1);
|
||||
x += size1 / 2;
|
||||
if (x > dev->width - size1) //换行
|
||||
{
|
||||
x = 0;
|
||||
y += 2;
|
||||
}
|
||||
chr++;
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_SPos(OLED_T *dev, uint8_t x, uint8_t y) {
|
||||
uint8_t cmd[3];
|
||||
cmd[0] = (uint8_t) 0xb0 + y;
|
||||
cmd[1] = ((x & 0xf0) >> 4) | 0x10;
|
||||
cmd[2] = (x & 0x0f) | 0x01;
|
||||
if (dev->state == IDLE) {
|
||||
dev->state = WRITE;
|
||||
dev->cmd(cmd, 3);
|
||||
dev->state = IDLE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user