move rgb resource to resource folder

This commit is contained in:
Harald Pichler 2016-11-03 11:27:42 +01:00
parent d2a650f514
commit 20820a11e0

View file

@ -50,7 +50,9 @@ ChainableLED leds(8, 9, NUM_LEDS);
uint8_t color_rgb [3] = {0, 0, 0}; uint8_t color_rgb [3] = {0, 0, 0};
static uint8_t name_to_offset (const char * name)
static uint8_t
name_to_offset (const char * name)
{ {
uint8_t offset = 0; uint8_t offset = 0;
if (0 == strcmp (name, "green")) { if (0 == strcmp (name, "green")) {
@ -67,13 +69,24 @@ color_to_string (const char *name, const char *uri, char *buf, size_t bsize)
return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]); return snprintf (buf, bsize, "%d", color_rgb [name_to_offset (name)]);
} }
int color_from_string (const char *name, const char *uri, const char *s) int
color_from_string (const char *name, const char *uri, const char *s)
{ {
color_rgb [name_to_offset (name)] = atoi (s); color_rgb [name_to_offset (name)] = atoi (s);
leds.setColorRGB(0,color_rgb [0], color_rgb [1], color_rgb [2]); leds.setColorRGB(0,color_rgb [0], color_rgb [1], color_rgb [2]);
return 0; return 0;
} }
int
color_rgb_from_string (const char *r, const char *g, const char *b)
{
color_rgb [0] = atoi (r);
color_rgb [1] = atoi (g);
color_rgb [2] = atoi (b);
leds.setColorRGB(0,color_rgb [0], color_rgb [1], color_rgb [2]);
return 0;
}
#pragma GCC diagnostic ignored "-Wwrite-strings" #pragma GCC diagnostic ignored "-Wwrite-strings"
GENERIC_RESOURCE GENERIC_RESOURCE
( red ( red
@ -103,76 +116,6 @@ GENERIC_RESOURCE
); );
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
static void res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
static void res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset);
/* A simple getter example. Returns the reading from the sensor with a simple etag */
RESOURCE(res_rgb,
"title=\"LED: , POST/PUT mode=on|off\";rt=\"Control\"",
res_get_handler,
res_post_put_handler,
res_post_put_handler,
NULL);
static void
res_get_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
unsigned int accept = -1;
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%d %d %d",color_rgb [0],color_rgb [1],color_rgb [2] );
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{'r':%d\n,'g':%d\n,'b':%d}",color_rgb [0],color_rgb [1],color_rgb [2]);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
const char *msg = "Supporting content-types text/plain and application/json";
REST.set_response_payload(response, msg, strlen(msg));
}
}
static void
res_post_put_handler(void *request, void *response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
size_t len = 0;
const char *valr = NULL;
const char *valg = NULL;
const char *valb = NULL;
int success = 1;
if(success && (len = REST.get_post_variable(request, "r", &valr))) {
if(len != 0) {
color_rgb [0] = atoi (valr);
} else {
success = 0;
}
if(success && (len = REST.get_post_variable(request, "g", &valg))) {
if(len != 0) {
color_rgb [1] = atoi (valg);
} else {
success = 0;
}
}
if(success && (len = REST.get_post_variable(request, "b", &valb))) {
if(len != 0) {
color_rgb [2] = atoi (valb);
} else {
success = 0;
}
}
} else {
success = 0;
} if(!success) {
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
}
void setup (void) void setup (void)
{ {
// switch off the led // switch off the led