mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-04-01 05:17:13 +08:00
疯狂写bsp
This commit is contained in:
BIN
assets/User_code/device/.DS_Store
vendored
BIN
assets/User_code/device/.DS_Store
vendored
Binary file not shown.
@@ -1,301 +0,0 @@
|
||||
#include "bmp280_i2c.h"
|
||||
#include "bsp/i2c.h"
|
||||
|
||||
#define I2C_Handle BSP_I2C_GetHandle(BSP_I2C_BMP280)
|
||||
|
||||
/**
|
||||
* @brief 读寄存器
|
||||
* @param regAdd 寄存器开始地址
|
||||
* @param pdata 存储数据的指针
|
||||
* @param size 寄存器个数
|
||||
* @retval 无
|
||||
*/
|
||||
void bmp280_readReg(uint8_t regAdd, uint8_t *pdata, uint8_t size) {
|
||||
HAL_I2C_Mem_Read(I2C_Handle, BMP280_I2C_ADDR << 1, regAdd, I2C_MEMADD_SIZE_8BIT, pdata, size, 1000);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 写1个寄存器
|
||||
* @param regAdd 寄存器开始地址
|
||||
* @param pdata 存储数据的指针
|
||||
* @retval 0 写入成功
|
||||
* 1 写入失败
|
||||
*/
|
||||
uint8_t bmp280_writeReg(uint8_t regAdd, uint8_t *pdata) {
|
||||
if (HAL_I2C_Mem_Write(I2C_Handle, BMP280_I2C_ADDR << 1, regAdd, I2C_MEMADD_SIZE_8BIT, pdata, 1, 1000) == HAL_OK) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 读取设备物理id,用于调试
|
||||
* @param 无
|
||||
* @retval 设备id
|
||||
*/
|
||||
uint8_t bmp280_get_id(void) {
|
||||
uint8_t temp = 0;
|
||||
bmp280_readReg(BMP280_ID, &temp, 1);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 重启设备
|
||||
* @param 无
|
||||
* @retval 0 重启成功
|
||||
* 1 重启失败
|
||||
*/
|
||||
uint8_t bmp280_reset(void) {
|
||||
uint8_t temp = 0xB6;
|
||||
return bmp280_writeReg(BMP280_RESET, &temp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取设备状态
|
||||
* @param 无
|
||||
* @retval 0 空闲
|
||||
* 1 正在测量或者正在复制
|
||||
*/
|
||||
uint8_t bmp280_getStatus(void) {
|
||||
uint8_t temp = 0;
|
||||
bmp280_readReg(BMP280_STATUS, &temp, 1);
|
||||
return (temp & 0x09) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 工作模式设置推荐
|
||||
* @param mode 0 睡眠模式
|
||||
* 1 单次测量模式,测量完成后回到休眠模式
|
||||
* 2 连续测量模式
|
||||
* @retval 0 设置成功
|
||||
* 1 设置失败
|
||||
*/
|
||||
uint8_t bmp280_setMode(uint8_t mode)
|
||||
{
|
||||
uint8_t temp=0;
|
||||
bmp280_readReg(BMP280_CTRL_MEAS,&temp,1);
|
||||
switch(mode)
|
||||
{
|
||||
case 0:
|
||||
temp&=0xFC;
|
||||
break;
|
||||
case 1:
|
||||
temp&=0xFC;
|
||||
temp|=0x01;
|
||||
break;
|
||||
case 2:
|
||||
temp&=0xFC;
|
||||
temp|=0x03;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
return bmp280_writeReg(BMP280_CTRL_MEAS,&temp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 过采样设置
|
||||
* @param mode temp&press 0 禁用
|
||||
* 1 过采样×1
|
||||
* 2 过采样×2
|
||||
* 3 过采样×4
|
||||
* .. .....
|
||||
* 5 过采样×16
|
||||
* @retval 0 设置成功
|
||||
* 1 设置失败
|
||||
*/
|
||||
uint8_t bmp280_setOversampling(uint8_t osrs_p,uint8_t osrs_t)
|
||||
{
|
||||
uint8_t temp=0;
|
||||
bmp280_readReg(BMP280_CTRL_MEAS,&temp,1);
|
||||
temp&=0xE3;
|
||||
osrs_p = osrs_p<<2;
|
||||
osrs_p&= 0x1C;
|
||||
temp|=osrs_p;
|
||||
|
||||
temp&=0x1F;
|
||||
osrs_t = osrs_t<<5;
|
||||
osrs_t&= 0xE0;
|
||||
temp|=osrs_t;
|
||||
return bmp280_writeReg(BMP280_CTRL_MEAS,&temp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 滤波器系数和采样间隔时间设置
|
||||
* @param Standbyt 0 0.5ms filter 0 关闭滤波器
|
||||
* 1 62.5ms 1 2
|
||||
* 2 125ms 2 4
|
||||
* 3 250ms 3 8
|
||||
* 4 500ms 4 16
|
||||
* 5 1000ms
|
||||
* 6 2000ms
|
||||
* 7 4000ms
|
||||
* @retval 0 设置成功
|
||||
* 1 设置失败
|
||||
*/
|
||||
uint8_t bmp280_setConfig(uint8_t Standbyt,uint8_t filter)
|
||||
{
|
||||
uint8_t temp=0;
|
||||
temp = Standbyt<<5;
|
||||
filter&=0x07;
|
||||
filter=filter<<2;
|
||||
temp|=filter;
|
||||
return bmp280_writeReg(BMP280_CONFIG,&temp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 获取校准系数
|
||||
* @param calib 储存系数的结构体
|
||||
* @retval 无
|
||||
*/
|
||||
void bmp280_getCalibration(bmp280_calib *calib)
|
||||
{
|
||||
uint8_t buf[20];
|
||||
bmp280_readReg(BMP280_DIGT,buf,6);
|
||||
calib->dig_t1 =(uint16_t)(bmp280_msblsb_to_u16(buf[1], buf[0]));
|
||||
calib->dig_t2 =(int16_t)(bmp280_msblsb_to_u16(buf[3], buf[2]));
|
||||
calib->dig_t3 =(int16_t)(bmp280_msblsb_to_u16(buf[5], buf[4]));
|
||||
bmp280_readReg(BMP280_DIGP,buf,18);
|
||||
calib->dig_p1 = (uint16_t)(bmp280_msblsb_to_u16(buf[1], buf[0]));
|
||||
calib->dig_p2 =(int16_t)(bmp280_msblsb_to_u16(buf[3], buf[2]));
|
||||
calib->dig_p3 =(int16_t)(bmp280_msblsb_to_u16(buf[5], buf[4]));
|
||||
calib->dig_p4 =(int16_t)(bmp280_msblsb_to_u16(buf[7], buf[6]));
|
||||
calib->dig_p5 =(int16_t)(bmp280_msblsb_to_u16(buf[9], buf[8]));
|
||||
calib->dig_p6 =(int16_t)(bmp280_msblsb_to_u16(buf[11], buf[10]));
|
||||
calib->dig_p7 =(int16_t)(bmp280_msblsb_to_u16(buf[13], buf[12]));
|
||||
calib->dig_p8 =(int16_t)(bmp280_msblsb_to_u16(buf[15], buf[14]));
|
||||
calib->dig_p9 =(int16_t)(bmp280_msblsb_to_u16(buf[17], buf[16]));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief 获取温度
|
||||
* @param calib 系数的结构体
|
||||
* *temperature 温度值指针
|
||||
* *t_fine 精细分辨率温度值指针
|
||||
* @retval 无
|
||||
*/
|
||||
void bmp280_getTemperature(bmp280_calib *calib,double *temperature,int32_t *t_fine)
|
||||
{
|
||||
uint8_t buf[3];
|
||||
uint32_t data_xlsb;
|
||||
uint32_t data_lsb;
|
||||
uint32_t data_msb;
|
||||
int32_t uncomp_temperature;
|
||||
double var1, var2;
|
||||
|
||||
bmp280_readReg(BMP280_TEMP,buf,3);
|
||||
data_msb = (int32_t)buf[0] << 12;
|
||||
data_lsb = (int32_t)buf[1] << 4;
|
||||
data_xlsb = (int32_t)buf[2] >> 4;
|
||||
uncomp_temperature = (int32_t)(data_msb | data_lsb | data_xlsb);
|
||||
|
||||
|
||||
var1 = (((double) uncomp_temperature) / 16384.0 - ((double) calib->dig_t1) / 1024.0) *
|
||||
((double) calib->dig_t2);
|
||||
var2 =
|
||||
((((double) uncomp_temperature) / 131072.0 - ((double) calib->dig_t1) / 8192.0) *
|
||||
(((double) uncomp_temperature) / 131072.0 - ((double) calib->dig_t1) / 8192.0)) *
|
||||
((double) calib->dig_t3);
|
||||
*t_fine = (int32_t) (var1 + var2);
|
||||
*temperature = (var1 + var2) / 5120.0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 获取气压
|
||||
* @param calib 系数的结构体
|
||||
* *pressure 气压值指针
|
||||
* *t_fine 精细分辨率温度值指针
|
||||
* @retval 无
|
||||
*/
|
||||
void bmp280_getPressure(bmp280_calib *calib,double *pressure,int32_t *t_fine)
|
||||
{
|
||||
uint8_t buf[3];
|
||||
uint32_t data_xlsb;
|
||||
uint32_t data_lsb;
|
||||
uint32_t data_msb;
|
||||
int32_t uncomp_pressure;
|
||||
double var1, var2;
|
||||
|
||||
bmp280_readReg(BMP280_PRES,buf,3);
|
||||
data_msb = (uint32_t)buf[0] << 12;
|
||||
data_lsb = (uint32_t)buf[1] << 4;
|
||||
data_xlsb = (uint32_t)buf[2] >> 4;
|
||||
uncomp_pressure = (data_msb | data_lsb | data_xlsb);
|
||||
|
||||
var1 = ((double) *t_fine / 2.0) - 64000.0;
|
||||
var2 = var1 * var1 * ((double) calib->dig_p6) / 32768.0;
|
||||
var2 = var2 + var1 * ((double) calib->dig_p5) * 2.0;
|
||||
var2 = (var2 / 4.0) + (((double) calib->dig_p4) * 65536.0);
|
||||
var1 = (((double)calib->dig_p3) * var1 * var1 / 524288.0 + ((double)calib->dig_p2) * var1) /
|
||||
524288.0;
|
||||
var1 = (1.0 + var1 / 32768.0) * ((double) calib->dig_p1);
|
||||
*pressure = 1048576.0 - (double)uncomp_pressure;
|
||||
*pressure = (*pressure - (var2 / 4096.0)) * 6250.0 / var1;
|
||||
var1 = ((double)calib->dig_p9) * *pressure * *pressure / 2147483648.0;
|
||||
var2 = *pressure * ((double)calib->dig_p8) / 32768.0;
|
||||
|
||||
*pressure = *pressure + (var1 + var2 + ((double)calib->dig_p7)) / 16.0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 初始化
|
||||
* @param *calib 系数的结构体指针
|
||||
* @retval 0 设置成功
|
||||
* 1 设置失败
|
||||
*/
|
||||
uint8_t bmp280_init(bmp280_calib *calib)
|
||||
{
|
||||
uint8_t rslt;
|
||||
rslt = bmp280_get_id();
|
||||
if(rslt == BMP2_CHIP_ID)
|
||||
{
|
||||
bmp280_getCalibration(calib);
|
||||
rslt = bmp280_setOversampling(5,2);
|
||||
if(rslt)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
rslt = bmp280_setConfig(0,4);
|
||||
if(rslt)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
rslt = bmp280_setMode(2);
|
||||
if(rslt)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 获取最终数据
|
||||
* @param *calib 系数的结构体指针
|
||||
* @retval 无
|
||||
*/
|
||||
void bmp280_getdata(bmp280_calib *calib,float *temperature,float *pressure)
|
||||
{
|
||||
double temp_T,temp_P;
|
||||
int32_t t_fine;
|
||||
bmp280_getTemperature(calib,&temp_T,&t_fine);
|
||||
bmp280_getPressure(calib,&temp_P,&t_fine);
|
||||
*temperature = (float)temp_T;
|
||||
*pressure = (float)temp_P;
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/*底层接口定义*/
|
||||
#include "bsp/i2c.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define BMP280_I2C_ADDR 0x76 // BMP280 默认 I2C 地址
|
||||
|
||||
/*寄存器地址*/
|
||||
#define BMP280_ID 0xD0 // 设备ID地址
|
||||
#define BMP280_RESET 0xE0 // 设备重启
|
||||
#define BMP280_STATUS 0xF3 // 设备状态
|
||||
#define BMP280_CTRL_MEAS 0xF4 // 数据采集和模式设置
|
||||
#define BMP280_CONFIG 0xF5 // 采样速率,滤波器和接口设置
|
||||
#define BMP280_DIGT 0x88 // 温度校准系数起始位置
|
||||
#define BMP280_DIGP 0x8E // 气压校准系数起始位置
|
||||
#define BMP280_TEMP 0xFA // 温度储存起始位置
|
||||
#define BMP280_PRES 0xF7 // 气压储存起始位置
|
||||
|
||||
#define BMP2_CHIP_ID 0x58 // 设备ID地址
|
||||
|
||||
#define bmp280_msblsb_to_u16(msb, lsb) (((uint16_t)msb << 8) | ((uint16_t)lsb))
|
||||
|
||||
typedef struct {
|
||||
unsigned short dig_t1;
|
||||
signed short dig_t2;
|
||||
signed short dig_t3;
|
||||
unsigned short dig_p1;
|
||||
signed short dig_p2;
|
||||
signed short dig_p3;
|
||||
signed short dig_p4;
|
||||
signed short dig_p5;
|
||||
signed short dig_p6;
|
||||
signed short dig_p7;
|
||||
signed short dig_p8;
|
||||
signed short dig_p9;
|
||||
} bmp280_calib;
|
||||
|
||||
uint8_t bmp280_get_id(void);
|
||||
uint8_t bmp280_reset(void);
|
||||
uint8_t bmp280_getStatus(void);
|
||||
uint8_t bmp280_setMode(uint8_t mode);
|
||||
uint8_t bmp280_setOversampling(uint8_t osrs_p, uint8_t osrs_t);
|
||||
uint8_t bmp280_setConfig(uint8_t Standbyt, uint8_t filter);
|
||||
void bmp280_getCalibration(bmp280_calib *calib);
|
||||
void bmp280_getTemperature(bmp280_calib *calib, double *temperature, int32_t *t_fine);
|
||||
void bmp280_getPressure(bmp280_calib *calib, double *pressure, int32_t *t_fine);
|
||||
uint8_t bmp280_init(bmp280_calib *calib);
|
||||
void bmp280_getdata(bmp280_calib *calib, float *temperature, float *pressure);
|
||||
@@ -1,65 +0,0 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "key_gpio.h"
|
||||
#include "device.h"
|
||||
#include "bsp/gpio_exti.h"
|
||||
#include "gpio.h"
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
#define DEBOUNCE_TIME_MS 20
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
/* Private function -------------------------------------------------------- */
|
||||
/* 外部声明标志位(标志位) */
|
||||
volatile uint8_t key_flag = 0; // 1=按下,0=松开
|
||||
volatile uint8_t key_exti = 0;
|
||||
volatile uint8_t key_pressed = 0; // 全局标志位
|
||||
static uint32_t last_debounce_time = 0; // 消抖
|
||||
|
||||
/* Private function -------------------------------------------------------- */
|
||||
static void KEY_Interrupt_Callback(void) {
|
||||
// 切换标志位状态
|
||||
|
||||
key_flag = !key_flag;
|
||||
key_exti = 1;
|
||||
|
||||
}
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void KEY_Process(void)
|
||||
{
|
||||
BSP_GPIO_RegisterCallback(BSP_GPIO_USER_KEY, BSP_GPIO_EXTI_CB, KEY_Interrupt_Callback);
|
||||
|
||||
if(key_exti == 1)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
// 检查是否超过消抖时间
|
||||
if ((now - last_debounce_time) > DEBOUNCE_TIME_MS) {
|
||||
// 更新有效状态(假设按下为低电平)
|
||||
if(key_flag == 0)
|
||||
{
|
||||
key_pressed = DEVICE_KEY_RELEASED;
|
||||
}
|
||||
if(key_flag == 1)
|
||||
{
|
||||
key_pressed = DEVICE_KEY_PRESSED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);
|
||||
|
||||
}
|
||||
last_debounce_time = now; // 重置消抖计时器
|
||||
key_exti = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t KEY_Get_State(void) {
|
||||
return key_pressed;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef KEY_GPIO_H
|
||||
#define KEY_GPIO_H
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
#include "main.h"
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
|
||||
///* KEY按键状态,设置用 */
|
||||
typedef enum
|
||||
{
|
||||
DEVICE_KEY_RELEASED, //按键释放
|
||||
DEVICE_KEY_PRESSED, //按键按下
|
||||
} DEVICE_KEY_Status_t;
|
||||
|
||||
void KEY_Process(void);
|
||||
uint8_t KEY_Get_State(void);
|
||||
|
||||
#endif
|
||||
381
assets/User_code/device/oled.c
Normal file
381
assets/User_code/device/oled.c
Normal file
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
OLED显示模块。
|
||||
|
||||
使用SPI通信时,需要#define OLED_USE_SPI
|
||||
使用I2C通信时,需要#define OLED_USE_I2C
|
||||
|
||||
*/
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
/* Include 自身的头文件。*/
|
||||
#include "oled.h"
|
||||
|
||||
/* Include 标准库。*/
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Include HAL相关的头文件。*/
|
||||
#include "bsp_delay.h"
|
||||
#include "bsp_spi.h"
|
||||
#include <gpio.h>
|
||||
|
||||
#ifdef OLED_USE_SPI
|
||||
#include "spi.h"
|
||||
#
|
||||
|
||||
#elif defined OLED_USE_I2C
|
||||
#include "i2c.h"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* Include Component相关的头文件 */
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
#ifdef OLED_USE_SPI
|
||||
|
||||
#define OLED_CMD_Set() HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, GPIO_PIN_SET)
|
||||
#define OLED_CMD_Clr() HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, GPIO_PIN_RESET)
|
||||
|
||||
#define OLED_RST_Set() HAL_GPIO_WritePin(OLED_RST_GPIO_Port, OLED_RST_Pin, GPIO_PIN_SET)
|
||||
#define OLED_RST_Clr() HAL_GPIO_WritePin(OLED_RST_GPIO_Port, OLED_RST_Pin, GPIO_PIN_RESET)
|
||||
|
||||
#elif defined OLED_USE_I2C
|
||||
#define OLED_I2C_HANDLE hi2c1
|
||||
|
||||
#endif
|
||||
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
typedef enum {
|
||||
OLED_WriteCMD = 0,
|
||||
OLED_WriteData = 1,
|
||||
}OLED_Write_t;
|
||||
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static const uint8_t oled_font[95][8] = {
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,}, /* " ", 0 */
|
||||
{0x00,0x00,0x00,0xcf,0xcf,0x00,0x00,0x00,}, /* "!", 1 */
|
||||
{0x00,0x0c,0x06,0x00,0x0c,0x06,0x00,0x00,}, /* """, 2 */
|
||||
{0x24,0xe4,0x3c,0x27,0xe4,0x3c,0x27,0x24,}, /* "#", 3 */
|
||||
{0x00,0x20,0x46,0xf9,0x9f,0x62,0x04,0x00,}, /* "$", 4 */
|
||||
{0x06,0x09,0xc6,0x30,0x0c,0x63,0x90,0x60,}, /* "%", 5 */
|
||||
{0x00,0x00,0x6e,0x91,0xa9,0x46,0xa0,0x00,}, /* "&", 6 */
|
||||
{0x00,0x00,0x00,0x1c,0x0e,0x00,0x00,0x00,}, /* "'", 7 */
|
||||
{0x00,0x00,0x3c,0x42,0x81,0x00,0x00,0x00,}, /* "(", 8 */
|
||||
{0x00,0x00,0x00,0x81,0x42,0x3c,0x00,0x00,}, /* ")", 9 */
|
||||
{0x00,0x10,0x54,0x38,0x38,0x54,0x10,0x00,}, /* "*", 10 */
|
||||
{0x00,0x10,0x10,0xfc,0x10,0x10,0x00,0x00,}, /* "+", 11 */
|
||||
{0x00,0x00,0x00,0xc0,0x60,0x00,0x00,0x00,}, /* ",", 12 */
|
||||
{0x00,0x00,0x10,0x10,0x10,0x10,0x00,0x00,}, /* "-", 13 */
|
||||
{0x00,0x00,0x00,0x00,0xc0,0xc0,0x00,0x00,}, /* ".", 14 */
|
||||
{0x00,0x00,0x00,0xc0,0x38,0x07,0x00,0x00,}, /* "/", 15 */
|
||||
{0x00,0x00,0x7c,0x92,0x8a,0x7c,0x00,0x00,}, /* "0", 16 */
|
||||
{0x00,0x00,0x00,0x84,0xfe,0x80,0x00,0x00,}, /* "1", 17 */
|
||||
{0x00,0x00,0x8c,0xc2,0xa2,0x9c,0x00,0x00,}, /* "2", 18 */
|
||||
{0x00,0x00,0x44,0x92,0x92,0x6c,0x00,0x00,}, /* "3", 19 */
|
||||
{0x00,0x20,0x38,0x24,0xfe,0x20,0x00,0x00,}, /* "4", 20 */
|
||||
{0x00,0x00,0x5e,0x92,0x92,0x62,0x00,0x00,}, /* "5", 21 */
|
||||
{0x00,0x00,0x78,0x94,0x92,0x62,0x00,0x00,}, /* "6", 22 */
|
||||
{0x00,0x00,0x82,0x62,0x1a,0x06,0x00,0x00,}, /* "7", 23 */
|
||||
{0x00,0x00,0x6c,0x92,0x92,0x6c,0x00,0x00,}, /* "8", 24 */
|
||||
{0x00,0x00,0x8c,0x52,0x32,0x1c,0x00,0x00,}, /* "9", 25 */
|
||||
{0x00,0x00,0x00,0x6c,0x6c,0x00,0x00,0x00,}, /* ":", 26 */
|
||||
{0x00,0x00,0x80,0xec,0x6c,0x00,0x00,0x00,}, /* ";", 27 */
|
||||
{0x00,0x00,0x10,0x28,0x44,0x00,0x00,0x00,}, /* "<", 28 */
|
||||
{0x00,0x00,0x24,0x24,0x24,0x24,0x00,0x00,}, /* "=", 29 */
|
||||
{0x00,0x00,0x00,0x44,0x28,0x10,0x00,0x00,}, /* ">", 30 */
|
||||
{0x00,0x00,0x0c,0xa2,0x92,0x1c,0x00,0x00,}, /* "?", 31 */
|
||||
{0x00,0x3c,0x42,0x99,0xa5,0xa2,0x3c,0x00,}, /* "@", 32 */
|
||||
{0x00,0xe0,0x1c,0x12,0x12,0x1c,0xe0,0x00,}, /* "A", 33 */
|
||||
{0x00,0xfe,0x92,0x92,0x9c,0x90,0x60,0x00,}, /* "B", 34 */
|
||||
{0x00,0x38,0x44,0x82,0x82,0x82,0x44,0x00,}, /* "C", 35 */
|
||||
{0x00,0xfe,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "D", 36 */
|
||||
{0x00,0xfe,0x92,0x92,0x92,0x92,0x92,0x00,}, /* "E", 37 */
|
||||
{0x00,0xfe,0x12,0x12,0x12,0x12,0x02,0x00,}, /* "F", 38 */
|
||||
{0x00,0x7c,0x82,0x92,0x92,0x72,0x00,0x00,}, /* "G", 39 */
|
||||
{0x00,0xfe,0x10,0x10,0x10,0x10,0xfe,0x00,}, /* "H", 40 */
|
||||
{0x00,0x82,0x82,0xfe,0x82,0x82,0x00,0x00,}, /* "I", 41 */
|
||||
{0x00,0x82,0x82,0x7e,0x02,0x02,0x00,0x00,}, /* "J", 42 */
|
||||
{0x00,0xfe,0x10,0x28,0x44,0x82,0x00,0x00,}, /* "K", 43 */
|
||||
{0x00,0xfe,0x80,0x80,0x80,0x80,0x00,0x00,}, /* "L", 44 */
|
||||
{0xfc,0x02,0x04,0xf8,0x04,0x02,0xfc,0x00,}, /* "M", 45 */
|
||||
{0x00,0xfe,0x04,0x18,0x30,0x40,0xfe,0x00,}, /* "N", 46 */
|
||||
{0x00,0x7c,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "O", 47 */
|
||||
{0x00,0x00,0xfe,0x12,0x12,0x0c,0x00,0x00,}, /* "P", 48 */
|
||||
{0x00,0x00,0x3c,0x42,0xc2,0xbc,0x00,0x00,}, /* "Q", 49 */
|
||||
{0x00,0x00,0xfe,0x32,0x52,0x8c,0x00,0x00,}, /* "R", 50 */
|
||||
{0x00,0x00,0x4c,0x92,0x92,0x64,0x00,0x00,}, /* "S", 51 */
|
||||
{0x00,0x02,0x02,0xfe,0x02,0x02,0x00,0x00,}, /* "T", 52 */
|
||||
{0x00,0x7e,0x80,0x80,0x80,0x80,0x7e,0x00,}, /* "U", 53 */
|
||||
{0x00,0x0c,0x30,0xc0,0x30,0x0c,0x00,0x00,}, /* "V", 54 */
|
||||
{0x7c,0x80,0x80,0x78,0x80,0x80,0x7c,0x00,}, /* "W", 55 */
|
||||
{0x00,0x84,0x48,0x30,0x30,0x48,0x84,0x00,}, /* "X", 56 */
|
||||
{0x00,0x06,0x08,0xf0,0x08,0x06,0x00,0x00,}, /* "Y", 57 */
|
||||
{0x00,0x00,0xc2,0xa2,0x92,0x8e,0x00,0x00,}, /* "Z", 58 */
|
||||
{0x00,0x00,0xfe,0x82,0x82,0x82,0x00,0x00,}, /* "[", 59 */
|
||||
{0x00,0x00,0x06,0x18,0x60,0x80,0x00,0x00,}, /* "\", 60 */
|
||||
{0x00,0x00,0x82,0x82,0x82,0xfe,0x00,0x00,}, /* "]", 61 */
|
||||
{0x00,0x30,0x0c,0x02,0x0c,0x30,0x00,0x00,}, /* "^", 62 */
|
||||
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,}, /* "_", 63 */
|
||||
{0x00,0x00,0x04,0x0c,0x18,0x00,0x00,0x00,}, /* "`", 64 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0xe0,0x00,0x00,}, /* "a", 65 */
|
||||
{0x00,0x00,0xf8,0xa0,0xe0,0x00,0x00,0x00,}, /* "b", 66 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0x00,0x00,0x00,}, /* "c", 67 */
|
||||
{0x00,0x00,0xe0,0xa0,0xf8,0x00,0x00,0x00,}, /* "d", 68 */
|
||||
{0x00,0x00,0x70,0xa8,0xa8,0x90,0x00,0x00,}, /* "e", 69 */
|
||||
{0x00,0x00,0x10,0xf8,0x14,0x00,0x00,0x00,}, /* "f", 70 */
|
||||
{0x00,0x00,0xd8,0xa4,0x7c,0x00,0x00,0x00,}, /* "g", 71 */
|
||||
{0x00,0x00,0xf8,0x20,0xe0,0x00,0x00,0x00,}, /* "h", 72 */
|
||||
{0x00,0x00,0x00,0xe8,0x00,0x00,0x00,0x00,}, /* "i", 73 */
|
||||
{0x00,0x00,0x40,0x90,0x74,0x00,0x00,0x00,}, /* "j", 74 */
|
||||
{0x00,0x00,0xf8,0x60,0x90,0x00,0x00,0x00,}, /* "k", 75 */
|
||||
{0x00,0x00,0x78,0x80,0x80,0x00,0x00,0x00,}, /* "l", 76 */
|
||||
{0x00,0xe0,0x10,0xe0,0x10,0xe0,0x00,0x00,}, /* "m", 77 */
|
||||
{0x00,0x00,0xf0,0x10,0x10,0xe0,0x00,0x00,}, /* "n", 78 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0x60,0x00,0x00,}, /* "o", 79 */
|
||||
{0x00,0x00,0xf0,0x48,0x48,0x30,0x00,0x00,}, /* "p", 80 */
|
||||
{0x00,0x00,0x30,0x48,0x48,0xf0,0x00,0x00,}, /* "q", 81 */
|
||||
{0x00,0x00,0x00,0xf0,0x20,0x10,0x00,0x00,}, /* "r", 82 */
|
||||
{0x00,0x00,0x90,0xa8,0xa8,0x48,0x00,0x00,}, /* "s", 83 */
|
||||
{0x00,0x10,0x10,0xf8,0x90,0x90,0x00,0x00,}, /* "t", 84 */
|
||||
{0x00,0x00,0x78,0x80,0x80,0xf8,0x00,0x00,}, /* "u", 85 */
|
||||
{0x00,0x18,0x60,0x80,0x60,0x18,0x00,0x00,}, /* "v", 86 */
|
||||
{0x00,0x38,0xc0,0x38,0xc0,0x38,0x00,0x00,}, /* "w", 87 */
|
||||
{0x00,0x88,0x50,0x20,0x50,0x88,0x00,0x00,}, /* "x", 88 */
|
||||
{0x00,0x8c,0x50,0x20,0x10,0x0c,0x00,0x00,}, /* "y", 89 */
|
||||
{0x00,0x88,0xc8,0xa8,0x98,0x88,0x00,0x00,}, /* "z", 90 */
|
||||
{0x00,0x00,0x10,0x7c,0x82,0x00,0x00,0x00,}, /* "{", 91 */
|
||||
{0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,}, /* "|", 92 */
|
||||
{0x00,0x00,0x00,0x82,0x7c,0x10,0x00,0x00,}, /* "}", 93 */
|
||||
{0x00,0x08,0x04,0x04,0x08,0x10,0x10,0x08,}, /* "~", 94 */
|
||||
};
|
||||
|
||||
static const uint8_t gram_logo[8][128] = {
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x06,0x0E,0X1E,0X3E,0X7E,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFE,0XFC,0XFC,0XF8,0XF8,0XF0,0XE0,0XC0,0X80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XF7,0XE7,0XC7,0X87,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x0F,0X1F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0XC0,0XFC,0XFC,0XFC,0XFC,0XFC,0XFD,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,0XFC,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X7F,0X3F,0x0F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0X80,0XF0,0XFE,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X1F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0X1F,0X3F,0X7F,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XEF,0XCF,0X87,0x07,0x03,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0XE0,0XFC,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0X3F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0X40,0X40,0X40,0XC0,0XC0,0XC0,0XC0,0XC1,0XC3,0XC7,0XCF,0XDF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFE,0XFC,0XF8,0XF0,0XE0,0XC0,0X80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x0C,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x07,0x07,0x0F,0x0F,0X1F,0X1F,0X3F,0X3F,0X7F,0X7F,0XFF,0XF3,0XE3,0XC3,0X83,0x03,0x03,0x03,0x03,0x03,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x06,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x78,0x7B,0x1B,0x1B,0x1B,0x7B,0x7F,0x6F,0x4E,0x00,0x3E,0x7F,0x63,0x63,0x63,0x7F,0x3E,0x00,0x78,0x7B,0x6B,0x6B,0x6B,0x6B,0x7B,0x7F,0x36,0x00,0x3E,0x7F,0x63,0x63,0x63,0x7F,0x3E,0x40,0x60,0x78,0x3D,0x07,0x1F,0x7C,0x70,0x1D,0x07,0x7F,0x78,0x40,0x00,0x60,0x70,0x38,0x5D,0x6F,0x67,0x6F,0x7C,0x70,0x40,0x00,0x40,0x66,0x6F,0x6B,0x6B,0x6B,0x6B,0x7B,0x31,0x02,0x03,0x03,0x7F,0x7F,0x03,0x03,0x61,0x68,0x6B,0x6B,0x6B,0x6B,0x6B,0x0B,0x03,0x60,0x78,0x7B,0x1B,0x1B,0x1B,0x7B,0x7B,0x6F,0x4E,0x00,0x46,0x6F,0x6F,0x6B,0x6B,0x6B,0x7B,0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
||||
};
|
||||
|
||||
static OLED_t *goled;
|
||||
static bool inited = false;
|
||||
|
||||
/* Private function ---------------------------------------------- */
|
||||
static void OLED_WriteByte(uint8_t data, OLED_Write_t type) {
|
||||
|
||||
#ifdef OLED_USE_SPI
|
||||
switch(type) {
|
||||
case OLED_WriteCMD:
|
||||
OLED_CMD_Clr();
|
||||
break;
|
||||
|
||||
case OLED_WriteData:
|
||||
OLED_CMD_Set();
|
||||
break;
|
||||
}
|
||||
BSP_SPI_Transmit(BSP_SPI_OLED, &data, 1);
|
||||
|
||||
#elif defined OLED_USE_I2C
|
||||
uint8_t cmd_data[2];
|
||||
|
||||
switch(type) {
|
||||
case OLED_WriteCMD:
|
||||
cmd_data[0] = 0x00;
|
||||
break;
|
||||
|
||||
case OLED_WriteData:
|
||||
cmd_data[0] = 0x40;
|
||||
break;
|
||||
}
|
||||
cmd_data[1] = data;
|
||||
HAL_I2C_Master_Transmit_DMA(OLED_I2C, OLED_I2C_ADDRESS, cmd_data, 2);
|
||||
#endif
|
||||
|
||||
BSP_Delay(1);
|
||||
}
|
||||
|
||||
static void OLED_WriteSequenceData(uint8_t *data, uint16_t len) {
|
||||
#ifdef OLED_USE_SPI
|
||||
OLED_CMD_Set();
|
||||
HAL_SPI_Transmit_DMA(&hspi1, data, len);
|
||||
|
||||
#elif defined OLED_USE_I2C
|
||||
/* TODO */
|
||||
uint8_t cmd_data[2];
|
||||
cmd_data[0] = 0x40;
|
||||
cmd_data[1] = data;
|
||||
HAL_I2C_Master_Transmit_DMA(OLED_I2C, OLED_I2C_ADDRESS, cmd_data, 2);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
int8_t OLED_Init(OLED_t *oled) {
|
||||
if (oled == NULL)
|
||||
return -1;
|
||||
|
||||
if (inited)
|
||||
return -1;
|
||||
|
||||
oled->cursor.column = 0;
|
||||
oled->cursor.page = 0;
|
||||
oled->modified = true;
|
||||
|
||||
goled = oled;
|
||||
|
||||
OLED_RST_Clr();
|
||||
BSP_Delay(1);
|
||||
OLED_RST_Set();
|
||||
BSP_Delay(5);
|
||||
|
||||
OLED_WriteByte(0xae, OLED_WriteCMD); /* Dot martix display off. */
|
||||
OLED_WriteByte(0x00, OLED_WriteCMD); /* Set lower column address:(00H~0FH). */
|
||||
OLED_WriteByte(0x10, OLED_WriteCMD); /* Set higher column address:(10H~1FH). */
|
||||
OLED_WriteByte(0x20, OLED_WriteCMD); /* Set Memory Addressing Mode */
|
||||
OLED_WriteByte(0x10, OLED_WriteCMD); /* 00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid */
|
||||
OLED_WriteByte(0x32, OLED_WriteCMD); /* Set pump voltage 8v. */
|
||||
OLED_WriteByte(0x40, OLED_WriteCMD); /* Set display start line:(40H~7FH). */
|
||||
OLED_WriteByte(0x81, OLED_WriteCMD); /* Contarst control. */
|
||||
OLED_WriteByte(0x6f, OLED_WriteCMD); /* Contarst: 0x00~0xff. */
|
||||
OLED_WriteByte(0xa1, OLED_WriteCMD); /* Set segment re-map. 0xa0 or 0xa1. */
|
||||
OLED_WriteByte(0xa4, OLED_WriteCMD); /* Entire display off. */
|
||||
OLED_WriteByte(0xa6, OLED_WriteCMD); /* Set normal display. */
|
||||
OLED_WriteByte(0xa8, OLED_WriteCMD); /* Set multiplex ratio. */
|
||||
OLED_WriteByte(0x3f, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xad, OLED_WriteCMD); /* Set DC/DC booster. */
|
||||
OLED_WriteByte(0x8b, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xb0, OLED_WriteCMD); /* Set page address. */
|
||||
OLED_WriteByte(0xc8, OLED_WriteCMD); /* Com scan COM0->COM63. */
|
||||
OLED_WriteByte(0xd3, OLED_WriteCMD); /* Set display offset. */
|
||||
OLED_WriteByte(0x00, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xd5, OLED_WriteCMD); /* Set frame frequency. */
|
||||
OLED_WriteByte(0xf0, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xd9, OLED_WriteCMD); /* Set pre_charge period. */
|
||||
OLED_WriteByte(0x22, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xda, OLED_WriteCMD); /* Com pin configuration. */
|
||||
OLED_WriteByte(0x12, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xdb, OLED_WriteCMD); /* Set vcom deselect level. */
|
||||
OLED_WriteByte(0x20, OLED_WriteCMD);
|
||||
OLED_WriteByte(0x8d, OLED_WriteCMD); /* Set DC-DC enable */
|
||||
OLED_WriteByte(0x14, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xaf, OLED_WriteCMD); /* Display on. */
|
||||
|
||||
memcmp(oled->gram, gram_logo, sizeof(oled->gram));
|
||||
|
||||
OLED_SetAllRam(oled, OLED_PEN_WRITE); /* Clear memory. */
|
||||
|
||||
OLED_Refresh(oled); /* Display initial picture. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
OLED_t *OLED_GetDevice(void) {
|
||||
if (inited) {
|
||||
return goled;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int8_t OLED_PrintRam(OLED_t *oled, const char* str) {
|
||||
if (str == NULL)
|
||||
return -1;
|
||||
|
||||
oled->modified = true;
|
||||
|
||||
for (uint16_t i = 0; str[i] != '\0'; i++) {
|
||||
uint8_t c = str[i];
|
||||
if (c < 32) {
|
||||
switch (c) {
|
||||
case '\n':
|
||||
oled->cursor.page ++;
|
||||
oled->cursor.column = 0;
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
oled->cursor.column += 16;
|
||||
break;
|
||||
}
|
||||
} else if (c < 126) {
|
||||
if (oled->cursor.column == 128) {
|
||||
oled->cursor.page ++;
|
||||
oled->cursor.column = 0;
|
||||
}
|
||||
if (oled->cursor.page == 8) {
|
||||
OLED_SetAllRam(oled, OLED_PEN_CLEAR);
|
||||
oled->cursor.page = 0;
|
||||
}
|
||||
memcpy(&oled->gram[oled->cursor.page][oled->cursor.column], oled_font[c- 32], sizeof(oled_font[0]));
|
||||
oled->cursor.column += 8;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t OLED_RewindRam(OLED_t *oled) {
|
||||
oled->modified = true;
|
||||
|
||||
OLED_SetAllRam(oled, OLED_PEN_CLEAR);
|
||||
|
||||
oled->cursor.page = 0;
|
||||
oled->cursor.column = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t OLED_SetAllRam(OLED_t *oled, OLED_Pen_t pen) {
|
||||
switch(pen) {
|
||||
case OLED_PEN_WRITE:
|
||||
memset(oled->gram, -1, sizeof(oled->gram));
|
||||
break;
|
||||
|
||||
case OLED_PEN_CLEAR:
|
||||
memset(oled->gram, 0, sizeof(oled->gram));
|
||||
break;
|
||||
|
||||
case OLED_PEN_INVERSION:
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
for (uint8_t n = 0; n < 128; n++) {
|
||||
oled->gram[i][n] = ~oled->gram[i][n];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t OLED_DisplayOn(OLED_t *oled) {
|
||||
OLED_WriteByte(0x8d, OLED_WriteCMD);
|
||||
OLED_WriteByte(0x14, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xaf, OLED_WriteCMD);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t OLED_DisplayOff(OLED_t *oled) {
|
||||
OLED_WriteByte(0x8d, OLED_WriteCMD);
|
||||
OLED_WriteByte(0x10, OLED_WriteCMD);
|
||||
OLED_WriteByte(0xae, OLED_WriteCMD);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void OLED_SetCursor(uint8_t x, uint8_t y)
|
||||
{
|
||||
OLED_WriteByte((0xb0 + y), OLED_WriteCMD); /*set page address y */
|
||||
OLED_WriteByte(((x&0xf0)>>4)|0x10, OLED_WriteCMD); /*set column high address */
|
||||
OLED_WriteByte((x&0x0f), OLED_WriteCMD); /*set column low address */
|
||||
}
|
||||
|
||||
uint8_t OLED_Refresh(OLED_t *oled) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
OLED_SetCursor(0, i);
|
||||
for (uint8_t n = 0; n < 8; n++) {
|
||||
OLED_WriteByte(oled->gram[i][n], OLED_WriteData);
|
||||
}
|
||||
}
|
||||
oled->modified = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
assets/User_code/device/oled.h
Normal file
45
assets/User_code/device/oled.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
typedef enum {
|
||||
OLED_PEN_CLEAR = 0,
|
||||
OLED_PEN_WRITE = 1,
|
||||
OLED_PEN_INVERSION = 2,
|
||||
} OLED_Pen_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t column;
|
||||
s uint8_t page;
|
||||
} OLED_Cursor_t;
|
||||
|
||||
typedef struct {
|
||||
OLED_Cursor_t cursor;
|
||||
uint8_t gram[8][128];
|
||||
bool modified;
|
||||
} OLED_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
uint8_t OLED_Init(OLED_t *oled);
|
||||
OLED_t *OLED_GetDevice(void);
|
||||
|
||||
uint8_t OLED_PrintRam(OLED_t *oled, const char *str);
|
||||
uint8_t OLED_RewindRam(OLED_t *oled);
|
||||
uint8_t OLED_SetAllRam(OLED_t *oled, OLED_Pen_t pen);
|
||||
|
||||
uint8_t OLED_DisplayOn(OLED_t *oled);
|
||||
uint8_t OLED_DisplayOff(OLED_t *oled);
|
||||
uint8_t OLED_Refresh(OLED_t *oled);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,267 +0,0 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "device/oled_i2c.h"
|
||||
#include "bsp/i2c.h"
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
#define OLED_I2C_ADDR 0x78 // OLED I2C 地址
|
||||
#define OLED_WIDTH 128
|
||||
#define OLED_HEIGHT 64
|
||||
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
static uint8_t oled_buffer[OLED_WIDTH * OLED_HEIGHT / 8];
|
||||
static struct {
|
||||
uint8_t x_min;
|
||||
uint8_t x_max;
|
||||
uint8_t y_min;
|
||||
uint8_t y_max;
|
||||
uint8_t dirty; // 标志是否有脏区域
|
||||
} dirty_rect = {0, 0, 0, 0, 0};
|
||||
|
||||
/* Private function prototypes ---------------------------------------------- */
|
||||
static void OLED_WriteCommand(uint8_t cmd) {
|
||||
uint8_t data[2] = {0x00, cmd};
|
||||
HAL_I2C_Master_Transmit(BSP_I2C_GetHandle(BSP_I2C_OLED), OLED_I2C_ADDR, data, 2, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
static void OLED_WriteData(uint8_t *data, uint16_t size) {
|
||||
uint8_t buffer[size + 1];
|
||||
buffer[0] = 0x40;
|
||||
memcpy(&buffer[1], data, size);
|
||||
HAL_I2C_Master_Transmit(BSP_I2C_GetHandle(BSP_I2C_OLED), OLED_I2C_ADDR, buffer, size + 1, HAL_MAX_DELAY);
|
||||
}
|
||||
|
||||
static void OLED_MarkDirty(uint8_t x, uint8_t y);
|
||||
static void OLED_UpdateDirtyScreen(void);
|
||||
|
||||
static const uint8_t oled_font[95][8] = {
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,}, /* " ", 0 */
|
||||
{0x00,0x00,0x00,0xcf,0xcf,0x00,0x00,0x00,}, /* "!", 1 */
|
||||
{0x00,0x0c,0x06,0x00,0x0c,0x06,0x00,0x00,}, /* """, 2 */
|
||||
{0x24,0xe4,0x3c,0x27,0xe4,0x3c,0x27,0x24,}, /* "#", 3 */
|
||||
{0x00,0x20,0x46,0xf9,0x9f,0x62,0x04,0x00,}, /* "$", 4 */
|
||||
{0x06,0x09,0xc6,0x30,0x0c,0x63,0x90,0x60,}, /* "%", 5 */
|
||||
{0x00,0x00,0x6e,0x91,0xa9,0x46,0xa0,0x00,}, /* "&", 6 */
|
||||
{0x00,0x00,0x00,0x1c,0x0e,0x00,0x00,0x00,}, /* "'", 7 */
|
||||
{0x00,0x00,0x3c,0x42,0x81,0x00,0x00,0x00,}, /* "(", 8 */
|
||||
{0x00,0x00,0x00,0x81,0x42,0x3c,0x00,0x00,}, /* ")", 9 */
|
||||
{0x00,0x10,0x54,0x38,0x38,0x54,0x10,0x00,}, /* "*", 10 */
|
||||
{0x00,0x10,0x10,0xfc,0x10,0x10,0x00,0x00,}, /* "+", 11 */
|
||||
{0x00,0x00,0x00,0xc0,0x60,0x00,0x00,0x00,}, /* ",", 12 */
|
||||
{0x00,0x00,0x10,0x10,0x10,0x10,0x00,0x00,}, /* "-", 13 */
|
||||
{0x00,0x00,0x00,0x00,0xc0,0xc0,0x00,0x00,}, /* ".", 14 */
|
||||
{0x00,0x00,0x00,0xc0,0x38,0x07,0x00,0x00,}, /* "/", 15 */
|
||||
{0x00,0x00,0x7c,0x92,0x8a,0x7c,0x00,0x00,}, /* "0", 16 */
|
||||
{0x00,0x00,0x00,0x84,0xfe,0x80,0x00,0x00,}, /* "1", 17 */
|
||||
{0x00,0x00,0x8c,0xc2,0xa2,0x9c,0x00,0x00,}, /* "2", 18 */
|
||||
{0x00,0x00,0x44,0x92,0x92,0x6c,0x00,0x00,}, /* "3", 19 */
|
||||
{0x00,0x20,0x38,0x24,0xfe,0x20,0x00,0x00,}, /* "4", 20 */
|
||||
{0x00,0x00,0x5e,0x92,0x92,0x62,0x00,0x00,}, /* "5", 21 */
|
||||
{0x00,0x00,0x78,0x94,0x92,0x62,0x00,0x00,}, /* "6", 22 */
|
||||
{0x00,0x00,0x82,0x62,0x1a,0x06,0x00,0x00,}, /* "7", 23 */
|
||||
{0x00,0x00,0x6c,0x92,0x92,0x6c,0x00,0x00,}, /* "8", 24 */
|
||||
{0x00,0x00,0x8c,0x52,0x32,0x1c,0x00,0x00,}, /* "9", 25 */
|
||||
{0x00,0x00,0x00,0x6c,0x6c,0x00,0x00,0x00,}, /* ":", 26 */
|
||||
{0x00,0x00,0x80,0xec,0x6c,0x00,0x00,0x00,}, /* ";", 27 */
|
||||
{0x00,0x00,0x10,0x28,0x44,0x00,0x00,0x00,}, /* "<", 28 */
|
||||
{0x00,0x00,0x24,0x24,0x24,0x24,0x00,0x00,}, /* "=", 29 */
|
||||
{0x00,0x00,0x00,0x44,0x28,0x10,0x00,0x00,}, /* ">", 30 */
|
||||
{0x00,0x00,0x0c,0xa2,0x92,0x1c,0x00,0x00,}, /* "?", 31 */
|
||||
{0x00,0x3c,0x42,0x99,0xa5,0xa2,0x3c,0x00,}, /* "@", 32 */
|
||||
{0x00,0xe0,0x1c,0x12,0x12,0x1c,0xe0,0x00,}, /* "A", 33 */
|
||||
{0x00,0xfe,0x92,0x92,0x9c,0x90,0x60,0x00,}, /* "B", 34 */
|
||||
{0x00,0x38,0x44,0x82,0x82,0x82,0x44,0x00,}, /* "C", 35 */
|
||||
{0x00,0xfe,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "D", 36 */
|
||||
{0x00,0xfe,0x92,0x92,0x92,0x92,0x92,0x00,}, /* "E", 37 */
|
||||
{0x00,0xfe,0x12,0x12,0x12,0x12,0x02,0x00,}, /* "F", 38 */
|
||||
{0x00,0x7c,0x82,0x92,0x92,0x72,0x00,0x00,}, /* "G", 39 */
|
||||
{0x00,0xfe,0x10,0x10,0x10,0x10,0xfe,0x00,}, /* "H", 40 */
|
||||
{0x00,0x82,0x82,0xfe,0x82,0x82,0x00,0x00,}, /* "I", 41 */
|
||||
{0x00,0x82,0x82,0x7e,0x02,0x02,0x00,0x00,}, /* "J", 42 */
|
||||
{0x00,0xfe,0x10,0x28,0x44,0x82,0x00,0x00,}, /* "K", 43 */
|
||||
{0x00,0xfe,0x80,0x80,0x80,0x80,0x00,0x00,}, /* "L", 44 */
|
||||
{0xfc,0x02,0x04,0xf8,0x04,0x02,0xfc,0x00,}, /* "M", 45 */
|
||||
{0x00,0xfe,0x04,0x18,0x30,0x40,0xfe,0x00,}, /* "N", 46 */
|
||||
{0x00,0x7c,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "O", 47 */
|
||||
{0x00,0x00,0xfe,0x12,0x12,0x0c,0x00,0x00,}, /* "P", 48 */
|
||||
{0x00,0x00,0x3c,0x42,0xc2,0xbc,0x00,0x00,}, /* "Q", 49 */
|
||||
{0x00,0x00,0xfe,0x32,0x52,0x8c,0x00,0x00,}, /* "R", 50 */
|
||||
{0x00,0x00,0x4c,0x92,0x92,0x64,0x00,0x00,}, /* "S", 51 */
|
||||
{0x00,0x02,0x02,0xfe,0x02,0x02,0x00,0x00,}, /* "T", 52 */
|
||||
{0x00,0x7e,0x80,0x80,0x80,0x80,0x7e,0x00,}, /* "U", 53 */
|
||||
{0x00,0x0c,0x30,0xc0,0x30,0x0c,0x00,0x00,}, /* "V", 54 */
|
||||
{0x7c,0x80,0x80,0x78,0x80,0x80,0x7c,0x00,}, /* "W", 55 */
|
||||
{0x00,0x84,0x48,0x30,0x30,0x48,0x84,0x00,}, /* "X", 56 */
|
||||
{0x00,0x06,0x08,0xf0,0x08,0x06,0x00,0x00,}, /* "Y", 57 */
|
||||
{0x00,0x00,0xc2,0xa2,0x92,0x8e,0x00,0x00,}, /* "Z", 58 */
|
||||
{0x00,0x00,0xfe,0x82,0x82,0x82,0x00,0x00,}, /* "[", 59 */
|
||||
{0x00,0x00,0x06,0x18,0x60,0x80,0x00,0x00,}, /* "\", 60 */
|
||||
{0x00,0x00,0x82,0x82,0x82,0xfe,0x00,0x00,}, /* "]", 61 */
|
||||
{0x00,0x30,0x0c,0x02,0x0c,0x30,0x00,0x00,}, /* "^", 62 */
|
||||
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,}, /* "_", 63 */
|
||||
{0x00,0x00,0x04,0x0c,0x18,0x00,0x00,0x00,}, /* "`", 64 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0xe0,0x00,0x00,}, /* "a", 65 */
|
||||
{0x00,0x00,0xf8,0xa0,0xe0,0x00,0x00,0x00,}, /* "b", 66 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0x00,0x00,0x00,}, /* "c", 67 */
|
||||
{0x00,0x00,0xe0,0xa0,0xf8,0x00,0x00,0x00,}, /* "d", 68 */
|
||||
{0x00,0x00,0x70,0xa8,0xa8,0x90,0x00,0x00,}, /* "e", 69 */
|
||||
{0x00,0x00,0x10,0xf8,0x14,0x00,0x00,0x00,}, /* "f", 70 */
|
||||
{0x00,0x00,0xd8,0xa4,0x7c,0x00,0x00,0x00,}, /* "g", 71 */
|
||||
{0x00,0x00,0xf8,0x20,0xe0,0x00,0x00,0x00,}, /* "h", 72 */
|
||||
{0x00,0x00,0x00,0xe8,0x00,0x00,0x00,0x00,}, /* "i", 73 */
|
||||
{0x00,0x00,0x40,0x90,0x74,0x00,0x00,0x00,}, /* "j", 74 */
|
||||
{0x00,0x00,0xf8,0x60,0x90,0x00,0x00,0x00,}, /* "k", 75 */
|
||||
{0x00,0x00,0x78,0x80,0x80,0x00,0x00,0x00,}, /* "l", 76 */
|
||||
{0x00,0xe0,0x10,0xe0,0x10,0xe0,0x00,0x00,}, /* "m", 77 */
|
||||
{0x00,0x00,0xf0,0x10,0x10,0xe0,0x00,0x00,}, /* "n", 78 */
|
||||
{0x00,0x00,0x60,0x90,0x90,0x60,0x00,0x00,}, /* "o", 79 */
|
||||
{0x00,0x00,0xf0,0x48,0x48,0x30,0x00,0x00,}, /* "p", 80 */
|
||||
{0x00,0x00,0x30,0x48,0x48,0xf0,0x00,0x00,}, /* "q", 81 */
|
||||
{0x00,0x00,0x00,0xf0,0x20,0x10,0x00,0x00,}, /* "r", 82 */
|
||||
{0x00,0x00,0x90,0xa8,0xa8,0x48,0x00,0x00,}, /* "s", 83 */
|
||||
{0x00,0x10,0x10,0xf8,0x90,0x90,0x00,0x00,}, /* "t", 84 */
|
||||
{0x00,0x00,0x78,0x80,0x80,0xf8,0x00,0x00,}, /* "u", 85 */
|
||||
{0x00,0x18,0x60,0x80,0x60,0x18,0x00,0x00,}, /* "v", 86 */
|
||||
{0x00,0x38,0xc0,0x38,0xc0,0x38,0x00,0x00,}, /* "w", 87 */
|
||||
{0x00,0x88,0x50,0x20,0x50,0x88,0x00,0x00,}, /* "x", 88 */
|
||||
{0x00,0x8c,0x50,0x20,0x10,0x0c,0x00,0x00,}, /* "y", 89 */
|
||||
{0x00,0x88,0xc8,0xa8,0x98,0x88,0x00,0x00,}, /* "z", 90 */
|
||||
{0x00,0x00,0x10,0x7c,0x82,0x00,0x00,0x00,}, /* "{", 91 */
|
||||
{0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,}, /* "|", 92 */
|
||||
{0x00,0x00,0x00,0x82,0x7c,0x10,0x00,0x00,}, /* "}", 93 */
|
||||
{0x00,0x08,0x04,0x04,0x08,0x10,0x10,0x08,}, /* "~", 94 */
|
||||
};
|
||||
|
||||
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
void OLED_Init(void) {
|
||||
OLED_WriteCommand(0xAE); // 关闭显示
|
||||
OLED_WriteCommand(0x20); // 设置内存寻址模式
|
||||
OLED_WriteCommand(0x10); // 页寻址模式
|
||||
OLED_WriteCommand(0xB0); // 设置页起始地址
|
||||
OLED_WriteCommand(0xC8); // 设置COM扫描方向
|
||||
OLED_WriteCommand(0x00); // 设置低列地址
|
||||
OLED_WriteCommand(0x10); // 设置高列地址
|
||||
OLED_WriteCommand(0x40); // 设置显示起始行
|
||||
OLED_WriteCommand(0x81); // 设置对比度
|
||||
OLED_WriteCommand(0xFF); // 最大对比度
|
||||
OLED_WriteCommand(0xA1); // 设置段重映射
|
||||
OLED_WriteCommand(0xA6); // 正常显示
|
||||
OLED_WriteCommand(0xA8); // 设置多路复用比率
|
||||
OLED_WriteCommand(0x3F); // 1/64
|
||||
OLED_WriteCommand(0xA4); // 输出跟随 RAM 内容
|
||||
OLED_WriteCommand(0xD3); // 设置显示偏移
|
||||
OLED_WriteCommand(0x00); // 无偏移
|
||||
OLED_WriteCommand(0xD5); // 设置显示时钟分频比/振荡频率
|
||||
OLED_WriteCommand(0xF0); // 高频
|
||||
OLED_WriteCommand(0xD9); // 设置预充电周期
|
||||
OLED_WriteCommand(0x22); // 修复缺少分号
|
||||
OLED_WriteCommand(0xDA); // 设置COM引脚硬件配置
|
||||
OLED_WriteCommand(0x12); // 修复缺少分号
|
||||
OLED_WriteCommand(0xDB); // 设置VCOMH电压
|
||||
OLED_WriteCommand(0x20); // 修复缺少分号
|
||||
OLED_WriteCommand(0x8D); // 设置充电泵
|
||||
OLED_WriteCommand(0x14); // 修复缺少分号
|
||||
OLED_WriteCommand(0xAF); // 打开显示
|
||||
}
|
||||
|
||||
void OLED_Clear(void) {
|
||||
memset(oled_buffer, 0, sizeof(oled_buffer));
|
||||
dirty_rect.x_min = 0;
|
||||
dirty_rect.x_max = OLED_WIDTH - 1;
|
||||
dirty_rect.y_min = 0;
|
||||
dirty_rect.y_max = OLED_HEIGHT - 1;
|
||||
dirty_rect.dirty = 1;
|
||||
OLED_UpdateScreen();
|
||||
}
|
||||
|
||||
void OLED_UpdateScreen(void) {
|
||||
OLED_UpdateDirtyScreen();
|
||||
}
|
||||
|
||||
void OLED_DrawPixel(uint8_t x, uint8_t y, uint8_t color) {
|
||||
if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return;
|
||||
|
||||
if (color) {
|
||||
if (!(oled_buffer[x + (y / 8) * OLED_WIDTH] & (1 << (y % 8)))) {
|
||||
oled_buffer[x + (y / 8) * OLED_WIDTH] |= (1 << (y % 8));
|
||||
OLED_MarkDirty(x, y);
|
||||
}
|
||||
} else {
|
||||
if (oled_buffer[x + (y / 8) * OLED_WIDTH] & (1 << (y % 8))) {
|
||||
oled_buffer[x + (y / 8) * OLED_WIDTH] &= ~(1 << (y % 8));
|
||||
OLED_MarkDirty(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_DrawChar(uint8_t x, uint8_t y, char ch, uint8_t color) {
|
||||
if (ch < ' ' || ch > '~') return;
|
||||
|
||||
if (x >= OLED_WIDTH || y >= OLED_HEIGHT || x + 8 > OLED_WIDTH || y + 8 > OLED_HEIGHT) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *font_data = oled_font[ch - ' '];
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
uint8_t column_data = font_data[i];
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
if (column_data & (1 << j)) {
|
||||
OLED_DrawPixel(x + i, y + j, color);
|
||||
} else {
|
||||
OLED_DrawPixel(x + i, y + j, !color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OLED_DrawString(uint8_t x, uint8_t y, const char *str, uint8_t color) {
|
||||
while (*str) {
|
||||
OLED_DrawChar(x, y, *str, color);
|
||||
x += 8;
|
||||
if (x + 8 > OLED_WIDTH) {
|
||||
x = 0;
|
||||
y += 8;
|
||||
}
|
||||
if (y + 8 > OLED_HEIGHT) {
|
||||
break;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Private functions -------------------------------------------------------- */
|
||||
|
||||
static void OLED_MarkDirty(uint8_t x, uint8_t y) {
|
||||
if (!dirty_rect.dirty) {
|
||||
dirty_rect.x_min = x;
|
||||
dirty_rect.x_max = x;
|
||||
dirty_rect.y_min = y;
|
||||
dirty_rect.y_max = y;
|
||||
dirty_rect.dirty = 1;
|
||||
} else {
|
||||
if (x < dirty_rect.x_min) dirty_rect.x_min = x;
|
||||
if (x > dirty_rect.x_max) dirty_rect.x_max = x;
|
||||
if (y < dirty_rect.y_min) dirty_rect.y_min = y;
|
||||
if (y > dirty_rect.y_max) dirty_rect.y_max = y;
|
||||
}
|
||||
}
|
||||
|
||||
static void OLED_UpdateDirtyScreen(void) {
|
||||
if (!dirty_rect.dirty) return;
|
||||
|
||||
uint8_t y_start = dirty_rect.y_min / 8;
|
||||
uint8_t y_end = dirty_rect.y_max / 8;
|
||||
|
||||
for (uint8_t i = y_start; i <= y_end; i++) {
|
||||
OLED_WriteCommand(0xB0 + i);
|
||||
OLED_WriteCommand(dirty_rect.x_min & 0x0F);
|
||||
OLED_WriteCommand(0x10 | (dirty_rect.x_min >> 4));
|
||||
uint8_t width = dirty_rect.x_max - dirty_rect.x_min + 1;
|
||||
OLED_WriteData(&oled_buffer[dirty_rect.x_min + i * OLED_WIDTH], width);
|
||||
}
|
||||
|
||||
dirty_rect.dirty = 0;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
#define OLED_COLOR_BLACK 0
|
||||
#define OLED_COLOR_WHITE 1
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
void OLED_Init(void);
|
||||
void OLED_Clear(void);
|
||||
void OLED_UpdateScreen(void);
|
||||
void OLED_DrawPixel(uint8_t x, uint8_t y, uint8_t color);
|
||||
void OLED_DrawString(uint8_t x, uint8_t y, const char *str, uint8_t color);
|
||||
void OLED_DrawChar(uint8_t x, uint8_t y, char ch, uint8_t color);
|
||||
void OLED_ShowChinese(uint8_t x, uint8_t y, uint8_t index);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,57 +0,0 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "pc_uart.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "bsp\uart.h"
|
||||
#include "device.h"
|
||||
|
||||
#define UART_HANDLE BSP_UART_GetHandle(BSP_UART_PC)
|
||||
|
||||
#define AI_LEN_RX_BUFF (sizeof(UART_RxData_t))
|
||||
|
||||
static bool rx_flag = false;
|
||||
|
||||
static uint8_t rxbuf[AI_LEN_RX_BUFF];
|
||||
|
||||
static void UART_RxCpltCallback(void) { rx_flag = true; }
|
||||
|
||||
int UART_Init(UART_t *huart)
|
||||
{
|
||||
UNUSED(huart);
|
||||
//注册回调函数
|
||||
HAL_UART_RegisterCallback(UART_HANDLE, BSP_UART_RX_CPLT_CB, UART_RxCpltCallback);
|
||||
return DEVICE_OK
|
||||
}
|
||||
|
||||
int UART_StartReceive(UART_t *huart)
|
||||
{
|
||||
UNUSED(huart);
|
||||
HAL_UART_Receive_DMA(UART_HANDLE, rxbuf, AI_LEN_RX_BUFF);
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
bool UART_IsReceiveComplete(void)
|
||||
{
|
||||
return rx_flag;
|
||||
}
|
||||
|
||||
int8_t UART_ParseData(UART_t *huart)
|
||||
{
|
||||
|
||||
memcpy(&huart->rx_data, rxbuf, sizeof(UART_RxData_t));
|
||||
}
|
||||
|
||||
void UART_PackTx(UART_t *huart, UART_TxData_t *tx_data)
|
||||
{
|
||||
memcpy(tx_data, huart->tx_data, sizeof(UART_TxData_t));
|
||||
}
|
||||
|
||||
int8_t UART_StartSend(UART_t *huart)
|
||||
{
|
||||
if (HAL_UART_Transmit_DMA(UART_HANDLE, huart->tx_data, sizeof(UART_TxData_t)) == HAL_OK)
|
||||
{
|
||||
return DEVICE_OK
|
||||
}
|
||||
return DEVICE_ERR;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
UART通讯模板
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t head;
|
||||
uint8_t data;
|
||||
uint8_t crc;
|
||||
} UART_RxData_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t head;
|
||||
uint8_t data;
|
||||
uint8_t crc;
|
||||
} UART_TxData_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
UART_RxData_t rx_data; // Received data buffer
|
||||
UART_TxData_t tx_data; // Transmit data buffer
|
||||
} UART_t;
|
||||
|
||||
/* Exported functions prototypes -------------------------------------------- */
|
||||
|
||||
int UART_Init(UART_t *huart);
|
||||
int UART_StartReceive(UART_t *huart);
|
||||
bool UART_IsReceiveComplete(void);
|
||||
int8_t UART_ParseData(UART_t *huart);
|
||||
void UART_PackTx(UART_t *huart, UART_TxData_t *tx_data);
|
||||
int8_t UART_StartSend(UART_t *huart);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include "main.h"
|
||||
#include "servo.h"
|
||||
|
||||
#include "bsp/servo_pwm.h"
|
||||
|
||||
|
||||
/* Private define ----------------------------------------------------------- */
|
||||
#define MIN_CYCLE 0.5f //change begin
|
||||
#define MAX_CYCLE 2.5f
|
||||
#define ANGLE_LIMIT 180 //change end
|
||||
/* Private macro ------------------------------------------------------------ */
|
||||
/* Private typedef ---------------------------------------------------------- */
|
||||
/* Private variables -------------------------------------------------------- */
|
||||
/* Private function -------------------------------------------------------- */
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
int serve_Init(BSP_PWM_Channel_t ch)
|
||||
{
|
||||
if(BSP_PWM_Start(ch)!=0){
|
||||
return -1;
|
||||
}else return 0;
|
||||
}
|
||||
|
||||
|
||||
int set_servo_angle(BSP_PWM_Channel_t ch,float angle)
|
||||
{
|
||||
if (angle < 0.0f || angle > ANGLE_LIMIT) {
|
||||
return -1; // ÎÞЧµÄ½Ç¶È
|
||||
}
|
||||
|
||||
float duty_cycle=MIN_CYCLE+(MAX_CYCLE-MIN_CYCLE)*(angle/ANGLE_LIMIT);
|
||||
if(BSP_PWM_Set(ch,duty_cycle)!=0){
|
||||
return -1;
|
||||
}else return 0;
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ----------------------------------------------------------------- */
|
||||
#include <stdint.h>
|
||||
#include "tim.h"
|
||||
#include "bsp/bsp.h"
|
||||
#include "bsp/servo_pwm.h"
|
||||
/* Exported constants ------------------------------------------------------- */
|
||||
/* Exported macro ----------------------------------------------------------- */
|
||||
/* Exported types ----------------------------------------------------------- */
|
||||
|
||||
|
||||
|
||||
extern int serve_Init(BSP_PWM_Channel_t ch);
|
||||
extern int set_servo_angle(BSP_PWM_Channel_t ch,float angle);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user