Skip to content

Commit c97e872

Browse files
vikrant8052jhedberg
authored andcommitted
samples: mesh: nrf52: used constrain functions whereever possible
Added support of constrain_temperature() function. Used constrain_lightness() & constrain_temperature() whereever possible. Signed-off-by: Vikrant More <[email protected]>
1 parent ff323ce commit c97e872

File tree

4 files changed

+25
-57
lines changed

4 files changed

+25
-57
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static void light_default_status_init(void)
6262
ctl->light->def = constrain_lightness(ctl->light->def);
6363
ctl->light->target = constrain_lightness(ctl->light->target);
6464

65+
ctl->temp->def = constrain_temperature(ctl->temp->def);
66+
ctl->temp->target = constrain_temperature(ctl->temp->target);
67+
6568
ctl->temp->current = ctl->temp->def;
6669
ctl->duv->current = ctl->duv->def;
6770

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

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,13 +1134,6 @@ static void light_lightness_set_unack(struct bt_mesh_model *model,
11341134
ctl->transition->tt = tt;
11351135
ctl->transition->delay = delay;
11361136
ctl->transition->type = NON_MOVE;
1137-
1138-
if (actual > 0 && actual < ctl->light->range_min) {
1139-
actual = ctl->light->range_min;
1140-
} else if (actual > ctl->light->range_max) {
1141-
actual = ctl->light->range_max;
1142-
}
1143-
11441137
set_target(ACTUAL, &actual);
11451138

11461139
if (ctl->light->target != ctl->light->current) {
@@ -1206,13 +1199,6 @@ static void light_lightness_set(struct bt_mesh_model *model,
12061199
ctl->transition->tt = tt;
12071200
ctl->transition->delay = delay;
12081201
ctl->transition->type = NON_MOVE;
1209-
1210-
if (actual > 0 && actual < ctl->light->range_min) {
1211-
actual = ctl->light->range_min;
1212-
} else if (actual > ctl->light->range_max) {
1213-
actual = ctl->light->range_max;
1214-
}
1215-
12161202
set_target(ACTUAL, &actual);
12171203

12181204
if (ctl->light->target != ctl->light->current) {
@@ -1490,9 +1476,10 @@ static void light_lightness_default_set_unack(struct bt_mesh_model *model,
14901476
u16_t lightness;
14911477

14921478
lightness = net_buf_simple_pull_le16(buf);
1479+
lightness = constrain_lightness(lightness);
14931480

14941481
if (ctl->light->def != lightness) {
1495-
ctl->light->def = constrain_lightness(lightness);
1482+
ctl->light->def = lightness;
14961483

14971484
light_lightness_default_publish(model);
14981485
save_on_flash(DEF_STATES);
@@ -1745,13 +1732,6 @@ static void light_ctl_set_unack(struct bt_mesh_model *model,
17451732
ctl->transition->delay = delay;
17461733
ctl->transition->type = NON_MOVE;
17471734
set_target(CTL_LIGHT, &lightness);
1748-
1749-
if (temp < ctl->temp->range_min) {
1750-
temp = ctl->temp->range_min;
1751-
} else if (temp > ctl->temp->range_max) {
1752-
temp = ctl->temp->range_max;
1753-
}
1754-
17551735
set_target(CTL_TEMP, &temp);
17561736
set_target(CTL_DELTA_UV, &delta_uv);
17571737

@@ -1830,13 +1810,6 @@ static void light_ctl_set(struct bt_mesh_model *model,
18301810
ctl->transition->delay = delay;
18311811
ctl->transition->type = NON_MOVE;
18321812
set_target(CTL_LIGHT, &lightness);
1833-
1834-
if (temp < ctl->temp->range_min) {
1835-
temp = ctl->temp->range_min;
1836-
} else if (temp > ctl->temp->range_max) {
1837-
temp = ctl->temp->range_max;
1838-
}
1839-
18401813
set_target(CTL_TEMP, &temp);
18411814
set_target(CTL_DELTA_UV, &delta_uv);
18421815

@@ -1933,15 +1906,12 @@ static bool light_ctl_default_setunack(struct bt_mesh_model *model,
19331906
return false;
19341907
}
19351908

1936-
if (temp < ctl->temp->range_min) {
1937-
temp = ctl->temp->range_min;
1938-
} else if (temp > ctl->temp->range_max) {
1939-
temp = ctl->temp->range_max;
1940-
}
1909+
lightness = constrain_lightness(lightness);
1910+
temp = constrain_temperature(temp);
19411911

19421912
if (ctl->light->def != lightness || ctl->temp->def != temp ||
19431913
ctl->duv->def != delta_uv) {
1944-
ctl->light->def = constrain_lightness(lightness);
1914+
ctl->light->def = lightness;
19451915
ctl->temp->def = temp;
19461916
ctl->duv->def = delta_uv;
19471917

@@ -2205,13 +2175,6 @@ static void light_ctl_temp_set_unack(struct bt_mesh_model *model,
22052175
ctl->transition->tt = tt;
22062176
ctl->transition->delay = delay;
22072177
ctl->transition->type = NON_MOVE;
2208-
2209-
if (temp < ctl->temp->range_min) {
2210-
temp = ctl->temp->range_min;
2211-
} else if (temp > ctl->temp->range_max) {
2212-
temp = ctl->temp->range_max;
2213-
}
2214-
22152178
set_target(CTL_TEMP, &temp);
22162179
set_target(CTL_DELTA_UV, &delta_uv);
22172180

@@ -2286,13 +2249,6 @@ static void light_ctl_temp_set(struct bt_mesh_model *model,
22862249
ctl->transition->tt = tt;
22872250
ctl->transition->delay = delay;
22882251
ctl->transition->type = NON_MOVE;
2289-
2290-
if (temp < ctl->temp->range_min) {
2291-
temp = ctl->temp->range_min;
2292-
} else if (temp > ctl->temp->range_max) {
2293-
temp = ctl->temp->range_max;
2294-
}
2295-
22962252
set_target(CTL_TEMP, &temp);
22972253
set_target(CTL_DELTA_UV, &delta_uv);
22982254

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ u16_t constrain_lightness(u16_t light)
7272
return light;
7373
}
7474

75-
static void constrain_target_lightness(void)
76-
{
77-
ctl->light->target = constrain_lightness(ctl->light->target);
78-
}
79-
8075
static void constrain_target_lightness2(void)
8176
{
8277
/* This is as per Mesh Model Specification 3.3.2.2.3 */
@@ -92,6 +87,17 @@ static void constrain_target_lightness2(void)
9287
}
9388
}
9489

90+
u16_t constrain_temperature(u16_t temp)
91+
{
92+
if (temp < ctl->temp->range_min) {
93+
temp = ctl->temp->range_min;
94+
} else if (temp > ctl->temp->range_max) {
95+
temp = ctl->temp->range_max;
96+
}
97+
98+
return temp;
99+
}
100+
95101
static s16_t light_ctl_temp_to_level(u16_t temp)
96102
{
97103
float tmp;
@@ -143,28 +149,30 @@ void set_target(u8_t type, void *dptr)
143149
break;
144150
case LEVEL_LIGHT:
145151
ctl->light->target = *((s16_t *) dptr) - INT16_MIN;
146-
constrain_target_lightness();
152+
ctl->light->target = constrain_lightness(ctl->light->target);
147153
break;
148154
case DELTA_LEVEL_LIGHT:
149155
ctl->light->target = *((s16_t *) dptr) - INT16_MIN;
150156
constrain_target_lightness2();
151157
break;
152158
case ACTUAL:
153159
ctl->light->target = *((u16_t *) dptr);
160+
ctl->light->target = constrain_lightness(ctl->light->target);
154161
break;
155162
case LINEAR:
156163
ctl->light->target = linear_to_actual(*((u16_t *) dptr));
157-
constrain_target_lightness();
164+
ctl->light->target = constrain_lightness(ctl->light->target);
158165
break;
159166
case CTL_LIGHT:
160167
ctl->light->target = *((u16_t *) dptr);
161-
constrain_target_lightness();
168+
ctl->light->target = constrain_lightness(ctl->light->target);
162169
break;
163170
case LEVEL_TEMP:
164171
ctl->temp->target = level_to_light_ctl_temp(*((s16_t *) dptr));
165172
break;
166173
case CTL_TEMP:
167174
ctl->temp->target = *((u16_t *) dptr);
175+
ctl->temp->target = constrain_temperature(ctl->temp->target);
168176
break;
169177
case CTL_DELTA_UV:
170178
ctl->duv->target = *((s16_t *) dptr);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum state_binding {
2525
};
2626

2727
u16_t constrain_lightness(u16_t light);
28+
u16_t constrain_temperature(u16_t temp);
2829
u16_t level_to_light_ctl_temp(s16_t level);
2930

3031
void set_target(u8_t type, void *dptr);

0 commit comments

Comments
 (0)