From de0c58813a2c42b2b59715dc18bc164bd68eaa28 Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Wed, 16 Nov 2011 23:52:28 +0100 Subject: [PATCH] exercise: LED remote control via UDP --- exercise_remotecontrol/simple_udp/Makefile | 11 +++ .../simple_udp/led_remote_control.c | 78 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 exercise_remotecontrol/simple_udp/Makefile create mode 100644 exercise_remotecontrol/simple_udp/led_remote_control.c diff --git a/exercise_remotecontrol/simple_udp/Makefile b/exercise_remotecontrol/simple_udp/Makefile new file mode 100644 index 0000000..7d24ada --- /dev/null +++ b/exercise_remotecontrol/simple_udp/Makefile @@ -0,0 +1,11 @@ +include ../../Makefile.properties +CONTIKI_PROJECT=led_remote_control +CFLAGS += -DWITH_UIP=1 +UIP_CONF_IPV6=1 + +all: $(CONTIKI_PROJECT) + +upload: $(CONTIKI_PROJECT).upload + +include $(CONTIKI)/Makefile.include + diff --git a/exercise_remotecontrol/simple_udp/led_remote_control.c b/exercise_remotecontrol/simple_udp/led_remote_control.c new file mode 100644 index 0000000..985586c --- /dev/null +++ b/exercise_remotecontrol/simple_udp/led_remote_control.c @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include +#include + +////////////////// DEBUGGING STUFF +#define DEBUG DEBUG_PRINT +#include "net/uip-debug.h" +////////////////////////////////// + +PROCESS(led_remote_control, "binary LED counter via button over UDP"); +AUTOSTART_PROCESSES(&led_remote_control); + +static uip_ipaddr_t ipaddr; +static uint8_t seq_id; +static struct simple_udp_connection udp_conn; +#define UDP_PORT 1337 + +static void recv(struct simple_udp_connection *c, const uip_ipaddr_t *source_addr, + uint16_t source_port, const uip_ipaddr_t *dest_addr, uint16_t dest_port, + const uint8_t *data, uint16_t datalen) { + + PRINTF("recv from "); + PRINT6ADDR(source_addr); + PRINTF(" port %d: %d\n", source_port, *data); + + if((*data) & 0x01) { + leds_on(LEDS_GREEN); + } else { + leds_off(LEDS_GREEN); + } + if((*data) & 0x02) { + leds_on(LEDS_YELLOW); + } else { + leds_off(LEDS_YELLOW); + } + PRINTF("toggled LEDs\n"); +} + + +PROCESS_THREAD(led_remote_control, ev, data) { + PROCESS_BEGIN(); + + uip_create_linklocal_allnodes_mcast(&ipaddr); + if(!simple_udp_register(&udp_conn, uip_htons(UDP_PORT), NULL, + uip_htons(UDP_PORT), recv)) { + printf("o_O could not allocate simple UDP connection!\n"); + } + + leds_init(); + leds_off(LEDS_ALL); + + SENSORS_ACTIVATE(button_sensor); + + static struct etimer timer; + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); + + if(ev == sensors_event) { + // debounce for 250 ms + etimer_set(&timer, CLOCK_SECOND * 0.25); + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); + + seq_id++; // start with 1 for better effect + + PRINTF("Sending message to "); + PRINT6ADDR(&ipaddr); + PRINTF(": %d\n", seq_id); + + simple_udp_sendto(&udp_conn, &seq_id, 1, &ipaddr); + } + } + + PROCESS_END(); +} -- 2.20.1