Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
Light.cpp@160:24337c83dd1d, 2014-07-01 (annotated)
- Committer:
- ansond
- Date:
- Tue Jul 01 17:12:18 2014 +0000
- Revision:
- 160:24337c83dd1d
- Parent:
- 146:8c05a119d9d4
- Child:
- 167:2529c18d0eb1
switches for rtos support
Who changed what in which revision?
User | Revision | Line number | New 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 | 144:e4d24c2c8f18 | 56 | #ifdef LIGHT_PERSONALITY |
ansond | 105:759dee018363 | 57 | // Setup External LED Light if enabled |
ansond | 105:759dee018363 | 58 | if (EXT_LED_ENABLE) this->m_ext_led = new ExternalLEDLight(new PwmOut(EXT_LED_PIN),this->logger()); |
ansond | 105:759dee018363 | 59 | else this->m_ext_led = NULL; |
ansond | 144:e4d24c2c8f18 | 60 | #endif |
ansond | 9:90fadae5489a | 61 | |
ansond | 0:4c9bfcb3e759 | 62 | // DEBUG |
ansond | 29:9a99f076129a | 63 | if (PL_ENABLE) this->logger()->log("Light name: %s (Philips Light %d)",this->getName(),PL_LIGHT_ID); |
ansond | 91:8732d54328ae | 64 | else this->logger()->log("Light name: %s", this->getName()); |
ansond | 92:330746c526b7 | 65 | |
ansond | 91:8732d54328ae | 66 | // we are activated |
ansond | 91:8732d54328ae | 67 | _instance = (void *)this; |
ansond | 0:4c9bfcb3e759 | 68 | } |
ansond | 0:4c9bfcb3e759 | 69 | |
ansond | 9:90fadae5489a | 70 | // destructor |
ansond | 9:90fadae5489a | 71 | Light::~Light() { |
ansond | 47:fa96ddc36f04 | 72 | if (this->m_dimmer_action != NULL) delete this->m_dimmer_action; |
ansond | 47:fa96ddc36f04 | 73 | if (this->m_switch_action != NULL) delete this->m_switch_action; |
ansond | 47:fa96ddc36f04 | 74 | if (this->m_pl != NULL) delete this->m_pl; |
ansond | 105:759dee018363 | 75 | if (this->m_ext_led != NULL) delete this->m_ext_led; |
ansond | 91:8732d54328ae | 76 | this->stopBlinkingThread(); |
ansond | 0:4c9bfcb3e759 | 77 | } |
ansond | 0:4c9bfcb3e759 | 78 | |
ansond | 92:330746c526b7 | 79 | // initialize the light |
ansond | 92:330746c526b7 | 80 | void Light::initLight() { |
ansond | 142:b7a75144b0f1 | 81 | sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_current_state); |
ansond | 92:330746c526b7 | 82 | this->m_last_state = this->m_current_state; |
ansond | 92:330746c526b7 | 83 | } |
ansond | 92:330746c526b7 | 84 | |
ansond | 29:9a99f076129a | 85 | // get the Philips light |
ansond | 29:9a99f076129a | 86 | PhilipsLight *Light::pl() { return this->m_pl; } |
ansond | 29:9a99f076129a | 87 | |
ansond | 105:759dee018363 | 88 | // get the External LED light |
ansond | 105:759dee018363 | 89 | ExternalLEDLight *Light::extled() { return this->m_ext_led; } |
ansond | 105:759dee018363 | 90 | |
ansond | 9:90fadae5489a | 91 | // set the dimmer action |
ansond | 9:90fadae5489a | 92 | void Light::setDimmerAction(void *dimmer_action) { this->m_dimmer_action = dimmer_action; } |
ansond | 9:90fadae5489a | 93 | |
ansond | 9:90fadae5489a | 94 | // set the switch actino |
ansond | 9:90fadae5489a | 95 | void Light::setSwitchAction(void *switch_action) {this->m_switch_action = switch_action; } |
ansond | 9:90fadae5489a | 96 | |
ansond | 9:90fadae5489a | 97 | // get the dimmer action |
ansond | 9:90fadae5489a | 98 | void *Light::getDimmerAction() { return this->m_dimmer_action; } |
ansond | 9:90fadae5489a | 99 | |
ansond | 9:90fadae5489a | 100 | // get the switch action |
ansond | 9:90fadae5489a | 101 | void *Light::getSwitchAction() { return this->m_switch_action; } |
ansond | 134:58e7537a8c5f | 102 | |
ansond | 29:9a99f076129a | 103 | // turn ON |
ansond | 105:759dee018363 | 104 | void Light::on() { |
ansond | 105:759dee018363 | 105 | this->m_current_state = 1; |
ansond | 105:759dee018363 | 106 | this->manageBlinkingThread(); |
ansond | 105:759dee018363 | 107 | if (PL_ENABLE && this->pl() != NULL) this->pl()->on(); |
ansond | 105:759dee018363 | 108 | if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->on(); |
ansond | 105:759dee018363 | 109 | } |
ansond | 9:90fadae5489a | 110 | |
ansond | 29:9a99f076129a | 111 | // turn OFF |
ansond | 105:759dee018363 | 112 | void Light::off() { |
ansond | 105:759dee018363 | 113 | this->m_current_state = 0; |
ansond | 105:759dee018363 | 114 | this->manageBlinkingThread(); |
ansond | 105:759dee018363 | 115 | if (PL_ENABLE && this->pl() != NULL) this->pl()->off(); |
ansond | 105:759dee018363 | 116 | if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->off(); |
ansond | 105:759dee018363 | 117 | } |
ansond | 91:8732d54328ae | 118 | |
ansond | 91:8732d54328ae | 119 | // initiate blinking |
ansond | 91:8732d54328ae | 120 | void Light::blink() { |
ansond | 91:8732d54328ae | 121 | this->m_last_state = this->m_current_state; |
ansond | 91:8732d54328ae | 122 | this->startBlinkingThread(); |
ansond | 91:8732d54328ae | 123 | } |
ansond | 91:8732d54328ae | 124 | |
ansond | 91:8732d54328ae | 125 | // manage the blinking thread |
ansond | 91:8732d54328ae | 126 | void Light::manageBlinkingThread() { |
ansond | 91:8732d54328ae | 127 | if (this->m_is_blinking == false) this->stopBlinkingThread(); |
ansond | 91:8732d54328ae | 128 | this->m_is_blinking = false; |
ansond | 91:8732d54328ae | 129 | } |
ansond | 91:8732d54328ae | 130 | |
ansond | 91:8732d54328ae | 131 | // stop blinking |
ansond | 91:8732d54328ae | 132 | void Light::stopBlinking() { |
ansond | 91:8732d54328ae | 133 | this->m_is_blinking = false; |
ansond | 91:8732d54328ae | 134 | if (this->m_last_state == 1) this->on(); |
ansond | 91:8732d54328ae | 135 | if (this->m_last_state == 0) this->off(); |
ansond | 91:8732d54328ae | 136 | this->m_current_state = this->m_last_state; |
ansond | 91:8732d54328ae | 137 | } |
ansond | 91:8732d54328ae | 138 | |
ansond | 91:8732d54328ae | 139 | // start blinking thread |
ansond | 91:8732d54328ae | 140 | void Light::startBlinkingThread() { |
ansond | 160:24337c83dd1d | 141 | #ifdef ENABLE_THREADS |
ansond | 91:8732d54328ae | 142 | if (this->m_blinking_thread == NULL) |
ansond | 91:8732d54328ae | 143 | this->m_blinking_thread = new Thread(blinking_action); |
ansond | 160:24337c83dd1d | 144 | #endif |
ansond | 91:8732d54328ae | 145 | } |
ansond | 91:8732d54328ae | 146 | |
ansond | 91:8732d54328ae | 147 | // stop blinking thread |
ansond | 91:8732d54328ae | 148 | void Light::stopBlinkingThread() { |
ansond | 160:24337c83dd1d | 149 | #ifdef ENABLE_THREADS |
ansond | 91:8732d54328ae | 150 | if (this->m_blinking_thread != NULL) { |
ansond | 91:8732d54328ae | 151 | this->m_blinking_thread->terminate(); |
ansond | 91:8732d54328ae | 152 | delete this->m_blinking_thread; |
ansond | 91:8732d54328ae | 153 | } |
ansond | 91:8732d54328ae | 154 | this->m_blinking_thread = NULL; |
ansond | 160:24337c83dd1d | 155 | #endif |
ansond | 91:8732d54328ae | 156 | } |
ansond | 9:90fadae5489a | 157 | |
ansond | 101:8747b6612e32 | 158 | // update our blinking sequencing to properly mesh with the lights current state |
ansond | 101:8747b6612e32 | 159 | void Light::updateDirection() { |
ansond | 101:8747b6612e32 | 160 | this->m_forward = true; |
ansond | 101:8747b6612e32 | 161 | if (this->m_current_state == 0) this->m_forward = false; |
ansond | 101:8747b6612e32 | 162 | } |
ansond | 101:8747b6612e32 | 163 | |
ansond | 87:e9d77e9f9eae | 164 | // Blink |
ansond | 91:8732d54328ae | 165 | void Light::blinkLight() { |
ansond | 91:8732d54328ae | 166 | this->m_is_blinking = true; |
ansond | 146:8c05a119d9d4 | 167 | #ifdef COPCAR_PERSONALITY |
ansond | 146:8c05a119d9d4 | 168 | if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); } |
ansond | 146:8c05a119d9d4 | 169 | wait_ms(125); |
ansond | 146:8c05a119d9d4 | 170 | if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); } |
ansond | 146:8c05a119d9d4 | 171 | this->m_current_state = 0; |
ansond | 146:8c05a119d9d4 | 172 | this->manageBlinkingThread(); |
ansond | 146:8c05a119d9d4 | 173 | #endif |
ansond | 146:8c05a119d9d4 | 174 | #ifdef LIGHT_PERSONALITY |
ansond | 101:8747b6612e32 | 175 | if (this->m_forward) this->on(); else this->off(); |
ansond | 101:8747b6612e32 | 176 | if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->on(); else this->pl()->off(); } |
ansond | 105:759dee018363 | 177 | if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->on(); else this->extled()->off(); } |
ansond | 146:8c05a119d9d4 | 178 | #endif |
ansond | 98:3ea8058f4c54 | 179 | wait_ms(LIGHT_BLINK_WAIT_MS); |
ansond | 91:8732d54328ae | 180 | this->m_is_blinking = true; |
ansond | 146:8c05a119d9d4 | 181 | #ifdef COPCAR_PERSONALITY |
ansond | 146:8c05a119d9d4 | 182 | if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->on(); } |
ansond | 146:8c05a119d9d4 | 183 | wait_ms(125); |
ansond | 146:8c05a119d9d4 | 184 | if (EXT_LED_ENABLE && this->extled() != NULL) { ((CopCarLEDFlasher *)this->extled())->off(); } |
ansond | 146:8c05a119d9d4 | 185 | this->m_current_state = 0; |
ansond | 146:8c05a119d9d4 | 186 | this->manageBlinkingThread(); |
ansond | 146:8c05a119d9d4 | 187 | #endif |
ansond | 146:8c05a119d9d4 | 188 | #ifdef LIGHT_PERSONALITY |
ansond | 101:8747b6612e32 | 189 | if (this->m_forward) this->off(); else this->on(); |
ansond | 101:8747b6612e32 | 190 | if (PL_ENABLE && this->pl() != NULL) { if (this->m_forward) this->pl()->off(); else this->pl()->on(); } |
ansond | 105:759dee018363 | 191 | if (EXT_LED_ENABLE && this->extled() != NULL) { if (this->m_forward) this->extled()->off(); else this->extled()->on(); } |
ansond | 146:8c05a119d9d4 | 192 | #endif |
ansond | 98:3ea8058f4c54 | 193 | wait_ms(LIGHT_BLINK_WAIT_MS); |
ansond | 91:8732d54328ae | 194 | } |
ansond | 91:8732d54328ae | 195 | |
ansond | 29:9a99f076129a | 196 | // dim |
ansond | 105:759dee018363 | 197 | void Light::dim(int value) { |
ansond | 105:759dee018363 | 198 | if (PL_ENABLE && this->pl() != NULL) this->pl()->dim(value); |
ansond | 105:759dee018363 | 199 | if (EXT_LED_ENABLE && this->extled() != NULL) this->extled()->dim(value); |
ansond | 105:759dee018363 | 200 | } |
ansond | 9:90fadae5489a | 201 | |
ansond | 0:4c9bfcb3e759 | 202 | |
ansond | 0:4c9bfcb3e759 | 203 |