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:
Fri Sep 26 05:16:07 2014 +0000
Revision:
192:54b758a8eaaa
Parent:
180:eb43d5cd0ed7
updates to new logger name

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 192:54b758a8eaaa 38 Light::Light(Logger *logger,Transport *transports[NUM_TRANSPORTS],int instance,void *endpoint) : Personality(logger,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 160:24337c83dd1d 42 #ifdef ENABLE_THREADS
ansond 91:8732d54328ae 43 this->m_blinking_thread = NULL;
ansond 160:24337c83dd1d 44 #endif
ansond 91:8732d54328ae 45 this->m_is_blinking = false;
ansond 91:8732d54328ae 46
ansond 29:9a99f076129a 47 // Setup Philips Light if enabled
ansond 29:9a99f076129a 48 if (PL_ENABLE) this->m_pl = new PhilipsLight(PL_LIGHT_ID,this->m_transports[HTTP_TRANSPORT],this->logger());
ansond 29:9a99f076129a 49 else this->m_pl = NULL;
ansond 144:e4d24c2c8f18 50
ansond 144:e4d24c2c8f18 51 #ifdef COPCAR_PERSONALITY
ansond 144:e4d24c2c8f18 52 // Setup External CopCar Flasher if enabled
ansond 144:e4d24c2c8f18 53 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 54 else this->m_ext_led = NULL;
ansond 144:e4d24c2c8f18 55 #endif
ansond 167:2529c18d0eb1 56
ansond 144:e4d24c2c8f18 57 #ifdef LIGHT_PERSONALITY
ansond 105:759dee018363 58 // Setup External LED Light if enabled
ansond 105:759dee018363 59 if (EXT_LED_ENABLE) this->m_ext_led = new ExternalLEDLight(new PwmOut(EXT_LED_PIN),this->logger());
ansond 105:759dee018363 60 else this->m_ext_led = NULL;
ansond 144:e4d24c2c8f18 61 #endif
ansond 167:2529c18d0eb1 62
ansond 167:2529c18d0eb1 63 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 64 // Setup APM Light if enabled
ansond 167:2529c18d0eb1 65 if (APM_LIGHT_ENABLE) this->m_apm_light = new APMDemoLight(this->logger());
ansond 167:2529c18d0eb1 66 else this->m_apm_light = NULL;
ansond 167:2529c18d0eb1 67 #endif
ansond 9:90fadae5489a 68
ansond 0:4c9bfcb3e759 69 // DEBUG
ansond 29:9a99f076129a 70 if (PL_ENABLE) this->logger()->log("Light name: %s (Philips Light %d)",this->getName(),PL_LIGHT_ID);
ansond 91:8732d54328ae 71 else this->logger()->log("Light name: %s", this->getName());
ansond 92:330746c526b7 72
ansond 91:8732d54328ae 73 // we are activated
ansond 91:8732d54328ae 74 _instance = (void *)this;
ansond 0:4c9bfcb3e759 75 }
ansond 0:4c9bfcb3e759 76
ansond 9:90fadae5489a 77 // destructor
ansond 9:90fadae5489a 78 Light::~Light() {
ansond 47:fa96ddc36f04 79 if (this->m_dimmer_action != NULL) delete this->m_dimmer_action;
ansond 47:fa96ddc36f04 80 if (this->m_switch_action != NULL) delete this->m_switch_action;
ansond 47:fa96ddc36f04 81 if (this->m_pl != NULL) delete this->m_pl;
ansond 105:759dee018363 82 if (this->m_ext_led != NULL) delete this->m_ext_led;
ansond 169:5ba15f5f7f87 83 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 84 if (this->m_apm_light != NULL) delete this->m_apm_light;
ansond 169:5ba15f5f7f87 85 #endif
ansond 91:8732d54328ae 86 this->stopBlinkingThread();
ansond 0:4c9bfcb3e759 87 }
ansond 0:4c9bfcb3e759 88
ansond 92:330746c526b7 89 // initialize the light
ansond 92:330746c526b7 90 void Light::initLight() {
ansond 142:b7a75144b0f1 91 sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_current_state);
ansond 92:330746c526b7 92 this->m_last_state = this->m_current_state;
ansond 92:330746c526b7 93 }
ansond 92:330746c526b7 94
ansond 29:9a99f076129a 95 // get the Philips light
ansond 29:9a99f076129a 96 PhilipsLight *Light::pl() { return this->m_pl; }
ansond 29:9a99f076129a 97
ansond 105:759dee018363 98 // get the External LED light
ansond 105:759dee018363 99 ExternalLEDLight *Light::extled() { return this->m_ext_led; }
ansond 105:759dee018363 100
ansond 169:5ba15f5f7f87 101 #ifdef APM_LIGHT_PERSONALITY
ansond 169:5ba15f5f7f87 102 // get the APM light
ansond 167:2529c18d0eb1 103 APMDemoLight *Light::apmlight() { return this->m_apm_light; }
ansond 169:5ba15f5f7f87 104 #endif
ansond 167:2529c18d0eb1 105
ansond 9:90fadae5489a 106 // set the dimmer action
ansond 9:90fadae5489a 107 void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; }
ansond 9:90fadae5489a 108
ansond 9:90fadae5489a 109 // set the switch actino
ansond 9:90fadae5489a 110 void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; }
ansond 9:90fadae5489a 111
ansond 9:90fadae5489a 112 // get the dimmer action
ansond 9:90fadae5489a 113 void *Light::getDimmerAction() { return this->m_dimmer_action; }
ansond 9:90fadae5489a 114
ansond 9:90fadae5489a 115 // get the switch action
ansond 9:90fadae5489a 116 void *Light::getSwitchAction() { return this->m_switch_action; }
ansond 134:58e7537a8c5f 117
ansond 29:9a99f076129a 118 // turn ON
ansond 105:759dee018363 119 void Light::on() {
ansond 105:759dee018363 120 this->m_current_state = 1;
ansond 105:759dee018363 121 this->manageBlinkingThread();
ansond 105:759dee018363 122 if (PL_ENABLE && this->pl() != NULL) this->pl()->on();
ansond 105:759dee018363 123 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->on();
ansond 169:5ba15f5f7f87 124 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 125 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->on();
ansond 169:5ba15f5f7f87 126 #endif
ansond 105:759dee018363 127 }
ansond 9:90fadae5489a 128
ansond 29:9a99f076129a 129 // turn OFF
ansond 105:759dee018363 130 void Light::off() {
ansond 105:759dee018363 131 this->m_current_state = 0;
ansond 105:759dee018363 132 this->manageBlinkingThread();
ansond 105:759dee018363 133 if (PL_ENABLE && this->pl() != NULL) this->pl()->off();
ansond 105:759dee018363 134 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->off();
ansond 169:5ba15f5f7f87 135 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 136 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->off();
ansond 169:5ba15f5f7f87 137 #endif
ansond 105:759dee018363 138 }
ansond 91:8732d54328ae 139
ansond 91:8732d54328ae 140 // initiate blinking
ansond 91:8732d54328ae 141 void Light::blink() {
ansond 91:8732d54328ae 142 this->m_last_state = this->m_current_state;
ansond 173:41c3cb81aba3 143 #ifdef APM_COPCAR_ENABLE
ansond 173:41c3cb81aba3 144 // just enable the external pin to go high
ansond 173:41c3cb81aba3 145 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 173:41c3cb81aba3 146 #else
ansond 91:8732d54328ae 147 this->startBlinkingThread();
ansond 173:41c3cb81aba3 148 #endif
ansond 91:8732d54328ae 149 }
ansond 91:8732d54328ae 150
ansond 91:8732d54328ae 151 // manage the blinking thread
ansond 91:8732d54328ae 152 void Light::manageBlinkingThread() {
ansond 91:8732d54328ae 153 if (this->m_is_blinking == false) this->stopBlinkingThread();
ansond 91:8732d54328ae 154 this->m_is_blinking = false;
ansond 91:8732d54328ae 155 }
ansond 91:8732d54328ae 156
ansond 91:8732d54328ae 157 // stop blinking
ansond 91:8732d54328ae 158 void Light::stopBlinking() {
ansond 91:8732d54328ae 159 this->m_is_blinking = false;
ansond 91:8732d54328ae 160 if (this->m_last_state == 1) this->on();
ansond 91:8732d54328ae 161 if (this->m_last_state == 0) this->off();
ansond 91:8732d54328ae 162 this->m_current_state = this->m_last_state;
ansond 173:41c3cb81aba3 163 #ifdef APM_COPCAR_ENABLE
ansond 173:41c3cb81aba3 164 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 173:41c3cb81aba3 165 #endif
ansond 91:8732d54328ae 166 }
ansond 91:8732d54328ae 167
ansond 91:8732d54328ae 168 // start blinking thread
ansond 91:8732d54328ae 169 void Light::startBlinkingThread() {
ansond 160:24337c83dd1d 170 #ifdef ENABLE_THREADS
ansond 91:8732d54328ae 171 if (this->m_blinking_thread == NULL)
ansond 91:8732d54328ae 172 this->m_blinking_thread = new Thread(blinking_action);
ansond 160:24337c83dd1d 173 #endif
ansond 91:8732d54328ae 174 }
ansond 91:8732d54328ae 175
ansond 91:8732d54328ae 176 // stop blinking thread
ansond 91:8732d54328ae 177 void Light::stopBlinkingThread() {
ansond 160:24337c83dd1d 178 #ifdef ENABLE_THREADS
ansond 91:8732d54328ae 179 if (this->m_blinking_thread != NULL) {
ansond 91:8732d54328ae 180 this->m_blinking_thread->terminate();
ansond 91:8732d54328ae 181 delete this->m_blinking_thread;
ansond 91:8732d54328ae 182 }
ansond 91:8732d54328ae 183 this->m_blinking_thread = NULL;
ansond 160:24337c83dd1d 184 #endif
ansond 91:8732d54328ae 185 }
ansond 9:90fadae5489a 186
ansond 101:8747b6612e32 187 // update our blinking sequencing to properly mesh with the lights current state
ansond 101:8747b6612e32 188 void Light::updateDirection() {
ansond 101:8747b6612e32 189 this->m_forward = true;
ansond 101:8747b6612e32 190 if (this->m_current_state == 0) this->m_forward = false;
ansond 101:8747b6612e32 191 }
ansond 101:8747b6612e32 192
ansond 87:e9d77e9f9eae 193 // Blink
ansond 91:8732d54328ae 194 void Light::blinkLight() {
ansond 91:8732d54328ae 195 this->m_is_blinking = true;
ansond 146:8c05a119d9d4 196 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 197 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 180:eb43d5cd0ed7 198 Thread::wait(125);
ansond 146:8c05a119d9d4 199 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 200 this->m_current_state = 0;
ansond 146:8c05a119d9d4 201 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 202 #endif
ansond 170:9e72f2d0f2b2 203 #ifdef LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 204 if (this->m_forward) this->on(); else this->off();
ansond 101:8747b6612e32 205 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->on(); else this->pl()->off(); }
ansond 105:759dee018363 206 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->on(); else this->extled()->off(); }
ansond 146:8c05a119d9d4 207 #endif
ansond 167:2529c18d0eb1 208 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 209 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) { if (this->m_forward) this->apmlight()->on(); else this->apmlight()->off(); }
ansond 167:2529c18d0eb1 210 #endif
ansond 170:9e72f2d0f2b2 211
ansond 180:eb43d5cd0ed7 212 Thread::wait(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 213 this->m_is_blinking = true;
ansond 146:8c05a119d9d4 214 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 215 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 180:eb43d5cd0ed7 216 Thread::wait(125);
ansond 146:8c05a119d9d4 217 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 218 this->m_current_state = 0;
ansond 146:8c05a119d9d4 219 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 220 #endif
ansond 146:8c05a119d9d4 221 #ifdef LIGHT_PERSONALITY
ansond 170:9e72f2d0f2b2 222 if (this->m_forward) this->off(); else this->on();
ansond 101:8747b6612e32 223 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); }
ansond 105:759dee018363 224 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); }
ansond 146:8c05a119d9d4 225 #endif
ansond 167:2529c18d0eb1 226 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 227 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) { if (this->m_forward) this->apmlight()->off(); else this->apmlight()->on(); }
ansond 167:2529c18d0eb1 228 #endif
ansond 180:eb43d5cd0ed7 229 Thread::wait(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 230 }
ansond 91:8732d54328ae 231
ansond 29:9a99f076129a 232 // dim
ansond 105:759dee018363 233 void Light::dim(int value) {
ansond 105:759dee018363 234 if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value);
ansond 105:759dee018363 235 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value);
ansond 169:5ba15f5f7f87 236 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 237 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->dim(value);
ansond 169:5ba15f5f7f87 238 #endif
ansond 105:759dee018363 239 }
ansond 9:90fadae5489a 240
ansond 0:4c9bfcb3e759 241
ansond 0:4c9bfcb3e759 242