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

Light.cpp

Committer:
ansond
Date:
2014-03-14
Revision:
99:0fa83e641f75
Parent:
98:3ea8058f4c54
Child:
101:8747b6612e32

File content as of revision 99:0fa83e641f75:

/* Copyright C2013 Doug Anson, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Light.h"
 #include "MBEDEndpoint.h"
 
 // include the relevant action support
 #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;
     this->m_resources = ((MBEDEndpoint *)endpoint)->initResourceFactory();
     this->m_endpoint = endpoint;
     this->m_ioc_id = 0;
     for(int i=0;i<NUM_TRANSPORTS;++i) this->m_transports[i] = transports[i];
     memset(this->m_name,0,LIGHT_NAME_LEN+1);
     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()); 
          
     // we are activated
     _instance = (void *)this;
 }
 
 // destructor
 Light::~Light() {   
    if (this->m_resources != NULL) delete this->m_resources;
    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();
 }
 
 // initialize the light
 void Light::initLight() {
     this->m_current_state = LIGHT_DEFAULT_STATE;
     this->m_last_state = this->m_current_state;
 }
 
 // get the resource factory
 ResourceFactory *Light::getResourceFactory() { return this->m_resources; }
 
 // get the Philips light
 PhilipsLight *Light::pl() { return this->m_pl; }
 
 // set the dimmer action
 void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; }
 
 // set the switch actino
 void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; }
 
 // get the dimmer action
 void *Light::getDimmerAction() { return this->m_dimmer_action; }
 
 // get the switch action
 void *Light::getSwitchAction() { return this->m_switch_action; }
 
 // get the requested transport
 Transport *Light::getTransport(int index) { return this->m_transports[index]; }
 
 // get the light name
 char *Light::getName() { return this->m_name; }
  
 // get the error handler
 ErrorHandler *Light::logger() { return this->m_error_handler; }
 
 // get the resources factory
 ResourceFactory *Light::resources() { return this->m_resources; }
  
 // turn 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() {  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::blinkLight() { 
    bool forward = true;
    if (this->m_current_state == 0) forward = false;
    this->m_is_blinking = true;
    if (forward) this->on(); else this->off();
    if (PL_ENABLE && this->pl() != NULL) { if (forward) this->pl()->on(); else this->pl()->off(); }
    wait_ms(LIGHT_BLINK_WAIT_MS);
    this->m_is_blinking = true;
    if (forward) this->off(); else this->on();
    if (PL_ENABLE && this->pl() != NULL) { if (forward) this->pl()->off(); else this->pl()->on(); }
    wait_ms(LIGHT_BLINK_WAIT_MS);
  }
  
 // dim 
 void Light::dim(int value) { if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value); }
 
 // set the IOC ID
 void Light::setIOCID(int ioc_id) { this->m_ioc_id = ioc_id; }
 
 // get the IOC ID
 int Light::getIOCID() { return this->m_ioc_id; }