Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
Diff: Light.cpp
- Revision:
- 91:8732d54328ae
- Parent:
- 87:e9d77e9f9eae
- Child:
- 92:330746c526b7
--- a/Light.cpp Thu Mar 13 00:07:59 2014 +0000 +++ b/Light.cpp Thu Mar 13 17:25:18 2014 +0000 @@ -23,6 +23,16 @@ #include "LightDimmerAction.h" #include "LightSwitchAction.h" + void *_instance = NULL; + + // Blinking Looper + static void blinking_action(void const *args) { + if (_instance != NULL) { + Light *self = (Light *)_instance; + while(true) self->blinkLight(); + } + } + // default constructor Light::Light(ErrorHandler *error_handler,Transport *transports[NUM_TRANSPORTS],int instance,void *endpoint) { this->m_error_handler = error_handler; @@ -34,13 +44,27 @@ sprintf(this->m_name,LIGHT_NAME,instance); this->resources()->createResources(this->getName()); + // setup the blinking thread + this->m_blinking_thread = NULL; + this->m_is_blinking = false; + // Setup Philips Light if enabled if (PL_ENABLE) this->m_pl = new PhilipsLight(PL_LIGHT_ID,this->m_transports[HTTP_TRANSPORT],this->logger()); else this->m_pl = NULL; // DEBUG if (PL_ENABLE) this->logger()->log("Light name: %s (Philips Light %d)",this->getName(),PL_LIGHT_ID); - else this->logger()->log("Light name: %s", this->getName()); + else this->logger()->log("Light name: %s", this->getName()); + + // initialize the default light state + this->m_current_state = LIGHT_DEFAULT_STATE; + this->m_last_state = this->m_current_state; + if (this->m_current_state == 1) this->on(); + if (this->m_current_state == 0) this->off(); + if (this->m_current_state == 2) this->blink(); + + // we are activated + _instance = (void *)this; } // destructor @@ -49,6 +73,7 @@ if (this->m_dimmer_action != NULL) delete this->m_dimmer_action; if (this->m_switch_action != NULL) delete this->m_switch_action; if (this->m_pl != NULL) delete this->m_pl; + this->stopBlinkingThread(); } // get the resource factory @@ -80,16 +105,60 @@ // get the resources factory ResourceFactory *Light::resources() { return this->m_resources; } - + // turn ON - void Light::on() { if (PL_ENABLE && this->pl() != NULL) this->pl()->on(); } + void Light::on() { this->m_current_state = 1; this->manageBlinkingThread(); if (PL_ENABLE && this->pl() != NULL) this->pl()->on(); } // turn OFF - void Light::off() { if (PL_ENABLE && this->pl() != NULL) this->pl()->off(); } + void Light::off() { this->m_current_state = 0; this->manageBlinkingThread(); if (PL_ENABLE && this->pl() != NULL) this->pl()->off(); } + + // initiate blinking + void Light::blink() { + this->m_last_state = this->m_current_state; + this->startBlinkingThread(); + } + + // manage the blinking thread + void Light::manageBlinkingThread() { + if (this->m_is_blinking == false) this->stopBlinkingThread(); + this->m_is_blinking = false; + } + + // stop blinking + void Light::stopBlinking() { + this->m_is_blinking = false; + if (this->m_last_state == 1) this->on(); + if (this->m_last_state == 0) this->off(); + this->m_current_state = this->m_last_state; + } + + // start blinking thread + void Light::startBlinkingThread() { + if (this->m_blinking_thread == NULL) + this->m_blinking_thread = new Thread(blinking_action); + } + + // stop blinking thread + void Light::stopBlinkingThread() { + if (this->m_blinking_thread != NULL) { + this->m_blinking_thread->terminate(); + delete this->m_blinking_thread; + } + this->m_blinking_thread = NULL; + } // Blink - void Light::blink() { if (PL_ENABLE && this->pl() != NULL) this->pl()->blink(); } - + void Light::blinkLight() { + this->m_is_blinking = true; + this->on(); + if (PL_ENABLE && this->pl() != NULL) this->pl()->on(); + wait_ms(500); + this->m_is_blinking = true; + this->off(); + if (PL_ENABLE && this->pl() != NULL) this->pl()->off(); + wait_ms(500); + } + // dim void Light::dim(int value) { if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value); }