Commit 57d5e207 authored by Johny Mattsson's avatar Johny Mattsson
Browse files

Imported latest SDK 1.4.0 on the side in prep for upgrade.

parent 093a8959
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: esp_platform_user_timer.c
*
* Description:
*
* Modification history:
* 2014/5/09, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "mem.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "user_esp_platform.h"
#define ESP_DEBUG
#ifdef ESP_DEBUG
#define ESP_DBG os_printf
#else
#define ESP_DBG
#endif
LOCAL os_timer_t device_timer;
uint32 min_wait_second;
char timestamp_str[11];
int timestamp = 0;
char *timer_splits[20] = {NULL};
struct esp_platform_wait_timer_param {
uint8 wait_time_param[11];
uint8 wait_action[15];
int wait_time_second;
};
struct wait_param {
uint8 action[20][15];
uint16 action_number;
uint16 count;
uint32 min_time_backup;
};
void esp_platform_timer_action(struct esp_platform_wait_timer_param *timer_wait_param, uint16 count);
/******************************************************************************
* FunctionName : split
* Description : split string p1 according to sting p2 and save the splits
* Parameters : p1 , p2 ,splits[]
* Returns : the number of splits
*******************************************************************************/
uint16 ICACHE_FLASH_ATTR
split(char *p1, char *p2, char *splits[])
{
int i = 0;
int j = 0;
while (i != -1) {
int start = i;
int end = indexof(p1, p2, start);
if (end == -1) {
end = os_strlen(p1);
}
char *p = (char *) os_zalloc(100);
os_memcpy(p, p1 + start, end - start);
p[end - start] = '\0';
splits[j] = p;
j++;
i = end + 1;
if (i > os_strlen(p1)) {
break;
}
}
return j;
}
/******************************************************************************
* FunctionName : indexof
* Description : calculate the offset of p2 relate to start of p1
* Parameters : p1,p1,start
* Returns : the offset of p2 relate to the start
*******************************************************************************/
int ICACHE_FLASH_ATTR
indexof(char *p1, char *p2, int start)
{
char *find = (char *)os_strstr(p1 + start, p2);
if (find != NULL) {
return (find - p1);
}
return -1;
}
/******************************************************************************
* FunctionName : esp_platform_find_min_time
* Description : find the minimum wait second in timer list
* Parameters : timer_wait_param -- param of timer action and wait time param
* count -- The number of timers given by server
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
esp_platform_find_min_time(struct esp_platform_wait_timer_param *timer_wait_param , uint16 count)
{
uint16 i = 0;
min_wait_second = 0xFFFFFFF;
for (i = 0; i < count ; i++) {
if (timer_wait_param[i].wait_time_second < min_wait_second && timer_wait_param[i].wait_time_second >= 0) {
min_wait_second = timer_wait_param[i].wait_time_second;
}
}
}
/******************************************************************************
* FunctionName : user_platform_timer_first_start
* Description : calculate the wait time of each timer
* Parameters : count -- The number of timers given by server
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_platform_timer_first_start(uint16 count)
{
int i = 0;
struct esp_platform_wait_timer_param timer_wait_param[100] = {0};
ESP_DBG("current timestamp= %ds\n", timestamp);
timestamp = timestamp + min_wait_second;
for (i = 0 ; i < count ; i++) {
char *str = timer_splits[i];
if (indexof(str, "f", 0) == 0) {
char *fixed_wait[2];
ESP_DBG("timer is fixed mode\n");
split(str, "=", fixed_wait);
os_memcpy(timer_wait_param[i].wait_time_param, fixed_wait[0] + 1, os_strlen(fixed_wait[0]) - 1);
os_memcpy(timer_wait_param[i].wait_action, fixed_wait[1], os_strlen(fixed_wait[1]));
timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - timestamp;
os_free(fixed_wait[0]);
os_free(fixed_wait[1]);
}
else if (indexof(str, "l", 0) == 0) {
char *loop_wait[2];
ESP_DBG("timer is loop mode\n");
split(str, "=", loop_wait);
os_memcpy(timer_wait_param[i].wait_time_param, loop_wait[0] + 1, os_strlen(loop_wait[0]) - 1);
os_memcpy(timer_wait_param[i].wait_action, loop_wait[1], os_strlen(loop_wait[1]));
timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - (timestamp % atoi(timer_wait_param[i].wait_time_param));
os_free(loop_wait[0]);
os_free(loop_wait[1]);
} else if (indexof(str, "w", 0) == 0) {
char *week_wait[2];
int monday_wait_time = 0;
ESP_DBG("timer is weekend mode\n");
split(str, "=", week_wait);
os_memcpy(timer_wait_param[i].wait_time_param, week_wait[0] + 1, os_strlen(week_wait[0]) - 1);
os_memcpy(timer_wait_param[i].wait_action, week_wait[1], os_strlen(week_wait[1]));
monday_wait_time = (timestamp - 1388937600) % (7 * 24 * 3600);
ESP_DBG("monday_wait_time == %d", monday_wait_time);
if (atoi(timer_wait_param[i].wait_time_param) > monday_wait_time) {
timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - monday_wait_time;
} else {
timer_wait_param[i].wait_time_second = 7 * 24 * 3600 - monday_wait_time + atoi(timer_wait_param[i].wait_time_param);
}
os_free(week_wait[0]);
os_free(week_wait[1]);
}
}
esp_platform_find_min_time(timer_wait_param, count);
if(min_wait_second == 0) {
return;
}
esp_platform_timer_action(timer_wait_param, count);
}
/******************************************************************************
* FunctionName : user_esp_platform_device_action
* Description : Execute the actions of minimum wait time
* Parameters : pwait_action -- point the list of actions which need execute
*
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_device_action(struct wait_param *pwait_action)
{
uint8 i = 0;
uint16 count = pwait_action->count;
uint16 action_number = pwait_action->action_number;
ESP_DBG("there is %d action at the same time\n", pwait_action->action_number);
#if PLUG_DEVICE
for (i = 0; i < action_number && pwait_action->action[i][0] != '0'; i++) {
ESP_DBG("%s\n",pwait_action->action[i]);
if (os_strcmp(pwait_action->action[i], "on_switch", 9) == 0) {
user_plug_set_status(0x01);
} else if (os_strcmp(pwait_action->action[i], "off_switch", 10) == 0) {
user_plug_set_status(0x00);
} else if (os_strcmp(pwait_action->action[i], "on_off_switch", 13) == 0) {
if (user_plug_get_status() == 0) {
user_plug_set_status(0x01);
} else {
user_plug_set_status(0x00);
}
} else {
return;
}
}
user_platform_timer_first_start(count);
#endif
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : timer_wait_param -- The received data from the server
* count -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_esp_platform_wait_time_overflow_check(struct wait_param *pwait_action)
{
ESP_DBG("min_wait_second = %d", min_wait_second);
if (pwait_action->min_time_backup >= 3600) {
os_timer_disarm(&device_timer);
os_timer_setfn(&device_timer, (os_timer_func_t *)user_esp_platform_wait_time_overflow_check, pwait_action);
os_timer_arm(&device_timer, 3600000, 0);
ESP_DBG("min_wait_second is extended\n");
} else {
os_timer_disarm(&device_timer);
os_timer_setfn(&device_timer, (os_timer_func_t *)user_esp_platform_device_action, pwait_action);
os_timer_arm(&device_timer, pwait_action->min_time_backup * 1000, 0);
ESP_DBG("min_wait_second is = %dms\n", pwait_action->min_time_backup * 1000);
}
pwait_action->min_time_backup -= 3600;
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : timer_wait_param -- The received data from the server
* count -- the espconn used to connetion with the host
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
esp_platform_timer_action(struct esp_platform_wait_timer_param *timer_wait_param, uint16 count)
{
uint16 i = 0;
uint16 action_number;
struct wait_param pwait_action = {0};
pwait_action.count = count;
action_number = 0;
for (i = 0; i < count ; i++) {
if (timer_wait_param[i].wait_time_second == min_wait_second) {
os_memcpy(pwait_action.action[action_number], timer_wait_param[i].wait_action, os_strlen(timer_wait_param[i].wait_action));
ESP_DBG("*****%s*****\n", timer_wait_param[i].wait_action);
action_number++;
}
}
pwait_action.action_number = action_number;
pwait_action.min_time_backup = min_wait_second;
user_esp_platform_wait_time_overflow_check(&pwait_action);
}
/******************************************************************************
* FunctionName : user_platform_timer_start
* Description : Processing the message about timer from the server
* Parameters : pbuffer -- The received data from the server
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_platform_timer_start(char *pbuffer)
{
int str_begin = 0;
int str_end = 0;
uint8 i = 0;
char *pstr_start = NULL;
char *pstr_end = NULL;
struct esp_platform_wait_timer_param timer_wait_param[20];
char *pstr = NULL;
min_wait_second = 0;
if ((pstr = (char *)os_strstr(pbuffer, "\"timestamp\":")) != NULL) {
pstr_start = pstr + 13;
pstr_end = (char *)os_strstr(pstr_start, ",");
if (pstr != NULL) {
os_memcpy(timestamp_str, pstr_start, pstr_end - pstr_start);
timestamp = atoi(timestamp_str);
}
}
for (i = 0 ; i < 20 ; i++) {
if (timer_splits[i] != NULL) {
os_free(timer_splits[i]);
timer_splits[i] = NULL;
}
}
if ((pstr_start = (char *)os_strstr(pbuffer, "\"timers\": \"")) != NULL) {
str_begin = 11;
str_end = indexof(pstr_start, "\"", str_begin);
if (str_begin == str_end) {
os_timer_disarm(&device_timer);
return;
}
char *split_buffer = (char *)os_zalloc(str_end - str_begin + 1);
os_memcpy(split_buffer, pstr_start + str_begin, str_end - str_begin);
uint16 count = split(split_buffer , ";" , timer_splits);
os_free(split_buffer);
user_platform_timer_first_start(count);
}
}
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_json.c
*
* Description: JSON format set up and parse.
* Check your hardware transmation while use this data format.
*
* Modification history:
* 2014/5/09, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_json.h"
LOCAL char *json_buf;
LOCAL int pos;
LOCAL int size;
/******************************************************************************
* FunctionName : find_json_path
* Description : find the JSON format tree's path
* Parameters : json -- A pointer to a JSON set up
* path -- A pointer to the JSON format tree's path
* Returns : A pointer to the JSON format tree
*******************************************************************************/
struct jsontree_value *ICACHE_FLASH_ATTR
find_json_path(struct jsontree_context *json, const char *path)
{
struct jsontree_value *v;
const char *start;
const char *end;
int len;
v = json->values[0];
start = path;
do {
end = (const char *)os_strstr(start, "/");
if (end == start) {
break;
}
if (end != NULL) {
len = end - start;
end++;
} else {
len = os_strlen(start);
}
if (v->type != JSON_TYPE_OBJECT) {
v = NULL;
} else {
struct jsontree_object *o;
int i;
o = (struct jsontree_object *)v;
v = NULL;
for (i = 0; i < o->count; i++) {
if (os_strncmp(start, o->pairs[i].name, len) == 0) {
v = o->pairs[i].value;
json->index[json->depth] = i;
json->depth++;
json->values[json->depth] = v;
json->index[json->depth] = 0;
break;
}
}
}
start = end;
} while (end != NULL && *end != '\0' && v != NULL);
json->callback_state = 0;
return v;
}
/******************************************************************************
* FunctionName : json_putchar
* Description : write the value to the JSON format tree
* Parameters : c -- the value which write the JSON format tree
* Returns : result
*******************************************************************************/
int ICACHE_FLASH_ATTR
json_putchar(int c)
{
if (json_buf != NULL && pos <= size) {
json_buf[pos++] = c;
return c;
}
return 0;
}
/******************************************************************************
* FunctionName : json_ws_send
* Description : set up the JSON format tree for string
* Parameters : tree -- A pointer to the JSON format tree
* path -- A pointer to the JSON format tree's path
* pbuf -- A pointer for the data sent
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
json_ws_send(struct jsontree_value *tree, const char *path, char *pbuf)
{
struct jsontree_context json;
/* maxsize = 128 bytes */
json_buf = (char *)os_malloc(jsonSize);
/* reset state and set max-size */
/* NOTE: packet will be truncated at 512 bytes */
pos = 0;
size = jsonSize;
json.values[0] = (struct jsontree_value *)tree;
jsontree_reset(&json);
find_json_path(&json, path);
json.path = json.depth;
json.putchar = json_putchar;
while (jsontree_print_next(&json) && json.path <= json.depth);
json_buf[pos] = 0;
os_memcpy(pbuf, json_buf, pos);
os_free(json_buf);
}
/******************************************************************************
* FunctionName : json_parse
* Description : parse the data as a JSON format
* Parameters : js_ctx -- A pointer to a JSON set up
* ptrJSONMessage -- A pointer to the data
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
json_parse(struct jsontree_context *json, char *ptrJSONMessage)
{
/* Set value */
struct jsontree_value *v;
struct jsontree_callback *c;
struct jsontree_callback *c_bak = NULL;
while ((v = jsontree_find_next(json, JSON_TYPE_CALLBACK)) != NULL) {
c = (struct jsontree_callback *)v;
if (c == c_bak) {
continue;
}
c_bak = c;
if (c->set != NULL) {
struct jsonparse_state js;
jsonparse_setup(&js, ptrJSONMessage, os_strlen(ptrJSONMessage));
c->set(json, &js);
}
}
}
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_light.c
*
* Description: light demo's function realization
*
* Modification history:
* 2014/5/1, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_interface.h"
#include "user_light.h"
#include "pwm.h"
#if LIGHT_DEVICE
struct light_saved_param light_param;
/******************************************************************************
* FunctionName : user_light_get_duty
* Description : get duty of each channel
* Parameters : uint8 channel : LIGHT_RED/LIGHT_GREEN/LIGHT_BLUE
* Returns : NONE
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_light_get_duty(uint8 channel)
{
return light_param.pwm_duty[channel];
}
/******************************************************************************
* FunctionName : user_light_set_duty
* Description : set each channel's duty params
* Parameters : uint8 duty : 0 ~ PWM_DEPTH
* uint8 channel : LIGHT_RED/LIGHT_GREEN/LIGHT_BLUE
* Returns : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_light_set_duty(uint32 duty, uint8 channel)
{
if (duty != light_param.pwm_duty[channel]) {
pwm_set_duty(duty, channel);
light_param.pwm_duty[channel] = pwm_get_duty(channel);
}
}
/******************************************************************************
* FunctionName : user_light_get_period
* Description : get pwm period
* Parameters : NONE
* Returns : uint32 : pwm period
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR
user_light_get_period(void)
{
return light_param.pwm_period;
}
/******************************************************************************
* FunctionName : user_light_set_duty
* Description : set pwm frequency
* Parameters : uint16 freq : 100hz typically
* Returns : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_light_set_period(uint32 period)
{
if (period != light_param.pwm_period) {
pwm_set_period(period);
light_param.pwm_period = pwm_get_period();
}
}
void ICACHE_FLASH_ATTR
user_light_restart(void)
{
spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
pwm_start();
}
/******************************************************************************
* FunctionName : user_light_init
* Description : light demo init, mainy init pwm
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_light_init(void)
{
spi_flash_read((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
if(light_param.pwm_period>10000 || light_param.pwm_period <1000){
light_param.pwm_period = 1000;
}
uint32 io_info[][3] = { {PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
{PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
{PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM},
{PWM_3_OUT_IO_MUX,PWM_3_OUT_IO_FUNC,PWM_3_OUT_IO_NUM},
{PWM_4_OUT_IO_MUX,PWM_4_OUT_IO_FUNC,PWM_4_OUT_IO_NUM},
};
uint32 pwm_duty_init[PWM_CHANNEL] = {0};
/*PIN FUNCTION INIT FOR PWM OUTPUT*/
pwm_init(light_param.pwm_period, pwm_duty_init ,PWM_CHANNEL,io_info);
os_printf("LIGHT PARAM: R: %d \r\n",light_param.pwm_duty[LIGHT_RED]);
os_printf("LIGHT PARAM: G: %d \r\n",light_param.pwm_duty[LIGHT_GREEN]);
os_printf("LIGHT PARAM: B: %d \r\n",light_param.pwm_duty[LIGHT_BLUE]);
if(PWM_CHANNEL>LIGHT_COLD_WHITE){
os_printf("LIGHT PARAM: CW: %d \r\n",light_param.pwm_duty[LIGHT_COLD_WHITE]);
os_printf("LIGHT PARAM: WW: %d \r\n",light_param.pwm_duty[LIGHT_WARM_WHITE]);
}
os_printf("LIGHT PARAM: P: %d \r\n",light_param.pwm_period);
uint32 light_init_target[8]={0};
os_memcpy(light_init_target,light_param.pwm_duty,sizeof(light_param.pwm_duty));
light_set_aim(
light_init_target[LIGHT_RED],
light_init_target[LIGHT_GREEN],
light_init_target[LIGHT_BLUE],
light_init_target[LIGHT_COLD_WHITE],
light_init_target[LIGHT_WARM_WHITE],
light_param.pwm_period);
set_pwm_debug_en(0);//disable debug print in pwm driver
os_printf("PWM version : %08x \r\n",get_pwm_version());
}
#endif
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "mem.h"
#include "user_interface.h"
#include "user_light.h"
#include "user_light_adj.h"
#include "pwm.h"
#define ABS_MINUS(x,y) (x<y?(y-x):(x-y))
uint8 light_sleep_flg = 0;
uint16 min_ms = 15;
uint32 current_duty[PWM_CHANNEL] = {0};
bool change_finish=true;
os_timer_t timer_pwm_adj;
static u32 duty_now[PWM_CHANNEL] = {0};
//-----------------------------------Light para storage---------------------------
#define LIGHT_EVT_QNUM (40)
static struct pwm_param LightEvtArr[LIGHT_EVT_QNUM];
static u8 CurFreeLightEvtIdx = 0;
static u8 TotalUsedLightEvtNum = 0;
static u8 CurEvtIdxToBeUse = 0;
static struct pwm_param *LightEvtMalloc(void)
{
struct pwm_param *tmp = NULL;
TotalUsedLightEvtNum++;
if(TotalUsedLightEvtNum > LIGHT_EVT_QNUM ){
TotalUsedLightEvtNum--;
}
else{
tmp = &(LightEvtArr[CurFreeLightEvtIdx]);
CurFreeLightEvtIdx++;
if( CurFreeLightEvtIdx > (LIGHT_EVT_QNUM-1) )
CurFreeLightEvtIdx = 0;
}
os_printf("malloc:%u\n",TotalUsedLightEvtNum);
return tmp;
}
static void ICACHE_FLASH_ATTR LightEvtFree(void)
{
TotalUsedLightEvtNum--;
os_printf("free:%u\n",TotalUsedLightEvtNum);
}
//------------------------------------------------------------------------------------
static void ICACHE_FLASH_ATTR light_pwm_smooth_adj_proc(void);
void ICACHE_FLASH_ATTR
light_save_target_duty()
{
extern struct light_saved_param light_param;
os_memcpy(light_param.pwm_duty,current_duty,sizeof(light_param.pwm_duty));
light_param.pwm_period = pwm_get_period();
#if SAVE_LIGHT_PARAM
spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
(uint32 *)&light_param, sizeof(struct light_saved_param));
#endif
}
void ICACHE_FLASH_ATTR
light_set_aim_r(uint32 r)
{
current_duty[LIGHT_RED]=r;
light_pwm_smooth_adj_proc();
}
void ICACHE_FLASH_ATTR
light_set_aim_g(uint32 g)
{
current_duty[LIGHT_GREEN]=g;
light_pwm_smooth_adj_proc();
}
void ICACHE_FLASH_ATTR
light_set_aim_b(uint32 b)
{
current_duty[LIGHT_BLUE]=b;
light_pwm_smooth_adj_proc();
}
void ICACHE_FLASH_ATTR
light_set_aim_cw(uint32 cw)
{
current_duty[LIGHT_COLD_WHITE]=cw;
light_pwm_smooth_adj_proc();
}
void ICACHE_FLASH_ATTR
light_set_aim_ww(uint32 ww)
{
current_duty[LIGHT_WARM_WHITE]=ww;
light_pwm_smooth_adj_proc();
}
LOCAL bool ICACHE_FLASH_ATTR
check_pwm_current_duty_diff()
{
int i;
for(i=0;i<PWM_CHANNEL;i++){
if(pwm_get_duty(i) != current_duty[i]){
return true;
}
}
return false;
}
void light_dh_pwm_adj_proc(void *Targ)
{
uint8 i;
for(i=0;i<PWM_CHANNEL;i++){
duty_now[i] = (duty_now[i]*15 + current_duty[i])>>4;
if( ABS_MINUS(duty_now[i],current_duty[i])<20 )
duty_now[i] = current_duty[i];
user_light_set_duty(duty_now[i],i);
}
//os_printf("duty:%u,%u,%u\r\n", pwm.duty[0],pwm.duty[1],pwm.duty[2] );
pwm_start();
if(check_pwm_current_duty_diff()){
change_finish = 0;
os_timer_disarm(&timer_pwm_adj);
os_timer_setfn(&timer_pwm_adj, (os_timer_func_t *)light_dh_pwm_adj_proc, NULL);
os_timer_arm(&timer_pwm_adj, min_ms, 0);
}
else{
os_printf("finish\n");
change_finish = 1;
//light_save_target_duty();
os_timer_disarm(&timer_pwm_adj);
light_pwm_smooth_adj_proc();
}
}
LOCAL bool ICACHE_FLASH_ATTR
check_pwm_duty_zero()
{
int i;
for(i=0;i<PWM_CHANNEL;i++){
if(pwm_get_duty(i) != 0){
return false;
}
}
return true;
}
static void ICACHE_FLASH_ATTR light_pwm_smooth_adj_proc(void)
{
if( TotalUsedLightEvtNum>0 ){
user_light_set_period( LightEvtArr[CurEvtIdxToBeUse].period );
os_memcpy(current_duty,LightEvtArr[CurEvtIdxToBeUse].duty,sizeof(current_duty));
CurEvtIdxToBeUse++;
if(CurEvtIdxToBeUse > (LIGHT_EVT_QNUM-1) ){
CurEvtIdxToBeUse = 0;
}
LightEvtFree();
if(change_finish){
light_dh_pwm_adj_proc(NULL);
}
}
if(change_finish){
light_save_target_duty();
if(check_pwm_duty_zero()){
if(light_sleep_flg==0){
os_printf("light sleep en\r\n");
wifi_set_sleep_type(LIGHT_SLEEP_T);
light_sleep_flg = 1;
}
}
}
}
#if LIGHT_CURRENT_LIMIT
uint32 light_get_cur(uint32 duty , uint8 channel, uint32 period)
{
uint32 duty_max_limit = (period*1000/45);
uint32 duty_mapped = duty*22727/duty_max_limit;
switch(channel){
case LIGHT_RED :
if(duty_mapped>=0 && duty_mapped<23000){
return (duty_mapped*151000/22727);
}
break;
case LIGHT_GREEN:
if(duty_mapped>=0 && duty_mapped<23000){
return (duty_mapped*82000/22727);
}
break;
case LIGHT_BLUE:
if(duty_mapped>=0 && duty_mapped<23000){
return (duty_mapped*70000/22727);
}
break;
case LIGHT_COLD_WHITE:
case LIGHT_WARM_WHITE:
if(duty_mapped>=0 && duty_mapped<23000){
return (duty_mapped*115000/22727);
}
break;
default:
os_printf("CHANNEL ERROR IN GET_CUR\r\n");
break;
}
}
#endif
void ICACHE_FLASH_ATTR
light_set_aim(uint32 r,uint32 g,uint32 b,uint32 cw,uint32 ww,uint32 period)
{
struct pwm_param *tmp = LightEvtMalloc();
if(tmp != NULL){
tmp->period = (period<10000?period:10000);
uint32 duty_max_limit = (period*1000/45);
tmp->duty[LIGHT_RED] = (r<duty_max_limit?r:duty_max_limit);
tmp->duty[LIGHT_GREEN] = (g<duty_max_limit?g:duty_max_limit);
tmp->duty[LIGHT_BLUE] = (b<duty_max_limit?b:duty_max_limit);
tmp->duty[LIGHT_COLD_WHITE] = (cw<duty_max_limit?cw:duty_max_limit);
tmp->duty[LIGHT_WARM_WHITE] = (ww<duty_max_limit?ww:duty_max_limit);//chg
#if LIGHT_CURRENT_LIMIT
uint32 cur_r,cur_g,cur_b,cur_rgb;
//if(cw>0 || ww>0){
cur_r = light_get_cur(tmp->duty[LIGHT_RED] , LIGHT_RED, tmp->period);
cur_g = light_get_cur(tmp->duty[LIGHT_GREEN] , LIGHT_GREEN, tmp->period);
cur_b = light_get_cur(tmp->duty[LIGHT_BLUE] , LIGHT_BLUE, tmp->period);
cur_rgb = (cur_r+cur_g+cur_b);
//}
uint32 cur_cw = light_get_cur( tmp->duty[LIGHT_COLD_WHITE],LIGHT_COLD_WHITE, tmp->period);
uint32 cur_ww = light_get_cur( tmp->duty[LIGHT_WARM_WHITE],LIGHT_WARM_WHITE, tmp->period);
uint32 cur_remain,cur_mar;
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
cur_mar = LIGHT_CURRENT_MARGIN;
/*
if((cur_cw < 50000) || (cur_ww < 50000)){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
cur_mar = LIGHT_CURRENT_MARGIN;
}else if((cur_cw < 99000) || (cur_ww < 99000)){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}else{
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}
*/
/*
if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>120){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN);
cur_mar = LIGHT_CURRENT_MARGIN;
}else if((LIGHT_TOTAL_CURRENT_MAX-cur_rgb)>100){
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L2);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}else{
cur_remain = (LIGHT_TOTAL_CURRENT_MAX - cur_rgb -LIGHT_CURRENT_MARGIN_L3);
cur_mar = LIGHT_CURRENT_MARGIN_L2;
}
*/
os_printf("cur_remain: %d \r\n",cur_remain);
while((cur_cw+cur_ww) > cur_remain){
tmp->duty[LIGHT_COLD_WHITE] = tmp->duty[LIGHT_COLD_WHITE] * 9 / 10;
tmp->duty[LIGHT_WARM_WHITE] = tmp->duty[LIGHT_WARM_WHITE] * 9 / 10;
cur_cw = light_get_cur( tmp->duty[LIGHT_COLD_WHITE],LIGHT_COLD_WHITE, tmp->period);
cur_ww = light_get_cur( tmp->duty[LIGHT_WARM_WHITE],LIGHT_WARM_WHITE, tmp->period);
}
os_printf("debug : %d %d %d %d %d\r\n",cur_r/1000,cur_g/1000,cur_b/1000,cur_cw/1000,cur_ww/1000);
os_printf("debug:total current after adj : %d + %d mA \r\n",(cur_cw+cur_ww+cur_r+cur_g+cur_b)/1000,cur_mar/1000);
#endif
os_printf("prd:%u r : %u g: %u b: %u cw: %u ww: %u \r\n",period,
tmp->duty[0],tmp->duty[1],tmp->duty[2],tmp->duty[3],tmp->duty[4]);
light_pwm_smooth_adj_proc();
}
else{
os_printf("light para full\n");
}
}
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: user_main.c
*
* Description: entry file of user application
*
* Modification history:
* 2014/1/1, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "user_devicefind.h"
#include "user_webserver.h"
#if ESP_PLATFORM
#include "user_esp_platform.h"
#endif
void user_rf_pre_init(void)
{
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void user_init(void)
{
os_printf("SDK version:%s\n", system_get_sdk_version());
#if ESP_PLATFORM
/*Initialization of the peripheral drivers*/
/*For light demo , it is user_light_init();*/
/* Also check whether assigned ip addr by the router.If so, connect to ESP-server */
user_esp_platform_init();
#endif
/*Establish a udp socket to receive local device detect info.*/
/*Listen to the port 1025, as well as udp broadcast.
/*If receive a string of device_find_request, it rely its IP address and MAC.*/
user_devicefind_init();
/*Establish a TCP server for http(with JSON) POST or GET command to communicate with the device.*/
/*You can find the command in "2B-SDK-Espressif IoT Demo.pdf" to see the details.*/
/*the JSON command for curl is like:*/
/*3 Channel mode: curl -X POST -H "Content-Type:application/json" -d "{\"period\":1000,\"rgb\":{\"red\":16000,\"green\":16000,\"blue\":16000}}" http://192.168.4.1/config?command=light */
/*5 Channel mode: curl -X POST -H "Content-Type:application/json" -d "{\"period\":1000,\"rgb\":{\"red\":16000,\"green\":16000,\"blue\":16000,\"cwhite\":3000,\"wwhite\",3000}}" http://192.168.4.1/config?command=light */
#ifdef SERVER_SSL_ENABLE
user_webserver_init(SERVER_SSL_PORT);
#else
user_webserver_init(SERVER_PORT);
#endif
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Notice: AT added some functions so it's larger than before, if you want to compile it, please compile it as 1024KB or larger flash in compilation STEP 5.
1compile options
(1) COMPILE
Possible value: gcc
Default value:
If not set, use xt-xcc by default.
(2) BOOT
Possible value: none/old/new
none: no need boot
old: use boot_v1.1
new: use boot_v1.2+
Default value: none
(3) APP
Possible value: 0/1/2
0: original mode, generate eagle.app.v6.flash.bin and eagle.app.v6.irom0text.bin
1: generate user1
2: generate user2
Default value: 0
(3) SPI_SPEED
Possible value: 20/26.7/40/80
Default value: 40
(4) SPI_MODE
Possible value: QIO/QOUT/DIO/DOUT
Default value: QIO
(4) SPI_SIZE
Possible value: 0/2/3/4/5/6
Default value: 0
For example:
make COMPILE=gcc BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=0
2You can also use gen_misc to make and generate specific bin you needed.
Linux: ./gen_misc.sh
Windows: gen_misc.bat
Follow the tips and steps.
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#ifndef __USER_CONFIG_H__
#define __USER_CONFIG_H__
#define AT_CUSTOM_UPGRADE
#ifdef AT_CUSTOM_UPGRADE
#ifndef AT_UPGRADE_SUPPORT
#error "upgrade is not supported when eagle.flash.bin+eagle.irom0text.bin!!!"
#endif
#endif
#endif
#############################################################
# Required variables for each makefile
# Discard this section from all parent makefiles
# Expected variables (with automatic defaults):
# CSRCS (all "C" files in the dir)
# SUBDIRS (all subdirs with a Makefile)
# GEN_LIBS - list of libs to be generated ()
# GEN_IMAGES - list of images to be generated ()
# COMPONENTS_xxx - a list of libs/objs in the form
# subdir/lib to be extracted and rolled up into
# a generated lib/image xxx.a ()
#
ifndef PDIR
GEN_LIBS = libuser.a
endif
#############################################################
# Configuration i.e. compile options etc.
# Target specific stuff (defines etc.) goes in here!
# Generally values applying to a tree are captured in the
# makefile at its root level - these are then overridden
# for a subtree within the makefile rooted therein
#
#DEFINES +=
#############################################################
# Recursion Magic - Don't touch this!!
#
# Each subtree potentially has an include directory
# corresponding to the common APIs applicable to modules
# rooted at that subtree. Accordingly, the INCLUDE PATH
# of a module can only contain the include directories up
# its parent path, and not its siblings
#
# Required for each makefile to inherit from the parent
#
INCLUDES := $(INCLUDES) -I $(PDIR)include
INCLUDES += -I ./
INCLUDES += -I ../../include/ets
PDIR := ../$(PDIR)
sinclude $(PDIR)Makefile
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment