xref: /nrf52832-nimble/packages/NimBLE-latest/apps/blemesh_models_example_2/src/main.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
2  *
3  * Copyright (c) 2018 Vikrant More
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "bsp/bsp.h"
9 #include "console/console.h"
10 #include "hal/hal_gpio.h"
11 
12 #include "common.h"
13 #include "ble_mesh.h"
14 #include "device_composition.h"
15 #include "publisher.h"
16 #include "state_binding.h"
17 #include "transition.h"
18 
19 int button_device[] = {
20 	BUTTON_1,
21 	BUTTON_2,
22 	BUTTON_3,
23 	BUTTON_4,
24 };
25 
26 int led_device[] = {
27 	LED_1,
28 	LED_2,
29 	LED_3,
30 	LED_4,
31 };
32 
33 static struct ble_npl_callout button_work;
34 
button_pressed(struct os_event * ev)35 static void button_pressed(struct os_event *ev)
36 {
37 	k_work_submit(&button_work);
38 }
39 
40 static struct os_event button_event;
41 
42 static void
gpio_irq_handler(void * arg)43 gpio_irq_handler(void *arg)
44 {
45 	button_event.ev_arg = arg;
46 	os_eventq_put(os_eventq_dflt_get(), &button_event);
47 }
48 
gpio_init(void)49 static void gpio_init(void)
50 {
51 	/* LEDs configiuratin & setting */
52 
53 	hal_gpio_init_out(led_device[0], 1);
54 	hal_gpio_init_out(led_device[1], 1);
55 	hal_gpio_init_out(led_device[2], 1);
56 	hal_gpio_init_out(led_device[3], 1);
57 
58 	/* Buttons configiuratin & setting */
59 
60 	k_work_init(&button_work, publish);
61 
62 	button_event.ev_cb = button_pressed;
63 
64 	hal_gpio_irq_init(button_device[0], gpio_irq_handler, NULL,
65 			  HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_UP);
66 	hal_gpio_irq_enable(button_device[0]);
67 
68 	hal_gpio_irq_init(button_device[1], gpio_irq_handler, NULL,
69 			  HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_UP);
70 	hal_gpio_irq_enable(button_device[1]);
71 
72 	hal_gpio_irq_init(button_device[2], gpio_irq_handler, NULL,
73 			  HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_UP);
74 	hal_gpio_irq_enable(button_device[2]);
75 
76 	hal_gpio_irq_init(button_device[3], gpio_irq_handler, NULL,
77 			  HAL_GPIO_TRIG_FALLING, HAL_GPIO_PULL_UP);
78 	hal_gpio_irq_enable(button_device[3]);
79 }
80 
light_default_status_init(void)81 void light_default_status_init(void)
82 {
83 	/* Assume vaules are retrived from Persistence Storage (Start).
84 	 * These had saved by respective Setup Servers.
85 	 */
86 	gen_power_onoff_srv_user_data.onpowerup = STATE_DEFAULT;
87 
88 	light_lightness_srv_user_data.light_range_min = LIGHTNESS_MIN;
89 	light_lightness_srv_user_data.light_range_max = LIGHTNESS_MAX;
90 	light_lightness_srv_user_data.def = LIGHTNESS_MAX;
91 
92 	/* Following 2 values are as per specification */
93 	light_ctl_srv_user_data.temp_range_min = TEMP_MIN;
94 	light_ctl_srv_user_data.temp_range_max = TEMP_MAX;
95 
96 	light_ctl_srv_user_data.temp_def = TEMP_MIN;
97 	/* (End) */
98 
99 	/* Assume following values are retrived from Persistence
100 	 * Storage (Start).
101 	 * These values had saved before power down.
102 	 */
103 	light_lightness_srv_user_data.last = LIGHTNESS_MAX;
104 	light_ctl_srv_user_data.temp_last = TEMP_MIN;
105 	/* (End) */
106 
107 	light_ctl_srv_user_data.temp = light_ctl_srv_user_data.temp_def;
108 
109 	if (gen_power_onoff_srv_user_data.onpowerup == STATE_OFF) {
110 		gen_onoff_srv_root_user_data.onoff = STATE_OFF;
111 		state_binding(ONOFF, ONOFF_TEMP);
112 	} else if (gen_power_onoff_srv_user_data.onpowerup == STATE_DEFAULT) {
113 		gen_onoff_srv_root_user_data.onoff = STATE_ON;
114 		state_binding(ONOFF, ONOFF_TEMP);
115 	} else if (gen_power_onoff_srv_user_data.onpowerup == STATE_RESTORE) {
116 		/* Assume following values is retrived from Persistence
117 		 * Storage (Start).
118 		 * This value had saved before power down.
119 		 */
120 		gen_onoff_srv_root_user_data.onoff = STATE_ON;
121 		/* (End) */
122 
123 		light_ctl_srv_user_data.temp =
124 			light_ctl_srv_user_data.temp_last;
125 
126 		state_binding(ONPOWERUP, ONOFF_TEMP);
127 	}
128 }
129 
update_light_state(void)130 void update_light_state(void)
131 {
132 	u8_t power, color;
133 
134 	power = 100 * ((float) light_lightness_srv_user_data.actual / 65535);
135 	color = 100 * ((float) (gen_level_srv_s0_user_data.level + 32768)
136 		       / 65535);
137 
138 	printk("power-> %d, color-> %d\n", power, color);
139 
140 	if (gen_onoff_srv_root_user_data.onoff == STATE_ON) {
141 		/* LED1 On */
142 		hal_gpio_write(led_device[0], 0);
143 	} else {
144 		/* LED1 Off */
145 		hal_gpio_write(led_device[0], 1);
146 	}
147 
148 	if (power < 50) {
149 		/* LED3 On */
150 		hal_gpio_write(led_device[2], 0);
151 	} else {
152 		/* LED3 Off */
153 		hal_gpio_write(led_device[2], 1);
154 	}
155 
156 	if (color < 50) {
157 		/* LED4 On */
158 		hal_gpio_write(led_device[3], 0);
159 	} else {
160 		/* LED4 Off */
161 		hal_gpio_write(led_device[3], 1);
162 	}
163 }
164 
main(void)165 int main(void)
166 {
167 #ifdef ARCH_sim
168 	mcu_sim_parse_args(argc, argv);
169 #endif
170 
171 	/* Initialize OS */
172 	sysinit();
173 
174 	light_default_status_init();
175 
176 	transition_timers_init();
177 
178 	gpio_init();
179 
180 	update_light_state();
181 
182 	init_pub();
183 
184 	printk("Initializing...\n");
185 
186 	/* Initialize the NimBLE host configuration. */
187 	ble_hs_cfg.reset_cb = blemesh_on_reset;
188 	ble_hs_cfg.sync_cb = blemesh_on_sync;
189 	ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
190 
191 	randomize_publishers_TID();
192 
193 	while (1) {
194 		os_eventq_run(os_eventq_dflt_get());
195 	}
196 
197 	return 0;
198 }
199