#include "system/includes.h" #include "app_config.h" #include "asm/pwm_led.h" #include "tone_player.h" #include "ui_manage.h" #include "app_main.h" #include "app_task.h" #include "asm/charge.h" #include "app_power_manage.h" #include "app_charge.h" #include "user_cfg.h" #include "audio.h" #include "vm.h" #include "sys_time.h" extern void usr_set_up_moto(u8 dir,u16 duty); extern void usr_set_down_moto(u8 dir,u16 duty); extern void io_ext_interrupt_test(void); extern void usr_intterupt_close(); extern void usr_le_120_notify_data(); extern void usr_write_device_sta(); extern void usr_pwm_reinit(); void usr_moto_main(); void usr_moto_timer_init() { usr_var.usr_intterupt_reinit_falg=1; usr_var.usr_auto_off_cnt=0; io_ext_interrupt_test(); usr_intterupt_close(); _usr_module_cfg.power_on_flag=1; } #define HIG_DUTY 90 #define LOW_DUTY 50 #define OFF_DUTY 0 #define DIR_L 1 #define DIR_R 2 #define DIR_F 0 /* // 模式切换中断服务函数 void modeSwitchISR() interrupt 0 { modeDirection = !modeDirection; // 切换方向 while(!MODE_SWITCH); // 等待按键释放 } */ // 定义模式枚举 typedef enum { MODE_1, MODE_2, MODE_3, MODE_COUNT } MotorMode; // 定义状态枚举 typedef enum { STATE_IDLE, STATE_FORWARD, STATE_REVERSE, STATE_STOP } MotorState; // 全局变量 MotorMode currentMode = MODE_1; u8 modeDirection = 1; // 1为正序,0为反序 MotorState currentState = STATE_IDLE; unsigned int timerCount = 0; unsigned int stepCount = 0; // 马达正转 void motorForward() { usr_set_up_moto(2,LOW_DUTY); currentState = STATE_FORWARD; } // 马达反转 void motorReverse() { usr_set_up_moto(1,LOW_DUTY); currentState = STATE_REVERSE; } // 马达停止 void motorStop() { usr_set_up_moto(0,OFF_DUTY); currentState = STATE_STOP; } void usr_up_moto_mode() { if((_usr_module_cfg.local_moto_mode==1)||(_usr_module_cfg.local_moto_mode==2)) { timerCount++; // 模式1:正转3秒(6个周期),反转3秒(6个周期) if(currentMode == MODE_1) { if(timerCount <= 6) { motorForward(); } else if(timerCount <= 12) { motorReverse(); } else { timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } } // 模式2:正转1秒(2个周期)停1秒(2个周期),再正1秒停1秒,反转1秒停1秒,再正转1秒停1秒 else if(currentMode == MODE_2) { switch(stepCount) { case 0: case 1: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 2: case 3: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 4: case 5: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 6: case 7: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 8: case 9: // 反转1秒 motorReverse(); if(timerCount % 2 == 0) stepCount++; break; case 10: case 11: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 12: case 13: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 14: case 15: // 停止1秒 motorStop(); if(timerCount % 2 == 0) { stepCount = 0; timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } break; } } // 模式3:连续4次,每次转500ms(1个周期)停1秒(2个周期) else if(currentMode == MODE_3) { if(stepCount < 8) { // 总共8个步骤(4次转停) if(stepCount % 2 == 0) { // 奇数步骤:转500ms motorForward(); } else { // 偶数步骤:停1秒 motorStop(); } stepCount++; } else { stepCount = 0; timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } } usr_var.usr_auto_off_cnt++; if(usr_var.usr_auto_off_cnt>=1200) { usr_var.usr_auto_off_cnt=0; _usr_module_cfg.local_moto_mode=0; //_usr_module_cfg.power_on_flag=0; } } else { //if(_usr_module_cfg.local_moto_mode==0) //{ usr_var.usr_auto_off_cnt=0; //} //else //{ //} } } void usr_up_moto_mode2() { timerCount++; // 模式1:正转3秒(6个周期),反转3秒(6个周期) if(currentMode == MODE_1) { if(timerCount <= 6) { motorForward(); } else if(timerCount <= 12) { motorReverse(); } else { timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } } // 模式2:正转1秒(2个周期)停1秒(2个周期),再正1秒停1秒,反转1秒停1秒,再正转1秒停1秒 else if(currentMode == MODE_2) { switch(stepCount) { case 0: case 1: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 2: case 3: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 4: case 5: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 6: case 7: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 8: case 9: // 反转1秒 motorReverse(); if(timerCount % 2 == 0) stepCount++; break; case 10: case 11: // 停止1秒 motorStop(); if(timerCount % 2 == 0) stepCount++; break; case 12: case 13: // 正转1秒 motorForward(); if(timerCount % 2 == 0) stepCount++; break; case 14: case 15: // 停止1秒 motorStop(); if(timerCount % 2 == 0) { stepCount = 0; timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } break; } } // 模式3:连续4次,每次转500ms(1个周期)停1秒(2个周期) else if(currentMode == MODE_3) { if(stepCount < 8) { // 总共8个步骤(4次转停) if(stepCount % 2 == 0) { // 奇数步骤:转500ms motorForward(); } else { // 偶数步骤:停1秒 motorStop(); } stepCount++; } else { stepCount = 0; timerCount = 0; // 切换到下一个模式 if(modeDirection) { currentMode = (currentMode + 1) % MODE_COUNT; } else { currentMode = (currentMode == 0) ? MODE_COUNT - 1 : currentMode - 1; } } } if((_usr_module_cfg.local_moto_mode>0)&&(_usr_module_cfg.local_moto_mode<3))usr_var.usr_auto_off_cnt++; if(usr_var.usr_auto_off_cnt>=1200) { usr_var.usr_auto_off_cnt=0; usr_var.usr_allow_to_sleep_cnt=6; _usr_module_cfg.local_moto_mode=0; //_usr_module_cfg.power_on_flag=0; //sys_timeout_add(NULL,usr_le_120_notify_data,100); sys_timeout_add(NULL,usr_write_device_sta,600); } } void usr_clear_up() { timerCount = 0; stepCount = 0; } void usr_clear_moto() { _usr_module_cfg.local_moto_mode=0; _usr_module_cfg.usr_dizhuo_mode=0; _usr_module_cfg.usr_douban_mode=0; _usr_module_cfg.usr_doumaoshoudong=0; } u8 usr_get_work_status() { if((_usr_module_cfg.local_moto_mode==0)&&\ (_usr_module_cfg.usr_dizhuo_mode==0)&&\ (_usr_module_cfg.usr_douban_mode==0)&&\ (_usr_module_cfg.usr_doumaoshoudong==0)) { return 0; } return 1; } u8 usr_allow_to_sleep() { if((usr_get_work_status()==0)&&(usr_var.usr_power_charge_flag==2)) { if(usr_var.usr_goto_pair==0) { if((_usr_module_cfg.local_moto_mode==0)||(_usr_module_cfg.local_moto_mode==3)) { return 1; } else { return 0; } } else { return 0; } } return 0; } static u8 usr_work_idle_query(void) { if(usr_var.usr_allow_to_sleep_cnt)return 0; if((_usr_module_cfg.power_on_flag==0)&&(usr_var.usr_power_charge_flag==2)) { return 1; } if(usr_allow_to_sleep()) { return 1; } else { return 0; } } REGISTER_LP_TARGET(usr_work_lp_target) = { .name = "usr_work_lp", .is_idle = usr_work_idle_query, }; void usr_printf_sta() { //printf("local_moto %d usr_dizhuo %d usr_douban %d usr_shoudong%d charge_flag %d usr_var.usr_goto_pair %d\n",_usr_module_cfg.local_moto_mode,_usr_module_cfg.usr_dizhuo_mode,_usr_module_cfg.usr_douban_mode,_usr_module_cfg.usr_doumaoshoudong,usr_var.usr_power_charge_flag,usr_var.usr_goto_pair); } void usr_moto_main() { static u8 down_cycle_cnt=0; static u8 up_cycle_cnt=0; static u8 up_dir=1; //printf("usr_moto_main\n"); usr_printf_sta(); if(usr_var.usr_allow_to_sleep_cnt)usr_var.usr_allow_to_sleep_cnt--; if(usr_var.usr_power_charge_flag==1) { //_usr_module_cfg.local_moto_mode=3; usr_set_up_moto(0,DIR_F); usr_set_down_moto(0,DIR_F); return; } if(_usr_module_cfg.power_on_flag==0) { usr_var.usr_auto_off_cnt=0; usr_set_up_moto(0,DIR_F); usr_set_down_moto(0,DIR_F); return; } if(usr_var.usr_goto_pair) { _usr_module_cfg.local_moto_mode=0x03; usr_var.usr_auto_off_cnt=0; usr_set_up_moto(0,DIR_F); usr_set_down_moto(0,DIR_F); return; } switch(_usr_module_cfg.local_moto_mode) { case 0x00:///关闭 if(_usr_module_cfg.power_on_flag==0) { usr_var.usr_auto_off_cnt=0; usr_set_up_moto(0,DIR_F); usr_set_down_moto(0,DIR_F); } else { switch(_usr_module_cfg.usr_dizhuo_mode)///0:关闭;1:平稳;2:狂转 { case 0x01:///平稳 usr_pwm_reinit(); if(down_cycle_cnt<=3) { usr_set_down_moto(1,LOW_DUTY-10); } else if(down_cycle_cnt==4) { usr_set_down_moto(0,OFF_DUTY); } else { usr_set_down_moto(2,LOW_DUTY-10); } down_cycle_cnt++; if(down_cycle_cnt>8) { down_cycle_cnt=0; usr_set_down_moto(0,DIR_F); } break; case 0x02:///狂转 usr_pwm_reinit(); if(down_cycle_cnt<=5) { usr_set_down_moto(1,HIG_DUTY); } else if(down_cycle_cnt==6) { usr_set_down_moto(0,OFF_DUTY); } else { usr_set_down_moto(2,HIG_DUTY); } down_cycle_cnt++; // printf("down_cycle_cnt = %d\n",down_cycle_cnt); if(down_cycle_cnt>12) { down_cycle_cnt=0; usr_set_down_moto(0,DIR_F); } break; default: usr_set_down_moto(0,OFF_DUTY); break; } if(_usr_module_cfg.usr_douban_mode==0) { if(_usr_module_cfg.usr_doumaoshoudong==0) { usr_set_up_moto(0,DIR_F); } else { usr_pwm_reinit(); usr_set_up_moto(_usr_module_cfg.usr_doumaoshoudong,LOW_DUTY+10); up_cycle_cnt++; if(up_cycle_cnt>=3) { _usr_module_cfg.usr_doumaoshoudong=0; up_cycle_cnt=0; } } } else { //up_cycle_cnt=0; switch(_usr_module_cfg.usr_douban_mode)//逗猫棒摇动模式 | 0:关闭;1:自动; 2:转圈; 3:抖动; { case 0x01: usr_pwm_reinit(); up_cycle_cnt=0; usr_up_moto_mode2(); // putchar('1'); break; case 0x02: usr_pwm_reinit(); usr_clear_up(); if(up_cycle_cnt<=10) { usr_set_up_moto(1,LOW_DUTY+20); } else if(up_cycle_cnt==11) { usr_set_up_moto(0,0); } else { usr_set_up_moto(2,LOW_DUTY+20); } up_cycle_cnt++; if(up_cycle_cnt>22) { usr_set_up_moto(0,0); up_cycle_cnt=0; } //printf("up_cycle_cnt = %d\n",up_cycle_cnt); break; case 0x03: usr_pwm_reinit(); usr_clear_up(); up_cycle_cnt++; if(up_dir==0)up_dir=1; if((up_cycle_cnt%2)==0) { usr_set_up_moto(0,0); } else { usr_set_up_moto(up_dir,LOW_DUTY+10); } if(up_cycle_cnt>20) { up_cycle_cnt=0; if(up_dir==1)up_dir=2; else up_dir=1; } //putchar('3'); break; default: //putchar('4'); break; } } } break; case 0x01:///打鸡血 up_cycle_cnt=0; usr_pwm_reinit(); usr_up_moto_mode(); if(down_cycle_cnt<=5) { usr_set_down_moto(1,HIG_DUTY); } else if(down_cycle_cnt==6) { usr_set_down_moto(0,DIR_F); } else { usr_set_down_moto(2,HIG_DUTY); } down_cycle_cnt++; // printf("down_cycle_cnt = %d\n",down_cycle_cnt); if(down_cycle_cnt>12) { down_cycle_cnt=0; usr_set_down_moto(0,DIR_F); } break; case 0x02:///佛系 up_cycle_cnt=0; usr_pwm_reinit(); usr_up_moto_mode(); if(down_cycle_cnt<=3) { usr_set_down_moto(1,LOW_DUTY-10); } else if(down_cycle_cnt==4) { usr_set_down_moto(0,DIR_F); } else { usr_set_down_moto(2,LOW_DUTY-10); } down_cycle_cnt++; if(down_cycle_cnt>8) { down_cycle_cnt=0; usr_set_down_moto(0,DIR_F); } break; case 0x03:///配对中 up_cycle_cnt=0; down_cycle_cnt=0; usr_var.usr_auto_off_cnt=0; usr_set_up_moto(0,DIR_F); usr_set_down_moto(0,DIR_F); break; } //printf("down_cycle_cnt = %d local_moto_mode = %d\n ",down_cycle_cnt,local_moto_mode); }