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