【平头哥RVB2601创意应用开发】4 长按短按消除字母A
[复制链接]
本帖最后由 cybertovsky 于 2022-4-9 21:20 编辑
上周,实现了字母A的由上至下往下落。
本周,实现长按短按消除字母A。思路是,长按短按组成一个字符串,通过对比字符串,判断输入的十分正确。
通过查询摩尔斯电码得知,字母A的摩尔斯电码是“.-"
因此,用一个名为input的变量,接受输入:
char input[12];
对于长按短按,使用字符串拼接函数进行拼接:
//长按
strcat(input,"-");
//短按
strcat(input,".");
莫尔斯码判断函数:
if(strcmp(input,".-")==0){
lv_label_set_text(text_label, "");//把字母A设为"”
strcpy(input,"");//把输入的字符串设置为“”
lv_label_set_text(input_label, 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; //没有按下时 计数
lv_obj_t *text_label;
lv_obj_t *input_label;
int y=-10;
int time_for_y=0;
char text[200];
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_label_text_create(void)
{
text_label = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(text_label, LV_LABEL_LONG_BREAK);
lv_label_set_align(text_label, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(text_label, 0, y);
lv_obj_set_size(text_label, 128, 30);
lv_label_set_text(text_label, "A");
}
static void gui_label_input_create(void)
{
input_label = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(input_label, LV_LABEL_LONG_BREAK);
lv_label_set_align(input_label, LV_LABEL_ALIGN_CENTER);
lv_obj_set_pos(input_label, 0, 50);
lv_obj_set_size(input_label, 128, 30);
lv_label_set_text(input_label, "");
// lv_label_set_align(input_label, LV_LABEL_ALIGN_RIGHT);
}
static void text_append(char* c){
strcat(text,c);
lv_label_set_text(text_label, text);
}
static void check_input(void){
if(strcmp(input,".-")==0){
//text_append("A");
lv_label_set_text(text_label, "");
strcpy(input,"");
lv_label_set_text(input_label, input);
}
}
static void gui_lvgl_task(void *arg)
{
lv_init();
/*Initialize for LittlevGL*/
oled_init();
/*Select display 1*/
// demo_create();
gui_label_text_create();
gui_label_input_create();
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();
time_for_y++;
if(time_for_y % 20==0){
y++;
}
lv_obj_set_pos(text_label, 0, 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();
}
}
if(button1_pressed==1){
nopress_time=0;
// printf("%d",time_for_pressed);
//通过判断time_for_pressed的大小,得出是长按还是短按
if(time_for_pressed>20){
//长按
printf("-");
strcat(input,"-");
}else{
//短按
printf(".");
strcat(input,".");
}
time_for_pressed=0;//恢复计数
button1_pressed = 0;
lv_label_set_text(input_label, input);
}
}
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;
strcat(input,"");
}
}
aos_msleep(5);
lv_tick_inc(1);
}
}
视频效果:
|