Qubit 2020 / presensfirmwareupdate

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers light.cpp Source File

light.cpp

00001 #include "mbed.h"
00002 #include "light.h"
00003 #include "debug.h"
00004 
00005 // PWM out to handle light
00006 static PwmOut lightPwm(p24);
00007 
00008 // Light PWM period is 10 ms changed to 1 ms for use with new LED driver Aug. 2018
00009 #define LIGHT_PWM_PERIOD_US 1000
00010 
00011 // 100% duty cycle is used to disable light changed to 0% equals off for new LED driver Aug. 2018
00012 #define LIGHT_OFF_DUTY_CYCLE 0.0f
00013 
00014 void lightInit(void)
00015 {
00016     // Turn light off
00017     // Don't need to set period as 100% duty cycle generates
00018     // the same signal for all periods
00019     lightPwm.write(LIGHT_OFF_DUTY_CYCLE);
00020 }
00021 
00022 void lightSet(unsigned char intensity)
00023 {
00024     if (intensity > LIGHT_MAX_INTENSITY) {
00025         // Don't support intensity more than 100%
00026         ERROR("Request to set too big light intensity %u", intensity);
00027         return;
00028     }
00029 
00030 //    if (lightRead() == intensity) {
00031 //        INFO("Discard request to set light intensity, intensity %u already applied", intensity);
00032 //        return;
00033 //    }
00034 
00035     if (intensity != LIGHT_OFF_INTENSITY) {
00036         lightPwm.period_us(LIGHT_PWM_PERIOD_US);
00037     }
00038 
00039     // Duty cycle 0% turns light off, 100% - full brightness changed Aug. 2018 for new LED driver
00040     float dutyCycle = LIGHT_OFF_DUTY_CYCLE + (float)intensity / LIGHT_MAX_INTENSITY;
00041     lightPwm.write(dutyCycle);
00042     
00043     DEBUG1("Light intensity %u was successfully applied.", intensity);
00044 }
00045 
00046 unsigned char lightRead(void)
00047 {
00048     float dutyCycle = lightPwm.read();
00049     unsigned char intensity = (LIGHT_OFF_DUTY_CYCLE + dutyCycle) * LIGHT_MAX_INTENSITY;
00050     return intensity;
00051 }
00052 
00053 bool lightOn(void)
00054 {
00055     return (lightRead() > LIGHT_OFF_INTENSITY);
00056 }
00057