stm32菜单按键的设计
Posted htsky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了stm32菜单按键的设计相关的知识,希望对你有一定的参考价值。
有点懒。看注释吧
// k0,enter/enable;k3:esc/disable
// k1,value+/menu+;k2:menu-/value-;
#include "sysmenu.h"
#define KUP 2
#define KDWN 1
#define KNA 0
uint8_t ktimeout; //0:logging time forever;n>0:logging n mins for keyed
uint8_t kloginlife; //the count for logging refer to logtimeout;0:logoff
uint8_t kstat; //logoff=0;1=menu;2=value
uint8_t kstep; //menu and value step count
// uint8_t km 0:kstep,1:kstep--,2:kstep++
uint16_t kvar; //For the value to change
uint8_t kmenu_init(void)
{
kstat=0;
kstep=0;
kvar=0;
}
void kinfo(void)
{
/*
printf("1-SystemCoreClock %d\r\n",SystemCoreClock);
printf("2-RCC_APB2Periph_USART1 %d\r\n",RCC_APB2Periph_USART1);
printf("3-USART1->BRR \t %d\r\n",USART1->BRR);
printf("Now is %d-%02d-%02d %02d:%02d:%02d Week %d \n\r",mcalendar.year,mcalendar.month,mcalendar.day,mcalendar.hour,mcalendar.min,mcalendar.sec,mcalendar.week);
// return 0;
*/
printf("Kylin MINI\r");
}
void kdata(void)
{
printf("%d-%02d-%02d %02d:%02d:%02d Week %d \r",mcalendar.year,mcalendar.month,mcalendar.day,mcalendar.hour,mcalendar.min,mcalendar.sec,mcalendar.week);
}
void kclean(void)
{
printf("\r \r");
}
void klogon(void)
{
if(ktimeout>0){
kloginlife=ktimeout;
}
}
void klogoff(void)
{
kloginlife=0;
kclean();
printf("\rLogoffing");
}
// key0 for enter or enable
void kset0(void){
kclean();
switch(kstat){
case 0:
kstat=1;
klogon();
kmenu(0);
// printf("\r loggin \r");
break;
case 1:
kstat=2;
klogon();
kedit();
break;
case 2:
klogon();
kack();
kstat=1;
break;
default:kstat=1;kmenu(KNA);
}
}
//key1 for menu++ or value++
void kset1(void)
{
kclean();
switch(kstat){
case 0:
printf("\rKEY up");
break;
case 1:
klogon();
kmenu(KUP);
break;
case 2:
klogon();
kvalue(KUP);
break;
}
}
//key2 for menu-- or value--
void kset2(void)
{
kclean();
switch(kstat){
case 0:
printf("\rKEY down");
break;
case 1:
klogon();
kmenu(KDWN);
break;
case 2:
klogon();
kvalue(KDWN);
break;
}
}
//ke3 for quit or disable
void kset3(void)
{
kclean();
switch(kstat){
case 0:
klogoff();
printf("\rLogoffed");
break;
case 1:
kstat=0;
klogoff();
break;
case 2:
kstat=1;
klogon();
kvar=0;
kmenu(KNA);
break;
default:printf("\r KEY quit \r");
}
}
void kmenu(uint8_t km)
{
if(km==KDWN){
if(kstep){
kstep--;
}
}
if(km==KUP){
kstep++;
}
//km=KNA;
switch(kstep){
case 0:
printf("\rShow info");
break;
case 1:
printf("\rShow data");
break;
case 2:
printf("\rShow temp");
break;
case 3:
printf("\rShow RH");
break;
case 4:
printf("\rSet year");
break;
case 5:
printf("\rSet mon");
break;
case 6:
printf("\rSet day");
break;
case 7:
printf("\rSet hour");
break;
case 8:
printf("\rSet min");
break;
case 9:
printf("\rSet sec");
break;
default:kstep=0; printf("\rLogoff");
}
}
void kedit(void)
{
switch(kstep){
case 0:
printf("\rShow info:");
kinfo();
break;
case 1:
printf("\rShow data:");
kdata();
break;
case 2:
printf("\rShow temp:");
printf("%.2f",SHT2x_MeasureTempPoll());
break;
case 3:
printf("\rShow RH:");
printf("%.2f ",SHT2x_MeasureHumiPoll());
break;
case 4:
printf("\rSet year:");
kvar=mcalendar.year;
break;
case 5:
printf("\rSet mon:");
kvar=mcalendar.month;
break;
case 6:
printf("\rSet day:");
kvar=mcalendar.day;
break;
case 7:
printf("\rSet hour:");
kvar=mcalendar.hour;
break;
case 8:
printf("\rSet min:");
kvar=mcalendar.min;
break;
case 9:
printf("\rSet sec:");
kvar=mcalendar.sec;
break;
default: kstep=0;printf("\r Nothing \r");
}
if(kvar>0){
printf("%d",kvar);
}
}
void kvalue(uint8_t km)
{
switch(kstep){
/*
case 0:
printf("\rkeypress info: ");
if(km==KUP){
printf("\rKEY UP ");
}
if(km==KDWN){
printf("\rKEY DOWN");
}
break;
case 1:
printf("\rkeypress data:");
if(km==KUP){
printf("\rKEY UP ");
}
if(km==KDWN){
printf("\rKEY DOWN");
}
break;
case 2:
printf("\rShow temp: ");
if(km==KUP){
printf("\rKEY UP ");
}
if(km==KDWN){
printf("\rKEY DOWN");
}
break;
case 3:
printf("\rShow RH:");
if(km==KUP){
printf("\rKEY UP ");
}
if(km==KDWN){
printf("\rKEY DOWN");
}
break;
*/
case 4:
printf("\rSet year:");
if(km==KUP){
if(kvar<2100){
kvar+=1;
}
}
if(km==KDWN){
if(kvar>2000){
kvar-=1;
}
}
break;
case 5:
printf("\rSet mon:");
if(km==KUP){
if(kvar<12){
kvar++;
}
}
if(km==KDWN){
if(kvar>0){
kvar--;
}
}
break;
case 6:
printf("\rSet day:");
if(km==KUP){
if(kvar<31){
kvar++;
}
}
if(km==KDWN){
if(kvar>0){
kvar--;
}
}
break;
case 7:
printf("\rSet hour:");
if(km==KUP){
if(kvar<23){
kvar++;
}
}
if(km==KDWN){
if(kvar>0){
kvar--;
}
}
break;
case 8:
printf("\rSet min:");
if(km==KUP){
if(kvar<59){
kvar++;
}
}
if(km==KDWN){
if(kvar>0){
kvar--;
}
}
break;
case 9:
printf("\rSet sec:");
if(km==KUP){
if(kvar<59){
kvar++;
}
}
if(km==KDWN){
if(kvar>0){
kvar--;
}
}
break;
default: kstep=0;printf("\rNothing");
}
if(kvar>0)
{
printf("%d",kvar);
}
}
void kack(void)
{
switch(kstep){
/*
case 0:
// printf("\rkeypress info:");
kmenu(kstep);
break;
case 1:
// printf("\rkeypress data:");
kmenu(kstep);
break;
case 2:
// printf("\rShow temp: ");
kmenu(kstep);
break;
case 3:
// printf("\rShow RH:");
kmenu(kstep);
break;
*/
case 4:
// printf("\rSet year:");
if(kvar!=mcalendar.year){
setTime(kvar,mcalendar.month,mcalendar.day,mcalendar.hour,mcalendar.min,mcalendar.sec);
}
break;
case 5:
// printf("\rSet mon:");
setTime(mcalendar.year,kvar,mcalendar.day,mcalendar.hour,mcalendar.min,mcalendar.sec);
break;
case 6:
// printf("\rSet day:");
setTime(mcalendar.year,mcalendar.month,kvar,mcalendar.hour,mcalendar.min,mcalendar.sec);
break;
case 7:
// printf("\rSet hour:");
setTime(mcalendar.year,mcalendar.month,mcalendar.day,kvar,mcalendar.min,mcalendar.sec);
break;
case 8:
// printf("\rSet min:");
setTime(mcalendar.year,mcalendar.month,mcalendar.day,mcalendar.hour,kvar,mcalendar.sec);
break;
case 9:
// printf("\rSet sec:");
setTime(mcalendar.year,mcalendar.month,mcalendar.day,mcalendar.hour,mcalendar.min,kvar);
break;
default: kstep=1;kmenu(KNA);
}
if(kvar){
kdata();
}
kvar=0;
}
以上是关于stm32菜单按键的设计的主要内容,如果未能解决你的问题,请参考以下文章
基于STM32的智能温室控制系统仿真电路设计(温控补光)-基于STM32的智能蓝牙温控风扇控制系统设计-基于STM32的无线蓝牙心电监护仪系统设计毕设课设分享