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:
Thu Jul 17 19:50:57 2014 +0000
Revision:
169:5ba15f5f7f87
Parent:
167:2529c18d0eb1
Child:
170:9e72f2d0f2b2
more fixes for tuning APM light

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 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 91:8732d54328ae 143 this->startBlinkingThread();
ansond 91:8732d54328ae 144 }
ansond 91:8732d54328ae 145
ansond 91:8732d54328ae 146 // manage the blinking thread
ansond 91:8732d54328ae 147 void Light::manageBlinkingThread() {
ansond 91:8732d54328ae 148 if (this->m_is_blinking == false) this->stopBlinkingThread();
ansond 91:8732d54328ae 149 this->m_is_blinking = false;
ansond 91:8732d54328ae 150 }
ansond 91:8732d54328ae 151
ansond 91:8732d54328ae 152 // stop blinking
ansond 91:8732d54328ae 153 void Light::stopBlinking() {
ansond 91:8732d54328ae 154 this->m_is_blinking = false;
ansond 91:8732d54328ae 155 if (this->m_last_state == 1) this->on();
ansond 91:8732d54328ae 156 if (this->m_last_state == 0) this->off();
ansond 91:8732d54328ae 157 this->m_current_state = this->m_last_state;
ansond 91:8732d54328ae 158 }
ansond 91:8732d54328ae 159
ansond 91:8732d54328ae 160 // start blinking thread
ansond 91:8732d54328ae 161 void Light::startBlinkingThread() {
ansond 160:24337c83dd1d 162 #ifdef ENABLE_THREADS
ansond 91:8732d54328ae 163 if (this->m_blinking_thread == NULL)
ansond 91:8732d54328ae 164 this->m_blinking_thread = new Thread(blinking_action);
ansond 160:24337c83dd1d 165 #endif
ansond 91:8732d54328ae 166 }
ansond 91:8732d54328ae 167
ansond 91:8732d54328ae 168 // stop blinking thread
ansond 91:8732d54328ae 169 void Light::stopBlinkingThread() {
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->terminate();
ansond 91:8732d54328ae 173 delete this->m_blinking_thread;
ansond 91:8732d54328ae 174 }
ansond 91:8732d54328ae 175 this->m_blinking_thread = NULL;
ansond 160:24337c83dd1d 176 #endif
ansond 91:8732d54328ae 177 }
ansond 9:90fadae5489a 178
ansond 101:8747b6612e32 179 // update our blinking sequencing to properly mesh with the lights current state
ansond 101:8747b6612e32 180 void Light::updateDirection() {
ansond 101:8747b6612e32 181 this->m_forward = true;
ansond 101:8747b6612e32 182 if (this->m_current_state == 0) this->m_forward = false;
ansond 101:8747b6612e32 183 }
ansond 101:8747b6612e32 184
ansond 87:e9d77e9f9eae 185 // Blink
ansond 91:8732d54328ae 186 void Light::blinkLight() {
ansond 91:8732d54328ae 187 this->m_is_blinking = true;
ansond 146:8c05a119d9d4 188 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 189 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 146:8c05a119d9d4 190 wait_ms(125);
ansond 146:8c05a119d9d4 191 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 192 this->m_current_state = 0;
ansond 146:8c05a119d9d4 193 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 194 #endif
ansond 167:2529c18d0eb1 195
ansond 167:2529c18d0eb1 196 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 197 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 198 #else
ansond 167:2529c18d0eb1 199 #ifdef LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 200 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 201 #endif
ansond 167:2529c18d0eb1 202 #endif
ansond 167:2529c18d0eb1 203
ansond 146:8c05a119d9d4 204 #ifdef LIGHT_PERSONALITY
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 167:2529c18d0eb1 211
ansond 167:2529c18d0eb1 212 // wait a bit...
ansond 98:3ea8058f4c54 213 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 214 this->m_is_blinking = true;
ansond 167:2529c18d0eb1 215
ansond 146:8c05a119d9d4 216 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 217 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 146:8c05a119d9d4 218 wait_ms(125);
ansond 146:8c05a119d9d4 219 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 220 this->m_current_state = 0;
ansond 146:8c05a119d9d4 221 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 222 #endif
ansond 167:2529c18d0eb1 223
ansond 167:2529c18d0eb1 224 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 225 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 226 #else
ansond 167:2529c18d0eb1 227 #ifdef LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 228 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 229 #endif
ansond 167:2529c18d0eb1 230 #endif
ansond 167:2529c18d0eb1 231
ansond 146:8c05a119d9d4 232 #ifdef LIGHT_PERSONALITY
ansond 101:8747b6612e32 233 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); }
ansond 105:759dee018363 234 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); }
ansond 146:8c05a119d9d4 235 #endif
ansond 167:2529c18d0eb1 236 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 237 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) { if (this->m_forward) this->apmlight()->off(); else this->apmlight()->on(); }
ansond 167:2529c18d0eb1 238 #endif
ansond 167:2529c18d0eb1 239
ansond 98:3ea8058f4c54 240 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 241 }
ansond 91:8732d54328ae 242
ansond 29:9a99f076129a 243 // dim
ansond 105:759dee018363 244 void Light::dim(int value) {
ansond 105:759dee018363 245 if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value);
ansond 105:759dee018363 246 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value);
ansond 169:5ba15f5f7f87 247 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 248 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->dim(value);
ansond 169:5ba15f5f7f87 249 #endif
ansond 105:759dee018363 250 }
ansond 9:90fadae5489a 251
ansond 0:4c9bfcb3e759 252
ansond 0:4c9bfcb3e759 253