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

APMDemoLight.cpp

Committer:
ansond
Date:
2014-07-17
Revision:
168:1dc1f4e6a6cd
Parent:
167:2529c18d0eb1
Child:
169:5ba15f5f7f87

File content as of revision 168:1dc1f4e6a6cd:

/* Copyright C2013 Doug Anson, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "APMDemoLight.h"
 
 // Configuration settings for the APM Demo Light...
 
 PwmOut apm_light_led(LED1);          // led 1 indicates dim value
 DigitalOut apm_light_led2(LED2);     // led 2 indicates delay time for interrupts
 
 // pin for ZeroCross tail input - NOTE: An external 1K pullup required
 InterruptIn apm_light_zerocross(p27);
 
 // pin for PowerSSRtail output
 DigitalOut apm_light_SSR(p21);
 
 //use timer interrupts to control dimming
 Timeout apm_light_SSRtriggerOn;
  
 // AC power line frequency
 const float apm_light_powerlinefrequency = 60.000;
 
 // Our instance
 APMDemoLight *_apm_light_instance = NULL;
 
 // this interrupt routine is activated after a time delay set by dim value
 extern "C" void apm_light_triggerOn()
 {
    apm_light_SSR = 1;
    apm_light_led2 = 0;
 }
 
 // this interrupt routine is activated by every AC line zero crossing
 // it is needed to synchronize the SCR turnon time delay to the AC line
 extern "C" void apm_light_dimmer()
 {
    // turn off SSR at zero crossing
    apm_light_SSR = 0;
    
    // compute time delay using dim value and set timer interrupt
    // triggers SSR after a small post zero crossing time delay
    apm_light_SSRtriggerOn.attach(&apm_light_triggerOn,(1.001-_apm_light_instance->getDimValue())/(2*apm_light_powerlinefrequency));
    apm_light_led2 = 1;
 }
   
 // default constructor
 APMDemoLight::APMDemoLight(ErrorHandler *error_handler) : BaseClass(error_handler,NULL) {
     this->m_led = &apm_light_led;
     this->m_dimming = false;
     sscanf(LIGHT_DEFAULT_STATE,"%d",&this->m_state);
     sscanf(LIGHT_DIM_STATE,"%f",&this->m_dim);
     
     // establish our instance
     _apm_light_instance = this;
     
     // ZeroCross configuration
     apm_light_zerocross.mode(PullNone);
     wait_ms(LIGHT_BLINK_WAIT_MS);
     apm_light_zerocross.rise(&apm_light_dimmer);
 }
 
 // destructor
 APMDemoLight::~APMDemoLight() {
 }
 
 // turn the light on
 void APMDemoLight::on() { this->m_state = 1; this->m_dimming = false; this->update(); }
 
 // turn the light off
 void APMDemoLight::off() { this->m_state = 0; this->m_dimming = false; this->update(); }
  
 // dim the light (dim is between 0 and 100)
 void APMDemoLight::dim(int dim) { this->m_dim = (float)(dim/100.0); this->m_dimming = true; this->update(); }
 
 // get the dimming value
 float APMDemoLight::getDimValue() { return this->m_dim; }
  
 // set the light state
 void APMDemoLight::update() {
    if (this->m_dimming) {
        // dim the light
        *(this->m_led) = this->m_dim;
    }
    else {
        // turn the light on or off
        if (this->m_state == 0) {
            // just turn the LED off
            *(this->m_led) = 0;
        }
        else {
            // turn the LED on - but re-adjust for the current dim setting:
            //  - if the dimming is set to 0, then lets go ahead and make the light full bright...
            //  - otherwise we wont see the transition change. 
            if (this->m_dim == 0.0) {
                *(this->m_led) = 1;
            }
            else {
                *(this->m_led) = this->m_dim;
            }
        }
    }
 }