old test code: uIP with IPv4, uIP unicast
[wsn-p.git] / exercise_remotecontrol / simple_udp / led_remote_control.c
1 #include <contiki.h>
2 #include <contiki-lib.h>
3 #include <contiki-net.h>
4 #include <stdio.h>
5 #include <dev/leds.h>
6 #include <dev/button-sensor.h>
7 #include <string.h>
8
9 ////////////////// DEBUGGING STUFF
10 #define DEBUG DEBUG_PRINT
11 #include "net/uip-debug.h"
12 //////////////////////////////////
13
14 PROCESS(led_remote_control, "binary LED counter via button over UDP");
15 AUTOSTART_PROCESSES(&led_remote_control);
16
17 static uip_ipaddr_t ipaddr;
18 static uint8_t seq_id;
19 static struct simple_udp_connection udp_conn;
20 #define UDP_PORT 1337
21
22 static void recv(struct simple_udp_connection *c, const uip_ipaddr_t *source_addr,
23 uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port,
24 const uint8_t *data, uint16_t datalen) {
25
26 PRINTF("recv from ");
27 PRINT6ADDR(source_addr);
28 PRINTF(" port %d: %d\n", source_port, *data);
29
30 if((*data) & 0x01) {
31 leds_on(LEDS_GREEN);
32 } else {
33 leds_off(LEDS_GREEN);
34 }
35 if((*data) & 0x02) {
36 leds_on(LEDS_YELLOW);
37 } else {
38 leds_off(LEDS_YELLOW);
39 }
40 PRINTF("toggled LEDs\n");
41 }
42
43
44 PROCESS_THREAD(led_remote_control, ev, data) {
45 PROCESS_BEGIN();
46
47 uip_create_linklocal_allnodes_mcast(&ipaddr);
48 if(!simple_udp_register(&udp_conn, uip_htons(UDP_PORT), NULL,
49 uip_htons(UDP_PORT), recv)) {
50 printf("o_O could not allocate simple UDP connection!\n");
51 }
52
53 leds_init();
54 leds_off(LEDS_ALL);
55
56 SENSORS_ACTIVATE(button_sensor);
57
58 static struct etimer timer;
59 while(1) {
60 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor);
61
62 if(ev == sensors_event) {
63 // debounce for 250 ms
64 etimer_set(&timer, CLOCK_SECOND * 0.25);
65 PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
66
67 seq_id++; // start with 1 for better effect
68
69 PRINTF("Sending message to ");
70 PRINT6ADDR(&ipaddr);
71 PRINTF(": %d\n", seq_id);
72
73 simple_udp_sendto(&udp_conn, &seq_id, 1, &ipaddr);
74 }
75 }
76
77 PROCESS_END();
78 }
This page took 0.123273 seconds and 5 git commands to generate.