Files
pwm32e_pro/project/example/ble/ble_peripheral/app/src/Mode.c
T

426 lines
9.0 KiB
C

#include "pwm.h"
#include "xc_drv_pwm.h"
#include "rgblight.h"
#include "mode.h"
#include "timer.h"
#include "math.h"
#include "timeslice.h"
#include "RF433.h"
#include "usr_server.h"
#include "light_transition.h"
uint16_t time_1[11]={8,10,12,14,16,18,20,22,24,26,26};
static const uint16_t time_1_neutral[11]={20,25,30,36,41,46,51,56,61,66,66};
uint16_t time_2[11]={100,200,300,400,500,600,700,800,900,1000,1000};
uint16_t time_3[11]={200, 350, 450, 550, 650, 750, 850, 950, 1200,1500,1500};
uint8_t deviceStatus=POWERON; //开机标志位
uint8_t Mode=mode0; //mode0 1 2 3 4
uint8_t Speed=5; //速度
uint8_t direction = 1; // 呼吸灯的亮度变化方向,1表示增加,0表示减小
uint8_t choose_mode_falsg=0; //模式值
uint8_t choose_mode_bh=0; //模式变化
static uint8_t mode_entry_initialized;
uint16_t Color_table_static[3][2]={
{100,0}, // warm light
{50,50}, // neutral light
{0,100}, // cool light
};
uint16_t Color_table[3][2]={ //动态时候的数组
{100,0}, //暖光
{50,50}, //中性光
{0,100}, //冷光
};
//设置定时
void Set_timing(){
if(is_time==1){
if(flag_1h>0){ //定时一个小时
flag_1h--;
if(flag_1h==0){
Stop_PWM();
}
}
}
}
void Start_PWM(){
Light_Transition_RequestOn();
deviceStatus=POWERON;
startTimer(&timer1,1);
}
void Stop_PWM(){
Light_Transition_RequestOff();
deviceStatus=POWEROFF;
}
void set_mode(){
Light_Transition_Task();
if(choose_mode_bh==0){
choose_mode_bh=1;
switch(choose_mode_falsg){
//
case 0:
//暖光
W_PWM=Color_table_static[0][0];
C_PWM=Color_table_static[0][1];
driveMode=1;//切换为暖光的底层输出
Mode=mode0;
break;
case 1:
//冷光
W_PWM=Color_table_static[2][0];
C_PWM=Color_table_static[2][1];
driveMode=2;//切换为冷光的底层输出
Mode=mode0;
break;
case 2:
//中性光
W_PWM=Color_table_static[1][0];
C_PWM=Color_table_static[1][1];
driveMode=0;//切换为中性光的底层输出
Mode=mode0;
break;
case 3:
//暖光呼吸
W_PWM=Color_table[0][0];
C_PWM=Color_table[0][1];
driveMode=1;//切换为暖光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode1;
break;
case 4:
//冷光呼吸
W_PWM=Color_table[2][0];
C_PWM=Color_table[2][1];
driveMode=2;//切换为中性光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode1;
break;
case 5:
//三个色温轮询呼吸//先从暖光开始呼吸
W_PWM=Color_table[0][0];
C_PWM=Color_table[0][1];
driveMode=1;//切换为暖光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode1;
break;
case 6:
//暖光跳变
W_PWM=Color_table[0][0];
C_PWM=Color_table[0][1];
driveMode=1;//切换为暖光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode2;
break;
case 7:
//中性光跳变
W_PWM=Color_table[1][0];
C_PWM=Color_table[1][1];
driveMode=0;//切换为中性光的底层输出
Mode=mode2;
break;
case 8:
//冷光跳变
W_PWM=Color_table[2][0];
C_PWM=Color_table[2][1];
driveMode=2;//切换为中性光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode2;
break;
case 9:
//暖光中性光冷交替跳变,先从暖光跳变
W_PWM=Color_table[0][0];
C_PWM=Color_table[0][1];
driveMode=1;//切换为暖光的底层输出
direction=1;//呼吸方向恢复默认值
Mode=mode2;
break;
case 10://圆环模式
Mode=mode0;
break;
}
if(mode_entry_initialized)
{
if(choose_mode_falsg <= 2U)
{
Light_Transition_BeginModeChange();
}
else
{
Light_Transition_ModeActive=0U;
}
startTimer(&timer1,1);
}
else
{
mode_entry_initialized=1;
}
}
}
void Mode0(){ //常量
(void)0;/* transition */
}
#define THREE_COLOR_WAIT_NEUTRAL 6U
#define THREE_COLOR_WAIT_COLD 7U
#define THREE_COLOR_WAIT_WARM 8U
static uint8_t breathespeed_flag=0; //该变量只是用来处理三种颜色呼吸时候,对中性光的速度变大
static void breathe_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag) {
if (flag == 0) {
*PWM_1 = 0;
if (direction == 1) {
(*PWM_2)--;
if (*PWM_2 <= 1) {
*PWM_2 = 1;
direction = 0;
}
} else if (direction == 0) {
(*PWM_2)++;
if (*PWM_2 >= 100) {
direction = 1;
}
}
} else if (flag == 2) {//丢弃
// if (direction == 1) {
// (*PWM_1)--;
// (*PWM_2)--;
// if (*PWM_1 <= 1) {
// *PWM_1 = 1;
// *PWM_2 = 1;
// direction = 0;
// }
// } else if (direction == 0) {
// (*PWM_1)++;
// (*PWM_2)++;
// if (*PWM_1 >= 4) {
// *PWM_1 = 4;
// *PWM_2 = 4;
// direction = 1;
// if(flag==2){choose_breathe=0;driveMode=1;}
// }
// }
}
}
void Mode1(){ //呼吸
if(choose_mode_falsg==3){ //暖光呼吸
breathe_fun(&C_PWM,&W_PWM,0);
}else if(choose_mode_falsg==4){//冷光呼吸
breathe_fun(&W_PWM,&C_PWM,0);
}else if(choose_mode_falsg==5){
switch (direction) {
case 0:
breathespeed_flag=0;
W_PWM += 1;
C_PWM = 0;
if (W_PWM >= 100) {
direction = 1;
}
break;
case 1:
breathespeed_flag=0;
W_PWM -= 1;
C_PWM = 0;
if (W_PWM <= 1) {
direction = THREE_COLOR_WAIT_NEUTRAL;
W_PWM = 0;
C_PWM = 0;
}
break;
case 2:
breathespeed_flag=1;
W_PWM += 1;
C_PWM += 1;
if (W_PWM >= 40) {
W_PWM=40;
C_PWM=40;
direction = 3;
}
break;
case 3:
breathespeed_flag=1;
W_PWM -= 1;
C_PWM -= 1;
if (W_PWM <=1) {
direction = THREE_COLOR_WAIT_COLD;
W_PWM = 0;
C_PWM = 0;
}
break;
case 4:
breathespeed_flag=0;
W_PWM = 0;
C_PWM += 1;
if (C_PWM >= 100) {
direction = 5;
}
break;
case 5:
breathespeed_flag=0;
W_PWM = 0;
C_PWM -= 1;
if (C_PWM <=1){
direction = THREE_COLOR_WAIT_WARM;
W_PWM = 0;
C_PWM = 0;
}
break;
case THREE_COLOR_WAIT_NEUTRAL:
breathespeed_flag=1;
W_PWM = 0;
C_PWM = 0;
if(W_PWM_duty == 0U && C_PWM_duty == 0U){
driveMode=0;
W_PWM = 1;
C_PWM = 1;
direction = 2;
}
break;
case THREE_COLOR_WAIT_COLD:
breathespeed_flag=0;
W_PWM = 0;
C_PWM = 0;
if(W_PWM_duty == 0U && C_PWM_duty == 0U){
driveMode=2;
C_PWM = 1;
direction = 4;
}
break;
case THREE_COLOR_WAIT_WARM:
breathespeed_flag=0;
W_PWM = 0;
C_PWM = 0;
if(W_PWM_duty == 0U && C_PWM_duty == 0U){
driveMode=1;
W_PWM = 1;
direction = 0;
}
break;
}
}
//(void)0;/* transition */
(void)0;/* transition */
}
void jump_fun(uint16_t *PWM_1, uint16_t *PWM_2, uint8_t flag){
if(flag==0){
*PWM_1 = 0;
if (!direction){
*PWM_2 = 100;
direction = 1;
} else {
*PWM_2 = 0;
direction = 0;
}
}else if(flag==1){
if (!direction){
*PWM_1 = 40;
*PWM_2 = 40;
direction = 1;
} else {
*PWM_1 = 0;
*PWM_2 = 0;
direction = 0;
}
}
}
void Mode2(){ //跳变
//printf("dd=%d--%d\r\n",W_PWM,W_PWM);
if(choose_mode_falsg==6){
jump_fun(&C_PWM,&W_PWM,0); //暖光跳变
}else if(choose_mode_falsg==7){
jump_fun(&W_PWM,&C_PWM,1); //中光跳变
}else if(choose_mode_falsg==8){
jump_fun(&W_PWM,&C_PWM,0); //冷光跳变
}else if(choose_mode_falsg==9){
switch (direction) {
case 1:
W_PWM =100;
C_PWM = 0;
direction = 2;
break;
case 2:
W_PWM = 0;
C_PWM = 0;
direction = 3;
driveMode=0;
break;
case 3:
W_PWM =40;
C_PWM =40;
direction = 4;
break;
case 4:
W_PWM =0;
C_PWM =0;
direction = 5;
driveMode=2;
break;
case 5:
W_PWM = 0;
C_PWM = 100;
direction = 6;
break;
case 6:
W_PWM = 0;
C_PWM = 0;
direction = 1;
driveMode=1;
break;
}
}
(void)0;/* transition */
}
void Mode3(){ //爆闪 //无
(void)0;/* transition */
}
pfunc modefunctin[] = { //模式切换,一共15个模式
Mode0,
Mode1,
Mode2,
Mode3,
};
void choice_mode(){
if(Light_Transition_ModeActive || Light_Transition_FeedbackActive()) return;
if (!timer1.active && timer1.count == 0){
if(Mode==mode0){
startTimer(&timer1,1);
}else if(Mode==mode1){
if(breathespeed_flag==0){
startTimer(&timer1,time_1[Speed]);
}else {
startTimer(&timer1,time_1_neutral[Speed]);
}
}else if(Mode==mode2){
startTimer(&timer1,time_2[Speed]);
}else if(Mode==mode3){
startTimer(&timer1,time_3[Speed]);//
}
modefunctin[Mode]();
}
}