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:
168:1dc1f4e6a6cd
Child:
170:9e72f2d0f2b2
more fixes for tuning APM light

Who changed what in which revision?

UserRevisionLine numberNew 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 167:2529c18d0eb1 23
ansond 167:2529c18d0eb1 24 PwmOut apm_light_led(LED1); // led 1 indicates dim value
ansond 167:2529c18d0eb1 25 DigitalOut apm_light_led2(LED2); // led 2 indicates delay time for interrupts
ansond 167:2529c18d0eb1 26
ansond 167:2529c18d0eb1 27 // pin for ZeroCross tail input - NOTE: An external 1K pullup required
ansond 167:2529c18d0eb1 28 InterruptIn apm_light_zerocross(p27);
ansond 167:2529c18d0eb1 29
ansond 167:2529c18d0eb1 30 // pin for PowerSSRtail output
ansond 167:2529c18d0eb1 31 DigitalOut apm_light_SSR(p21);
ansond 167:2529c18d0eb1 32
ansond 167:2529c18d0eb1 33 //use timer interrupts to control dimming
ansond 167:2529c18d0eb1 34 Timeout apm_light_SSRtriggerOn;
ansond 167:2529c18d0eb1 35
ansond 167:2529c18d0eb1 36 // AC power line frequency
ansond 167:2529c18d0eb1 37 const float apm_light_powerlinefrequency = 60.000;
ansond 167:2529c18d0eb1 38
ansond 167:2529c18d0eb1 39 // Our instance
ansond 167:2529c18d0eb1 40 APMDemoLight *_apm_light_instance = NULL;
ansond 167:2529c18d0eb1 41
ansond 167:2529c18d0eb1 42 // this interrupt routine is activated after a time delay set by dim value
ansond 168:1dc1f4e6a6cd 43 extern "C" void apm_light_triggerOn()
ansond 167:2529c18d0eb1 44 {
ansond 167:2529c18d0eb1 45 apm_light_SSR = 1;
ansond 167:2529c18d0eb1 46 apm_light_led2 = 0;
ansond 167:2529c18d0eb1 47 }
ansond 167:2529c18d0eb1 48
ansond 167:2529c18d0eb1 49 // this interrupt routine is activated by every AC line zero crossing
ansond 167:2529c18d0eb1 50 // it is needed to synchronize the SCR turnon time delay to the AC line
ansond 168:1dc1f4e6a6cd 51 extern "C" void apm_light_dimmer()
ansond 167:2529c18d0eb1 52 {
ansond 167:2529c18d0eb1 53 // turn off SSR at zero crossing
ansond 167:2529c18d0eb1 54 apm_light_SSR = 0;
ansond 167:2529c18d0eb1 55
ansond 167:2529c18d0eb1 56 // compute time delay using dim value and set timer interrupt
ansond 167:2529c18d0eb1 57 // triggers SSR after a small post zero crossing time delay
ansond 168:1dc1f4e6a6cd 58 apm_light_SSRtriggerOn.attach(&apm_light_triggerOn,(1.001-_apm_light_instance->getDimValue())/(2*apm_light_powerlinefrequency));
ansond 167:2529c18d0eb1 59 apm_light_led2 = 1;
ansond 167:2529c18d0eb1 60 }
ansond 167:2529c18d0eb1 61
ansond 167:2529c18d0eb1 62 // default constructor
ansond 167:2529c18d0eb1 63 APMDemoLight::APMDemoLight(ErrorHandler *error_handler) : BaseClass(error_handler,NULL) {
ansond 167:2529c18d0eb1 64 this->m_led = &apm_light_led;
ansond 167:2529c18d0eb1 65 this->m_dimming = false;
ansond 167:2529c18d0eb1 66 sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_state);
ansond 167:2529c18d0eb1 67 sscanf(LIGHT_DIM_STATE,"%f",&this->m_dim);
ansond 167:2529c18d0eb1 68
ansond 167:2529c18d0eb1 69 // establish our instance
ansond 167:2529c18d0eb1 70 _apm_light_instance = this;
ansond 167:2529c18d0eb1 71
ansond 167:2529c18d0eb1 72 // ZeroCross configuration
ansond 167:2529c18d0eb1 73 apm_light_zerocross.mode(PullNone);
ansond 167:2529c18d0eb1 74 wait_ms(LIGHT_BLINK_WAIT_MS);
ansond 168:1dc1f4e6a6cd 75 apm_light_zerocross.rise(&apm_light_dimmer);
ansond 167:2529c18d0eb1 76 }
ansond 167:2529c18d0eb1 77
ansond 167:2529c18d0eb1 78 // destructor
ansond 167:2529c18d0eb1 79 APMDemoLight::~APMDemoLight() {
ansond 167:2529c18d0eb1 80 }
ansond 167:2529c18d0eb1 81
ansond 167:2529c18d0eb1 82 // turn the light on
ansond 167:2529c18d0eb1 83 void APMDemoLight::on() { this->m_state = 1; this->m_dimming = false; this->update(); }
ansond 167:2529c18d0eb1 84
ansond 167:2529c18d0eb1 85 // turn the light off
ansond 167:2529c18d0eb1 86 void APMDemoLight::off() { this->m_state = 0; this->m_dimming = false; this->update(); }
ansond 167:2529c18d0eb1 87
ansond 167:2529c18d0eb1 88 // dim the light (dim is between 0 and 100)
ansond 167:2529c18d0eb1 89 void APMDemoLight::dim(int dim) { this->m_dim = (float)(dim/100.0); this->m_dimming = true; this->update(); }
ansond 167:2529c18d0eb1 90
ansond 167:2529c18d0eb1 91 // get the dimming value
ansond 167:2529c18d0eb1 92 float APMDemoLight::getDimValue() { return this->m_dim; }
ansond 167:2529c18d0eb1 93
ansond 167:2529c18d0eb1 94 // set the light state
ansond 167:2529c18d0eb1 95 void APMDemoLight::update() {
ansond 167:2529c18d0eb1 96 if (this->m_dimming) {
ansond 167:2529c18d0eb1 97 // dim the light
ansond 167:2529c18d0eb1 98 *(this->m_led) = this->m_dim;
ansond 167:2529c18d0eb1 99 }
ansond 167:2529c18d0eb1 100 else {
ansond 167:2529c18d0eb1 101 // turn the light on or off
ansond 167:2529c18d0eb1 102 if (this->m_state == 0) {
ansond 167:2529c18d0eb1 103 // just turn the LED off
ansond 167:2529c18d0eb1 104 *(this->m_led) = 0;
ansond 167:2529c18d0eb1 105 }
ansond 167:2529c18d0eb1 106 else {
ansond 167:2529c18d0eb1 107 // turn the LED on - but re-adjust for the current dim setting:
ansond 167:2529c18d0eb1 108 // - if the dimming is set to 0, then lets go ahead and make the light full bright...
ansond 167:2529c18d0eb1 109 // - otherwise we wont see the transition change.
ansond 167:2529c18d0eb1 110 if (this->m_dim == 0.0) {
ansond 167:2529c18d0eb1 111 *(this->m_led) = 1;
ansond 167:2529c18d0eb1 112 }
ansond 167:2529c18d0eb1 113 else {
ansond 167:2529c18d0eb1 114 *(this->m_led) = this->m_dim;
ansond 167:2529c18d0eb1 115 }
ansond 167:2529c18d0eb1 116 }
ansond 167:2529c18d0eb1 117 }
ansond 169:5ba15f5f7f87 118 }
ansond 169:5ba15f5f7f87 119
ansond 169:5ba15f5f7f87 120 #endif