samples: mesh: taking care of interrupted transition
With this commit, it is now possible that if (for e.g.) lightness transition is interrupted by temperature transition (which could be instantaneous or non-instanstaneous) then as soon as temperature transition get over, then algorithm would achieve target value of lightness. Signed-off-by: Vikrant More <vikrant8051@gmail.com>
This commit is contained in:
parent
aed74024b5
commit
49aeb1ffe3
5 changed files with 65 additions and 31 deletions
|
@ -8,6 +8,7 @@
|
||||||
#ifndef _COMMON_H
|
#ifndef _COMMON_H
|
||||||
#define _COMMON_H
|
#define _COMMON_H
|
||||||
|
|
||||||
|
void update_led_gpio(void);
|
||||||
void update_light_state(void);
|
void update_light_state(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -105,9 +105,12 @@ static void light_default_status_init(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
default_tt = gen_def_trans_time_srv_user_data.tt;
|
default_tt = gen_def_trans_time_srv_user_data.tt;
|
||||||
|
|
||||||
|
target_lightness = lightness;
|
||||||
|
target_temperature = temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_light_state(void)
|
void update_led_gpio(void)
|
||||||
{
|
{
|
||||||
u8_t power, color;
|
u8_t power, color;
|
||||||
|
|
||||||
|
@ -139,6 +142,11 @@ void update_light_state(void)
|
||||||
/* LED4 Off */
|
/* LED4 Off */
|
||||||
gpio_pin_write(led_device[3], LED3_GPIO_PIN, 1);
|
gpio_pin_write(led_device[3], LED3_GPIO_PIN, 1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_light_state(void)
|
||||||
|
{
|
||||||
|
update_led_gpio();
|
||||||
|
|
||||||
if (*ptr_counter == 0 || reset == false) {
|
if (*ptr_counter == 0 || reset == false) {
|
||||||
reset = true;
|
reset = true;
|
||||||
|
|
|
@ -5,9 +5,10 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
#include "ble_mesh.h"
|
#include "ble_mesh.h"
|
||||||
#include "device_composition.h"
|
#include "device_composition.h"
|
||||||
|
#include "state_binding.h"
|
||||||
#include "storage.h"
|
#include "storage.h"
|
||||||
|
|
||||||
static void unsolicitedly_publish_states_work_handler(struct k_work *work)
|
static void unsolicitedly_publish_states_work_handler(struct k_work *work)
|
||||||
|
@ -44,6 +45,30 @@ K_TIMER_DEFINE(save_lightness_temp_last_values_timer,
|
||||||
|
|
||||||
static void no_transition_work_handler(struct k_work *work)
|
static void no_transition_work_handler(struct k_work *work)
|
||||||
{
|
{
|
||||||
|
bool readjust_light_state;
|
||||||
|
|
||||||
|
readjust_light_state = false;
|
||||||
|
|
||||||
|
if (!bt_mesh_is_provisioned()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target_lightness != lightness) {
|
||||||
|
lightness = target_lightness;
|
||||||
|
readjust_lightness();
|
||||||
|
readjust_light_state = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target_temperature != temperature) {
|
||||||
|
temperature = target_temperature;
|
||||||
|
readjust_temperature();
|
||||||
|
readjust_light_state = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (readjust_light_state) {
|
||||||
|
update_led_gpio();
|
||||||
|
}
|
||||||
|
|
||||||
k_timer_start(&unsolicitedly_publish_states_timer, K_MSEC(5000), 0);
|
k_timer_start(&unsolicitedly_publish_states_timer, K_MSEC(5000), 0);
|
||||||
|
|
||||||
/* If Lightness & Temperature values remains stable for
|
/* If Lightness & Temperature values remains stable for
|
||||||
|
|
|
@ -49,7 +49,7 @@ static s32_t ceiling(float num)
|
||||||
return inum + 1;
|
return inum + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
u16_t actual_to_linear(u16_t val)
|
static u16_t actual_to_linear(u16_t val)
|
||||||
{
|
{
|
||||||
float tmp;
|
float tmp;
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ u16_t actual_to_linear(u16_t val)
|
||||||
return (u16_t) ceiling(65535 * tmp * tmp);
|
return (u16_t) ceiling(65535 * tmp * tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
u16_t linear_to_actual(u16_t val)
|
static u16_t linear_to_actual(u16_t val)
|
||||||
{
|
{
|
||||||
return (u16_t) (65535 * sqrt(((float) val / 65535)));
|
return (u16_t) (65535 * sqrt(((float) val / 65535)));
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,30 @@ static u16_t level_to_light_ctl_temp(s16_t level)
|
||||||
/* 6.1.3.1.1 1st formula end */
|
/* 6.1.3.1.1 1st formula end */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readjust_lightness(void)
|
||||||
|
{
|
||||||
|
if (lightness != 0) {
|
||||||
|
light_lightness_srv_user_data.last = lightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lightness) {
|
||||||
|
gen_onoff_srv_root_user_data.onoff = STATE_ON;
|
||||||
|
} else {
|
||||||
|
gen_onoff_srv_root_user_data.onoff = STATE_OFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
gen_level_srv_root_user_data.level = lightness - 32768;
|
||||||
|
light_lightness_srv_user_data.actual = lightness;
|
||||||
|
light_lightness_srv_user_data.linear = actual_to_linear(lightness);
|
||||||
|
light_ctl_srv_user_data.lightness = lightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
void readjust_temperature(void)
|
||||||
|
{
|
||||||
|
gen_level_srv_s0_user_data.level = temperature;
|
||||||
|
light_ctl_srv_user_data.temp = level_to_light_ctl_temp(temperature);
|
||||||
|
}
|
||||||
|
|
||||||
void state_binding(u8_t light, u8_t temp)
|
void state_binding(u8_t light, u8_t temp)
|
||||||
{
|
{
|
||||||
switch (temp) {
|
switch (temp) {
|
||||||
|
@ -197,29 +221,13 @@ void state_binding(u8_t light, u8_t temp)
|
||||||
constrain_lightness(lightness);
|
constrain_lightness(lightness);
|
||||||
|
|
||||||
jump:
|
jump:
|
||||||
if (lightness != 0) {
|
readjust_lightness();
|
||||||
light_lightness_srv_user_data.last = lightness;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lightness) {
|
|
||||||
gen_onoff_srv_root_user_data.onoff = STATE_ON;
|
|
||||||
} else {
|
|
||||||
gen_onoff_srv_root_user_data.onoff = STATE_OFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
gen_level_srv_root_user_data.level = lightness - 32768;
|
|
||||||
light_lightness_srv_user_data.actual = lightness;
|
|
||||||
light_lightness_srv_user_data.linear = actual_to_linear(lightness);
|
|
||||||
light_ctl_srv_user_data.lightness = lightness;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_lightness_target_values(u8_t type)
|
void calculate_lightness_target_values(u8_t type)
|
||||||
{
|
{
|
||||||
bool set_light_ctl_temp_target_value;
|
|
||||||
u16_t tmp;
|
u16_t tmp;
|
||||||
|
|
||||||
set_light_ctl_temp_target_value = true;
|
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ONOFF:
|
case ONOFF:
|
||||||
if (gen_onoff_srv_root_user_data.target_onoff == 0) {
|
if (gen_onoff_srv_root_user_data.target_onoff == 0) {
|
||||||
|
@ -242,8 +250,6 @@ void calculate_lightness_target_values(u8_t type)
|
||||||
tmp = linear_to_actual(light_lightness_srv_user_data.target_linear);
|
tmp = linear_to_actual(light_lightness_srv_user_data.target_linear);
|
||||||
break;
|
break;
|
||||||
case CTL:
|
case CTL:
|
||||||
set_light_ctl_temp_target_value = false;
|
|
||||||
|
|
||||||
tmp = light_ctl_srv_user_data.target_lightness;
|
tmp = light_ctl_srv_user_data.target_lightness;
|
||||||
|
|
||||||
target_temperature = light_ctl_temp_to_level(light_ctl_srv_user_data.target_temp);
|
target_temperature = light_ctl_temp_to_level(light_ctl_srv_user_data.target_temp);
|
||||||
|
@ -269,11 +275,6 @@ void calculate_lightness_target_values(u8_t type)
|
||||||
actual_to_linear(target_lightness);
|
actual_to_linear(target_lightness);
|
||||||
|
|
||||||
light_ctl_srv_user_data.target_lightness = target_lightness;
|
light_ctl_srv_user_data.target_lightness = target_lightness;
|
||||||
|
|
||||||
if (set_light_ctl_temp_target_value) {
|
|
||||||
target_temperature = light_ctl_srv_user_data.temp;
|
|
||||||
light_ctl_srv_user_data.target_temp = target_temperature;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_temp_target_values(u8_t type)
|
void calculate_temp_target_values(u8_t type)
|
||||||
|
@ -298,9 +299,6 @@ void calculate_temp_target_values(u8_t type)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
target_lightness = light_ctl_srv_user_data.lightness;
|
|
||||||
light_ctl_srv_user_data.target_lightness = target_lightness;
|
|
||||||
|
|
||||||
if (set_light_ctl_delta_uv_target_value) {
|
if (set_light_ctl_delta_uv_target_value) {
|
||||||
|
|
||||||
light_ctl_srv_user_data.target_delta_uv =
|
light_ctl_srv_user_data.target_delta_uv =
|
||||||
|
|
|
@ -27,6 +27,8 @@ enum state_binding {
|
||||||
extern u16_t lightness, target_lightness;
|
extern u16_t lightness, target_lightness;
|
||||||
extern s16_t temperature, target_temperature;
|
extern s16_t temperature, target_temperature;
|
||||||
|
|
||||||
|
void readjust_lightness(void);
|
||||||
|
void readjust_temperature(void);
|
||||||
void state_binding(u8_t lightness, u8_t temperature);
|
void state_binding(u8_t lightness, u8_t temperature);
|
||||||
void calculate_lightness_target_values(u8_t type);
|
void calculate_lightness_target_values(u8_t type);
|
||||||
void calculate_temp_target_values(u8_t type);
|
void calculate_temp_target_values(u8_t type);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue