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.
Dependencies: Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed
Inputs.h
00001 #ifndef INPUTS_H 00002 #define INPUTS_H 00003 00004 #include "SensorModel.h" 00005 #include "SensorSuite.h" 00006 00007 #define SCROLLBUTTON 1 00008 #define ENTERBUTTON 2 00009 #define BACKBUTTON 3 00010 00011 InterruptIn enterButton(p21); 00012 InterruptIn scrollButton(p22); 00013 InterruptIn backButton(p23); 00014 Timer debounce; 00015 Ticker timerRefresh; 00016 00017 AnalogIn ain(p20); 00018 //float ain = 0.5; 00019 //float ain1 = 0.5; 00020 //float ain2 = 0.5; 00021 AnalogIn ain1(p19); 00022 AnalogIn ain2(p18); 00023 00024 int getBatteryPercentage() 00025 { 00026 00027 // Define SensorSuite class to access the data 00028 SensorSuite suite; 00029 // create sensor model to get battery data 00030 BatteryModel model; 00031 model = suite.getBatteryData(); 00032 00033 float percentage1 = ((model.batteryOneVoltage-3.0)/1.2)*100; 00034 00035 float percentage2 = ((model.batteryTwoVoltage-3.0)/1.2)*100; 00036 00037 // because battery one has 6600mAH and battery two has 4400mAH 00038 int percentage = percentage1*0.6+percentage2*0.4; 00039 00040 return percentage; 00041 } 00042 00043 int getBatteryOnePercentage() 00044 { 00045 // Define SensorSuite class to access the data 00046 SensorSuite suite; 00047 // create sensor model to get battery data 00048 BatteryModel model; 00049 model = suite.getBatteryData(); 00050 00051 int percentage = ((model.batteryOneVoltage-3.0)/1.2)*100; 00052 return percentage; 00053 } 00054 00055 int getBatteryTwoPercentage() 00056 { 00057 // Define SensorSuite class to access the data 00058 SensorSuite suite; 00059 // create sensor model to get battery data 00060 BatteryModel model; 00061 model = suite.getBatteryData(); 00062 00063 int percentage = ((model.batteryTwoVoltage-3.0)/1.2)*100; 00064 00065 return percentage; 00066 } 00067 00068 int getBatteryStatus() 00069 { 00070 00071 /////// remember to use an integer //////// 00072 // 0 - none of the batteries are charging 00073 // 1 - battery one is charging 00074 // 2 - battery two is charging 00075 // 3 - all batteries are charging 00076 00077 00078 int batteryCharging; 00079 00080 // Define SensorSuite class to access the data 00081 SensorSuite suite; 00082 // create sensor model to get battery data 00083 BatteryModel model; 00084 model = suite.getBatteryData(); 00085 00086 // determine the batteries' status 00087 if(model.batteryOneCurrent > -0.5 && model.batteryTwoCurrent > -0.5) { 00088 // none of the batteries are charging 00089 batteryCharging = 0; 00090 } else if ( model.batteryOneCurrent < -0.5 && model.batteryTwoCurrent > -0.5) { 00091 // only battery one is charging 00092 batteryCharging = 1; 00093 } else if (model.batteryOneCurrent > -0.5 && model.batteryTwoCurrent < -0.5) { 00094 // only battery two is charging 00095 batteryCharging = 2; 00096 } else if (model.batteryOneCurrent < -0.5 && model.batteryTwoCurrent < -0.5) { 00097 // both batteries are charging 00098 batteryCharging = 3; 00099 } 00100 00101 // return the battery status 00102 return batteryCharging; 00103 } 00104 00105 00106 00107 bool getOutputStatus() 00108 { 00109 00110 bool outputStatus; 00111 00112 // Define SensorSuite class to access the data 00113 SensorSuite suite; 00114 // create sensor model to get consumption data 00115 ConsumptionModel model = suite.getConsumptionData(); 00116 00117 // read the current 00118 if (model.consumptionCurrent > 20) { 00119 outputStatus = true; 00120 } else { 00121 outputStatus = false; 00122 } 00123 00124 // return the output status 00125 return outputStatus; 00126 } 00127 00128 bool getSolarStatus() 00129 { 00130 00131 bool radiationReceived; 00132 00133 // Define SensorSuite class to access the data 00134 SensorSuite suite; 00135 // create sensor model to get battery data 00136 SolarModel model = suite.getSolarData(); 00137 00138 // read the potentiometer 00139 if (model.solarCurrent > 0.5) { 00140 radiationReceived = true; 00141 } else { 00142 radiationReceived = false; 00143 } 00144 00145 // return the solar panel status 00146 return radiationReceived; 00147 } 00148 00149 float getSolarPower() 00150 { 00151 // Define SensorSuite class to access the data 00152 SensorSuite suite; 00153 // create sensor model to get battery data 00154 SolarModel model = suite.getSolarData(); 00155 00156 // return the power 00157 if (model.solarCurrent > 0.5) { 00158 return model.solarCurrent*model.solarVoltage*0.001; 00159 } else { 00160 return 0.0; 00161 } 00162 } 00163 00164 float getConsumptionPower() 00165 { 00166 // Define SensorSuite class to access the data 00167 SensorSuite suite; 00168 // create sensor model to get battery data 00169 ConsumptionModel model = suite.getConsumptionData(); 00170 00171 // return the power 00172 if (model.consumptionCurrent > 0.5) { 00173 return model.consumptionCurrent*model.consumptionVoltage*0.001; 00174 } else { 00175 return 0.0; 00176 } 00177 } 00178 00179 template < class T > 00180 class Button 00181 { 00182 00183 public: 00184 00185 //virtual void buttonPressed(int button) = 0; 00186 00187 void activateButtons(T *ptr) { 00188 00189 var = ptr; 00190 00191 enterButton.rise(callback(this,&Button::enterButtonPressed)); 00192 scrollButton.rise(callback(this,&Button::scrollButtonPressed)); 00193 backButton.rise(callback(this,&Button::backButtonPressed)); 00194 00195 debounceRefresher(); 00196 startDebouncer(); 00197 00198 } 00199 00200 void deActivateButtons() { 00201 00202 enterButton.rise(NULL); 00203 scrollButton.rise(NULL); 00204 backButton.rise(NULL); 00205 } 00206 00207 00208 void scrollButtonPressed() { 00209 00210 if(debounceSuccessful()) { 00211 // toggle led 00212 var->buttonPressed(SCROLLBUTTON); 00213 resetDebouncer(); 00214 } 00215 00216 } 00217 00218 void enterButtonPressed() { 00219 if(debounceSuccessful()) { 00220 // toggle led 00221 var->buttonPressed(ENTERBUTTON); 00222 resetDebouncer(); 00223 } 00224 } 00225 00226 void backButtonPressed() { 00227 if(debounceSuccessful()) { 00228 // toggle led 00229 var->buttonPressed(BACKBUTTON); 00230 resetDebouncer(); 00231 } 00232 } 00233 00234 void startDebouncer() { 00235 // start the debounce timer 00236 debounce.start(); 00237 } 00238 00239 void resetDebouncer() { 00240 // reset the dobounce timer 00241 debounce.reset(); 00242 } 00243 00244 void debounceRefresher() { 00245 // endsuer the debounce timer is 00246 // reset every 20 minutes to 00247 // prevent ghosting/memory loss 00248 timerRefresh.attach(callback(this,&Button::resetDebouncer),1200.0); 00249 } 00250 00251 bool debounceSuccessful() { 00252 00253 if (debounce.read_ms() > 200) { // check if the debounce timer is more than 200ms 00254 00255 return true; 00256 } else { 00257 return false; 00258 } 00259 00260 } 00261 00262 // void pointer 00263 public: 00264 T *var; 00265 00266 00267 }; 00268 00269 #endif
Generated on Wed Jul 13 2022 02:31:29 by
