452 lines
11 KiB
C
452 lines
11 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]={12,15,18,21,24,27,30,33,35,38,38};
|
|
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(){
|
|
if(choose_mode_bh==0){
|
|
choose_mode_bh=1;
|
|
switch(choose_mode_falsg){
|
|
case 0:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; Mode=mode0;
|
|
break;
|
|
case 1:
|
|
W_PWM=0U; C_PWM=100U; driveMode=2U; Mode=mode0;
|
|
break;
|
|
case 2:
|
|
W_PWM=50U; C_PWM=50U; driveMode=0U; Mode=mode0;
|
|
break;
|
|
case 3:
|
|
W_PWM=1U; C_PWM=0U; driveMode=1U; direction=0U; Mode=mode1;
|
|
break;
|
|
case 4:
|
|
W_PWM=0U; C_PWM=1U; driveMode=2U; direction=0U; Mode=mode1;
|
|
break;
|
|
case 5:
|
|
W_PWM=1U; C_PWM=1U; driveMode=0U; direction=0U; Mode=mode1;
|
|
break;
|
|
case 6:
|
|
W_PWM=1U; C_PWM=0U; driveMode=1U; direction=0U; Mode=mode1;
|
|
break;
|
|
case 7:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode3;
|
|
break;
|
|
case 8:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=0U; Mode=mode3;
|
|
break;
|
|
case 9:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=1U; Mode=mode2;
|
|
break;
|
|
case 10:
|
|
W_PWM=0U; C_PWM=100U; driveMode=2U; direction=1U; Mode=mode2;
|
|
break;
|
|
case 11:
|
|
W_PWM=50U; C_PWM=50U; driveMode=0U; direction=1U; Mode=mode2;
|
|
break;
|
|
case 12:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; Mode=mode2;
|
|
break;
|
|
case 13:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U; Mode=mode2;
|
|
break;
|
|
case CUSTOM_TEMPERATURE_MODE:
|
|
Mode=mode0;
|
|
break;
|
|
default:
|
|
choose_mode_falsg=0U;
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; Mode=mode0;
|
|
break;
|
|
}
|
|
if(mode_entry_initialized)
|
|
{
|
|
if(choose_mode_falsg <= 2U)
|
|
{
|
|
Light_Transition_BeginModeChange();
|
|
}
|
|
else if(choose_mode_falsg == CUSTOM_TEMPERATURE_MODE)
|
|
{
|
|
/*
|
|
* APP CCT packets can arrive continuously while the color
|
|
* ring is dragged. Restart the same 200 ms linear transition
|
|
* used by static mode changes from the current visible
|
|
* output, so the latest W/C target is followed without a
|
|
* discontinuity.
|
|
*/
|
|
Light_Transition_BeginModeChange();
|
|
}
|
|
else
|
|
{
|
|
Light_Transition_ModeActive=0U;
|
|
Light_Transition_SyncBrightness(Brightness);
|
|
}
|
|
if(Mode==mode1){
|
|
if(choose_mode_falsg==5U)
|
|
startTimer(&timer1,time_1_neutral[Speed]);
|
|
else
|
|
startTimer(&timer1,time_1[Speed]);
|
|
}else if(Mode==mode2){
|
|
startTimer(&timer1,time_2[Speed]);
|
|
}else if(Mode==mode3){
|
|
startTimer(&timer1,time_1[Speed]);
|
|
}else{
|
|
startTimer(&timer1,1U);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
mode_entry_initialized=1;
|
|
}
|
|
}
|
|
Light_Transition_Task();
|
|
}
|
|
void Mode0(){ //常量
|
|
(void)0;/* transition */
|
|
}
|
|
|
|
|
|
#define THREE_COLOR_WAIT_NEUTRAL 6U
|
|
#define THREE_COLOR_WAIT_COLD 7U
|
|
#define THREE_COLOR_WAIT_WARM 8U
|
|
#define OVERLAP_GRADIENT_LEVEL 40U
|
|
#define OVERLAP_NEUTRAL_LOW 20U
|
|
#define OVERLAP_NEUTRAL_PEAK 50U
|
|
|
|
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;}
|
|
// }
|
|
// }
|
|
}
|
|
}
|
|
static void neutral_breathe_fun(void)
|
|
{
|
|
breathespeed_flag=1U;
|
|
if(direction==1U){
|
|
if(W_PWM>1U){ W_PWM--; C_PWM--; }
|
|
if(W_PWM<=1U){ W_PWM=1U; C_PWM=1U; direction=0U; }
|
|
}else{
|
|
if(W_PWM<68U){ W_PWM++; C_PWM++; }
|
|
if(W_PWM>=68U){ W_PWM=68U; C_PWM=68U; direction=1U; }
|
|
}
|
|
}
|
|
|
|
void Mode1(void)
|
|
{
|
|
if(choose_mode_falsg==3U){
|
|
breathespeed_flag=0U;
|
|
breathe_fun(&C_PWM,&W_PWM,0U);
|
|
}else if(choose_mode_falsg==4U){
|
|
breathespeed_flag=0U;
|
|
breathe_fun(&W_PWM,&C_PWM,0U);
|
|
}else if(choose_mode_falsg==5U){
|
|
neutral_breathe_fun();
|
|
}else if(choose_mode_falsg==6U){
|
|
switch(direction){
|
|
case 0U:
|
|
breathespeed_flag=0U;
|
|
W_PWM++; C_PWM=0U;
|
|
if(W_PWM>=100U){ W_PWM=100U; direction=1U; }
|
|
break;
|
|
case 1U:
|
|
breathespeed_flag=0U;
|
|
if(W_PWM>0U) W_PWM--;
|
|
C_PWM=0U;
|
|
if(W_PWM<=1U){ W_PWM=0U; direction=THREE_COLOR_WAIT_COLD; }
|
|
break;
|
|
case 4U:
|
|
breathespeed_flag=0U;
|
|
W_PWM=0U; C_PWM++;
|
|
if(C_PWM>=100U){ C_PWM=100U; direction=5U; }
|
|
break;
|
|
case 5U:
|
|
breathespeed_flag=0U;
|
|
W_PWM=0U;
|
|
if(C_PWM>0U) C_PWM--;
|
|
if(C_PWM<=1U){ C_PWM=0U; direction=THREE_COLOR_WAIT_NEUTRAL; }
|
|
break;
|
|
case 2U:
|
|
breathespeed_flag=1U;
|
|
W_PWM++; C_PWM++;
|
|
if(W_PWM>=68U){ W_PWM=68U; C_PWM=68U; direction=3U; }
|
|
break;
|
|
case 3U:
|
|
breathespeed_flag=1U;
|
|
if(W_PWM>0U) W_PWM--;
|
|
if(C_PWM>0U) C_PWM--;
|
|
if(W_PWM<=1U){ W_PWM=0U; C_PWM=0U; direction=THREE_COLOR_WAIT_WARM; }
|
|
break;
|
|
case THREE_COLOR_WAIT_COLD:
|
|
breathespeed_flag=0U; W_PWM=0U; C_PWM=0U;
|
|
if(W_PWM_duty==0U && C_PWM_duty==0U){
|
|
driveMode=2U; C_PWM=1U; direction=4U;
|
|
}
|
|
break;
|
|
case THREE_COLOR_WAIT_NEUTRAL:
|
|
breathespeed_flag=1U; W_PWM=0U; C_PWM=0U;
|
|
if(W_PWM_duty==0U && C_PWM_duty==0U){
|
|
driveMode=0U; W_PWM=1U; C_PWM=1U; direction=2U;
|
|
}
|
|
break;
|
|
case THREE_COLOR_WAIT_WARM:
|
|
default:
|
|
breathespeed_flag=0U; W_PWM=0U; C_PWM=0U;
|
|
if(W_PWM_duty==0U && C_PWM_duty==0U){
|
|
driveMode=1U; W_PWM=1U; direction=0U;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
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 = 50;
|
|
*PWM_2 = 50;
|
|
direction = 1;
|
|
} else {
|
|
*PWM_1 = 0;
|
|
*PWM_2 = 0;
|
|
direction = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Mode2(void)
|
|
{
|
|
if(choose_mode_falsg==9U){
|
|
jump_fun(&C_PWM,&W_PWM,0U);
|
|
}else if(choose_mode_falsg==10U){
|
|
jump_fun(&W_PWM,&C_PWM,0U);
|
|
}else if(choose_mode_falsg==11U){
|
|
jump_fun(&W_PWM,&C_PWM,1U);
|
|
}else if(choose_mode_falsg==12U){
|
|
switch(direction){
|
|
case 1U:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U;
|
|
break;
|
|
case 2U:
|
|
W_PWM=0U; C_PWM=100U; driveMode=2U; direction=3U;
|
|
break;
|
|
default:
|
|
W_PWM=50U; C_PWM=50U; driveMode=0U; direction=1U;
|
|
break;
|
|
}
|
|
}else if(choose_mode_falsg==13U){
|
|
switch(direction){
|
|
case 1U:
|
|
W_PWM=100U; C_PWM=0U; driveMode=1U; direction=2U;
|
|
break;
|
|
case 2U:
|
|
W_PWM=0U; C_PWM=0U; driveMode=2U; direction=3U;
|
|
break;
|
|
case 3U:
|
|
W_PWM=0U; C_PWM=100U; direction=4U;
|
|
break;
|
|
case 4U:
|
|
W_PWM=0U; C_PWM=0U; driveMode=0U; direction=5U;
|
|
break;
|
|
case 5U:
|
|
W_PWM=50U; C_PWM=50U; direction=6U;
|
|
break;
|
|
default:
|
|
W_PWM=0U; C_PWM=0U; driveMode=1U; direction=1U;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Mode3(void)
|
|
{
|
|
if(choose_mode_falsg==7U){
|
|
if(direction==1U){
|
|
if(C_PWM<100U) C_PWM++;
|
|
W_PWM=100U-C_PWM;
|
|
if(C_PWM>=100U){ C_PWM=100U; W_PWM=0U; direction=0U; }
|
|
}else{
|
|
if(C_PWM>0U) C_PWM--;
|
|
W_PWM=100U-C_PWM;
|
|
if(C_PWM==0U){ W_PWM=100U; direction=1U; }
|
|
}
|
|
}else if(choose_mode_falsg==8U){
|
|
switch(direction){
|
|
case 0U:
|
|
C_PWM=0U; driveMode=1U;
|
|
if(W_PWM>OVERLAP_GRADIENT_LEVEL) W_PWM--;
|
|
if(W_PWM<=OVERLAP_GRADIENT_LEVEL){ W_PWM=OVERLAP_GRADIENT_LEVEL; direction=1U; }
|
|
break;
|
|
case 1U:
|
|
if(W_PWM>0U){ W_PWM--; C_PWM++; }
|
|
driveMode=0U;
|
|
if(W_PWM==0U){ C_PWM=OVERLAP_GRADIENT_LEVEL; driveMode=2U; direction=2U; }
|
|
break;
|
|
case 2U:
|
|
W_PWM=0U; driveMode=2U;
|
|
if(C_PWM<100U) C_PWM++;
|
|
if(C_PWM>=100U){ C_PWM=100U; direction=3U; }
|
|
break;
|
|
case 3U:
|
|
W_PWM=0U; driveMode=2U;
|
|
if(C_PWM>OVERLAP_GRADIENT_LEVEL) C_PWM--;
|
|
if(C_PWM<=OVERLAP_GRADIENT_LEVEL){ C_PWM=OVERLAP_GRADIENT_LEVEL; direction=4U; }
|
|
break;
|
|
case 4U:
|
|
if(W_PWM<OVERLAP_NEUTRAL_LOW && C_PWM>OVERLAP_NEUTRAL_LOW){ W_PWM++; C_PWM--; }
|
|
driveMode=0U;
|
|
if(W_PWM>=OVERLAP_NEUTRAL_LOW){ W_PWM=OVERLAP_NEUTRAL_LOW; C_PWM=OVERLAP_NEUTRAL_LOW; direction=5U; }
|
|
break;
|
|
case 5U:
|
|
if(W_PWM<OVERLAP_NEUTRAL_PEAK){ W_PWM++; C_PWM++; }
|
|
driveMode=0U;
|
|
if(W_PWM>=OVERLAP_NEUTRAL_PEAK){ W_PWM=OVERLAP_NEUTRAL_PEAK; C_PWM=OVERLAP_NEUTRAL_PEAK; direction=6U; }
|
|
break;
|
|
case 6U:
|
|
if(W_PWM>OVERLAP_NEUTRAL_LOW){ W_PWM--; C_PWM--; }
|
|
driveMode=0U;
|
|
if(W_PWM<=OVERLAP_NEUTRAL_LOW){ W_PWM=OVERLAP_NEUTRAL_LOW; C_PWM=OVERLAP_NEUTRAL_LOW; direction=7U; }
|
|
break;
|
|
case 7U:
|
|
if(C_PWM>0U){ C_PWM--; W_PWM++; }
|
|
driveMode=0U;
|
|
if(C_PWM==0U){ W_PWM=OVERLAP_GRADIENT_LEVEL; driveMode=1U; direction=8U; }
|
|
break;
|
|
case 8U:
|
|
default:
|
|
C_PWM=0U; driveMode=1U;
|
|
if(W_PWM<100U) W_PWM++;
|
|
if(W_PWM>=100U){ W_PWM=100U; direction=0U; }
|
|
break;
|
|
}
|
|
}else{
|
|
return;
|
|
}
|
|
if(C_PWM==0U) driveMode=1U;
|
|
else if(W_PWM==0U) driveMode=2U;
|
|
else driveMode=0U;
|
|
}
|
|
pfunc modefunctin[] = { //模式切换,一共15个模式
|
|
Mode0,
|
|
Mode1,
|
|
Mode2,
|
|
Mode3,
|
|
};
|
|
void choice_mode(){
|
|
if(choose_mode_bh==0U) return;
|
|
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_1[Speed]);//
|
|
}
|
|
|
|
modefunctin[Mode]();
|
|
}
|
|
}
|