【平头哥RVB2601创意应用开发】6 分数显示,消除字母后分数+1
[复制链接]
上一期实现了消除字母,这一期在这基础上加一个分数的lvgl控件。
所以要有一个变量来记录分数:
int score=0;//分数初始为0
还有设置一个label标签:
lv_obj_t *score_label = lv_label_create(lv_scr_act(), NULL);
lv_obj_set_pos(score_label, 0, 0);
lv_obj_set_size(score_label, 128, 30);
lv_label_set_text(score_label,"score:0");
关键代码其实是消除字母后,让分数+1后的数字转字符:
char value[10];
char text_score[20]="score:";
score++;
sprintf(value, "%d", score);
strcat(text_score,value);
lv_label_set_text(score_label,text_score);
其中,数组转字符用的是 sprintf 函数。字符串拼接用的是 strcat 函数。
以下是全部代码(里面有包括用gpio中断但被注释的代码,因为gpio中断不能实现长按):
/*
* 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];
int score=0;//分数初始为0
#define GPIO_CHECK_RETURN(ret) \
do { \
if (ret != CSI_OK) { \
return -1; \
} \
} while(0);
static void key1_handler(csi_gpio_pin_t *pin, void *arg)
{
printf("\n");
printf(time_for_pressed);
time_for_pressed ++;
}
static void key2_handler(csi_gpio_pin_t *pin, void *arg)
{
// time_for_pressed ++;
}
/**
* main
*/
int main(void)
{
int ret;
board_yoc_init();
// 下面是针对PA11和PA12的中断,之所以注释掉,是因为中短不能实现长按。但是用于一般的按键,比如普通游戏里的方向键什么的是可以的。
// ret = csi_gpio_pin_init(&key1, PA11);
// GPIO_CHECK_RETURN(ret);
//
// /* Attach callback */
// ret = csi_gpio_pin_attach_callback(&key1, key1_handler, NULL);
// GPIO_CHECK_RETURN(ret);
//
// /* Set pull-up mode */
// ret = csi_gpio_pin_mode(&key1, GPIO_MODE_PULLUP);
// GPIO_CHECK_RETURN(ret);
//
// /* Set input mode */
// ret = csi_gpio_pin_dir(&key1, GPIO_DIRECTION_INPUT);
// GPIO_CHECK_RETURN(ret);
//
// /* Set falling-edge triger mode */
// ret = csi_gpio_pin_irq_mode(&key1, GPIO_IRQ_MODE_FALLING_EDGE);
// GPIO_CHECK_RETURN(ret);
//
// /* Enable irq */
// ret = csi_gpio_pin_irq_enable(&key1, true);
// GPIO_CHECK_RETURN(ret);
//
//
// ret = csi_gpio_pin_init(&key2, PA12);
// GPIO_CHECK_RETURN(ret);
//
// /* Attach callback */
// ret = csi_gpio_pin_attach_callback(&key2, key2_handler, NULL);
// GPIO_CHECK_RETURN(ret);
//
// /* Set pull-up mode */
// ret = csi_gpio_pin_mode(&key2, GPIO_MODE_PULLUP);
// GPIO_CHECK_RETURN(ret);
//
// /* Set input mode */
// ret = csi_gpio_pin_dir(&key2, GPIO_DIRECTION_INPUT);
// GPIO_CHECK_RETURN(ret);
//
// /* Set falling-edge triger mode */
// ret = csi_gpio_pin_irq_mode(&key2, GPIO_IRQ_MODE_FALLING_EDGE);
// GPIO_CHECK_RETURN(ret);
//
// /* Enable irq */
// ret = csi_gpio_pin_irq_enable(&key2, true);
// GPIO_CHECK_RETURN(ret);
aos_task_new("gui", gui_lvgl_task, NULL, 10 * 1024);
return 0;
}
static void gui_lvgl_task(void *arg)
{
lv_init();
oled_init();
lv_obj_t *score_label = lv_label_create(lv_scr_act(), NULL);
lv_obj_set_pos(score_label, 0, 0);
lv_obj_set_size(score_label, 128, 30);
lv_label_set_text(score_label,"score:0");
struct Obj{
char name[20];
char code[20];
lv_obj_t *label;
int x;
int y;
}objs[2]={
{"A",".-",lv_label_create(lv_scr_act(), NULL),30,-20},
{"B","-...",lv_label_create(lv_scr_act(), NULL),60,-10}
};
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, "");
char value[10];
char text_score[20]="score:";
score++;
sprintf(value, "%d", score);
strcat(text_score,value);
lv_label_set_text(score_label,text_score);
}
}
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,"");
//lv_textarea_del_char(text);
}
}
aos_msleep(5);
lv_tick_inc(1);
}
}
演示效果如下:
|