Skip to content

Commit b80b359

Browse files
vikrant8052jhedberg
authored andcommitted
samples: mesh: nrf52: minor improvement in MOVE msg handlers
Changes in MOVE message handler are as per Mesh Model Specification which says: "Upon receiving a Generic Move Set message, the Generic Level Server shall respond with a Generic Level Status message. The target Generic Level state is the upper limit of the Generic Level state when the transition speed is positive, or the lower limit of the Generic Level state when the transition speed is negative." Signed-off-by: Vikrant More <[email protected]>
1 parent 102d402 commit b80b359

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/device_composition.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,8 @@ static void gen_move_set_unack(struct bt_mesh_model *model,
642642
struct net_buf_simple *buf)
643643
{
644644
u8_t tid, tt, delay;
645-
s16_t delta, target;
645+
s16_t delta;
646+
u16_t target;
646647
s64_t now;
647648

648649
delta = (s16_t) net_buf_simple_pull_le16(buf);
@@ -686,14 +687,13 @@ static void gen_move_set_unack(struct bt_mesh_model *model,
686687
ctl->light->delta = delta;
687688

688689
if (delta < 0) {
689-
target = INT16_MIN;
690-
set_target(LEVEL_LIGHT, &target);
690+
target = ctl->light->range_min;
691691
} else if (delta > 0) {
692-
target = INT16_MAX;
693-
set_target(LEVEL_LIGHT, &target);
692+
target = ctl->light->range_max;
694693
} else if (delta == 0) {
695-
ctl->light->target = ctl->light->current;
694+
target = ctl->light->current;
696695
}
696+
set_target(MOVE_LIGHT, &target);
697697

698698
if (ctl->light->target != ctl->light->current) {
699699
set_transition_values(MOVE_LIGHT);
@@ -715,7 +715,8 @@ static void gen_move_set(struct bt_mesh_model *model,
715715
struct net_buf_simple *buf)
716716
{
717717
u8_t tid, tt, delay;
718-
s16_t delta, target;
718+
s16_t delta;
719+
u16_t target;
719720
s64_t now;
720721

721722
delta = (s16_t) net_buf_simple_pull_le16(buf);
@@ -760,14 +761,13 @@ static void gen_move_set(struct bt_mesh_model *model,
760761
ctl->light->delta = delta;
761762

762763
if (delta < 0) {
763-
target = INT16_MIN;
764-
set_target(LEVEL_LIGHT, &target);
764+
target = ctl->light->range_min;
765765
} else if (delta > 0) {
766-
target = INT16_MAX;
767-
set_target(LEVEL_LIGHT, &target);
766+
target = ctl->light->range_max;
768767
} else if (delta == 0) {
769-
ctl->light->target = ctl->light->current;
768+
target = ctl->light->current;
770769
}
770+
set_target(MOVE_LIGHT, &target);
771771

772772
if (ctl->light->target != ctl->light->current) {
773773
set_transition_values(MOVE_LIGHT);
@@ -2685,7 +2685,8 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model,
26852685
struct net_buf_simple *buf)
26862686
{
26872687
u8_t tid, tt, delay;
2688-
s16_t delta, target;
2688+
s16_t delta;
2689+
u16_t target;
26892690
s64_t now;
26902691

26912692
delta = (s16_t) net_buf_simple_pull_le16(buf);
@@ -2729,14 +2730,13 @@ static void gen_move_set_unack_temp(struct bt_mesh_model *model,
27292730
ctl->temp->delta = delta;
27302731

27312732
if (delta < 0) {
2732-
target = INT16_MIN;
2733-
set_target(LEVEL_TEMP, &target);
2733+
target = ctl->temp->range_min;
27342734
} else if (delta > 0) {
2735-
target = INT16_MAX;
2736-
set_target(LEVEL_TEMP, &target);
2735+
target = ctl->temp->range_max;
27372736
} else if (delta == 0) {
2738-
ctl->temp->target = ctl->temp->current;
2737+
target = ctl->temp->current;
27392738
}
2739+
set_target(MOVE_TEMP, &target);
27402740

27412741
if (ctl->temp->target != ctl->temp->current) {
27422742
set_transition_values(MOVE_TEMP);
@@ -2758,7 +2758,8 @@ static void gen_move_set_temp(struct bt_mesh_model *model,
27582758
struct net_buf_simple *buf)
27592759
{
27602760
u8_t tid, tt, delay;
2761-
s16_t delta, target;
2761+
s16_t delta;
2762+
u16_t target;
27622763
s64_t now;
27632764

27642765
delta = (s16_t) net_buf_simple_pull_le16(buf);
@@ -2803,14 +2804,13 @@ static void gen_move_set_temp(struct bt_mesh_model *model,
28032804
ctl->temp->delta = delta;
28042805

28052806
if (delta < 0) {
2806-
target = INT16_MIN;
2807-
set_target(LEVEL_TEMP, &target);
2807+
target = ctl->temp->range_min;
28082808
} else if (delta > 0) {
2809-
target = INT16_MAX;
2810-
set_target(LEVEL_TEMP, &target);
2809+
target = ctl->temp->range_max;
28112810
} else if (delta == 0) {
2812-
ctl->temp->target = ctl->temp->current;
2811+
target = ctl->temp->current;
28132812
}
2813+
set_target(MOVE_TEMP, &target);
28142814

28152815
if (ctl->temp->target != ctl->temp->current) {
28162816
set_transition_values(MOVE_TEMP);

samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/state_binding.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ void set_target(u8_t type, void *dptr)
155155
ctl->light->target = *((s16_t *) dptr) - INT16_MIN;
156156
constrain_target_lightness2();
157157
break;
158+
case MOVE_LIGHT:
159+
ctl->light->target = *((u16_t *) dptr);
160+
break;
158161
case ACTUAL:
159162
ctl->light->target = *((u16_t *) dptr);
160163
ctl->light->target = constrain_lightness(ctl->light->target);
@@ -170,6 +173,9 @@ void set_target(u8_t type, void *dptr)
170173
case LEVEL_TEMP:
171174
ctl->temp->target = level_to_light_ctl_temp(*((s16_t *) dptr));
172175
break;
176+
case MOVE_TEMP:
177+
ctl->temp->target = *((u16_t *) dptr);
178+
break;
173179
case CTL_TEMP:
174180
ctl->temp->target = *((u16_t *) dptr);
175181
ctl->temp->target = constrain_temperature(ctl->temp->target);

samples/boards/nrf/mesh/onoff_level_lighting_vnd_app/src/mesh/transition.c

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,33 @@ static void onoff_work_handler(struct k_work *work)
180180

181181
static void level_move_lightness_work_handler(void)
182182
{
183-
s16_t level;
183+
int light;
184184

185-
level = (s16_t) get_current(LEVEL_LIGHT) + ctl->light->delta;
185+
light = 0;
186186

187-
ctl->light->current = constrain_lightness((u16_t) (level - INT16_MIN));
187+
if (ctl->light->current) {
188+
light = ctl->light->current + ctl->light->delta;
189+
} else {
190+
if (ctl->light->delta < 0) {
191+
light = UINT16_MAX + ctl->light->delta;
192+
} else if (ctl->light->delta > 0) {
193+
light = ctl->light->delta;
194+
}
195+
}
196+
197+
if (light > ctl->light->range_max) {
198+
light = ctl->light->range_max;
199+
} else if (light < ctl->light->range_min) {
200+
light = ctl->light->range_min;
201+
}
202+
203+
ctl->light->current = light;
188204
update_light_state();
205+
206+
if (ctl->light->target == light) {
207+
ctl->transition->counter = 0;
208+
k_timer_stop(&ctl->transition->timer);
209+
}
189210
}
190211

191212
static void level_lightness_work_handler(struct k_work *work)
@@ -221,12 +242,33 @@ static void level_lightness_work_handler(struct k_work *work)
221242

222243
static void level_move_temp_work_handler(void)
223244
{
224-
s16_t level;
245+
int temp;
225246

226-
level = (s16_t) get_current(LEVEL_TEMP) + ctl->temp->delta;
247+
temp = 0;
227248

228-
ctl->temp->current = level_to_light_ctl_temp(level);
249+
if (ctl->temp->current) {
250+
temp = ctl->temp->current + ctl->temp->delta;
251+
} else {
252+
if (ctl->temp->delta < 0) {
253+
temp = UINT16_MAX + ctl->temp->delta;
254+
} else if (ctl->temp->delta > 0) {
255+
temp = ctl->temp->delta;
256+
}
257+
}
258+
259+
if (temp > ctl->temp->range_max) {
260+
temp = ctl->temp->range_max;
261+
} else if (temp < ctl->temp->range_min) {
262+
temp = ctl->temp->range_min;
263+
}
264+
265+
ctl->temp->current = temp;
229266
update_light_state();
267+
268+
if (ctl->temp->target == temp) {
269+
ctl->transition->counter = 0;
270+
k_timer_stop(&ctl->transition->timer);
271+
}
230272
}
231273

232274
static void level_temp_work_handler(struct k_work *work)

0 commit comments

Comments
 (0)