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
Light.cpp
- Committer:
- ansond
- Date:
- 2014-03-13
- Revision:
- 91:8732d54328ae
- Parent:
- 87:e9d77e9f9eae
- Child:
- 92:330746c526b7
File content as of revision 91:8732d54328ae:
/* 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()); // 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 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(); } // 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() { 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); } // 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; }