Maxim Integrated / Mbed OS MAXREFDES220_HR_SPO2_MONITOR

Dependencies:   max32630fthr USBDevice

Fork of MAXREFDES220_HEART_RATE_MONITOR by Maxim Integrated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EventStats.cpp Source File

EventStats.cpp

00001 /***************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 ****************************************************************************
00032 */
00033 #ifdef ENABLE_EVENTSTATS
00034 #include "EventStats.h"
00035 #include "Peripherals.h"
00036 
00037 EventStats::EventStats() :
00038     single_evt(), all_evts()
00039 {
00040     name = "";
00041     report_period = 1000000;
00042     num_evts = 0;
00043     total_evt_time = 0;
00044 
00045     evt_in_prog = false;
00046 }
00047 
00048 EventStats::EventStats(int report_period, const char* name) :
00049     single_evt(), all_evts()
00050 {
00051     if (name)
00052         this->name = name;
00053     else
00054         this->name = "";
00055 
00056     this->report_period = report_period;
00057     num_evts = 0;
00058     total_evt_time = 0;
00059     evt_in_prog = false;
00060 
00061 }
00062 
00063 void EventStats::start()
00064 {
00065     if (evt_in_prog) {
00066         pr_debug("EventStats::start() called twice without calling EventStats::stop()!");
00067     }
00068 
00069 
00070     if (!num_evts)
00071         all_evts.start();
00072 
00073     single_evt.start();
00074     evt_in_prog = true;
00075 
00076 }
00077 
00078 void EventStats::stop()
00079 {
00080     if (!evt_in_prog) {
00081         pr_debug("EventStats::stop() called before EventStats::start()!");
00082     }
00083 
00084     single_evt.stop();
00085     num_evts++;
00086     evt_in_prog = false;
00087     total_evt_time += single_evt.read_us();
00088     single_evt.reset();
00089 
00090     int time_since_start = all_evts.read_us();
00091     if (time_since_start > report_period) {
00092         print_events(true);
00093     }
00094 }
00095 
00096 void EventStats::print_events(bool reset)
00097 {
00098     if (num_evts > 0) {
00099         int time_since_start = all_evts.read_us();
00100         int avg = total_evt_time / num_evts;
00101 
00102         pr_debug("[Event (%s) hit %d times in %dus. Avg: %dus, Total: %dus\r\n",
00103                 name,
00104                 num_evts,
00105                 time_since_start,
00106                 avg,
00107                 total_evt_time);
00108     } else {
00109         pr_debug("Event (%s) has not yet occurred\r\n", name);
00110     }
00111 
00112     if (reset) {
00113         num_evts = 0;
00114         all_evts.reset();
00115         total_evt_time = 0;
00116     }
00117 }
00118 
00119 #endif //ENABLE_EVENTSTATS