Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Committer:
ansond
Date:
Sun Mar 16 04:35:20 2014 +0000
Revision:
105:759dee018363
Parent:
101:8747b6612e32
Child:
115:4e13cc450568
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:4c9bfcb3e759 1 /* Copyright C2013 Doug Anson, MIT License
ansond 0:4c9bfcb3e759 2 *
ansond 0:4c9bfcb3e759 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:4c9bfcb3e759 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:4c9bfcb3e759 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:4c9bfcb3e759 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:4c9bfcb3e759 7 * furnished to do so, subject to the following conditions:
ansond 0:4c9bfcb3e759 8 *
ansond 0:4c9bfcb3e759 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:4c9bfcb3e759 10 * substantial portions of the Software.
ansond 0:4c9bfcb3e759 11 *
ansond 0:4c9bfcb3e759 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:4c9bfcb3e759 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:4c9bfcb3e759 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:4c9bfcb3e759 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:4c9bfcb3e759 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:4c9bfcb3e759 17 */
ansond 0:4c9bfcb3e759 18
ansond 0:4c9bfcb3e759 19 #include "Light.h"
ansond 1:5d332fa199ce 20 #include "MBEDEndpoint.h"
ansond 0:4c9bfcb3e759 21
ansond 9:90fadae5489a 22 // include the relevant action support
ansond 9:90fadae5489a 23 #include "LightDimmerAction.h"
ansond 9:90fadae5489a 24 #include "LightSwitchAction.h"
ansond 9:90fadae5489a 25
ansond 91:8732d54328ae 26 void *_instance = NULL;
ansond 91:8732d54328ae 27
ansond 91:8732d54328ae 28 // Blinking Looper
ansond 91:8732d54328ae 29 static void blinking_action(void const *args) {
ansond 91:8732d54328ae 30 if (_instance != NULL) {
ansond 91:8732d54328ae 31 Light *self = (Light *)_instance;
ansond 101:8747b6612e32 32 self->updateDirection();
ansond 91:8732d54328ae 33 while(true) self->blinkLight();
ansond 91:8732d54328ae 34 }
ansond 91:8732d54328ae 35 }
ansond 91:8732d54328ae 36
ansond 0:4c9bfcb3e759 37 // default constructor
ansond 0:4c9bfcb3e759 38 Light::Light(ErrorHandler *error_handler,Transport *transports[NUM_TRANSPORTS],int instance,void *endpoint) {
ansond 0:4c9bfcb3e759 39 this->m_error_handler = error_handler;
ansond 1:5d332fa199ce 40 this->m_resources = ((MBEDEndpoint *)endpoint)->initResourceFactory();
ansond 0:4c9bfcb3e759 41 this->m_endpoint = endpoint;
ansond 28:52b7ca6aacd6 42 this->m_ioc_id = 0;
ansond 101:8747b6612e32 43 this->m_forward = true;
ansond 20:f2dbbd852e08 44 for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = transports[i];
ansond 16:fda7dbb8b47a 45 memset(this->m_name,0,LIGHT_NAME_LEN+1);
ansond 0:4c9bfcb3e759 46 sprintf(this->m_name,LIGHT_NAME,instance);
ansond 5:c1c4d511b589 47 this->resources()->createResources(this->getName());
ansond 29:9a99f076129a 48
ansond 91:8732d54328ae 49 // setup the blinking thread
ansond 91:8732d54328ae 50 this->m_blinking_thread = NULL;
ansond 91:8732d54328ae 51 this->m_is_blinking = false;
ansond 91:8732d54328ae 52
ansond 29:9a99f076129a 53 // Setup Philips Light if enabled
ansond 29:9a99f076129a 54 if (PL_ENABLE) this->m_pl = new PhilipsLight(PL_LIGHT_ID,this->m_transports[HTTP_TRANSPORT],this->logger());
ansond 29:9a99f076129a 55 else this->m_pl = NULL;
ansond 105:759dee018363 56
ansond 105:759dee018363 57 // Setup External LED Light if enabled
ansond 105:759dee018363 58 if (EXT_LED_ENABLE) this->m_ext_led = new ExternalLEDLight(new PwmOut(EXT_LED_PIN),this->logger());
ansond 105:759dee018363 59 else this->m_ext_led = NULL;
ansond 9:90fadae5489a 60
ansond 0:4c9bfcb3e759 61 // DEBUG
ansond 29:9a99f076129a 62 if (PL_ENABLE) this->logger()->log("Light name: %s (Philips Light %d)",this->getName(),PL_LIGHT_ID);
ansond 91:8732d54328ae 63 else this->logger()->log("Light name: %s", this->getName());
ansond 92:330746c526b7 64
ansond 91:8732d54328ae 65 // we are activated
ansond 91:8732d54328ae 66 _instance = (void *)this;
ansond 0:4c9bfcb3e759 67 }
ansond 0:4c9bfcb3e759 68
ansond 9:90fadae5489a 69 // destructor
ansond 9:90fadae5489a 70 Light::~Light() {
ansond 47:fa96ddc36f04 71 if (this->m_resources != NULL) delete this->m_resources;
ansond 47:fa96ddc36f04 72 if (this->m_dimmer_action != NULL) delete this->m_dimmer_action;
ansond 47:fa96ddc36f04 73 if (this->m_switch_action != NULL) delete this->m_switch_action;
ansond 47:fa96ddc36f04 74 if (this->m_pl != NULL) delete this->m_pl;
ansond 105:759dee018363 75 if (this->m_ext_led != NULL) delete this->m_ext_led;
ansond 91:8732d54328ae 76 this->stopBlinkingThread();
ansond 0:4c9bfcb3e759 77 }
ansond 0:4c9bfcb3e759 78
ansond 92:330746c526b7 79 // initialize the light
ansond 92:330746c526b7 80 void Light::initLight() {
ansond 92:330746c526b7 81 this->m_current_state = LIGHT_DEFAULT_STATE;
ansond 92:330746c526b7 82 this->m_last_state = this->m_current_state;
ansond 92:330746c526b7 83 }
ansond 92:330746c526b7 84
ansond 21:cfdaee0a2b50 85 // get the resource factory
ansond 21:cfdaee0a2b50 86 ResourceFactory *Light::getResourceFactory() { return this->m_resources; }
ansond 21:cfdaee0a2b50 87
ansond 29:9a99f076129a 88 // get the Philips light
ansond 29:9a99f076129a 89 PhilipsLight *Light::pl() { return this->m_pl; }
ansond 29:9a99f076129a 90
ansond 105:759dee018363 91 // get the External LED light
ansond 105:759dee018363 92 ExternalLEDLight *Light::extled() { return this->m_ext_led; }
ansond 105:759dee018363 93
ansond 9:90fadae5489a 94 // set the dimmer action
ansond 9:90fadae5489a 95 void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; }
ansond 9:90fadae5489a 96
ansond 9:90fadae5489a 97 // set the switch actino
ansond 9:90fadae5489a 98 void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; }
ansond 9:90fadae5489a 99
ansond 9:90fadae5489a 100 // get the dimmer action
ansond 9:90fadae5489a 101 void *Light::getDimmerAction() { return this->m_dimmer_action; }
ansond 9:90fadae5489a 102
ansond 9:90fadae5489a 103 // get the switch action
ansond 9:90fadae5489a 104 void *Light::getSwitchAction() { return this->m_switch_action; }
ansond 9:90fadae5489a 105
ansond 0:4c9bfcb3e759 106 // get the requested transport
ansond 0:4c9bfcb3e759 107 Transport *Light::getTransport(int index) { return this->m_transports[index]; }
ansond 0:4c9bfcb3e759 108
ansond 0:4c9bfcb3e759 109 // get the light name
ansond 0:4c9bfcb3e759 110 char *Light::getName() { return this->m_name; }
ansond 1:5d332fa199ce 111
ansond 0:4c9bfcb3e759 112 // get the error handler
ansond 0:4c9bfcb3e759 113 ErrorHandler *Light::logger() { return this->m_error_handler; }
ansond 0:4c9bfcb3e759 114
ansond 1:5d332fa199ce 115 // get the resources factory
ansond 1:5d332fa199ce 116 ResourceFactory *Light::resources() { return this->m_resources; }
ansond 91:8732d54328ae 117
ansond 29:9a99f076129a 118 // turn ON
ansond 105:759dee018363 119 void Light::on() {
ansond 105:759dee018363 120 this->m_current_state = 1;
ansond 105:759dee018363 121 this->manageBlinkingThread();
ansond 105:759dee018363 122 if (PL_ENABLE && this->pl() != NULL) this->pl()->on();
ansond 105:759dee018363 123 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->on();
ansond 105:759dee018363 124 }
ansond 9:90fadae5489a 125
ansond 29:9a99f076129a 126 // turn OFF
ansond 105:759dee018363 127 void Light::off() {
ansond 105:759dee018363 128 this->m_current_state = 0;
ansond 105:759dee018363 129 this->manageBlinkingThread();
ansond 105:759dee018363 130 if (PL_ENABLE && this->pl() != NULL) this->pl()->off();
ansond 105:759dee018363 131 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->off();
ansond 105:759dee018363 132 }
ansond 91:8732d54328ae 133
ansond 91:8732d54328ae 134 // initiate blinking
ansond 91:8732d54328ae 135 void Light::blink() {
ansond 91:8732d54328ae 136 this->m_last_state = this->m_current_state;
ansond 91:8732d54328ae 137 this->startBlinkingThread();
ansond 91:8732d54328ae 138 }
ansond 91:8732d54328ae 139
ansond 91:8732d54328ae 140 // manage the blinking thread
ansond 91:8732d54328ae 141 void Light::manageBlinkingThread() {
ansond 91:8732d54328ae 142 if (this->m_is_blinking == false) this->stopBlinkingThread();
ansond 91:8732d54328ae 143 this->m_is_blinking = false;
ansond 91:8732d54328ae 144 }
ansond 91:8732d54328ae 145
ansond 91:8732d54328ae 146 // stop blinking
ansond 91:8732d54328ae 147 void Light::stopBlinking() {
ansond 91:8732d54328ae 148 this->m_is_blinking = false;
ansond 91:8732d54328ae 149 if (this->m_last_state == 1) this->on();
ansond 91:8732d54328ae 150 if (this->m_last_state == 0) this->off();
ansond 91:8732d54328ae 151 this->m_current_state = this->m_last_state;
ansond 91:8732d54328ae 152 }
ansond 91:8732d54328ae 153
ansond 91:8732d54328ae 154 // start blinking thread
ansond 91:8732d54328ae 155 void Light::startBlinkingThread() {
ansond 91:8732d54328ae 156 if (this->m_blinking_thread == NULL)
ansond 91:8732d54328ae 157 this->m_blinking_thread = new Thread(blinking_action);
ansond 91:8732d54328ae 158 }
ansond 91:8732d54328ae 159
ansond 91:8732d54328ae 160 // stop blinking thread
ansond 91:8732d54328ae 161 void Light::stopBlinkingThread() {
ansond 91:8732d54328ae 162 if (this->m_blinking_thread != NULL) {
ansond 91:8732d54328ae 163 this->m_blinking_thread->terminate();
ansond 91:8732d54328ae 164 delete this->m_blinking_thread;
ansond 91:8732d54328ae 165 }
ansond 91:8732d54328ae 166 this->m_blinking_thread = NULL;
ansond 91:8732d54328ae 167 }
ansond 9:90fadae5489a 168
ansond 101:8747b6612e32 169 // update our blinking sequencing to properly mesh with the lights current state
ansond 101:8747b6612e32 170 void Light::updateDirection() {
ansond 101:8747b6612e32 171 this->m_forward = true;
ansond 101:8747b6612e32 172 if (this->m_current_state == 0) this->m_forward = false;
ansond 101:8747b6612e32 173 }
ansond 101:8747b6612e32 174
ansond 87:e9d77e9f9eae 175 // Blink
ansond 91:8732d54328ae 176 void Light::blinkLight() {
ansond 91:8732d54328ae 177 this->m_is_blinking = true;
ansond 101:8747b6612e32 178 if (this->m_forward) this->on(); else this->off();
ansond 101:8747b6612e32 179 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->on(); else this->pl()->off(); }
ansond 105:759dee018363 180 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->on(); else this->extled()->off(); }
ansond 98:3ea8058f4c54 181 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 182 this->m_is_blinking = true;
ansond 101:8747b6612e32 183 if (this->m_forward) this->off(); else this->on();
ansond 101:8747b6612e32 184 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); }
ansond 105:759dee018363 185 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); }
ansond 98:3ea8058f4c54 186 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 187 }
ansond 91:8732d54328ae 188
ansond 29:9a99f076129a 189 // dim
ansond 105:759dee018363 190 void Light::dim(int value) {
ansond 105:759dee018363 191 if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value);
ansond 105:759dee018363 192 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value);
ansond 105:759dee018363 193 }
ansond 9:90fadae5489a 194
ansond 28:52b7ca6aacd6 195 // set the IOC ID
ansond 28:52b7ca6aacd6 196 void Light::setIOCID(int ioc_id) { this->m_ioc_id = ioc_id; }
ansond 28:52b7ca6aacd6 197
ansond 28:52b7ca6aacd6 198 // get the IOC ID
ansond 28:52b7ca6aacd6 199 int Light::getIOCID() { return this->m_ioc_id; }
ansond 28:52b7ca6aacd6 200
ansond 28:52b7ca6aacd6 201
ansond 0:4c9bfcb3e759 202
ansond 0:4c9bfcb3e759 203