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:
Mon Mar 31 20:37:35 2014 +0000
Revision:
142:b7a75144b0f1
Parent:
134:58e7537a8c5f
Child:
144:e4d24c2c8f18
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 134:58e7537a8c5f 38 Light::Light(ErrorHandler *error_handler,Transport *transports[NUM_TRANSPORTS],int instance,void *endpoint) : Personality(error_handler,transports,instance,endpoint,LIGHT_TYPE_STRING) {
ansond 101:8747b6612e32 39 this->m_forward = true;
ansond 134:58e7537a8c5f 40
ansond 91:8732d54328ae 41 // setup the blinking thread
ansond 91:8732d54328ae 42 this->m_blinking_thread = NULL;
ansond 91:8732d54328ae 43 this->m_is_blinking = false;
ansond 91:8732d54328ae 44
ansond 29:9a99f076129a 45 // Setup Philips Light if enabled
ansond 29:9a99f076129a 46 if (PL_ENABLE) this->m_pl = new PhilipsLight(PL_LIGHT_ID,this->m_transports[HTTP_TRANSPORT],this->logger());
ansond 29:9a99f076129a 47 else this->m_pl = NULL;
ansond 105:759dee018363 48
ansond 105:759dee018363 49 // Setup External LED Light if enabled
ansond 105:759dee018363 50 if (EXT_LED_ENABLE) this->m_ext_led = new ExternalLEDLight(new PwmOut(EXT_LED_PIN),this->logger());
ansond 105:759dee018363 51 else this->m_ext_led = NULL;
ansond 9:90fadae5489a 52
ansond 0:4c9bfcb3e759 53 // DEBUG
ansond 29:9a99f076129a 54 if (PL_ENABLE) this->logger()->log("Light name: %s (Philips Light %d)",this->getName(),PL_LIGHT_ID);
ansond 91:8732d54328ae 55 else this->logger()->log("Light name: %s", this->getName());
ansond 92:330746c526b7 56
ansond 91:8732d54328ae 57 // we are activated
ansond 91:8732d54328ae 58 _instance = (void *)this;
ansond 0:4c9bfcb3e759 59 }
ansond 0:4c9bfcb3e759 60
ansond 9:90fadae5489a 61 // destructor
ansond 9:90fadae5489a 62 Light::~Light() {
ansond 47:fa96ddc36f04 63 if (this->m_dimmer_action != NULL) delete this->m_dimmer_action;
ansond 47:fa96ddc36f04 64 if (this->m_switch_action != NULL) delete this->m_switch_action;
ansond 47:fa96ddc36f04 65 if (this->m_pl != NULL) delete this->m_pl;
ansond 105:759dee018363 66 if (this->m_ext_led != NULL) delete this->m_ext_led;
ansond 91:8732d54328ae 67 this->stopBlinkingThread();
ansond 0:4c9bfcb3e759 68 }
ansond 0:4c9bfcb3e759 69
ansond 92:330746c526b7 70 // initialize the light
ansond 92:330746c526b7 71 void Light::initLight() {
ansond 142:b7a75144b0f1 72 sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_current_state);
ansond 92:330746c526b7 73 this->m_last_state = this->m_current_state;
ansond 92:330746c526b7 74 }
ansond 92:330746c526b7 75
ansond 29:9a99f076129a 76 // get the Philips light
ansond 29:9a99f076129a 77 PhilipsLight *Light::pl() { return this->m_pl; }
ansond 29:9a99f076129a 78
ansond 105:759dee018363 79 // get the External LED light
ansond 105:759dee018363 80 ExternalLEDLight *Light::extled() { return this->m_ext_led; }
ansond 105:759dee018363 81
ansond 9:90fadae5489a 82 // set the dimmer action
ansond 9:90fadae5489a 83 void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; }
ansond 9:90fadae5489a 84
ansond 9:90fadae5489a 85 // set the switch actino
ansond 9:90fadae5489a 86 void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; }
ansond 9:90fadae5489a 87
ansond 9:90fadae5489a 88 // get the dimmer action
ansond 9:90fadae5489a 89 void *Light::getDimmerAction() { return this->m_dimmer_action; }
ansond 9:90fadae5489a 90
ansond 9:90fadae5489a 91 // get the switch action
ansond 9:90fadae5489a 92 void *Light::getSwitchAction() { return this->m_switch_action; }
ansond 134:58e7537a8c5f 93
ansond 29:9a99f076129a 94 // turn ON
ansond 105:759dee018363 95 void Light::on() {
ansond 105:759dee018363 96 this->m_current_state = 1;
ansond 105:759dee018363 97 this->manageBlinkingThread();
ansond 105:759dee018363 98 if (PL_ENABLE && this->pl() != NULL) this->pl()->on();
ansond 105:759dee018363 99 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->on();
ansond 105:759dee018363 100 }
ansond 9:90fadae5489a 101
ansond 29:9a99f076129a 102 // turn OFF
ansond 105:759dee018363 103 void Light::off() {
ansond 105:759dee018363 104 this->m_current_state = 0;
ansond 105:759dee018363 105 this->manageBlinkingThread();
ansond 105:759dee018363 106 if (PL_ENABLE && this->pl() != NULL) this->pl()->off();
ansond 105:759dee018363 107 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->off();
ansond 105:759dee018363 108 }
ansond 91:8732d54328ae 109
ansond 91:8732d54328ae 110 // initiate blinking
ansond 91:8732d54328ae 111 void Light::blink() {
ansond 91:8732d54328ae 112 this->m_last_state = this->m_current_state;
ansond 91:8732d54328ae 113 this->startBlinkingThread();
ansond 91:8732d54328ae 114 }
ansond 91:8732d54328ae 115
ansond 91:8732d54328ae 116 // manage the blinking thread
ansond 91:8732d54328ae 117 void Light::manageBlinkingThread() {
ansond 91:8732d54328ae 118 if (this->m_is_blinking == false) this->stopBlinkingThread();
ansond 91:8732d54328ae 119 this->m_is_blinking = false;
ansond 91:8732d54328ae 120 }
ansond 91:8732d54328ae 121
ansond 91:8732d54328ae 122 // stop blinking
ansond 91:8732d54328ae 123 void Light::stopBlinking() {
ansond 91:8732d54328ae 124 this->m_is_blinking = false;
ansond 91:8732d54328ae 125 if (this->m_last_state == 1) this->on();
ansond 91:8732d54328ae 126 if (this->m_last_state == 0) this->off();
ansond 91:8732d54328ae 127 this->m_current_state = this->m_last_state;
ansond 91:8732d54328ae 128 }
ansond 91:8732d54328ae 129
ansond 91:8732d54328ae 130 // start blinking thread
ansond 91:8732d54328ae 131 void Light::startBlinkingThread() {
ansond 91:8732d54328ae 132 if (this->m_blinking_thread == NULL)
ansond 91:8732d54328ae 133 this->m_blinking_thread = new Thread(blinking_action);
ansond 91:8732d54328ae 134 }
ansond 91:8732d54328ae 135
ansond 91:8732d54328ae 136 // stop blinking thread
ansond 91:8732d54328ae 137 void Light::stopBlinkingThread() {
ansond 91:8732d54328ae 138 if (this->m_blinking_thread != NULL) {
ansond 91:8732d54328ae 139 this->m_blinking_thread->terminate();
ansond 91:8732d54328ae 140 delete this->m_blinking_thread;
ansond 91:8732d54328ae 141 }
ansond 91:8732d54328ae 142 this->m_blinking_thread = NULL;
ansond 91:8732d54328ae 143 }
ansond 9:90fadae5489a 144
ansond 101:8747b6612e32 145 // update our blinking sequencing to properly mesh with the lights current state
ansond 101:8747b6612e32 146 void Light::updateDirection() {
ansond 101:8747b6612e32 147 this->m_forward = true;
ansond 101:8747b6612e32 148 if (this->m_current_state == 0) this->m_forward = false;
ansond 101:8747b6612e32 149 }
ansond 101:8747b6612e32 150
ansond 87:e9d77e9f9eae 151 // Blink
ansond 91:8732d54328ae 152 void Light::blinkLight() {
ansond 91:8732d54328ae 153 this->m_is_blinking = true;
ansond 101:8747b6612e32 154 if (this->m_forward) this->on(); else this->off();
ansond 101:8747b6612e32 155 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->on(); else this->pl()->off(); }
ansond 105:759dee018363 156 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->on(); else this->extled()->off(); }
ansond 98:3ea8058f4c54 157 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 158 this->m_is_blinking = true;
ansond 101:8747b6612e32 159 if (this->m_forward) this->off(); else this->on();
ansond 101:8747b6612e32 160 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); }
ansond 105:759dee018363 161 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); }
ansond 98:3ea8058f4c54 162 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 163 }
ansond 91:8732d54328ae 164
ansond 29:9a99f076129a 165 // dim
ansond 105:759dee018363 166 void Light::dim(int value) {
ansond 105:759dee018363 167 if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value);
ansond 105:759dee018363 168 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value);
ansond 105:759dee018363 169 }
ansond 9:90fadae5489a 170
ansond 0:4c9bfcb3e759 171
ansond 0:4c9bfcb3e759 172