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
Child:
168:1dc1f4e6a6cd
added APMDemoLight

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