Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: nucleo_encoder_stepper mbed-os-rest-api-V1-1
CPU_Usage.cpp
00001 /****************************************************** 00002 CPU_Usage Library V1.0 Ian Weston 15.06.2014 00003 CPU_Usage Library V1.01 Ian Weston 08.01.2016 00004 00005 A very lightweight tool for calculating the CPU usage 00006 of the mbed micro. Based in the time domain, this will 00007 calculate the percentage of CPU time used. 00008 00009 Please see the project page for more details. 00010 00011 Enjoy! 00012 00013 ******************************************************/ 00014 #include "mbed.h" 00015 #include "CPU_Usage.h" 00016 00017 00018 // Constructor - time_ballast can be considered as a smoothing 00019 // number, representing time to reset all data. 1 seconds is 00020 // a good place to start. 00021 CPU_Usage::CPU_Usage(Timer &t, float time_ballast) { 00022 _t = &t; 00023 _active = 0; 00024 _inactive = 0; 00025 _time_ballast = time_ballast; 00026 } 00027 00028 // Use this function when isolating precise areas of code for measurement 00029 // place it at the start of the code of interest 00030 void CPU_Usage::working(void) { 00031 _t->stop(); 00032 _inactive += _t->read(); 00033 _t->reset(); 00034 _t->start(); 00035 } 00036 00037 // Use this function when isolating precise areas of code for measurement 00038 // place this at the end of the code thats of interest 00039 void CPU_Usage::stopped(void) { 00040 _t->stop(); 00041 _active += _t->read(); 00042 _t->reset(); 00043 _t->start(); 00044 } 00045 00046 00047 // call this to return the usage value. returns the value as a percentage 00048 uint8_t CPU_Usage::update(void) { 00049 float total = _active + _inactive; 00050 float percentile = 100 / total; 00051 uint8_t usage = _active * percentile; 00052 00053 if (total > _time_ballast) { 00054 _active = 0; 00055 _inactive = 0; 00056 } 00057 00058 return usage; 00059 } 00060 00061 00062 // use this function instead of wait() to exclude CPU wait processing from the value. 00063 void CPU_Usage::delay(float duration) { 00064 this->stopped(); 00065 wait(duration); 00066 this->working(); 00067 } 00068 00069 // Destructor... empty... as you can see... 00070 CPU_Usage::~CPU_Usage(void) { 00071 00072 } 00073 00074 00075 00076 00077
Generated on Thu Jul 14 2022 06:11:45 by
1.7.2