上次的残留问题列举
1、刷新率位1Hz;
这个是因为,如果我在触摸回调函数中再写个LCD_1IN28_Display,就会冲突,导致黑屏。所以触摸回调里头我只是改变了数字,要想显示得等下次刷新;
2、左上角按钮容易误触,直接进入设置时间模式;
3、不能显示电池百分数;
4、不能调亮暗。
对应问题方案
1、开始想加快刷新,但是秒数自增和DEV_Delay_ms又是在同一个函数下,总不能一直画,到计时超过了目标就自增,那得差好多微秒。所以答案只有一个!!!!多线程。
可是free_rtos报错 缺少什么avr/io.h 用不了。。。之后陆续试了好多个多线程的函数库,都说不一定跟你的RP2040适配,所以我就搜了“rp2040 timer”,毕竟多线程似乎也不用,我只是需要个计时器。果然#include "RPi_Pico_TimerInterrupt.h"就好使了。这样时间自增就可以在计时器回调函数里头运行了,然后loop里头还是刷新界面。
2、自带的函数里头就有“长按”、“滑动”两个触屏动作,把进入设置状态,定义为长按触发,再次长按关闭,把进入陀螺仪设置成向下滑动。
3、问了客服,检测ADC电压就是测电池电量。
问题来了,这手表插上电池电压就上去了,如果平时电压4.0,插上电池4.7,那算出来就超过100%了;所以其实是拔下充电器,带电池的时候,4.0就是慢点,超过100%就让他等于100%,这样子定义吧。
4、DEV_SET_PWM(0);就是背光板亮度,0到100,不问不知道啊,DEV_GPIO_Mode(LCD_BL_PIN, 1);就是设置背光板为读入端口。我还发现,LCD背光板不是很线性,小值附近,突然就亮了,突然就灭了,有个临界,像是击穿区。100分10份儿,还是细度不够。
这是程序:
#include "LCD_Test.h"
// 【计时
#define TIMER_INTERRUPT_DEBUG 1
#define _TIMERINTERRUPT_LOGLEVEL_ 4
#define TIMER0_INTERVAL_MS 1000
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "RPi_Pico_TimerInterrupt.h"
RPI_PICO_Timer ITimer0(0);
// 计时】
void Touch_INT_callback();
uint8_t flag = 0;
int Hour = 22;
int Min = 22;
int Sec = 22;
uint8_t iLight = 5;
uint32_t LTIMSE = 0;
UWORD *BlackImage;
volatile int Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro = 0;
unsigned int tim_count = 0;
float acc[3], gyro[3];
const float conversion_factor = 3.3f / (1 << 12) * 3;
String comTemp = "";
char strTemp[8]={'\0'};
char temp;
int iCount = 0;
bool TimerHandler0(struct repeating_timer *t)
{
Sec++;
if (Sec > 59) {
Sec = 0;
Min++;
}
if (Min > 59) {
Min = 0;
Hour++;
}
if (Hour > 23) {
Hour = 0;
}
return true;
}
void SerialEvent()
{
while(Serial.available())
{
temp= char(Serial.read());
strTemp[iCount++]=temp;
comTemp += temp;
delay(2);
int len = comTemp.length();
if(len>=8)
{
Hour = (strTemp[0]-0x30)*10+(strTemp[1]-0x30);
Min = (strTemp[3]-0x30)*10+(strTemp[4]-0x30);
Sec = (strTemp[6]-0x30)*10+(strTemp[7]-0x30);
Serial.print(Hour);
Serial.print(Min);
Serial.print(Sec);
comTemp="";
iCount=0;
}
}
}
void MyTickgo()
{
//while (true) {
//uint32_t LTIMSE=millis();
// Sec++;
// if (Sec > 59) {
// Sec = 0;
// Min++;
// }
// if (Min > 59) {
// Min = 0;
// Hour++;
// }
// if (Hour > 23) {
// Hour = 0;
// }
Paint_Clear(BLACK);
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 0)
{
Paint_DrawString_EN(50, 40, "Run", &Font24,BLACK, WHITE);
//电池
//uint8_t val = DEV_Digital_Read(BAT_ADC_PIN);
uint16_t val = analogRead(BAT_ADC_PIN);
double batper = val*conversion_factor;
batper = batper/4.0*100.0;
if(batper>100)
{
batper=100;
}
Paint_DrawNum(110, 40, batper, &Font24, 1, WHITE, BLACK);
if(batper>=100)
{
Paint_DrawString_EN(195, 40, "%", &Font24,BLACK, WHITE);
}
else
{
Paint_DrawString_EN(175, 40, "%", &Font24,BLACK, WHITE);
}
double result = DEC_ADC_Read()*conversion_factor;
Paint_DrawNum(110, 65, result, &Font24, 1, WHITE, BLACK);
}
else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 5)
{
Paint_DrawString_EN(50, 40, "gyro", &Font24, BLACK, WHITE);
myTuoLuo();
}
else
{
//Paint_DrawRectangle(0, 160, 120, 240, 0X2595, DOT_PIXEL_2X2, DRAW_FILL_FULL);
//Paint_DrawRectangle(121, 160, 240, 240, 0X6634, DOT_PIXEL_2X2, DRAW_FILL_FULL);
Paint_DrawString_EN(60, 170, "-", &Font24, BLACK, WHITE);
Paint_DrawString_EN(180, 170, "+", &Font24, BLACK, WHITE);
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 1)
{
Paint_DrawString_EN(50, 40, "Hour", &Font24, BLACK, WHITE);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 2)
{
Paint_DrawString_EN(50, 40, "Min", &Font24, BLACK, WHITE);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 3)
{
Paint_DrawString_EN(50, 40, "Sec", &Font24, BLACK, WHITE);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 4)
{
Paint_DrawString_EN(50, 40, "Light", &Font24, BLACK, WHITE);
}
}
if(Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro!=5)
{
Paint_DrawNum(20, 88, Hour, &Font64, 0, WHITE, BLACK);
Paint_DrawString_EN(80, 88, ":", &Font64, BLACK, WHITE);
Paint_DrawNum(108, 88, Min, &Font64, 0, WHITE, BLACK);
Paint_DrawString_EN(170, 88, ":", &Font64, BLACK, WHITE);
Paint_DrawNum(198, 125, Sec, &Font24, 0, WHITE, BLACK);
}
else
{
}
SerialEvent();
LCD_1IN28_Display(BlackImage);
//uint32_t pp=millis();
//DEV_Delay_ms(1000-pp+LTIMSE);
//}
}
void myTuoLuo()
{
//uint16_t result;
//result = DEC_ADC_Read();
QMI8658_read_xyz(acc, gyro, &tim_count);
Paint_DrawString_EN(40, 100, "ACC_X=", &Font16, BLACK, WHITE);
Paint_DrawString_EN(40, 120, "ACC_Y=", &Font16, BLACK, WHITE);
Paint_DrawString_EN(40, 140, "ACC_Z=", &Font16, BLACK, WHITE);
Paint_DrawString_EN(40, 160, "GYR_X=", &Font16, BLACK, WHITE);
Paint_DrawString_EN(40, 180, "GYR_Y=", &Font16, BLACK, WHITE);
Paint_DrawString_EN(40, 200, "GYR_Z=", &Font16, BLACK, WHITE);
Paint_DrawNum(120, 100, acc[0], &Font16, 2, WHITE, BLACK);
Paint_DrawNum(120, 120, acc[1], &Font16, 2, WHITE, BLACK);
Paint_DrawNum(120, 140, acc[2], &Font16, 2, WHITE, BLACK);
Paint_DrawNum(120, 160, gyro[0], &Font16, 2, WHITE, BLACK);
Paint_DrawNum(120, 180, gyro[1], &Font16, 2, WHITE, BLACK);
Paint_DrawNum(120, 200, gyro[2], &Font16, 2, WHITE, BLACK);
//Paint_DrawNum(40, 80, result * conversion_factor, &Font16, 2, WHITE, BLACK);
}
void setup() {
// put your setup code here, to run once:
if (DEV_Module_Init() != 0)
Serial.println("GPIO Init Fail!");
else
Serial.println("GPIO Init successful!");
LCD_1IN28_Init(HORIZONTAL);
DEV_SET_PWM(0);
LCD_1IN28_Clear(WHITE);
DEV_SET_PWM(5);
UDOUBLE Imagesize = LCD_1IN28_HEIGHT * LCD_1IN28_WIDTH * 2;
//UWORD *BlackImage;
if ((BlackImage = (UWORD *)malloc(Imagesize)) == NULL) {
Serial.println("Failed to apply for black memory...");
exit(0);
}
// /*1.Create a new image cache named IMAGE_RGB and fill it with white*/
Paint_NewImage((UBYTE *)BlackImage, LCD_1IN28.WIDTH, LCD_1IN28.HEIGHT, 0, WHITE);
Paint_SetScale(65);
Paint_Clear(WHITE);
Paint_SetRotate(ROTATE_0);
Paint_Clear(WHITE);
// /* GUI */
Serial.println("drawing...\r\n");
// /*2.Drawing on the image*/
ITimer0.attachInterruptInterval(TIMER0_INTERVAL_MS * 1000, TimerHandler0);
#if 1
Paint_DrawLine(35, 35, 205, 35, MAGENTA, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
Paint_DrawLine(205, 35, 205, 205, MAGENTA, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
Paint_DrawLine(35, 205, 205, 205, MAGENTA, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
Paint_DrawLine(35, 35, 35, 205, MAGENTA, DOT_PIXEL_1X1, LINE_STYLE_SOLID);
//Paint_DrawString_EN(50, 100, "HAHA", &Font20, 0x000f, 0xfff0);
//Paint_DrawString_EN(50, 161, "WaveShare", &Font16, RED, WHITE);
Paint_DrawString_CN(54, 72, "恭\n", &Font24CN, BLUE, WHITE);
Paint_DrawString_CN(86, 72, "喜\n", &Font24CN, BLUE, WHITE);
Paint_DrawString_CN(118, 72, "發\n", &Font24CN, BLUE, WHITE);
Paint_DrawString_CN(150, 72, "财\n", &Font24CN, BLUE, WHITE);
// /*3.Refresh the picture in RAM to LCD*/
LCD_1IN28_Display(BlackImage);
DEV_Delay_ms(200);
#endif
#if 1
QMI8658_init();
CST816S_init(CST816S_Gesture_Mode);
DEV_KEY_Config(Touch_INT_PIN);
attachInterrupt(Touch_INT_PIN, &Touch_INT_callback, RISING);
Serial.println("QMI8658_init\r\n");
MyTickgo();
#endif
#if 0
flag = 1;
Paint_Clear(WHITE);
//Paint_DrawRectangle(0, 00, 240, 47, 0X2595, DOT_PIXEL_2X2, DRAW_FILL_FULL);
//Paint_DrawString_EN(60, 30, "Touch test", &Font16, WHITE, BLACK);
LCD_1IN28_Display(BlackImage);
CST816S_init(CST816S_Point_Mode);
while (true)
{
if (flag == 1)
{
Paint_DrawPoint(Touch_CTS816.x_point, Touch_CTS816.y_point, BLUE, DOT_PIXEL_1X1, DOT_FILL_AROUND);
LCD_1IN28_DisplayWindows(Touch_CTS816.x_point, Touch_CTS816.y_point, Touch_CTS816.x_point + 1, Touch_CTS816.y_point + 1, BlackImage);
Serial.printf("X:%d Y:%d\r\n", Touch_CTS816.x_point, Touch_CTS816.y_point);
flag = 0;
}
DEV_Delay_us(500);
}
#endif
}
void loop()
{
MyTickgo();
}
void Touch_INT_callback() {
uint8_t gesture = CST816S_Get_Gesture();
if (gesture == CST816S_Gesture_Long_Press)
{
if(Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro!=1)
{
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=1;
}
else
{
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=0;
}
// if(Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro==5)
// {
// Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=0;
// }
// else
// {
// Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=5;
// }
}
else if(gesture == CST816S_Gesture_Click)
{
CST816S mypoint = CST816S_Get_Point();
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 0) {
Paint_DrawString_EN(50, 40, "Run", &Font24, WHITE, BLACK);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 1) {
Paint_DrawString_EN(50, 40, "Hour", &Font24, WHITE, BLACK);
Paint_DrawString_EN(60, 170, "-", &Font24, WHITE, BLACK);
Paint_DrawString_EN(180, 170, "+", &Font24, WHITE, BLACK);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 2) {
Paint_DrawString_EN(50, 40, "Min", &Font24, WHITE, BLACK);
Paint_DrawString_EN(60, 170, "-", &Font24, WHITE, BLACK);
Paint_DrawString_EN(180, 170, "+", &Font24, WHITE, BLACK);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 3) {
Paint_DrawString_EN(50, 40, "Sec", &Font24, WHITE, BLACK);
Paint_DrawString_EN(60, 170, "-", &Font24, WHITE, BLACK);
Paint_DrawString_EN(180, 170, "+", &Font24, WHITE, BLACK);
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 4) {
Paint_DrawString_EN(50, 40, "Light", &Font24, WHITE, BLACK);
Paint_DrawString_EN(60, 170, "-", &Font24, WHITE, BLACK);
Paint_DrawString_EN(180, 170, "+", &Font24, WHITE, BLACK);
}
if (mypoint.x_point < 120 && mypoint.y_point < 120 && Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro !=0) //左上 模式修改
{
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro = Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro + 1;
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro > 4) {
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro = 0;
}
}
else if (mypoint.x_point > 121 && mypoint.y_point > 160) // 增加 0, 160, 120, 240
{
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 1)
{
Hour++;
if (Hour > 23)
{
Hour = 0;
}
}
else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 2)
{
Min++;
if (Min > 59)
{
Min = 0;
}
}
else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 3)
{
Sec++;
if (Sec > 59)
{
Sec = 0;
}
}
else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 4)
{
iLight=iLight+5;
if (iLight > 100)
{
iLight = 100;
}
DEV_SET_PWM(iLight);
}
}
else if (mypoint.x_point < 120 && mypoint.y_point > 160) // 减少 121, 160, 240, 240
{
if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 1) {
Hour--;
if (Hour < 0) {
Hour = 23;
}
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 2) {
Min--;
if (Min < 0) {
Min = 59;
}
} else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 3) {
Sec--;
if (Sec < 0) {
Sec = 59;
}
}else if (Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro == 4)
{
iLight=iLight-5;
if (iLight < 0)
{
iLight = 0;
}
DEV_SET_PWM(iLight);
}
}
}
else if(gesture == CST816S_Gesture_Down)
{
if(Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro==5)
{
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=0;
}
else
{
Mode_0Over_1Hou_2Min_3Sec_4Light_5gyro=5;
}
}
}
这次改进非常的开心,都是小问题,解决他们的工作量可不小,从自我怀疑,不确定能不能改好,到解决到自己满意,这个过程加结果很解压,感觉没有被造物主抛弃,可以连接到宇宙知识库,获取无限智慧。所以再次45度角,仰望天空,用忧郁的眼神审视墙角的蜘蛛网,生活的烦恼就暂时消散了。
用RP2040-Touch-LCD-1.28制作一个手表_再