From b37061f49fc298e35076fc8dc032c5b2c8b08eec Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Sun, 30 Oct 2011 02:37:29 +0100 Subject: [PATCH] remote control exercise: binary LED counter via button over Rime network --- exercise_remotecontrol/rime/Makefile | 7 +++ .../rime/led_remote_control_rime.c | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 exercise_remotecontrol/rime/Makefile create mode 100644 exercise_remotecontrol/rime/led_remote_control_rime.c diff --git a/exercise_remotecontrol/rime/Makefile b/exercise_remotecontrol/rime/Makefile new file mode 100644 index 0000000..c568329 --- /dev/null +++ b/exercise_remotecontrol/rime/Makefile @@ -0,0 +1,7 @@ +include ../../Makefile.properties +CONTIKI_PROJECT=led_remote_control_rime + +all: $(CONTIKI_PROJECT) + +include $(CONTIKI)/Makefile.include + diff --git a/exercise_remotecontrol/rime/led_remote_control_rime.c b/exercise_remotecontrol/rime/led_remote_control_rime.c new file mode 100644 index 0000000..b3b9899 --- /dev/null +++ b/exercise_remotecontrol/rime/led_remote_control_rime.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +PROCESS(led_remote_control_rime, "binary LED counter via button over Rime"); +AUTOSTART_PROCESSES(&led_remote_control_rime); + +void recv(struct broadcast_conn *ptr, const rimeaddr_t * sender) { + char * val = (char *) packetbuf_dataptr(); + printf("node %d: recv from %d: %d\n", rimeaddr_node_addr, *sender, *val); + // FIXME + if(*val % 2) { + leds_toggle(LEDS_GREEN); + } + leds_toggle(LEDS_YELLOW); + printf("toggled LEDs\n"); +} + +static struct broadcast_conn bc; +static const struct broadcast_callbacks bccb = { recv }; + +PROCESS_THREAD(led_remote_control_rime, ev, data) { + PROCESS_EXITHANDLER(broadcast_close(&bc)); + + PROCESS_BEGIN(); + + leds_init(); + leds_off(LEDS_ALL); + + SENSORS_ACTIVATE(button_sensor); + + broadcast_open(&bc, 129, &bccb); + + static struct etimer timer; + static char i; + for(i = 0; 1; i++) { + PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); + + // debounce for 250 ms + etimer_set(&timer, CLOCK_SECOND * 0.25); + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer)); + + packetbuf_copyfrom(&i, sizeof(i)); + broadcast_send(&bc); + printf("node %d: broadcast sent: %d\n", rimeaddr_node_addr, i); + } + + PROCESS_END(); +} -- 2.20.1