/* * mymain.c * * Created on: 2022年4月15日 * Author: wuwen */ #include "mymain.h" #define morse_play_t1 50 #define morse_play_t2 (morse_play_t1*3) #define morse_play_hz 1000 char config_buzzer_for_button_pass=1; char config_buzzer_for_button_longpass=1; char config_buzzer_for_sele_change=1; button B1,TACKA,TACKB;//创建一个按钮 encoder E1;//创建一个编码器 int mode=0; uint32_t run_tick=0,jump_tick=0; char str[32]; uint16_t fps=0,fps_=0; char sys_lan=0; int encode_c=0; int test_code=0; menu menu_main= { 0,0, "Main GUI\nInput mode\nBuzzer\nDisplay\nLanguage\nAbout", "主页\n输入模式\n蜂鸣器\n显示\n语言\n关于",5, NULL }; menu buzzer_setting= { 0,0, "Back\nClick\nPress\nSele change", "返回\n点击\n长按\n选择切换",3, }; word turnon= { "ON", "开" }; word turnoff= { "OF", "关" }; int SHOW_MENU(menu *m,int encoder,char button,char lan,char dismode) { int return_code=-1; const char *list; int sele_dis_len=0; char temp[10]; //get list switch(lan) { case 0: list=m->list_en; break; case 1: list=m->list_ch; break; default : return -1; } m->sele+=encoder; if(m->sele<0){m->sele=0;} if(m->sele>m->list_const){m->sele=m->list_const;} if(m->selelist_dis_top){m->list_dis_top--;} if((m->sele>m->list_dis_top+2)&&(m->list_dis_top+3list_const)){m->list_dis_top++;} OLED_Str_list(0,0,16,list,m->list_dis_top,4,1); if(m->r_sw_list!=NULL) { OLED_Str_list(96,0,16,m->r_sw_list,m->list_dis_top,4,1); } if(dismode==1) { sprintf(temp,"%d/%d",m->sele,m->list_const); OLED_Str(96,56,8,temp,0,1); } OLED_square(0,(m->sele-m->list_dis_top)*16,128,((m->sele-m->list_dis_top)*16)+16,2); if(button==1) { return_code=m->sele; } return return_code; } void di() { BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t1,50); BUZZER_PLAY_NOTES(0,morse_play_t1,0); mo_di(); } void da() { BUZZER_PLAY_NOTES(morse_play_hz,morse_play_t2,50); BUZZER_PLAY_NOTES(0,morse_play_t1,0); mo_da(); } void mymain() { PWR_EN(1); OLED_Init();//屏幕初始化 HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//启动n通道的pwm HAL_TIM_Base_Start_IT(&htim3); BUZZER_PLAY_INIT(); MUTE(0); BUZZER_PLAY_NOTES(1000,1000,50); BUZZER_PLAY_NOTES(0,1000,50); B1.config_longtimes=1;//启用B1按钮的长按检测 while(1) { GEI_BUTTON_CODE(&B1,en_c());//循环更新按钮 encode_c=GET_ENCODE(&E1); //更新编码器 //蜂鸣器控制 if(B1.code>0) { if(B1.code<255)//1-254 { if(config_buzzer_for_button_pass==1) { BUZZER_PLAY_NOTES(1000,100,50); BUZZER_PLAY_NOTES(0,0,0); } }else { } } if(encode_c!=0) { jump_tick=HAL_GetTick()+10000; if(config_buzzer_for_sele_change==1&&(mode==2||mode==3)) { BUZZER_PLAY_NOTES(1000,50,50); BUZZER_PLAY_NOTES(0,0,0); } } switch(mode) { case 0: //启动跳转模式 //用于读取系统配置后跳转到其他程序 //OLED_Str_list(0,0,8,"123\n456\n789",1,2,1); mode=1; break; case 1: //主界面 GEI_BUTTON_CODE(&TACKA,tack_a()); GEI_BUTTON_CODE(&TACKB,tack_b()); if(encode_c>0||TACKB.code!=0) { di(); } if(encode_c<0||TACKA.code!=0) { da(); } OLED_Str(0,0,8,moser_buff,0,1); fps_++; sprintf(str,"FPS:%d",fps); OLED_Str(0,56,8,str,0,1); if(B1.code==255) { if(config_buzzer_for_button_longpass==1) { BUZZER_PLAY_NOTES(1000,100,50); BUZZER_PLAY_NOTES(0,0,0); } mode=2; jump_tick=HAL_GetTick()+10000; } break; case 2: //菜单界面 switch(SHOW_MENU(&menu_main,encode_c,B1.code,sys_lan,1)) { case 0: //jump to main GUI mode=1; break; case 1: break; case 2: //jump to buzzer setting mode=3; break; } if(HAL_GetTick()>jump_tick){mode=1;} break; case 3: //buzzer setting buzzer_setting.r_sw_list=&str; sprintf(str,"../"); if(config_buzzer_for_button_pass) { sprintf(str,"%s%s",str,"\non"); }else { sprintf(str,"%s%s",str,"\nof"); } if(config_buzzer_for_button_longpass) { sprintf(str,"%s%s",str,"\non"); }else { sprintf(str,"%s%s",str,"\nof"); } if(config_buzzer_for_sele_change) { sprintf(str,"%s%s",str,"\non"); }else { sprintf(str,"%s%s",str,"\nof"); } switch(SHOW_MENU(&buzzer_setting,encode_c,B1.code,sys_lan,0)) { case 0: //jump to main GUI mode=2; break; case 1: if(config_buzzer_for_button_pass){config_buzzer_for_button_pass=0;}else{config_buzzer_for_button_pass=1;} break; case 2: if(config_buzzer_for_button_longpass){config_buzzer_for_button_longpass=0;}else{config_buzzer_for_button_longpass=1;} break; case 3: if(config_buzzer_for_sele_change){config_buzzer_for_sele_change=0;}else{config_buzzer_for_sele_change=1;} break; } if(HAL_GetTick()>jump_tick){mode=1;} break; } mo_server(); BUZZER_PLAY_SERVER(); OLED_Cache_to_hardware();//刷新屏幕 if(HAL_GetTick()>run_tick) { run_tick+=1000; RUNLED_TICK(); fps=fps_; fps_=0; } } } //外部中断回调 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { switch (GPIO_Pin)//中断线监测 { case en_a_Pin: //编码器A脚 这个脚在config是只监测下降沿 switch(HAL_GPIO_ReadPin(en_b_GPIO_Port,en_b_Pin))//监测编码器B脚 { case 1://通过监测B脚的状态识别正反转 E1.code+=1; E1.move_flag=1;//发生旋转的标记 break; case 0: E1.code-=1; E1.move_flag=1; break; } break; /* //外部电键输入 case tack_a_Pin: switch(tack_a()) { case 0: //di(); break; case 1: break; } break; case tack_b_Pin: switch(tack_b()) { case 0: //da(); break; case 1: break; } break; case en_c_Pin://编码器C脚 按钮 switch(en_c())//监测是上升沿还是下降沿 { case 0: //mo_one_tick(0); break; case 1: //mo_one_tick(1); break; } break; */ default: break; //__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin); } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//1ms { if (htim == (&htim3)) { } }