Rime remote control: start with 1, set the LEDs to the exact value that is sent
[wsn-p.git] / exercise_remotecontrol / rime / led_remote_control.c
1 #include <contiki.h>
2 #include <stdio.h>
3 #include <dev/leds.h>
4 #include <dev/button-sensor.h>
5 #include <net/rime.h>
6
7 PROCESS(led_remote_control_rime, "binary LED counter via button over Rime");
8 AUTOSTART_PROCESSES(&led_remote_control_rime);
9
10 void recv(struct broadcast_conn *ptr, const rimeaddr_t * sender) {
11 char * val = (char *) packetbuf_dataptr();
12 printf("node %d: recv from %d: %d\n", rimeaddr_node_addr, *sender, *val);
13
14 if((*val) & 0x01) {
15 leds_on(LEDS_GREEN);
16 } else {
17 leds_off(LEDS_GREEN);
18 }
19 if((*val) & 0x02) {
20 leds_on(LEDS_YELLOW);
21 } else {
22 leds_off(LEDS_YELLOW);
23 }
24
25 printf("toggled LEDs\n");
26 }
27
28 static struct broadcast_conn bc;
29 static const struct broadcast_callbacks bccb = { recv };
30
31 PROCESS_THREAD(led_remote_control_rime, ev, data) {
32 PROCESS_EXITHANDLER(broadcast_close(&bc));
33
34 PROCESS_BEGIN();
35
36 leds_init();
37 leds_off(LEDS_ALL);
38
39 SENSORS_ACTIVATE(button_sensor);
40
41 broadcast_open(&bc, 129, &bccb);
42
43 static struct etimer timer;
44 static char i;
45 for(i = 1; 1; i++) {
46 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
47
48 // debounce for 250 ms
49 etimer_set(&timer, CLOCK_SECOND * 0.25);
50 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
51
52 packetbuf_copyfrom(&i, sizeof(i));
53 broadcast_send(&bc);
54 printf("node %d: broadcast sent: %d\n", rimeaddr_node_addr, i);
55 }
56
57 PROCESS_END();
58 }
This page took 0.05063 seconds and 5 git commands to generate.