-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsky_broadcast_button.c
65 lines (48 loc) · 2.33 KB
/
sky_broadcast_button.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "dev/light-sensor.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include "sys/node-id.h"
#include <stdio.h>
PROCESS(broadcast_button_process, "Broadcast Button");
AUTOSTART_PROCESSES(&broadcast_button_process);
int button_pressed_counter=0;
int messages_received_counter=0;
static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from)
{
messages_received_counter++;
//printing the event message of the sender node and the total number of messages received
printf("Broadcast message received from %d.%d: '%s'\n", from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
printf("Button pressed on node %d.%d - %d messages received in total\n", from->u8[0], from->u8[1], messages_received_counter);
//changing the modes of the green and blue LEDs
leds_toggle(LEDS_GREEN);
leds_toggle(LEDS_BLUE);
}
static const struct broadcast_callbacks broadcast_call = {broadcast_recv};
static struct broadcast_conn broadcast;
PROCESS_THREAD(broadcast_button_process, ev, data)
{
static struct etimer timer;
PROCESS_EXITHANDLER(broadcast_close(&broadcast);)
PROCESS_BEGIN();
SENSORS_ACTIVATE(button_sensor);
SENSORS_ACTIVATE(light_sensor);
broadcast_open(&broadcast, 129, &broadcast_call);
while(1) //always waiting for a button click
{
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
button_pressed_counter++;
printf("Node %d: my button was pressed - %d times in total\n", node_id, button_pressed_counter);
leds_on(LEDS_RED); //switching on the red LED after the sensor's button is pressed
//waiting for 5 seconds
etimer_set(&timer, CLOCK_SECOND * 5);
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
leds_off(LEDS_RED); //switching off the red LED after 5 seconds
//broadcasting the event message to the neighbors
packetbuf_copyfrom("button pressed", 16);
broadcast_send(&broadcast);
}
PROCESS_END();
}