From 681c514a18178232082495725aa71ac3f1e76510 Mon Sep 17 00:00:00 2001 From: Harald Pichler Date: Mon, 25 Jan 2016 20:41:26 +0100 Subject: [PATCH] add coap resourcen rgb --- examples/osd/arduino-ledstrip/Makefile | 2 + examples/osd/arduino-ledstrip/sketch.pde | 101 ++++++++++++++++------- 2 files changed, 72 insertions(+), 31 deletions(-) diff --git a/examples/osd/arduino-ledstrip/Makefile b/examples/osd/arduino-ledstrip/Makefile index c867fa55e..09e4a28a5 100644 --- a/examples/osd/arduino-ledstrip/Makefile +++ b/examples/osd/arduino-ledstrip/Makefile @@ -50,6 +50,7 @@ SMALL=1 APPS += er-coap APPS += rest-engine APPS += arduino +APPS += json json-resource include $(CONTIKI)/Makefile.include include $(CONTIKI)/apps/arduino/Makefile.include @@ -84,3 +85,4 @@ connect-router-cooja: $(CONTIKI)/tools/tunslip6 connect-minimal: sudo ip address add fdfd::1/64 dev tap0 + diff --git a/examples/osd/arduino-ledstrip/sketch.pde b/examples/osd/arduino-ledstrip/sketch.pde index f1653407c..761512e6b 100644 --- a/examples/osd/arduino-ledstrip/sketch.pde +++ b/examples/osd/arduino-ledstrip/sketch.pde @@ -17,17 +17,76 @@ RGBdriver Driver(CLK,DIO); extern "C" { +#include +#include +#include +#include +#include "contiki.h" +#include "contiki-net.h" #include "rest-engine.h" +#include "generic_resource.h" #include "net/netstack.h" extern volatile uint8_t mcusleepcycle; // default 16 -extern resource_t res_door, res_battery; -uint8_t door_pin = 3; -uint8_t door_status = 0; #define LED_PIN 4 } +uint8_t color_rgb [3] = {255, 255, 255}; + +static uint8_t name_to_offset (const char * name) +{ + uint8_t offset = 0; + if (0 == strcmp (name, "green")) { + offset = 1; + } else if (0 == strcmp (name, "blue")) { + offset = 2; + } + return offset; +} + +static size_t +color_to_string (const char *name, uint8_t is_json, char *buf, size_t bsize) +{ + char *fmt = "%d"; + if (is_json) { + fmt = "\"%d\""; + } + return snprintf (buf, bsize, fmt, color_rgb [name_to_offset (name)]); +} + +void color_from_string (const char *name, const char *s) +{ + color_rgb [name_to_offset (name)] = atoi (s); + Driver.begin(); + Driver.SetColor(color_rgb [0], color_rgb [1], color_rgb [2]); + Driver.end(); +} + +GENERIC_RESOURCE + ( red + , RED_LED + , s + , color_from_string + , color_to_string + ); + +GENERIC_RESOURCE + ( green + , GREEN_LED + , s + , color_from_string + , color_to_string + ); + +GENERIC_RESOURCE + ( blue + , BLUE_LED + , s + , color_from_string + , color_to_string + ); + void setup (void) { // switch off the led @@ -35,38 +94,18 @@ void setup (void) digitalWrite(LED_PIN, HIGH); // init coap resourcen rest_init_engine (); - rest_activate_resource (&res_door, "s/door"); - rest_activate_resource (&res_battery, "s/battery"); NETSTACK_MAC.off(1); + + rest_activate_resource (&res_red, "led/R"); + rest_activate_resource (&res_green, "led/G"); + rest_activate_resource (&res_blue, "led/B"); + + Driver.begin(); + Driver.SetColor(color_rgb [0], color_rgb [1], color_rgb [2]); + Driver.end(); } void loop (void) { - static int a=1; - - mcusleepcycle=0; - - switch(a) { - case 1: printf("a ist eins\n"); - Driver.begin(); // begin - Driver.SetColor(255, 0, 0); //Red. first node data - Driver.end(); - a++; - break; - case 2: printf("a ist zwei\n"); - Driver.begin(); // begin - Driver.SetColor(0, 255, 0); //Green. first node data - Driver.end(); - a++; - break; - case 3: printf("a ist drei\n"); - Driver.begin(); // begin - Driver.SetColor(0, 0, 255);//Blue. first node data - Driver.end(); - a=1; - break; - default: printf("a ist irgendwas\n"); break; - } - mcusleepcycle=16; }