[RVB2601 Creative Application Development] 5 Display letters AB at the same time, and eliminate them by long pressing and short pressing
[Copy link]
In the last issue, we have eliminated A. In this issue, we need to display the letters AB at the same time, which requires two lvgl tags. How to do it? I thought of the structure:
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}
};
In this way, the structure is defined and an array of only 2 structures is set.
Next, you need to traverse the structure for lvgl settings, including position, initial text, etc.:
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);
}
Then you need to make these two labels fall from top to bottom. The principle is to facilitate the structure array and let the y-axis of each structure object increase automatically:
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);
}
}
This creates the effect of the letters falling down.
Next, we will learn how to eliminate letters. Based on the previous article, when nopress_time>30, it is input. Then we will traverse the structure array and compare the input with the structure code. If they are consistent, it means they match. Then we can set the label object in the structure to "":
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,"");
}
The full code is as follows:
/*
* 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);
}
}
Demonstration effect:
|