exercise: LED remote control via UDP
authorRoland Hieber <rohieb@rohieb.name>
Wed, 16 Nov 2011 22:52:28 +0000 (23:52 +0100)
committerRoland Hieber <rohieb@rohieb.name>
Wed, 16 Nov 2011 22:52:28 +0000 (23:52 +0100)
exercise_remotecontrol/simple_udp/Makefile [new file with mode: 0644]
exercise_remotecontrol/simple_udp/led_remote_control.c [new file with mode: 0644]

diff --git a/exercise_remotecontrol/simple_udp/Makefile b/exercise_remotecontrol/simple_udp/Makefile
new file mode 100644 (file)
index 0000000..7d24ada
--- /dev/null
@@ -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 (file)
index 0000000..985586c
--- /dev/null
@@ -0,0 +1,78 @@
+#include <contiki.h>
+#include <contiki-lib.h>
+#include <contiki-net.h>
+#include <stdio.h>
+#include <dev/leds.h>
+#include <dev/button-sensor.h>
+#include <string.h>
+
+////////////////// 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();
+}
This page took 0.033118 seconds and 4 git commands to generate.