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