TrailTraffic
Dependencies: Chainable_RGB_LED mbed mbedConnectorInterface mbedEndpointNetwork
Fork of IoT_LED_demo by
mbedEndpointResources/LightResource.h@1:b96a11b680dc, 2015-05-22 (annotated)
- Committer:
- cjwu15
- Date:
- Fri May 22 00:39:20 2015 +0000
- Revision:
- 1:b96a11b680dc
- Parent:
- 0:a7458d25d9c9
Share;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:a7458d25d9c9 | 1 | /** |
michaeljkoster | 0:a7458d25d9c9 | 2 | * @file LightResource.h |
michaeljkoster | 0:a7458d25d9c9 | 3 | * @brief mbed CoAP Endpoint Light resource supporting CoAP GET and PUT |
michaeljkoster | 0:a7458d25d9c9 | 4 | * @author Doug Anson, Michael Koster |
michaeljkoster | 0:a7458d25d9c9 | 5 | * @version 1.0 |
michaeljkoster | 0:a7458d25d9c9 | 6 | * @see |
michaeljkoster | 0:a7458d25d9c9 | 7 | * |
michaeljkoster | 0:a7458d25d9c9 | 8 | * Copyright (c) 2014 |
michaeljkoster | 0:a7458d25d9c9 | 9 | * |
michaeljkoster | 0:a7458d25d9c9 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michaeljkoster | 0:a7458d25d9c9 | 11 | * you may not use this file except in compliance with the License. |
michaeljkoster | 0:a7458d25d9c9 | 12 | * You may obtain a copy of the License at |
michaeljkoster | 0:a7458d25d9c9 | 13 | * |
michaeljkoster | 0:a7458d25d9c9 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
michaeljkoster | 0:a7458d25d9c9 | 15 | * |
michaeljkoster | 0:a7458d25d9c9 | 16 | * Unless required by applicable law or agreed to in writing, software |
michaeljkoster | 0:a7458d25d9c9 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
michaeljkoster | 0:a7458d25d9c9 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michaeljkoster | 0:a7458d25d9c9 | 19 | * See the License for the specific language governing permissions and |
michaeljkoster | 0:a7458d25d9c9 | 20 | * limitations under the License. |
michaeljkoster | 0:a7458d25d9c9 | 21 | */ |
michaeljkoster | 0:a7458d25d9c9 | 22 | |
michaeljkoster | 0:a7458d25d9c9 | 23 | #ifndef __LIGHT_RESOURCE_H__ |
michaeljkoster | 0:a7458d25d9c9 | 24 | #define __LIGHT_RESOURCE_H__ |
michaeljkoster | 0:a7458d25d9c9 | 25 | |
michaeljkoster | 0:a7458d25d9c9 | 26 | |
michaeljkoster | 0:a7458d25d9c9 | 27 | // Base class |
michaeljkoster | 0:a7458d25d9c9 | 28 | #include "DynamicResource.h" |
michaeljkoster | 0:a7458d25d9c9 | 29 | |
michaeljkoster | 0:a7458d25d9c9 | 30 | // our Light |
michaeljkoster | 0:a7458d25d9c9 | 31 | #include "ChainableLED.h" |
michaeljkoster | 0:a7458d25d9c9 | 32 | #define LED_COUNT 1 |
michaeljkoster | 0:a7458d25d9c9 | 33 | |
michaeljkoster | 0:a7458d25d9c9 | 34 | ChainableLED led_chain(D4, D5, LED_COUNT); |
michaeljkoster | 0:a7458d25d9c9 | 35 | |
michaeljkoster | 0:a7458d25d9c9 | 36 | static char * led_value = {"0000000"}; //RRGGBBII |
michaeljkoster | 0:a7458d25d9c9 | 37 | |
michaeljkoster | 0:a7458d25d9c9 | 38 | void set_led_array(char * color_string) |
michaeljkoster | 0:a7458d25d9c9 | 39 | { |
michaeljkoster | 0:a7458d25d9c9 | 40 | static uint8_t red, green, blue, index; |
michaeljkoster | 0:a7458d25d9c9 | 41 | int color_int; |
michaeljkoster | 0:a7458d25d9c9 | 42 | |
michaeljkoster | 0:a7458d25d9c9 | 43 | sscanf(color_string, "%X", &color_int); |
michaeljkoster | 0:a7458d25d9c9 | 44 | |
michaeljkoster | 0:a7458d25d9c9 | 45 | index = color_int & 255; |
michaeljkoster | 0:a7458d25d9c9 | 46 | blue = color_int >> 8 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 47 | green = color_int >> 16 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 48 | red = color_int >> 24 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 49 | |
michaeljkoster | 0:a7458d25d9c9 | 50 | if(index > 0 and index <= LED_COUNT) { |
michaeljkoster | 0:a7458d25d9c9 | 51 | led_chain.setColorRGB(index-1, red, green, blue); |
michaeljkoster | 0:a7458d25d9c9 | 52 | } |
michaeljkoster | 0:a7458d25d9c9 | 53 | else if(index == 0){ |
michaeljkoster | 0:a7458d25d9c9 | 54 | for(int i = 0; i < LED_COUNT; i++){ |
michaeljkoster | 0:a7458d25d9c9 | 55 | led_chain.setColorRGB(i, red, green, blue); |
michaeljkoster | 0:a7458d25d9c9 | 56 | } |
michaeljkoster | 0:a7458d25d9c9 | 57 | } |
michaeljkoster | 0:a7458d25d9c9 | 58 | } |
michaeljkoster | 0:a7458d25d9c9 | 59 | |
michaeljkoster | 0:a7458d25d9c9 | 60 | /** LightResource class |
michaeljkoster | 0:a7458d25d9c9 | 61 | */ |
michaeljkoster | 0:a7458d25d9c9 | 62 | class LightResource : public DynamicResource |
michaeljkoster | 0:a7458d25d9c9 | 63 | { |
michaeljkoster | 0:a7458d25d9c9 | 64 | |
michaeljkoster | 0:a7458d25d9c9 | 65 | public: |
michaeljkoster | 0:a7458d25d9c9 | 66 | /** |
michaeljkoster | 0:a7458d25d9c9 | 67 | Default constructor |
michaeljkoster | 0:a7458d25d9c9 | 68 | @param logger input logger instance for this resource |
michaeljkoster | 0:a7458d25d9c9 | 69 | @param name input the Light resource name |
michaeljkoster | 0:a7458d25d9c9 | 70 | @param observable input the resource is Observable (default: FALSE) |
michaeljkoster | 0:a7458d25d9c9 | 71 | */ |
michaeljkoster | 0:a7458d25d9c9 | 72 | LightResource(const Logger *logger,const char *name,const bool observable = false) : DynamicResource(logger,name,"Light",SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED,observable) { |
michaeljkoster | 0:a7458d25d9c9 | 73 | set_led_array("00000000"); |
michaeljkoster | 0:a7458d25d9c9 | 74 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 75 | set_led_array("FF000000"); |
michaeljkoster | 0:a7458d25d9c9 | 76 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 77 | set_led_array("00FF0000"); |
michaeljkoster | 0:a7458d25d9c9 | 78 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 79 | set_led_array("0000FF00"); |
michaeljkoster | 0:a7458d25d9c9 | 80 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 81 | set_led_array(led_value); |
michaeljkoster | 0:a7458d25d9c9 | 82 | } |
michaeljkoster | 0:a7458d25d9c9 | 83 | |
michaeljkoster | 0:a7458d25d9c9 | 84 | /** |
michaeljkoster | 0:a7458d25d9c9 | 85 | Get the value of the Light |
michaeljkoster | 0:a7458d25d9c9 | 86 | @returns string containing either "0" (light off) or "1" (light on) |
michaeljkoster | 0:a7458d25d9c9 | 87 | */ |
michaeljkoster | 0:a7458d25d9c9 | 88 | virtual string get() { |
michaeljkoster | 0:a7458d25d9c9 | 89 | return(led_value); |
michaeljkoster | 0:a7458d25d9c9 | 90 | } |
michaeljkoster | 0:a7458d25d9c9 | 91 | |
michaeljkoster | 0:a7458d25d9c9 | 92 | /** |
michaeljkoster | 0:a7458d25d9c9 | 93 | Set the value of the Light |
michaeljkoster | 0:a7458d25d9c9 | 94 | @param string input the string containing "0" (light off) or "1" (light on) |
michaeljkoster | 0:a7458d25d9c9 | 95 | */ |
michaeljkoster | 0:a7458d25d9c9 | 96 | virtual void put(const string value) { |
michaeljkoster | 0:a7458d25d9c9 | 97 | if( sizeof(value) == sizeof(led_value) ){ |
michaeljkoster | 0:a7458d25d9c9 | 98 | led_value = (char *)value.c_str(); |
michaeljkoster | 0:a7458d25d9c9 | 99 | set_led_array(led_value); |
michaeljkoster | 0:a7458d25d9c9 | 100 | } |
michaeljkoster | 0:a7458d25d9c9 | 101 | } |
michaeljkoster | 0:a7458d25d9c9 | 102 | }; |
michaeljkoster | 0:a7458d25d9c9 | 103 | |
michaeljkoster | 0:a7458d25d9c9 | 104 | #endif // __LIGHT_RESOURCE_H__ |