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
ListController.h
00001 #ifndef LISTCONTROLLER_H 00002 #define LISTCONTROLLER_H 00003 00004 #include "Inputs.h" 00005 00006 #define OPTION1 1 00007 #define OPTION2 2 00008 #define OPTION3 3 00009 #define OPTION4 4 00010 00011 class ListController 00012 { 00013 00014 public: 00015 ListController(uint8_t xCoord, uint8_t yCoord,char title[], char option1[], char option2[], char option3[], char option4[], uint16_t backgroundColor,int numberOfOptions); 00016 int getCurrentOption(); 00017 00018 void drawList(); 00019 void scroll(); 00020 00021 private: 00022 int nOOp; 00023 char t[32]; 00024 char op1[32]; 00025 char op2[32]; 00026 char op3[32]; 00027 char op4[32]; 00028 const char *ops[5][32]; 00029 uint16_t bgColor; 00030 uint16_t textColor; 00031 uint16_t highlightColor; 00032 int currentOption; 00033 00034 // Coordinates 00035 uint8_t startWidth; 00036 uint8_t startHeight; 00037 00038 void drawTitle(); 00039 void drawLines(); 00040 void drawText(); 00041 void drawSmallText(uint8_t x, uint8_t y, char *text); 00042 void deleteSmallText(uint8_t x, uint8_t y, char *text); 00043 void drawBigText(uint8_t x, uint8_t y, char *text); 00044 void deleteBigText(uint8_t x, uint8_t y, char *text); 00045 void highlightOption( int option); 00046 void deHighlightOption(int option); 00047 00048 }; 00049 00050 ListController::ListController(uint8_t xCoord, uint8_t yCoord,char title[], char option1[], char option2[], char option3[], char option4[], uint16_t backgroundColor,int numberOfOptions) 00051 { 00052 // list controller first initialiser 00053 00054 // set the background color 00055 bgColor = backgroundColor; 00056 00057 // set the texts appropriately 00058 strcpy(t,title); 00059 strcpy(op1,option1); 00060 strcpy(op2,option2); 00061 strcpy(op3,option3); 00062 strcpy(op4,option4); 00063 00064 00065 /* 00066 strcpy(t,options[0][0]); 00067 strcpy(op1,options[1][0]); 00068 strcpy(op2,options[2][0]); 00069 strcpy(op3,options[3][0]); 00070 strcpy(op4,options[4][0]); 00071 */ 00072 00073 // set the coordinates 00074 startWidth = xCoord; 00075 startHeight = yCoord; 00076 00077 // set the number of options 00078 nOOp = numberOfOptions; 00079 00080 00081 // determine the color of text 00082 if (bgColor == ST7735_WHITE ) { 00083 textColor = ST7735_BLACK; 00084 highlightColor = ST7735_RED; 00085 } else if (bgColor == ST7735_BLACK) { 00086 textColor = ST7735_WHITE; 00087 highlightColor = ST7735_CYAN; 00088 } else if (bgColor == ST7735_BLUE) { 00089 textColor = ST7735_WHITE; 00090 } 00091 } 00092 00093 void ListController::drawList() 00094 { 00095 // first draw a filled rectangle with the background color 00096 tft.fillRect(startWidth, startHeight, tft.width(), tft.height()-startHeight, bgColor); 00097 00098 // draw the title 00099 drawTitle(); 00100 00101 // then draw the cell demarcating lines 00102 drawLines(); 00103 00104 // then draw the text in each cell 00105 drawText(); 00106 00107 // set the first option as the current opion 00108 currentOption = OPTION1; 00109 00110 // highlight the first option / first cell 00111 highlightOption(OPTION1); 00112 } 00113 00114 00115 void ListController::drawTitle() 00116 { 00117 // draws the title of the list 00118 tft.setCursor((tft.width()/2)-30,startHeight+4); 00119 tft.setTextColor(textColor); 00120 tft.setTextWrap(true); 00121 tft.printf("%s",t); 00122 } 00123 00124 00125 void ListController::drawLines() 00126 { 00127 // draws the appropriate cell lines 00128 // these lines demarcate the cell elements 00129 for (int i = 0; i < nOOp+1; i++){ 00130 tft.drawFastHLine(0,startHeight+15+(20*i), tft.width(),textColor); 00131 } 00132 } 00133 00134 void ListController::drawText(){ 00135 // draws the text within the cell elements 00136 // ie the options 00137 00138 tft.setTextColor(textColor); 00139 tft.setTextSize(1); 00140 00141 // option 1 00142 tft.setCursor(startWidth +20, startHeight + 22); 00143 tft.printf("%s",op1); 00144 00145 // option 2 00146 tft.setCursor(startWidth + 20, startHeight + 42); 00147 tft.printf("%s",op2); 00148 00149 // option 3 00150 tft.setCursor(startWidth + 20, startHeight + 62); 00151 tft.printf("%s",op3); 00152 00153 // option 4 00154 tft.setCursor(startWidth + 20, startHeight + 82); 00155 tft.printf("%s",op4); 00156 } 00157 00158 int ListController::getCurrentOption() 00159 { 00160 return currentOption; 00161 } 00162 00163 void ListController::scroll() 00164 { 00165 // provides the mechanism that animates 00166 // highlighting from one option to the next i.e scrolling 00167 00168 00169 // increment the option tracker 00170 currentOption++; 00171 00172 // remove highlighting from the previous option 00173 deHighlightOption(currentOption-1); 00174 00175 // ensure the option tracker stays within range 00176 if (currentOption > nOOp ){ 00177 // set it back to the first option after the 00178 // last option is reached 00179 currentOption = 1; 00180 } 00181 00182 // hgighlight the new option 00183 highlightOption(currentOption); 00184 } 00185 00186 void ListController::highlightOption(int option){ 00187 00188 // this function implements the mechanism for 00189 // highlighting a given option on the list 00190 00191 switch (option){ 00192 case OPTION1: 00193 // delete the small text 00194 deleteSmallText(startWidth +20, startHeight + 22,op1); 00195 // draw the big text 00196 drawBigText(startWidth+10, startHeight+17,op1); 00197 break; 00198 case OPTION2: 00199 // delete the small text 00200 deleteSmallText(startWidth + 20, startHeight + 42,op2); 00201 // draw the big text 00202 drawBigText(startWidth+10, startHeight+37,op2); 00203 break; 00204 case OPTION3: 00205 // delete the small text 00206 deleteSmallText(startWidth + 20, startHeight + 62,op3); 00207 // draw the big text 00208 drawBigText(startWidth+10, startHeight+57,op3); 00209 break; 00210 case OPTION4: 00211 // delete the small text 00212 deleteSmallText(startWidth + 20, startHeight + 82,op4); 00213 // draw the big text 00214 drawBigText(startWidth+10, startHeight+77,op4); 00215 break; 00216 } 00217 } 00218 00219 void ListController::deHighlightOption(int option) 00220 { 00221 // this function implements the mechanism for 00222 // dehighlighting a given option on the list 00223 00224 switch (option){ 00225 case OPTION1: 00226 // delete the big text 00227 deleteBigText(startWidth+10, startHeight+17,op1); 00228 // draw the small text 00229 drawSmallText(startWidth +20, startHeight + 22,op1); 00230 break; 00231 case OPTION2: 00232 // delete the big text 00233 deleteBigText(startWidth+10, startHeight+37,op2); 00234 // draw the small text 00235 drawSmallText(startWidth + 20, startHeight + 42,op2); 00236 break; 00237 case OPTION3: 00238 // delete the big text 00239 deleteBigText(startWidth+10, startHeight+57,op3); 00240 // draw the small text 00241 drawSmallText(startWidth + 20, startHeight + 62,op3); 00242 break; 00243 case OPTION4: 00244 // delete the big text 00245 deleteBigText(startWidth+10, startHeight+77,op4); 00246 // draw the small text 00247 drawSmallText(startWidth + 20, startHeight + 82,op4); 00248 break; 00249 } 00250 } 00251 00252 void ListController::drawSmallText(uint8_t x, uint8_t y, char *text){ 00253 00254 // draws small text within a given cell element 00255 tft.setCursor(x, y); 00256 tft.setTextColor(textColor); 00257 tft.printf("%s",text); 00258 tft.setTextSize(1); 00259 } 00260 00261 void ListController::deleteSmallText(uint8_t x, uint8_t y, char *text){ 00262 00263 // deletes small text within a given cell element 00264 // by replacing it with background color 00265 tft.setCursor(x, y); 00266 tft.setTextColor(bgColor); 00267 tft.printf("%s",text); 00268 tft.setTextSize(1); 00269 } 00270 00271 void ListController::drawBigText(uint8_t x, uint8_t y, char *text){ 00272 00273 // draws big text within a given cell element 00274 tft.setTextSize(2); 00275 tft.setCursor(x, y); 00276 tft.setTextColor(highlightColor); 00277 tft.printf("%s",text); 00278 tft.setTextSize(1); 00279 00280 } 00281 00282 void ListController::deleteBigText(uint8_t x, uint8_t y, char *text){ 00283 00284 // deletes big text within a given cell element 00285 // by replacing it with background color 00286 tft.setTextSize(2); 00287 tft.setCursor(x, y); 00288 tft.setTextColor(bgColor); 00289 tft.printf("%s",text); 00290 tft.setTextSize(1); 00291 } 00292 #endif
Generated on Wed Jul 13 2022 02:31:29 by
