【平头哥RVB2601创意应用开发】5 同时显示字母AB,通过长按短按消除
[复制链接]
上一期,已经实现了消除A。这一期,同时显示字母AB,需要用到2个lvgl标签。怎么做呢?我想到了结构体:
struct Obj{
char name[20];
char code[20];
char *label;
int x;
int y;
}objs[2]={
{"A",".-",lv_label_create(lv_scr_act(), NULL),10,5},
{"B","-...",lv_label_create(lv_scr_act(), NULL),30,5}
};
这样,就定义了结构体,以及设置了一个只有2个结构体的数组。
接下来,还需要遍历结构体,用于lvgl的设置,包括位置,初始文字等:
for(int i=0;i<2;i++){
lv_obj_set_pos(objs[i].label, objs[i].x, objs[i].y);
lv_obj_set_size(objs[i].label, 128, 30);
lv_label_set_text(objs[i].label, objs[i].name);
}
然后还需要让这两个标签从上往下落,原理就是便利结构体数组,让每个结构体对象的y轴自增:
looptime+=1;
if(looptime % 10 ==0){
for(int i=0;i<2;i++){
objs[i].y+=1;
lv_obj_set_pos(objs[i].label, objs[i].x, objs[i].y);
}
}
这样就实现了字母往下落的效果。
接下来,就是如何消除字母,在上一期的基础上,nopress_time>30 时是输入,那么这时就遍历结构体数组,去用input和结构体的code去对比,如果发现一致,就说明匹配上了,然后去把结构体里的label对象设置为""就行了:
if(nopress_time>30){
for(int i=0;i<2;i++){
if(strcmp(input,objs[i].code)==0){
lv_label_set_text(objs[i].label, "");
}
}
strcpy(input,"");
}
全部代码如下:
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
/*********************
* INCLUDES
*********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <aos/aos.h>
#include "aos/cli.h"
#include "app_init.h"
#include "lvgl.h"
#include "lv_label.h"
#include "oled.h"
/*********************
* DEFINES
*********************/
#define TAG "app"
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
// GUI
static void gui_lvgl_task(void *arg);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
volatile uint32_t g_debug = 0;
volatile uint32_t g_debug_v = 0;
static void gui_lvgl_task(void *arg);
#include "csi_config.h"
#include "board_config.h"
#include "drv/gpio_pin.h"
#include <drv/pin.h>
#include "csi_core.h"
static csi_gpio_pin_t key1;
static csi_gpio_pin_t key2;
int button1_pressed = 0;// button1 是否被按下
int button2_pressed = 0;// button2 是否被按下
int time_for_pressed=0;// 按下的时间计数
int nopress_time=0; //没有按下时 计数
int looptime = 0;
char input[12];
/**
* main
*/
int main(void)
{
board_yoc_init();
aos_task_new("gui", gui_lvgl_task, NULL, 10 * 1024);
return 0;
}
static void gui_lvgl_task(void *arg)
{
lv_init();
oled_init();
struct Obj{
char name[20];
char code[20];
char *label;
int x;
int y;
}objs[2]={
{"A",".-",lv_label_create(lv_scr_act(), NULL),10,5},
{"B","-...",lv_label_create(lv_scr_act(), NULL),30,5}
};
for(int i=0;i<2;i++){
lv_obj_set_pos(objs[i].label, objs[i].x, objs[i].y);
lv_obj_set_size(objs[i].label, 128, 30);
lv_label_set_text(objs[i].label, objs[i].name);
}
csi_pin_set_mux(PA11, PIN_FUNC_GPIO);
csi_pin_set_mux(PA12, PIN_FUNC_GPIO);
csi_gpio_pin_init(&key1, PA11);
csi_gpio_pin_dir(&key1, GPIO_DIRECTION_INPUT);
csi_gpio_pin_init(&key2, PA12);
csi_gpio_pin_dir(&key2, GPIO_DIRECTION_INPUT);
while (1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_task_handler();
looptime+=1;
if(looptime % 10 ==0){
for(int i=0;i<2;i++){
objs[i].y+=1;
lv_obj_set_pos(objs[i].label, objs[i].x, objs[i].y);
}
}
if(GPIO_PIN_LOW == csi_gpio_pin_read(&key1)) {
//当低电平(按下时)
if(button1_pressed==0){
// 如果button1的状态没有被按下,则设为按下
button1_pressed = 1;
}
if(button1_pressed==1){
//如果button1的状态是按下,则计时
time_for_pressed++;
}
}
if(GPIO_PIN_HIGH == csi_gpio_pin_read(&key1)) {
if(button1_pressed==0){
nopress_time++;
if(nopress_time>30){
//check_input();
for(int i=0;i<2;i++){
if(strcmp(input,objs[i].code)==0){
lv_label_set_text(objs[i].label, "");
}
}
strcpy(input,"");
}
}
if(button1_pressed==1){
nopress_time=0;
// printf("%d",time_for_pressed);
//通过判断time_for_pressed的大小,得出是长按还是短按
if(time_for_pressed>20){
//长按
printf("-");
strcat(input,"-");
//lv_textarea_add_text(text,"-");
}else{
//短按
printf(".");
strcat(input,".");
//lv_textarea_add_text(text,".");
}
time_for_pressed=0;//恢复计数
button1_pressed = 0;
}
}
if(GPIO_PIN_LOW == csi_gpio_pin_read(&key2)) {
if(button2_pressed==0){
button2_pressed=1;
}
}
if(GPIO_PIN_HIGH == csi_gpio_pin_read(&key2)) {
if(button2_pressed==1){
printf("\n");
button2_pressed=0;
strcpy(input,"");
}
}
aos_msleep(5);
lv_tick_inc(1);
}
}
演示效果:
|