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
BatteryIcon.h
00001 #ifndef BATTERYICON_H 00002 #define BATTERYICON_H 00003 00004 #include "mbed.h" 00005 00006 class BatteryIcon 00007 { 00008 public: 00009 //BatteryIcon(); 00010 BatteryIcon(uint8_t xCoordinate, uint8_t yCoordinate, uint16_t backgroundColor, int batteryPercentage, bool chargeStatus); 00011 void setBatteryPercentage(int batteryPercentage, bool chargeStatus); 00012 void drawBatteryIcon(); 00013 void removeBatteryIcon(); 00014 00015 private: 00016 uint8_t xCoord; 00017 uint8_t yCoord; 00018 uint16_t bgColor; 00019 int previousPercentage; 00020 int percentage; 00021 int batteryLevel; 00022 int previousBatteryLevel; 00023 int previousBatteryColor; 00024 bool chargeStat; 00025 bool thunderboltShowing; 00026 bool battInitialised; 00027 uint16_t deleteColor; 00028 uint16_t textColor; 00029 00030 00031 uint16_t determineBatteryColor(); 00032 void drawBatteryOutline(); 00033 void displayPercentage(int pc, uint16_t tc); 00034 void fillBattery(); 00035 void drawThunderbolt(); 00036 void removeThunderbolt(); 00037 00038 }; 00039 00040 /** 00041 BatteryIcon::BatteryIcon(){ 00042 00043 } 00044 **/ 00045 00046 BatteryIcon::BatteryIcon(uint8_t xCoordinate, uint8_t yCoordinate, uint16_t backgroundColor, int batteryPercentage, bool chargeStatus) 00047 : previousPercentage(-1),previousBatteryLevel(0), previousBatteryColor(0),thunderboltShowing(false) 00048 { 00049 // make all necessary initialisations 00050 xCoord = xCoordinate; 00051 yCoord = yCoordinate; 00052 bgColor = backgroundColor; 00053 chargeStat = chargeStatus; 00054 percentage = batteryPercentage; 00055 00056 // determine the color to delete stuff 00057 deleteColor = bgColor; 00058 00059 // determine the color of text 00060 if (bgColor == ST7735_WHITE ) { 00061 textColor = ST7735_BLACK; 00062 } else if (bgColor == ST7735_BLACK) { 00063 textColor = ST7735_WHITE; 00064 } else if (bgColor == ST7735_BLUE) { 00065 textColor = ST7735_WHITE; 00066 } 00067 } 00068 00069 void BatteryIcon::drawBatteryIcon() 00070 { 00071 00072 // draw the outline first 00073 drawBatteryOutline(); 00074 00075 // dispaly the battery Percentage 00076 setBatteryPercentage(percentage,chargeStat); 00077 00078 } 00079 00080 void BatteryIcon::removeBatteryIcon() 00081 { 00082 00083 } 00084 00085 void BatteryIcon::setBatteryPercentage(int percentage, bool chargeStatus) 00086 { 00087 00088 // calculate batter level; 00089 float batteryLevelFloat = (percentage*32.0)/100; 00090 batteryLevel = batteryLevelFloat; 00091 00092 // write the battery percentage 00093 if (previousPercentage == -1) { 00094 00095 // write a new percentage 00096 displayPercentage(percentage, textColor); 00097 00098 } else if (percentage == previousPercentage) { 00099 // do nothing 00100 } else { 00101 00102 // first delete the current percentage 00103 displayPercentage(previousPercentage, bgColor); 00104 00105 // write a new percentage 00106 displayPercentage(percentage, textColor); 00107 } 00108 00109 00110 00111 // fill the battery 00112 fillBattery(); 00113 00114 00115 00116 // draw Thunderbolt at the end 00117 00118 // first check if battery is charging 00119 if (chargeStatus) { 00120 00121 // check if thunderbolt is not displayed 00122 if (!thunderboltShowing) { 00123 // draw the thundebolt 00124 drawThunderbolt(); 00125 } else { 00126 // do nothing 00127 } 00128 } else { // battery is not charging 00129 00130 // check if thunderbolt is showing 00131 if (thunderboltShowing) { 00132 // remove the thunderbolt 00133 removeThunderbolt(); 00134 } else { 00135 // do nothing 00136 } 00137 00138 } 00139 00140 // record the previous data 00141 previousPercentage = percentage; 00142 previousBatteryLevel = batteryLevel; 00143 00144 00145 00146 } 00147 00148 void BatteryIcon::displayPercentage(int pc,uint16_t tc ) 00149 { 00150 // dispalay the battery percentage 00151 tft.setCursor(xCoord+28, yCoord+20); 00152 tft.setTextColor(tc); 00153 tft.setTextSize(2); 00154 tft.printf("%ld%%",pc); 00155 tft.setTextSize(1); 00156 } 00157 00158 void BatteryIcon::drawThunderbolt() 00159 { 00160 00161 int16_t height = yCoord; 00162 int16_t width = xCoord + 40; 00163 00164 // draw first diagonal lines 00165 for (int n = 0; n < 3; n++ ) { 00166 tft.drawLine(width+n+4, height+1,width+n+1,height+7,ST7735_YELLOW); 00167 } 00168 // draw horizontal lines 00169 for (int n = 0; n < 2; n++) { 00170 tft.drawFastHLine(width+3,height+6+n,4,ST7735_YELLOW); 00171 } 00172 // draw second horizontal lines 00173 for (int n = 0; n < 3; n++ ) { 00174 tft.drawLine(width+n+6, height+6,width+3+n, height+11,ST7735_YELLOW); 00175 } 00176 // draw filled triangle 00177 tft.fillTriangle(width, height+12,width+8, height+12,width+4, height+16,ST7735_YELLOW); 00178 00179 thunderboltShowing = true; 00180 } 00181 00182 void BatteryIcon::removeThunderbolt() 00183 { 00184 00185 // here the thunderbolt lines are replaced by the background color, hence 00186 // making the thunderbolt disappear 00187 00188 int16_t height = yCoord; 00189 int16_t width = xCoord + 40; 00190 00191 // draw first diagonal lines 00192 for (int n = 0; n < 3; n++ ) { 00193 tft.drawLine(width+n+4, height+1,width+n+1,height+7,bgColor); 00194 } 00195 // draw horizontal lines 00196 for (int n = 0; n < 2; n++) { 00197 tft.drawFastHLine(width+3,height+6+n,4,bgColor); 00198 } 00199 // draw second horizontal lines 00200 for (int n = 0; n < 3; n++ ) { 00201 tft.drawLine(width+n+6, height+6,width+3+n, height+11,bgColor); 00202 } 00203 // draw filled triangle 00204 tft.fillTriangle(width, height+12,width+8, height+12,width+4, height+16,bgColor); 00205 00206 thunderboltShowing = false; 00207 } 00208 00209 void BatteryIcon::fillBattery() 00210 { 00211 00212 if ( batteryLevel > previousBatteryLevel ) { 00213 00214 // first determine the color of lines 00215 uint16_t theColor = determineBatteryColor(); 00216 00217 // increase the number of lines 00218 for (int y = previousBatteryLevel; y < batteryLevel; y++) { 00219 tft.drawFastHLine(xCoord + 1, yCoord + 38-y,23,theColor); 00220 } 00221 } else if (batteryLevel < previousBatteryLevel) { 00222 00223 // decrease the number of lines 00224 for (int y = previousBatteryLevel; y > batteryLevel; y--) { 00225 tft.drawFastHLine(xCoord + 1, yCoord + 38-y,23,bgColor); 00226 } 00227 00228 // first determine the color of lines 00229 uint16_t theColor = determineBatteryColor(); 00230 00231 } else { 00232 // do nothing 00233 } 00234 00235 } 00236 00237 uint16_t BatteryIcon::determineBatteryColor() 00238 { 00239 00240 uint16_t color; 00241 00242 if (batteryLevel >= 21) { 00243 00244 //first check if its already green 00245 if (previousBatteryColor != 1) { 00246 00247 // make the colors green 00248 for (int y = 0; y <= batteryLevel; y++) { 00249 tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_GREEN); 00250 } 00251 00252 previousBatteryColor = 1; 00253 } 00254 00255 // update the new battery color 00256 color = ST7735_GREEN; 00257 00258 } else if (batteryLevel < 21 && batteryLevel >= 7 ) { 00259 00260 //first check if its already yellow 00261 if (previousBatteryColor != 2) { 00262 00263 // make the colors yellow 00264 for (int y = 0; y <= batteryLevel; y++) { 00265 tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_YELLOW); 00266 } 00267 00268 previousBatteryColor = 2; 00269 } 00270 00271 // update the new battery color 00272 color = ST7735_YELLOW; 00273 } else { 00274 //first check if its already red 00275 if (previousBatteryColor != 3) { 00276 00277 // make the colors red 00278 for (int y = 0; y <= batteryLevel; y++) { 00279 tft.drawFastHLine(xCoord + 1,yCoord + 38-y,23, ST7735_RED); 00280 } 00281 00282 00283 previousBatteryColor = 3; 00284 } 00285 00286 // update the new battery color 00287 color = ST7735_RED; 00288 } 00289 00290 return color; 00291 00292 } 00293 00294 void BatteryIcon::drawBatteryOutline() 00295 { 00296 // small rectangle 00297 tft.drawRect( xCoord + 8, yCoord,10,6,ST7735_GREEN); 00298 00299 // big rectangle 00300 tft.drawRect( xCoord , yCoord + 5,25,35,ST7735_GREEN); 00301 } 00302 00303 #endif
Generated on Wed Jul 13 2022 02:31:29 by
