TrailTraffic
Dependencies: Chainable_RGB_LED mbed mbedConnectorInterface mbedEndpointNetwork
Fork of IoT_LED_demo by
mbedEndpointResources/OnBoardLED.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 __LED_RESOURCE_H__ |
michaeljkoster | 0:a7458d25d9c9 | 24 | #define __LED_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 | DigitalOut LED_red(LED1); |
michaeljkoster | 0:a7458d25d9c9 | 32 | DigitalOut LED_green(LED2); |
michaeljkoster | 0:a7458d25d9c9 | 33 | DigitalOut LED_blue(LED3); |
michaeljkoster | 0:a7458d25d9c9 | 34 | |
michaeljkoster | 0:a7458d25d9c9 | 35 | static char * LED_color_value = {"0000000"}; //RRGGBBII |
michaeljkoster | 0:a7458d25d9c9 | 36 | |
michaeljkoster | 0:a7458d25d9c9 | 37 | void LED_set_color(char * color_string) |
michaeljkoster | 0:a7458d25d9c9 | 38 | { |
michaeljkoster | 0:a7458d25d9c9 | 39 | static uint8_t red, green, blue, index; |
michaeljkoster | 0:a7458d25d9c9 | 40 | int color_int; |
michaeljkoster | 0:a7458d25d9c9 | 41 | |
michaeljkoster | 0:a7458d25d9c9 | 42 | sscanf(color_string, "%X", &color_int); |
michaeljkoster | 0:a7458d25d9c9 | 43 | |
michaeljkoster | 0:a7458d25d9c9 | 44 | index = color_int & 255; |
michaeljkoster | 0:a7458d25d9c9 | 45 | blue = color_int >> 8 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 46 | green = color_int >> 16 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 47 | red = color_int >> 24 & 255; |
michaeljkoster | 0:a7458d25d9c9 | 48 | |
michaeljkoster | 0:a7458d25d9c9 | 49 | if(index == 0) { |
michaeljkoster | 0:a7458d25d9c9 | 50 | LED_red = !(red & 1); |
michaeljkoster | 0:a7458d25d9c9 | 51 | LED_green = !(green & 1); |
michaeljkoster | 0:a7458d25d9c9 | 52 | LED_blue = !(blue & 1); |
michaeljkoster | 0:a7458d25d9c9 | 53 | } |
michaeljkoster | 0:a7458d25d9c9 | 54 | } |
michaeljkoster | 0:a7458d25d9c9 | 55 | |
michaeljkoster | 0:a7458d25d9c9 | 56 | /** LightResource class |
michaeljkoster | 0:a7458d25d9c9 | 57 | */ |
michaeljkoster | 0:a7458d25d9c9 | 58 | class LEDResource : public DynamicResource |
michaeljkoster | 0:a7458d25d9c9 | 59 | { |
michaeljkoster | 0:a7458d25d9c9 | 60 | |
michaeljkoster | 0:a7458d25d9c9 | 61 | public: |
michaeljkoster | 0:a7458d25d9c9 | 62 | /** |
michaeljkoster | 0:a7458d25d9c9 | 63 | Default constructor |
michaeljkoster | 0:a7458d25d9c9 | 64 | @param logger input logger instance for this resource |
michaeljkoster | 0:a7458d25d9c9 | 65 | @param name input the Light resource name |
michaeljkoster | 0:a7458d25d9c9 | 66 | @param observable input the resource is Observable (default: FALSE) |
michaeljkoster | 0:a7458d25d9c9 | 67 | */ |
michaeljkoster | 0:a7458d25d9c9 | 68 | LEDResource(const Logger *logger,const char *name,const bool observable = false) : DynamicResource(logger,name,"OnBoardLED",SN_GRS_GET_ALLOWED|SN_GRS_PUT_ALLOWED,observable) { |
michaeljkoster | 0:a7458d25d9c9 | 69 | LED_set_color("00000000"); |
michaeljkoster | 0:a7458d25d9c9 | 70 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 71 | LED_set_color("FF000000"); |
michaeljkoster | 0:a7458d25d9c9 | 72 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 73 | LED_set_color("00FF0000"); |
michaeljkoster | 0:a7458d25d9c9 | 74 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 75 | LED_set_color("0000FF00"); |
michaeljkoster | 0:a7458d25d9c9 | 76 | wait(0.5); |
michaeljkoster | 0:a7458d25d9c9 | 77 | LED_set_color(LED_color_value); |
michaeljkoster | 0:a7458d25d9c9 | 78 | } |
michaeljkoster | 0:a7458d25d9c9 | 79 | |
michaeljkoster | 0:a7458d25d9c9 | 80 | /** |
michaeljkoster | 0:a7458d25d9c9 | 81 | Get the value of the LED |
michaeljkoster | 0:a7458d25d9c9 | 82 | @returns string containing the last setting |
michaeljkoster | 0:a7458d25d9c9 | 83 | */ |
michaeljkoster | 0:a7458d25d9c9 | 84 | virtual string get() { |
michaeljkoster | 0:a7458d25d9c9 | 85 | return(LED_color_value); |
michaeljkoster | 0:a7458d25d9c9 | 86 | } |
michaeljkoster | 0:a7458d25d9c9 | 87 | |
michaeljkoster | 0:a7458d25d9c9 | 88 | /** |
michaeljkoster | 0:a7458d25d9c9 | 89 | Set the value of the LED |
michaeljkoster | 0:a7458d25d9c9 | 90 | @param string input the string containing the desired setting |
michaeljkoster | 0:a7458d25d9c9 | 91 | */ |
michaeljkoster | 0:a7458d25d9c9 | 92 | virtual void put(const string value) { |
michaeljkoster | 0:a7458d25d9c9 | 93 | if( sizeof(value) == sizeof(LED_color_value) ){ |
michaeljkoster | 0:a7458d25d9c9 | 94 | LED_color_value = (char *)value.c_str(); |
michaeljkoster | 0:a7458d25d9c9 | 95 | LED_set_color(LED_color_value); |
michaeljkoster | 0:a7458d25d9c9 | 96 | } |
michaeljkoster | 0:a7458d25d9c9 | 97 | } |
michaeljkoster | 0:a7458d25d9c9 | 98 | }; |
michaeljkoster | 0:a7458d25d9c9 | 99 | |
michaeljkoster | 0:a7458d25d9c9 | 100 | #endif // __LED_RESOURCE_H__ |