Demo project for IoT World Hackathon

Dependencies:   Chainable_RGB_LED mbed mbedConnectorInterface mbedEndpointNetwork

mbedEndpointResources/OnBoardLED.h

Committer:
michaeljkoster
Date:
2015-05-12
Revision:
0:a9025db1ac76

File content as of revision 0:a9025db1ac76:

/**
 * @file    LightResource.h
 * @brief   mbed CoAP Endpoint Light resource supporting CoAP GET and PUT
 * @author  Doug Anson, Michael Koster
 * @version 1.0
 * @see
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __LED_RESOURCE_H__
#define __LED_RESOURCE_H__


// Base class
#include "DynamicResource.h"

// our Light
DigitalOut LED_red(LED1);
DigitalOut LED_green(LED2);
DigitalOut LED_blue(LED3);

static char * LED_color_value = {"00000"}; //RRGGBBII

void LED_set_color(char * color_string)
{
    static uint8_t red, green, blue;
    int color_int;

    sscanf(color_string, "%X", &color_int);
    
    blue = color_int & 255;
    green = color_int >> 8 & 255;
    red = color_int >> 16 & 255;
        
    LED_red = !(red & 1);    
    LED_green = !(green & 1);    
    LED_blue = !(blue & 1);    
}

/** LightResource class
 */
class LEDResource : public DynamicResource
{

public:
    /**
    Default constructor
    @param logger input logger instance for this resource
    @param name input the Light resource name
    @param observable input the resource is Observable (default: FALSE)
    */
    LEDResource(const Logger *logger,const char *name,const bool observable = false) : DynamicResource(logger,name,"OnBoardLED",SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED,observable) {
    LED_set_color("000000");
    wait(0.5);
    LED_set_color("FF0000");
    wait(0.5);
    LED_set_color("00FF00");
    wait(0.5);
    LED_set_color("0000FF");
    wait(0.5);
    LED_set_color(LED_color_value);
    }

    /**
    Get the value of the LED
    @returns string containing the last setting
    */
    virtual string get() {
        return(LED_color_value);
    }

    /**
    Set the value of the LED
    @param string input the string containing the desired setting
    */
    virtual void put(const string value) {
        if( sizeof(value) == sizeof(LED_color_value) ){
            LED_color_value = (char *)value.c_str();
            LED_set_color(LED_color_value);
        }
    }
};

#endif // __LED_RESOURCE_H__