Maxim Integrated / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LEDStatus.cpp Source File

LEDStatus.cpp

00001 /*******************************************************************************
00002 * Author: Shaun Kelsey, shaun.kelsey@maximintegrated.com
00003 * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
00004 *
00005 * Permission is hereby granted, free of charge, to any person obtaining a
00006 * copy of this software and associated documentation files (the "Software"),
00007 * to deal in the Software without restriction, including without limitation
00008 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 * and/or sell copies of the Software, and to permit persons to whom the
00010 * Software is furnished to do so, subject to the following conditions:
00011 *
00012 * The above copyright notice and this permission notice shall be included
00013 * in all copies or substantial portions of the Software.
00014 *
00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00019 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00020 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00021 * OTHER DEALINGS IN THE SOFTWARE.
00022 *
00023 * Except as contained in this notice, the name of Maxim Integrated
00024 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00025 * Products, Inc. Branding Policy.
00026 *
00027 * The mere transfer of this software does not imply any licenses
00028 * of trade secrets, proprietary technology, copyrights, patents,
00029 * trademarks, maskwork rights, or any other form of intellectual
00030 * property whatsoever. Maxim Integrated Products, Inc. retains all
00031 * ownership rights.
00032 *******************************************************************************
00033 */
00034 
00035 #include "LEDStatus.h"
00036 #include "Peripherals.h"
00037 
00038 LEDStatus::LEDStatus(PinName red, int rstate, PinName green, int gstate, PinName blue, int bstate):
00039     rLED(red, rstate), gLED(green, gstate), bLED(blue, bstate), blinking(false), timer()
00040 {
00041 }
00042 
00043 void LEDStatus::set_state(int rstate, int gstate, int bstate)
00044 {
00045     r_act = rstate;
00046     g_act = gstate;
00047     b_act = bstate;
00048 
00049     if (!blinking) {
00050         rLED = r_act;
00051         gLED = g_act;
00052         bLED = b_act;
00053     }
00054 }
00055 
00056 void LEDStatus::solid()
00057 {
00058     blinking = false;
00059     rLED = r_act;
00060     gLED = g_act;
00061     bLED = b_act;
00062 
00063     timer.stop();
00064 }
00065 
00066 void LEDStatus::blink(int ontime, int offtime, int nb)
00067 {
00068     ont_us = 1000 * ontime;
00069     offt_us = 1000 * offtime;
00070     count = 1;
00071     total = nb;
00072     blinking = true;
00073 
00074     rLED = r_act;
00075     gLED = g_act;
00076     bLED = b_act;
00077     bs = true;
00078 
00079     timer.reset();
00080     timer.start();
00081 }
00082 
00083 void LEDStatus::blink(int period, float duty, int nb)
00084 {
00085     ont_us = 1000 * period * duty;
00086     offt_us = (1000 * period) - ont_us;
00087     count = 1;
00088     total = nb;
00089     blinking = true;
00090 
00091     rLED = r_act;
00092     gLED = g_act;
00093     bLED = b_act;
00094     bs = true;
00095 
00096     timer.reset();
00097 }
00098 
00099 bool LEDStatus::is_blinking()
00100 {
00101     return blinking;
00102 }
00103 
00104 void LEDStatus::update()
00105 {
00106     uint64_t time = timer.read_high_resolution_us();
00107     uint64_t frame_time = time % (ont_us + offt_us);
00108 
00109     if (bs && (frame_time > ont_us)) {
00110         bs = false;
00111         rLED = LED_OFF;
00112         gLED = LED_OFF;
00113         bLED = LED_OFF;
00114         count++;
00115 
00116     } else if (!bs && (frame_time <= ont_us)) {
00117         bs = true;
00118         rLED = r_act;
00119         gLED = g_act;
00120         bLED = b_act;
00121 
00122         if (total > 0 && count >= total) {
00123             blinking = false;
00124             timer.stop();
00125         }
00126     }
00127 }
00128