Treehouse Mbed Team / Mbed 2 deprecated APS_1U5x

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers menu.cpp Source File

menu.cpp

00001 //-------------------------------------------------------------------------------
00002 // 
00003 //  Treehouse Inc.
00004 //  Colorado Springs, Colorado
00005 // 
00006 //  Copyright (c) 2016 by Treehouse Designs Inc. 
00007 // 
00008 //  This code is the property of Treehouse, Inc. (Treehouse) and may not be redistributed
00009 //  in any form without prior written permission from the copyright holder, Treehouse.
00010 //
00011 //  The above copyright notice and this permission notice shall be included in
00012 //  all copies or substantial portions of the Software.
00013 //   
00014 //-------------------------------------------------------------------------------
00015 // 
00016 //  REVISION HISTORY:
00017 //  
00018 //   $Author: $
00019 //   $Rev: $
00020 //   $Date: $
00021 //   $URL: $
00022 // 
00023 //-------------------------------------------------------------------------------
00024 
00025 #include "mbed.h"
00026 #include "globals.h"
00027 #include "serial.h"
00028 #include "menu.h"
00029 #include "adc.h"
00030 #include "boards.h"
00031 #include "Ticker.h"
00032 #include "stdlib.h"
00033 #include "lut.h"
00034 
00035 Ticker drt;
00036 unsigned int oldTime=0;
00037 
00038 /*******************************************************************************
00039  drtInt - Display Refresh Timer interrupt handler
00040 *******************************************************************************/
00041 // real time clock interrupt to flash the LED
00042 void drtInt(void)
00043 {
00044    updateReady = TRUE;
00045 }
00046 
00047 /*******************************************************************************
00048  initDRT - Create Display Refresh Timer interrupt
00049 *******************************************************************************/
00050 // initialize display refresh timer (DRT)
00051 void initDRT(void)
00052 {
00053    drt.attach_us(&drtInt, UPDATE_REFRESH_DELAY_US); 
00054 }
00055 
00056 /*******************************************************************************
00057  refreshStatus - Refresh status info to the terminal
00058 *******************************************************************************/
00059 void refreshStatus(struct statusValues statVals){
00060     
00061     char strbuf_mode[15] = {0};
00062     char strbuf_running[16] = {0};
00063     char strbuf_v48[15] = {0};
00064     char strbuf_v24[15] = {0};
00065     char strbuf_v12[15] = {0};
00066     char strbuf_blank[50] = {0};
00067     
00068     if(buck){
00069         sprintf(strbuf_mode, "Buck Mode  | ");
00070     }else{
00071         sprintf(strbuf_mode, "Boost Mode | ");
00072     }
00073     //sendSerial(strbuf);
00074     
00075     if(running){
00076         sprintf(strbuf_running, "Running     | ");
00077     }else{
00078         sprintf(strbuf_running, "NOT Running | ");
00079     }
00080     //sendSerial(strbuf);
00081     
00082     if(statVals.V48_IS_HI){
00083         sprintf(strbuf_v48, "V48 HIGH | ");
00084     }else if(statVals.V48_IS_LO){
00085         sprintf(strbuf_v48, "V48 LOW  | ");
00086     }else{
00087     //    sprintf(strbuf, "V48 OK   | ");
00088         sprintf(strbuf_v48, "");
00089         sprintf(strbuf_blank, "           ");
00090     }
00091     //sendSerial(strbuf);
00092     
00093     if(statVals.V24_IS_HI){
00094         sprintf(strbuf_v24, "V24 HIGH | ");
00095     }else if(statVals.V24_IS_LO){
00096         sprintf(strbuf_v24, "V24 LOW  | ");
00097     }else{
00098     //    sprintf(strbuf, "V24 OK   | ");
00099         sprintf(strbuf_v24, "");
00100         sprintf(strbuf_blank, strcat(strbuf_blank, "           "));
00101     }
00102     //sendSerial(strbuf);
00103     
00104     if(statVals.V12_IS_HI){
00105         sprintf(strbuf_v12, "V12 HIGH ");
00106     }else if(statVals.V24_IS_LO){
00107         sprintf(strbuf_v12, "V12 LOW  ");
00108     }else{
00109     //    sprintf(strbuf, "V12 OK   | ");
00110         sprintf(strbuf_v12, "");
00111         sprintf(strbuf_blank, strcat(strbuf_blank, "           "));
00112     }
00113     //sendSerial(strbuf);
00114     
00115     //move cursor to STATUS_ROW and write status info
00116     //char row = STATUS_ROW + '0';
00117     sprintf( strbuf, "%c[9;0f", 27); // ESC=27, Move cursor position = [line;columnf
00118     sendSerial(strbuf);
00119     
00120     sprintf(strbuf, "Status: %s%s%s%s%s%s\r\n\r\n", strbuf_mode, strbuf_running, strbuf_v48, strbuf_v24, strbuf_v12, strbuf_blank);
00121     //sprintf(strbuf, "Status: %s%s%s%s%s%d  \r\n\r\n", strbuf_mode, strbuf_running, strbuf_v48, strbuf_v24, strbuf_v12, row_print);
00122     sendSerial(strbuf);
00123     
00124 }
00125 
00126 /*******************************************************************************
00127  refreshData - Refresh voltage and current readings to the terminal
00128 *******************************************************************************/
00129 void refreshData(struct adcValues adcVals){
00130     //loopTime = masterTimer.read_ms() - oldTime;
00131     //oldTime = masterTimer.read_ms();
00132     
00133     struct displayValues dvals = calcDisplayValues(adcVals);
00134     
00135     double pwr_out=dvals.v12f*dvals.i12f;
00136     double pwr_in=dvals.v48f*(dvals.i48f);
00137     double pwr_eff=100*pwr_out/pwr_in;
00138     
00139     //move cursor to DATA_ROW and write data
00140     //row = DATA_ROW + '0';
00141     sprintf( strbuf, "%c[10;0f", 27); // ESC=27, Move cursor position = [line;columnf
00142     sendSerial(strbuf);
00143     
00144     sprintf(strbuf, "V48=%2.1f, I48=%3.1f | V24=%2.1f, I24=%3.1f | V12=%2.1f, I12=%3.2f    \r\n", dvals.v48f, dvals.i48f, dvals.v24f, dvals.i24f, dvals.v12f, dvals.i12f);
00145     //sprintf(strbuf, "V48=%2.1f, I48=%2.2f | V24=%2.1f, I24=%2.2f | V12=%2.1f, I12=%2.2f || loop=%d    \r\n", v48f, i48f, v24f, i24f, v12f, i12f, loopTime);
00146     sendSerial(strbuf);
00147     //sprintf(strbuf, "adcVals.i12 = %d     \r\n", adcVals.i12);
00148     //sendSerial(strbuf);
00149     sprintf(strbuf, "Power Out = %4.1f W, Power In = %4.1f W     \r\n", pwr_out, pwr_in);
00150     sendSerial(strbuf);
00151     sprintf( strbuf, "%c[34m", 27 ); // ESC=27, Blue Text
00152     sendSerial(strbuf);
00153     sprintf(strbuf, "Power Efficiency = %2.2f percent    \r\n\r\n", pwr_eff);
00154     sendSerial(strbuf);
00155     sprintf( strbuf, "%c[30m", 27 ); // ESC=27, White Text
00156     sendSerial(strbuf);
00157     sprintf(strbuf, "BRDS_code=%d, MULT_code=%d    \r\n", wr_out_code, en_out_code);
00158     sendSerial(strbuf);
00159 }
00160 
00161 /*******************************************************************************
00162  updateTerminal - Save cursor, write data, restore cursor.
00163 *******************************************************************************/
00164 void updateTerminal(struct adcValues adcVals, struct statusValues statVals){
00165     //get cursor pos, store in CUR_POS
00166     sprintf( strbuf, "%c7", 27 ); // ESC=27, Save cursor position = [s
00167     sendSerial(strbuf);
00168     
00169     refreshStatus(statVals);
00170     refreshData(adcVals);
00171     
00172     //move cursor to CUR_POS
00173     sprintf( strbuf, "%c8", 27 ); // ESC=27, Save cursor position = [s
00174     sendSerial(strbuf);
00175 }
00176 
00177 /*******************************************************************************
00178  clrScrn - clears terminal and moves cursor to upper-left corner
00179 *******************************************************************************/
00180 // clears terminal
00181 void clrScrn(void){
00182     sprintf( strbuf, "%c[2J", 27 ); // ESC=27, Clear screen = [2J
00183     sendSerial(strbuf);
00184     sprintf( strbuf, "%c[f", 27 ); // ESC=27, Move cursor to upper-left corner = [f
00185     sendSerial(strbuf);
00186 }
00187 
00188 /*******************************************************************************
00189  menuRedraw - Write menu to terminal
00190 *******************************************************************************/
00191 // clears terminal and re-draws main menu
00192 void menuRedraw(bool prompt){
00193     clrScrn();
00194     menu_banner();
00195     
00196     sprintf( strbuf, "%c[34m", 27 ); // ESC=27, Blue Text
00197     sendSerial(strbuf);
00198     
00199     sprintf(strbuf, "                                Agility Power Systems DCM1 v1.0 LUT v%1.1f\r\n\r\n", LUT_VER);
00200     sendSerial(strbuf);
00201     
00202     sprintf( strbuf, "%c[0m", 27 ); // ESC=27, Normal
00203     sendSerial(strbuf);
00204     sprintf( strbuf, "%c[30m", 27 ); // ESC=27, Black Text
00205     sendSerial(strbuf);
00206     
00207     struct adcValues adcVals = getADCresults();
00208     struct statusValues statVals = checkLevels(adcVals);
00209     refreshStatus(statVals);
00210     refreshData(adcVals);
00211     
00212     sprintf(strbuf, "\r\nCommands: (Not Case-Sensitive)");
00213     sendSerial(strbuf);
00214     sprintf(strbuf, "\r\n MENU");
00215     sendSerial(strbuf);
00216     sprintf(strbuf, "\r\n BRDS");
00217     sendSerial(strbuf);
00218     sprintf(strbuf, "\r\n MULT");
00219     sendSerial(strbuf);
00220     sprintf(strbuf, "\r\n ALLOFF");
00221     sendSerial(strbuf);
00222     sprintf(strbuf, "\r\n RUN");
00223     sendSerial(strbuf);
00224     sprintf(strbuf, "\r\n STOP\r\n");
00225     sendSerial(strbuf);
00226     sprintf(strbuf, "\r\n Type \'=number\' after BRDS or MULT to enter value.");
00227     sendSerial(strbuf);
00228     sprintf(strbuf, "\r\n e.g. BRDS=4095 will enable all boards.\r\n");
00229     sendSerial(strbuf);
00230     sprintf(strbuf, "\r\n Type \'?\' at end of BRDS or MULT to get status.");
00231     sendSerial(strbuf);
00232     sprintf(strbuf, "\r\n e.g. BRDS? will print value of BRDS.\r\n");
00233     sendSerial(strbuf);
00234     
00235     if(prompt){
00236         menuPrompt(MENU_DCM1);
00237     }
00238 }
00239 
00240 /*******************************************************************************
00241  sendLevelsWarning
00242 *******************************************************************************/
00243 void sendLevelsWarning(void){
00244     sprintf(strbuf, "\r\nSome voltage levels are out of range. I would look into it if I were you.\r\n");
00245     sendSerial(strbuf);
00246 }
00247 
00248 /*******************************************************************************
00249  menuPrompt - Write menu prompt to terminal
00250 *******************************************************************************/
00251 // sends carriage return and linefeed and prompt character
00252 void menuPrompt(int menuType)
00253 {
00254  char strMenu[30] ={0};
00255 
00256     switch(menuType)
00257     {
00258       case  MENU_DCM1: strcpy(strMenu,"DCM1"); break;
00259       case  MENU_CALIBRATE: strcpy(strMenu,"CAL"); break;
00260       case  MENU_TEST: strcpy(strMenu,"TEST"); break;
00261       case  MENU_MAIN: strcpy(strMenu,"MAIN"); break;
00262       default: strcpy(strMenu,"UNDF"); break;
00263     }
00264 
00265     // append the CR,LF and the ready character
00266      if(serialStatus.computer)
00267      {
00268          sendSerial("\r");
00269      }
00270      else
00271      {
00272         // adds a carrot so you know you are talking in terminal
00273         sprintf(strbuf,"\n\r%s>",strMenu);
00274         sendSerial(strbuf);
00275         // sendSerial("\n\r>");
00276      }
00277 }
00278 
00279 void menu_banner(void)
00280 {
00281     clrScrn();
00282     sprintf( strbuf, "%c[30m", 27 ); // ESC=27, Black Text
00283     sendSerial(strbuf);
00284     sprintf(strbuf,"    ___         _ ___ __           ____                             _____            __                     \r\n");sendSerial(strbuf);
00285     sprintf(strbuf,"   /   | ____ _(_) (_) /___  __   / __ \\____ _      _____  _____   / ___/__  _______/ /____  ____ ___  _____\r\n");sendSerial(strbuf);
00286     sprintf(strbuf,"  / /| |/ __ `/ / / / __/ / / /  / /_/ / __ \\ | /| / / _ \\/ ___/   \\__ \\/ / / / ___/ __/ _ \\/ __ `__ \\/ ___/\r\n");sendSerial(strbuf);
00287     sprintf(strbuf," / ___ / /_/ / / / / /_/ /_/ /  / ____/ /_/ / |/ |/ /  __/ /      ___/ / /_/ (__  ) /_/  __/ / / / / (__  ) \r\n");sendSerial(strbuf);
00288     sprintf(strbuf,"/_/  |_\\__, /_/_/_/\\__/\\__, /  /_/    \\____/|__/|__/\\___/_/      /____/\\__, /____/\\__/\\___/_/ /_/ /_/____/  \r\n");sendSerial(strbuf);
00289     sprintf(strbuf,"      /____/          /____/                                          /____/                                \r\n");sendSerial(strbuf);
00290 }
00291 /*
00292     sprintf(strbuf,"    ___         _ ___ __           ____                             _____            __                     \r\n");
00293     sprintf(strbuf,"   /   | ____ _(_) (_) /___  __   / __ \____ _      _____  _____   / ___/__  _______/ /____  ____ ___  _____\r\n");
00294     sprintf(strbuf,"  / /| |/ __ `/ / / / __/ / / /  / /_/ / __ \ | /| / / _ \/ ___/   \__ \/ / / / ___/ __/ _ \/ __ `__ \/ ___/\r\n");
00295     sprintf(strbuf," / ___ / /_/ / / / / /_/ /_/ /  / ____/ /_/ / |/ |/ /  __/ /      ___/ / /_/ (__  ) /_/  __/ / / / / (__  ) \r\n");
00296     sprintf(strbuf,"/_/  |_\__, /_/_/_/\__/\__, /  /_/    \____/|__/|__/\___/_/      /____/\__, /____/\__/\___/_/ /_/ /_/____/  \r\n");
00297     sprintf(strbuf,"      /____/          /____/                                          /____/                                \r\n");
00298 */