osd-contiki/examples/osd/arduino-ledstrip/sketch.pde

139 lines
2.8 KiB
Plaintext
Raw Normal View History

2016-01-22 15:41:55 +01:00
/*
* Sample arduino sketch using contiki features.
* We turn the LED off
* We allow read the moisture sensor
* Unfortunately sleeping for long times in loop() isn't currently
* possible, something turns off the CPU (including PWM outputs) if a
* Proto-Thread is taking too long. We need to find out how to sleep in
* a Contiki-compatible way.
* Note that for a normal arduino sketch you won't have to include any
* of the contiki-specific files here, the sketch should just work.
*/
2016-01-23 17:49:51 +01:00
#include "RGBdriver.h"
2016-01-24 16:04:37 +01:00
#define CLK 3//pins definitions for the driver
#define DIO 14
2016-01-23 17:49:51 +01:00
RGBdriver Driver(CLK,DIO);
2016-01-22 15:41:55 +01:00
extern "C" {
2016-01-25 20:41:26 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "contiki.h"
#include "contiki-net.h"
2016-01-22 15:41:55 +01:00
#include "rest-engine.h"
2016-01-25 20:41:26 +01:00
#include "generic_resource.h"
2016-01-23 17:49:51 +01:00
#include "net/netstack.h"
2016-01-22 15:41:55 +01:00
2016-01-23 17:49:51 +01:00
extern volatile uint8_t mcusleepcycle; // default 16
2016-01-22 15:41:55 +01:00
#define LED_PIN 4
}
2016-01-28 17:08:56 +01:00
uint8_t color_rgb [3] = {0, 0, 0};
2016-01-25 20:41:26 +01:00
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, const char *uri, char *buf, size_t bsize)
2016-01-25 20:41:26 +01:00
{
return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]);
2016-01-25 20:41:26 +01:00
}
int color_from_string (const char *name, const char *uri, const char *s)
2016-01-25 20:41:26 +01:00
{
color_rgb [name_to_offset (name)] = atoi (s);
Driver.begin();
Driver.SetColor(color_rgb [0], color_rgb [1], color_rgb [2]);
Driver.end();
return 0;
2016-01-25 20:41:26 +01:00
}
GENERIC_RESOURCE
( red
, RED_LED
, s
, 1
2016-01-25 20:41:26 +01:00
, color_from_string
, color_to_string
);
GENERIC_RESOURCE
( green
, GREEN_LED
, s
, 1
2016-01-25 20:41:26 +01:00
, color_from_string
, color_to_string
);
GENERIC_RESOURCE
( blue
, BLUE_LED
, s
, 1
2016-01-25 20:41:26 +01:00
, color_from_string
, color_to_string
);
2016-01-22 15:41:55 +01:00
void setup (void)
{
// switch off the led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// init coap resourcen
rest_init_engine ();
2016-01-23 17:49:51 +01:00
NETSTACK_MAC.off(1);
2016-01-25 20:41:26 +01:00
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();
2016-01-22 15:41:55 +01:00
}
void loop (void)
{
2016-01-28 17:08:56 +01:00
/* // Test
static int a=1;
switch(a) {
case 1: printf("red\n");
Driver.begin();
Driver.SetColor(255, 0, 0);
Driver.end();
a++;
break;
case 2: printf("green\n");
Driver.begin();
Driver.SetColor(0, 255, 0);
Driver.end();
a++;
break;
case 3: printf("blue\n");
Driver.begin();
Driver.SetColor(0, 0, 255);
Driver.end();
a=1;
break;
default: printf("a ist irgendwas\n"); break;
}
*/
2016-02-23 20:31:10 +01:00
}