Skip to content

Commit 0bf0eb7

Browse files
vikrant8052jhedberg
authored andcommitted
samples: mesh: nrf52: Simplified implementation
Create single structure of light_ctl_state & simplified state binding algorithm. Signed-off-by: Vikrant More <[email protected]>
1 parent f2cc624 commit 0bf0eb7

File tree

10 files changed

+1438
-1266
lines changed

10 files changed

+1438
-1266
lines changed

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

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,95 +24,87 @@ static bool reset;
2424

2525
static void light_default_var_init(void)
2626
{
27-
gen_def_trans_time_srv_user_data.tt = 0x00;
27+
ctl->tt = 0x00;
2828

29-
gen_power_onoff_srv_user_data.onpowerup = STATE_DEFAULT;
29+
ctl->onpowerup = STATE_DEFAULT;
3030

31-
light_lightness_srv_user_data.light_range_min = LIGHTNESS_MIN;
32-
light_lightness_srv_user_data.light_range_max = LIGHTNESS_MAX;
33-
light_lightness_srv_user_data.last = LIGHTNESS_MAX;
34-
light_lightness_srv_user_data.def = LIGHTNESS_MAX;
31+
ctl->light->range_min = LIGHTNESS_MIN;
32+
ctl->light->range_max = LIGHTNESS_MAX;
33+
ctl->light->last = LIGHTNESS_MAX;
34+
ctl->light->def = LIGHTNESS_MAX;
3535

36-
light_ctl_srv_user_data.temp_range_min = TEMP_MIN;
37-
light_ctl_srv_user_data.temp_range_max = TEMP_MAX;
38-
light_ctl_srv_user_data.lightness_def = LIGHTNESS_MAX;
39-
light_ctl_srv_user_data.temp_def = TEMP_MIN;
36+
ctl->temp->range_min = TEMP_MIN;
37+
ctl->temp->range_max = TEMP_MAX;
38+
ctl->temp->def = TEMP_MAX;
4039

41-
light_ctl_srv_user_data.lightness_temp_last_tgt =
42-
(u32_t) ((LIGHTNESS_MAX << 16) | TEMP_MIN);
40+
ctl->duv->def = DELTA_UV_DEF;
41+
42+
ctl->light_temp_def = (u32_t) ((LIGHTNESS_MAX << 16) | TEMP_MAX);
43+
ctl->light_temp_last_tgt = (u32_t) ((LIGHTNESS_MAX << 16) | TEMP_MAX);
4344
}
4445

46+
/* This function should only get call after reading persistent data storage */
4547
static void light_default_status_init(void)
4648
{
47-
/* Retrieve Default Lightness & Temperature Values */
48-
49-
if (light_ctl_srv_user_data.lightness_temp_def) {
50-
light_ctl_srv_user_data.lightness_def = (u16_t)
51-
(light_ctl_srv_user_data.lightness_temp_def >> 16);
49+
u16_t light_def;
5250

53-
light_ctl_srv_user_data.temp_def = (u16_t)
54-
(light_ctl_srv_user_data.lightness_temp_def);
51+
/* Retrieve Range of Lightness */
52+
if (ctl->light->range) {
53+
ctl->light->range_max = (u16_t) (ctl->light->range >> 16);
54+
ctl->light->range_min = (u16_t) ctl->light->range;
5555
}
5656

57-
light_lightness_srv_user_data.def =
58-
light_ctl_srv_user_data.lightness_def;
59-
60-
light_ctl_srv_user_data.temp = light_ctl_srv_user_data.temp_def;
61-
62-
/* Retrieve Range of Lightness & Temperature */
63-
64-
if (light_lightness_srv_user_data.lightness_range) {
65-
light_lightness_srv_user_data.light_range_max = (u16_t)
66-
(light_lightness_srv_user_data.lightness_range >> 16);
67-
68-
light_lightness_srv_user_data.light_range_min = (u16_t)
69-
(light_lightness_srv_user_data.lightness_range);
57+
/* Retrieve Range of Temperature */
58+
if (ctl->temp->range) {
59+
ctl->temp->range_max = (u16_t) (ctl->temp->range >> 16);
60+
ctl->temp->range_min = (u16_t) ctl->temp->range;
7061
}
7162

72-
if (light_ctl_srv_user_data.temperature_range) {
73-
light_ctl_srv_user_data.temp_range_max = (u16_t)
74-
(light_ctl_srv_user_data.temperature_range >> 16);
63+
/* Retrieve Default Lightness Value */
64+
light_def = (u16_t) (ctl->light_temp_def >> 16);
65+
ctl->light->def = constrain_lightness(light_def);
7566

76-
light_ctl_srv_user_data.temp_range_min = (u16_t)
77-
(light_ctl_srv_user_data.temperature_range);
78-
}
67+
/* Retrieve Default Temperature Value */
68+
ctl->temp->def = (u16_t) ctl->light_temp_def;
69+
70+
ctl->temp->current = ctl->temp->def;
71+
ctl->duv->current = ctl->duv->def;
7972

80-
switch (gen_power_onoff_srv_user_data.onpowerup) {
73+
switch (ctl->onpowerup) {
8174
case STATE_OFF:
82-
gen_onoff_srv_root_user_data.onoff = STATE_OFF;
83-
state_binding(ONOFF, ONOFF_TEMP);
75+
ctl->light->current = 0U;
8476
break;
8577
case STATE_DEFAULT:
86-
gen_onoff_srv_root_user_data.onoff = STATE_ON;
87-
state_binding(ONOFF, ONOFF_TEMP);
78+
if (ctl->light->def == 0U) {
79+
ctl->light->current = ctl->light->last;
80+
} else {
81+
ctl->light->current = ctl->light->def;
82+
}
8883
break;
8984
case STATE_RESTORE:
90-
light_ctl_srv_user_data.lightness =
91-
(u16_t) (light_ctl_srv_user_data.lightness_temp_last_tgt >> 16);
92-
93-
light_ctl_srv_user_data.temp =
94-
(u16_t) (light_ctl_srv_user_data.lightness_temp_last_tgt);
95-
96-
state_binding(ONPOWERUP, ONOFF_TEMP);
85+
ctl->light->current = (u16_t) (ctl->light_temp_last_tgt >> 16);
86+
ctl->temp->current = (u16_t) ctl->light_temp_last_tgt;
9787
break;
9888
}
9989

100-
default_tt = gen_def_trans_time_srv_user_data.tt;
90+
default_tt = ctl->tt;
10191

102-
init_lightness_target_values();
103-
init_temp_target_values();
92+
ctl->light->target = ctl->light->current;
93+
ctl->temp->target = ctl->temp->current;
94+
ctl->duv->target = ctl->duv->current;
10495
}
10596

10697
void update_led_gpio(void)
10798
{
10899
u8_t power, color;
109100

110-
power = 100 * ((float) lightness / 65535);
111-
color = 100 * ((float) (temperature + 32768) / 65535);
101+
power = 100 * ((float) ctl->light->current / 65535);
102+
color = 100 * ((float) (ctl->temp->current - ctl->temp->range_min) /
103+
(ctl->temp->range_max - ctl->temp->range_min));
112104

113105
printk("power-> %d, color-> %d\n", power, color);
114106

115-
if (lightness) {
107+
if (ctl->light->current) {
116108
/* LED1 On */
117109
gpio_pin_write(led_device[0], DT_ALIAS_LED0_GPIOS_PIN, 0);
118110
} else {

samples/boards/nrf52/mesh/onoff_level_lighting_vnd_app/src/mesh/ble_mesh.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@
1616
#include <bluetooth/mesh.h>
1717

1818
/* Model Operation Codes */
19-
#define BT_MESH_MODEL_OP_GEN_ONOFF_GET BT_MESH_MODEL_OP_2(0x82, 0x01)
20-
#define BT_MESH_MODEL_OP_GEN_ONOFF_SET BT_MESH_MODEL_OP_2(0x82, 0x02)
21-
#define BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x03)
22-
#define BT_MESH_MODEL_OP_GEN_ONOFF_STATUS BT_MESH_MODEL_OP_2(0x82, 0x04)
19+
#define BT_MESH_MODEL_OP_GEN_ONOFF_GET BT_MESH_MODEL_OP_2(0x82, 0x01)
20+
#define BT_MESH_MODEL_OP_GEN_ONOFF_SET BT_MESH_MODEL_OP_2(0x82, 0x02)
21+
#define BT_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x03)
22+
#define BT_MESH_MODEL_OP_GEN_ONOFF_STATUS BT_MESH_MODEL_OP_2(0x82, 0x04)
2323

24-
#define BT_MESH_MODEL_OP_GEN_LEVEL_GET BT_MESH_MODEL_OP_2(0x82, 0x05)
25-
#define BT_MESH_MODEL_OP_GEN_LEVEL_SET BT_MESH_MODEL_OP_2(0x82, 0x06)
26-
#define BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x07)
27-
#define BT_MESH_MODEL_OP_GEN_LEVEL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x08)
28-
#define BT_MESH_MODEL_OP_GEN_DELTA_SET BT_MESH_MODEL_OP_2(0x82, 0x09)
29-
#define BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0A)
30-
#define BT_MESH_MODEL_OP_GEN_MOVE_SET BT_MESH_MODEL_OP_2(0x82, 0x0B)
31-
#define BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0C)
24+
#define BT_MESH_MODEL_OP_GEN_LEVEL_GET BT_MESH_MODEL_OP_2(0x82, 0x05)
25+
#define BT_MESH_MODEL_OP_GEN_LEVEL_SET BT_MESH_MODEL_OP_2(0x82, 0x06)
26+
#define BT_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x07)
27+
#define BT_MESH_MODEL_OP_GEN_LEVEL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x08)
28+
#define BT_MESH_MODEL_OP_GEN_DELTA_SET BT_MESH_MODEL_OP_2(0x82, 0x09)
29+
#define BT_MESH_MODEL_OP_GEN_DELTA_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0A)
30+
#define BT_MESH_MODEL_OP_GEN_MOVE_SET BT_MESH_MODEL_OP_2(0x82, 0x0B)
31+
#define BT_MESH_MODEL_OP_GEN_MOVE_SET_UNACK BT_MESH_MODEL_OP_2(0x82, 0x0C)
3232

33-
#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS BT_MESH_MODEL_OP_2(0x82, 0x10)
33+
#define BT_MESH_MODEL_GEN_DEF_TRANS_TIME_STATUS BT_MESH_MODEL_OP_2(0x82, 0x10)
3434

35-
#define BT_MESH_MODEL_GEN_ONPOWERUP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x12)
35+
#define BT_MESH_MODEL_GEN_ONPOWERUP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x12)
3636

37-
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS BT_MESH_MODEL_OP_2(0x82, 0x4E)
37+
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_STATUS BT_MESH_MODEL_OP_2(0x82, 0x4E)
3838
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LINEAR_STATUS \
39-
BT_MESH_MODEL_OP_2(0x82, 0x52)
39+
BT_MESH_MODEL_OP_2(0x82, 0x52)
4040
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_LAST_STATUS \
41-
BT_MESH_MODEL_OP_2(0x82, 0x54)
41+
BT_MESH_MODEL_OP_2(0x82, 0x54)
4242
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_DEFAULT_STATUS \
43-
BT_MESH_MODEL_OP_2(0x82, 0x56)
43+
BT_MESH_MODEL_OP_2(0x82, 0x56)
4444
#define BT_MESH_MODEL_LIGHT_LIGHTNESS_RANGE_STATUS \
45-
BT_MESH_MODEL_OP_2(0x82, 0x58)
45+
BT_MESH_MODEL_OP_2(0x82, 0x58)
4646

47-
#define BT_MESH_MODEL_LIGHT_CTL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x60)
47+
#define BT_MESH_MODEL_LIGHT_CTL_STATUS BT_MESH_MODEL_OP_2(0x82, 0x60)
4848
#define BT_MESH_MODEL_LIGHT_CTL_TEMP_RANGE_STATUS \
49-
BT_MESH_MODEL_OP_2(0x82, 0x63)
50-
#define BT_MESH_MODEL_LIGHT_CTL_TEMP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x66)
51-
#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS BT_MESH_MODEL_OP_2(0x82, 0x68)
49+
BT_MESH_MODEL_OP_2(0x82, 0x63)
50+
#define BT_MESH_MODEL_LIGHT_CTL_TEMP_STATUS BT_MESH_MODEL_OP_2(0x82, 0x66)
51+
#define BT_MESH_MODEL_LIGHT_CTL_DEFAULT_STATUS BT_MESH_MODEL_OP_2(0x82, 0x68)
5252

5353
void bt_ready(void);
5454

0 commit comments

Comments
 (0)