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
SolarScreen.h
00001 #ifndef SOLARSCREEN_H 00002 #define SOLARSCREEN_H 00003 00004 #include "ListController.h" 00005 00006 class SolarScreen: public Button <SolarScreen> 00007 { 00008 00009 public: 00010 SolarScreen(uint16_t backgroundColor); 00011 int start(); 00012 void buttonPressed(int button); 00013 00014 private: 00015 00016 void drawSolarScreen(); 00017 void drawTile(); 00018 void updateTile(); 00019 //void deAllocateMemory(); 00020 void drawWattage(float wattage, uint16_t color); 00021 00022 int changeScreen; 00023 float previousWattage; 00024 00025 ListController *list; // manages the list 00026 bool scroll; // updated when a scroll event occurs 00027 bool enter; // updated when an enter event occurs 00028 uint16_t bgColor; // background color 00029 00030 00031 00032 }; 00033 00034 00035 SolarScreen::SolarScreen(uint16_t backgroundColor) 00036 { 00037 00038 bgColor = backgroundColor; 00039 } 00040 00041 int SolarScreen::start(){ 00042 00043 previousWattage = -1; 00044 00045 // attach interrupts to buttons 00046 Button<SolarScreen>::activateButtons(this); 00047 00048 // draw the screen 00049 drawSolarScreen(); 00050 00051 00052 changeScreen = 0; 00053 scroll = false; 00054 enter = false; 00055 00056 00057 00058 while (changeScreen == 0){ 00059 updateTile(); 00060 00061 // scroll event occured 00062 if (scroll) { 00063 // perform scroll on the list 00064 list->scroll(); 00065 // reset variable 00066 scroll = false; 00067 } 00068 00069 // enter event occured 00070 if (enter) { 00071 // read the current option on the list 00072 changeScreen = list->getCurrentOption(); 00073 } 00074 //wait_ms(500); 00075 Thread::wait(200); 00076 } 00077 00078 // relese memory occupied by list controller 00079 delete list; 00080 00081 // dettach interrupts to buttons 00082 Button<SolarScreen>::deActivateButtons(); 00083 00084 return changeScreen; 00085 } 00086 00087 00088 void SolarScreen::drawSolarScreen(){ 00089 00090 // draw the tile 00091 drawTile(); 00092 00093 // construct the list 00094 list = new ListController( 0, 00095 63, 00096 "Solar", 00097 "Values", 00098 "Graph", 00099 "", 00100 "", 00101 backgroundColor, 00102 2); 00103 00104 // draw the list 00105 list->drawList(); 00106 } 00107 00108 void SolarScreen::drawTile() 00109 { 00110 // draw the green background 00111 tft.fillRect(0,0,tft.width(),63,ST7735_GREEN); 00112 00113 // draw the text 00114 tft.setTextSize(1); 00115 tft.setCursor(5,5); 00116 tft.setTextColor(ST7735_RED); 00117 tft.printf("Power:"); 00118 00119 // update the power values 00120 updateTile(); 00121 } 00122 00123 void SolarScreen::updateTile() 00124 { 00125 // read the current power 00126 float wattage = getSolarPower(); 00127 00128 // temporary storage 00129 char wattageString[5]; 00130 00131 // tracks previously measured power 00132 // ensure first value is ridiculous 00133 // to enable print on startup 00134 //static float previousWattage = -1; 00135 00136 // convert float to char* 00137 // to round off to 1 decimal place 00138 sprintf(wattageString,"%.1f",wattage); 00139 00140 // convert back to float 00141 wattage = atof(wattageString); 00142 00143 // multiply by 10 00144 wattage = wattage*10; 00145 00146 // convert float to integer 00147 float currentWattage = wattage; 00148 00149 if(currentWattage != previousWattage){ 00150 00151 // first delete the previous wattage 00152 drawWattage(previousWattage,ST7735_GREEN); 00153 // then write the new wattage 00154 drawWattage(currentWattage,ST7735_BLUE); 00155 00156 00157 }else{ 00158 // do nothing 00159 } 00160 00161 // update the previous wattage 00162 previousWattage = currentWattage; 00163 } 00164 00165 void SolarScreen::drawWattage(float wattage, uint16_t color) 00166 { 00167 // convert float to integer 00168 int wattageInt = wattage; 00169 00170 // divide by 10 00171 int firstInt = wattageInt/10; 00172 00173 // print the quotient as the first value 00174 int secondInt = wattageInt%10; 00175 00176 // the coordinates of the first large digit 00177 int startWidth = 40; 00178 int startHeight = 15; 00179 00180 // The first Large Digit 00181 tft.setTextSize(6); 00182 tft.setCursor(startWidth, startHeight); 00183 tft.setTextColor(color); 00184 tft.printf("%d",firstInt); 00185 00186 // the decimal point 00187 tft.setCursor(startWidth+ 28, startHeight + 21); 00188 tft.setTextSize(3); 00189 tft.printf("."); 00190 00191 // The second Large Digit 00192 tft.setCursor(startWidth + 45, startHeight); 00193 tft.setTextSize(6); 00194 tft.printf("%d", secondInt); 00195 00196 // The power units 00197 tft.setCursor(startWidth + 80, startHeight + 15); 00198 tft.setTextSize(3); 00199 tft.printf("W"); 00200 00201 // reset the text size to smallest 00202 tft.setTextSize(1); 00203 } 00204 00205 void SolarScreen::buttonPressed(int button) 00206 { 00207 switch(button){ 00208 00209 case BACKBUTTON: 00210 // navigate to the previous screen 00211 changeScreen = -1; 00212 break; 00213 00214 case SCROLLBUTTON: 00215 // scroll to the next option 00216 scroll = true; 00217 break; 00218 00219 case ENTERBUTTON: 00220 // navigate to the selected option 00221 enter = true; 00222 00223 default: 00224 // do nothing 00225 break; 00226 } 00227 } 00228 00229 #endif
Generated on Wed Jul 13 2022 02:31:29 by
