1799 views|4 replies

26

Posts

0

Resources
The OP
 

[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:


Latest reply

Come on, I look forward to your more exciting next time. You can make a game console.   Details Published on 2022-4-14 08:23
 
 

6841

Posts

11

Resources
2
 

Thank you for posting so late at night!

Long press and short press of a button is the most classic implementation of the state machine programming concept. You use it very well. The effect of the implementation is also very good.

I have a suggestion. If you have time, can you introduce a key interrupt to enable the timer and detect the key status in the timer? This will make the task easier to implement.

Comments

OK, I'll look into the usage of interrupts later.  Details Published on 2022-4-14 07:47
 
 
 

26

Posts

0

Resources
3
 
lugl4313820 posted on 2022-4-14 06:37 You are still posting so late, thank you for your hard work! Long press and short press of buttons are the most classic implementation of state machine programming ideas. You use them very well. See the actual...

OK, I'll look into the usage of interrupts later.

Comments

Come on, I look forward to your more exciting next one. You can make a game console.  Details Published on 2022-4-14 08:23
 
 
 

9720

Posts

24

Resources
4
 

Are you wearing anti-static gloves?

 
 
 

6841

Posts

11

Resources
5
 
cybertovsky posted on 2022-4-14 07:47 OK, I will study the usage of interrupts later.

Come on, I look forward to your more exciting next time. You can make a game console.

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list