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 Jul 16 17:29:00 2014 +0000
Revision:
167:2529c18d0eb1
Parent:
160:24337c83dd1d
Child:
169:5ba15f5f7f87
added APMDemoLight

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 167:2529c18d0eb1 83 if (this->m_apm_light != NULL) delete this->m_apm_light;
ansond 91:8732d54328ae 84 this->stopBlinkingThread();
ansond 0:4c9bfcb3e759 85 }
ansond 0:4c9bfcb3e759 86
ansond 92:330746c526b7 87 // initialize the light
ansond 92:330746c526b7 88 void Light::initLight() {
ansond 142:b7a75144b0f1 89 sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_current_state);
ansond 92:330746c526b7 90 this->m_last_state = this->m_current_state;
ansond 92:330746c526b7 91 }
ansond 92:330746c526b7 92
ansond 29:9a99f076129a 93 // get the Philips light
ansond 29:9a99f076129a 94 PhilipsLight *Light::pl() { return this->m_pl; }
ansond 29:9a99f076129a 95
ansond 105:759dee018363 96 // get the External LED light
ansond 105:759dee018363 97 ExternalLEDLight *Light::extled() { return this->m_ext_led; }
ansond 105:759dee018363 98
ansond 167:2529c18d0eb1 99 // get the APM light
ansond 167:2529c18d0eb1 100 APMDemoLight *Light::apmlight() { return this->m_apm_light; }
ansond 167:2529c18d0eb1 101
ansond 9:90fadae5489a 102 // set the dimmer action
ansond 9:90fadae5489a 103 void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; }
ansond 9:90fadae5489a 104
ansond 9:90fadae5489a 105 // set the switch actino
ansond 9:90fadae5489a 106 void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; }
ansond 9:90fadae5489a 107
ansond 9:90fadae5489a 108 // get the dimmer action
ansond 9:90fadae5489a 109 void *Light::getDimmerAction() { return this->m_dimmer_action; }
ansond 9:90fadae5489a 110
ansond 9:90fadae5489a 111 // get the switch action
ansond 9:90fadae5489a 112 void *Light::getSwitchAction() { return this->m_switch_action; }
ansond 134:58e7537a8c5f 113
ansond 29:9a99f076129a 114 // turn ON
ansond 105:759dee018363 115 void Light::on() {
ansond 105:759dee018363 116 this->m_current_state = 1;
ansond 105:759dee018363 117 this->manageBlinkingThread();
ansond 105:759dee018363 118 if (PL_ENABLE && this->pl() != NULL) this->pl()->on();
ansond 105:759dee018363 119 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->on();
ansond 167:2529c18d0eb1 120 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->on();
ansond 105:759dee018363 121 }
ansond 9:90fadae5489a 122
ansond 29:9a99f076129a 123 // turn OFF
ansond 105:759dee018363 124 void Light::off() {
ansond 105:759dee018363 125 this->m_current_state = 0;
ansond 105:759dee018363 126 this->manageBlinkingThread();
ansond 105:759dee018363 127 if (PL_ENABLE && this->pl() != NULL) this->pl()->off();
ansond 105:759dee018363 128 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->off();
ansond 167:2529c18d0eb1 129 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->off();
ansond 105:759dee018363 130 }
ansond 91:8732d54328ae 131
ansond 91:8732d54328ae 132 // initiate blinking
ansond 91:8732d54328ae 133 void Light::blink() {
ansond 91:8732d54328ae 134 this->m_last_state = this->m_current_state;
ansond 91:8732d54328ae 135 this->startBlinkingThread();
ansond 91:8732d54328ae 136 }
ansond 91:8732d54328ae 137
ansond 91:8732d54328ae 138 // manage the blinking thread
ansond 91:8732d54328ae 139 void Light::manageBlinkingThread() {
ansond 91:8732d54328ae 140 if (this->m_is_blinking == false) this->stopBlinkingThread();
ansond 91:8732d54328ae 141 this->m_is_blinking = false;
ansond 91:8732d54328ae 142 }
ansond 91:8732d54328ae 143
ansond 91:8732d54328ae 144 // stop blinking
ansond 91:8732d54328ae 145 void Light::stopBlinking() {
ansond 91:8732d54328ae 146 this->m_is_blinking = false;
ansond 91:8732d54328ae 147 if (this->m_last_state == 1) this->on();
ansond 91:8732d54328ae 148 if (this->m_last_state == 0) this->off();
ansond 91:8732d54328ae 149 this->m_current_state = this->m_last_state;
ansond 91:8732d54328ae 150 }
ansond 91:8732d54328ae 151
ansond 91:8732d54328ae 152 // start blinking thread
ansond 91:8732d54328ae 153 void Light::startBlinkingThread() {
ansond 160:24337c83dd1d 154 #ifdef ENABLE_THREADS
ansond 91:8732d54328ae 155 if (this->m_blinking_thread == NULL)
ansond 91:8732d54328ae 156 this->m_blinking_thread = new Thread(blinking_action);
ansond 160:24337c83dd1d 157 #endif
ansond 91:8732d54328ae 158 }
ansond 91:8732d54328ae 159
ansond 91:8732d54328ae 160 // stop blinking thread
ansond 91:8732d54328ae 161 void Light::stopBlinkingThread() {
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->terminate();
ansond 91:8732d54328ae 165 delete this->m_blinking_thread;
ansond 91:8732d54328ae 166 }
ansond 91:8732d54328ae 167 this->m_blinking_thread = NULL;
ansond 160:24337c83dd1d 168 #endif
ansond 91:8732d54328ae 169 }
ansond 9:90fadae5489a 170
ansond 101:8747b6612e32 171 // update our blinking sequencing to properly mesh with the lights current state
ansond 101:8747b6612e32 172 void Light::updateDirection() {
ansond 101:8747b6612e32 173 this->m_forward = true;
ansond 101:8747b6612e32 174 if (this->m_current_state == 0) this->m_forward = false;
ansond 101:8747b6612e32 175 }
ansond 101:8747b6612e32 176
ansond 87:e9d77e9f9eae 177 // Blink
ansond 91:8732d54328ae 178 void Light::blinkLight() {
ansond 91:8732d54328ae 179 this->m_is_blinking = true;
ansond 146:8c05a119d9d4 180 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 181 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 146:8c05a119d9d4 182 wait_ms(125);
ansond 146:8c05a119d9d4 183 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 184 this->m_current_state = 0;
ansond 146:8c05a119d9d4 185 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 186 #endif
ansond 167:2529c18d0eb1 187
ansond 167:2529c18d0eb1 188 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 189 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 190 #else
ansond 167:2529c18d0eb1 191 #ifdef LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 192 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 193 #endif
ansond 167:2529c18d0eb1 194 #endif
ansond 167:2529c18d0eb1 195
ansond 146:8c05a119d9d4 196 #ifdef LIGHT_PERSONALITY
ansond 101:8747b6612e32 197 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->on(); else this->pl()->off(); }
ansond 105:759dee018363 198 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->on(); else this->extled()->off(); }
ansond 146:8c05a119d9d4 199 #endif
ansond 167:2529c18d0eb1 200 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 201 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) { if (this->m_forward) this->apmlight()->on(); else this->apmlight()->off(); }
ansond 167:2529c18d0eb1 202 #endif
ansond 167:2529c18d0eb1 203
ansond 167:2529c18d0eb1 204 // wait a bit...
ansond 98:3ea8058f4c54 205 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 206 this->m_is_blinking = true;
ansond 167:2529c18d0eb1 207
ansond 146:8c05a119d9d4 208 #ifdef COPCAR_PERSONALITY
ansond 146:8c05a119d9d4 209 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); }
ansond 146:8c05a119d9d4 210 wait_ms(125);
ansond 146:8c05a119d9d4 211 if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); }
ansond 146:8c05a119d9d4 212 this->m_current_state = 0;
ansond 146:8c05a119d9d4 213 this->manageBlinkingThread();
ansond 146:8c05a119d9d4 214 #endif
ansond 167:2529c18d0eb1 215
ansond 167:2529c18d0eb1 216 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 217 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 218 #else
ansond 167:2529c18d0eb1 219 #ifdef LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 220 if (this->m_forward) this->on(); else this->off();
ansond 167:2529c18d0eb1 221 #endif
ansond 167:2529c18d0eb1 222 #endif
ansond 167:2529c18d0eb1 223
ansond 146:8c05a119d9d4 224 #ifdef LIGHT_PERSONALITY
ansond 101:8747b6612e32 225 if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); }
ansond 105:759dee018363 226 if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); }
ansond 146:8c05a119d9d4 227 #endif
ansond 167:2529c18d0eb1 228 #ifdef APM_LIGHT_PERSONALITY
ansond 167:2529c18d0eb1 229 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) { if (this->m_forward) this->apmlight()->off(); else this->apmlight()->on(); }
ansond 167:2529c18d0eb1 230 #endif
ansond 167:2529c18d0eb1 231
ansond 98:3ea8058f4c54 232 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 91:8732d54328ae 233 }
ansond 91:8732d54328ae 234
ansond 29:9a99f076129a 235 // dim
ansond 105:759dee018363 236 void Light::dim(int value) {
ansond 105:759dee018363 237 if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value);
ansond 105:759dee018363 238 if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value);
ansond 167:2529c18d0eb1 239 if (APM_LIGHT_ENABLE && this->apmlight() != NULL) this->apmlight()->dim(value);
ansond 105:759dee018363 240 }
ansond 9:90fadae5489a 241
ansond 0:4c9bfcb3e759 242
ansond 0:4c9bfcb3e759 243