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
APMDemoLight.cpp@192:54b758a8eaaa, 2014-09-26 (annotated)
- Committer:
- ansond
- Date:
- Fri Sep 26 05:16:07 2014 +0000
- Revision:
- 192:54b758a8eaaa
- Parent:
- 181:969d4e005e44
updates to new logger name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 167:2529c18d0eb1 | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 167:2529c18d0eb1 | 2 | * |
ansond | 167:2529c18d0eb1 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 167:2529c18d0eb1 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 167:2529c18d0eb1 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 167:2529c18d0eb1 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 167:2529c18d0eb1 | 7 | * furnished to do so, subject to the following conditions: |
ansond | 167:2529c18d0eb1 | 8 | * |
ansond | 167:2529c18d0eb1 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 167:2529c18d0eb1 | 10 | * substantial portions of the Software. |
ansond | 167:2529c18d0eb1 | 11 | * |
ansond | 167:2529c18d0eb1 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 167:2529c18d0eb1 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 167:2529c18d0eb1 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 167:2529c18d0eb1 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 167:2529c18d0eb1 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 167:2529c18d0eb1 | 17 | */ |
ansond | 167:2529c18d0eb1 | 18 | |
ansond | 167:2529c18d0eb1 | 19 | #include "APMDemoLight.h" |
ansond | 167:2529c18d0eb1 | 20 | |
ansond | 167:2529c18d0eb1 | 21 | // Configuration settings for the APM Demo Light... |
ansond | 169:5ba15f5f7f87 | 22 | #ifdef APM_LIGHT_PERSONALITY |
ansond | 170:9e72f2d0f2b2 | 23 | |
ansond | 167:2529c18d0eb1 | 24 | // pin for ZeroCross tail input - NOTE: An external 1K pullup required |
ansond | 167:2529c18d0eb1 | 25 | InterruptIn apm_light_zerocross(p27); |
ansond | 167:2529c18d0eb1 | 26 | |
ansond | 167:2529c18d0eb1 | 27 | // pin for PowerSSRtail output |
ansond | 167:2529c18d0eb1 | 28 | DigitalOut apm_light_SSR(p21); |
ansond | 167:2529c18d0eb1 | 29 | |
ansond | 167:2529c18d0eb1 | 30 | //use timer interrupts to control dimming |
ansond | 167:2529c18d0eb1 | 31 | Timeout apm_light_SSRtriggerOn; |
ansond | 167:2529c18d0eb1 | 32 | |
ansond | 167:2529c18d0eb1 | 33 | // AC power line frequency |
ansond | 172:ca0acfacc5c4 | 34 | const float apm_light_powerlinefrequency = 50.000; |
ansond | 167:2529c18d0eb1 | 35 | |
ansond | 167:2529c18d0eb1 | 36 | // Our instance |
ansond | 167:2529c18d0eb1 | 37 | APMDemoLight *_apm_light_instance = NULL; |
ansond | 167:2529c18d0eb1 | 38 | |
ansond | 167:2529c18d0eb1 | 39 | // this interrupt routine is activated after a time delay set by dim value |
ansond | 168:1dc1f4e6a6cd | 40 | extern "C" void apm_light_triggerOn() |
ansond | 167:2529c18d0eb1 | 41 | { |
ansond | 167:2529c18d0eb1 | 42 | apm_light_SSR = 1; |
ansond | 167:2529c18d0eb1 | 43 | } |
ansond | 167:2529c18d0eb1 | 44 | |
ansond | 167:2529c18d0eb1 | 45 | // this interrupt routine is activated by every AC line zero crossing |
ansond | 167:2529c18d0eb1 | 46 | // it is needed to synchronize the SCR turnon time delay to the AC line |
ansond | 168:1dc1f4e6a6cd | 47 | extern "C" void apm_light_dimmer() |
ansond | 167:2529c18d0eb1 | 48 | { |
ansond | 167:2529c18d0eb1 | 49 | // turn off SSR at zero crossing |
ansond | 167:2529c18d0eb1 | 50 | apm_light_SSR = 0; |
ansond | 167:2529c18d0eb1 | 51 | |
ansond | 167:2529c18d0eb1 | 52 | // compute time delay using dim value and set timer interrupt |
ansond | 167:2529c18d0eb1 | 53 | // triggers SSR after a small post zero crossing time delay |
ansond | 168:1dc1f4e6a6cd | 54 | apm_light_SSRtriggerOn.attach(&apm_light_triggerOn,(1.001-_apm_light_instance->getDimValue())/(2*apm_light_powerlinefrequency)); |
ansond | 167:2529c18d0eb1 | 55 | } |
ansond | 167:2529c18d0eb1 | 56 | |
ansond | 167:2529c18d0eb1 | 57 | // default constructor |
ansond | 192:54b758a8eaaa | 58 | APMDemoLight::APMDemoLight(Logger *logger) : BaseClass(logger,NULL) { |
ansond | 167:2529c18d0eb1 | 59 | this->m_dimming = false; |
ansond | 167:2529c18d0eb1 | 60 | sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_state); |
ansond | 167:2529c18d0eb1 | 61 | sscanf(LIGHT_DIM_STATE,"%f",&this->m_dim); |
ansond | 167:2529c18d0eb1 | 62 | |
ansond | 167:2529c18d0eb1 | 63 | // establish our instance |
ansond | 167:2529c18d0eb1 | 64 | _apm_light_instance = this; |
ansond | 167:2529c18d0eb1 | 65 | |
ansond | 167:2529c18d0eb1 | 66 | // ZeroCross configuration |
ansond | 167:2529c18d0eb1 | 67 | apm_light_zerocross.mode(PullNone); |
ansond | 181:969d4e005e44 | 68 | Thread::wait(LIGHT_BLINK_WAIT_MS); |
ansond | 168:1dc1f4e6a6cd | 69 | apm_light_zerocross.rise(&apm_light_dimmer); |
ansond | 167:2529c18d0eb1 | 70 | } |
ansond | 167:2529c18d0eb1 | 71 | |
ansond | 167:2529c18d0eb1 | 72 | // destructor |
ansond | 167:2529c18d0eb1 | 73 | APMDemoLight::~APMDemoLight() { |
ansond | 167:2529c18d0eb1 | 74 | } |
ansond | 167:2529c18d0eb1 | 75 | |
ansond | 167:2529c18d0eb1 | 76 | // turn the light on |
ansond | 167:2529c18d0eb1 | 77 | void APMDemoLight::on() { this->m_state = 1; this->m_dimming = false; this->update(); } |
ansond | 167:2529c18d0eb1 | 78 | |
ansond | 167:2529c18d0eb1 | 79 | // turn the light off |
ansond | 167:2529c18d0eb1 | 80 | void APMDemoLight::off() { this->m_state = 0; this->m_dimming = false; this->update(); } |
ansond | 167:2529c18d0eb1 | 81 | |
ansond | 167:2529c18d0eb1 | 82 | // dim the light (dim is between 0 and 100) |
ansond | 167:2529c18d0eb1 | 83 | void APMDemoLight::dim(int dim) { this->m_dim = (float)(dim/100.0); this->m_dimming = true; this->update(); } |
ansond | 167:2529c18d0eb1 | 84 | |
ansond | 167:2529c18d0eb1 | 85 | // get the dimming value |
ansond | 167:2529c18d0eb1 | 86 | float APMDemoLight::getDimValue() { return this->m_dim; } |
ansond | 167:2529c18d0eb1 | 87 | |
ansond | 167:2529c18d0eb1 | 88 | // set the light state |
ansond | 170:9e72f2d0f2b2 | 89 | void APMDemoLight::update() { |
ansond | 170:9e72f2d0f2b2 | 90 | // handled automatically |
ansond | 170:9e72f2d0f2b2 | 91 | ; |
ansond | 169:5ba15f5f7f87 | 92 | } |
ansond | 169:5ba15f5f7f87 | 93 | |
ansond | 169:5ba15f5f7f87 | 94 | #endif |