Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

src/menu.cpp

Committer:
mfwic
Date:
2018-12-05
Revision:
7:860b3a8275cb
Parent:
6:39442d493098
Child:
8:d3d7dca419b3

File content as of revision 7:860b3a8275cb:

//-------------------------------------------------------------------------------
// 
//  Treehouse Inc.
//  Colorado Springs, Colorado
// 
//  Copyright (c) 2016 by Treehouse Designs Inc. 
// 
//  This code is the property of Treehouse, Inc. (Treehouse) and may not be redistributed
//  in any form without prior written permission from the copyright holder, Treehouse.
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//   
//-------------------------------------------------------------------------------
// 
//  REVISION HISTORY:
//  
//   $Author: $
//   $Rev: $
//   $Date: $
//   $URL: $
// 
//-------------------------------------------------------------------------------

#include "mbed.h"
#include "globals.h"
#include "serial.h"
#include "menu.h"
#include "Ticker.h"

Ticker drt;
unsigned int oldTime=0;

/*******************************************************************************
 drtInt - Display Refresh Timer interrupt handler
*******************************************************************************/
// real time clock interrupt to flash the LED
void drtInt(void)
{
   updateReady = TRUE;
}

/*******************************************************************************
 initDRT - Create Display Refresh Timer interrupt
*******************************************************************************/
// initialize display refresh timer (DRT)
void initDRT(void)
{
   drt.attach_us(&drtInt, UPDATE_REFRESH_DELAY_US); 
}

/*******************************************************************************
 refreshData - Refresh voltage and current readings to the terminal
*******************************************************************************/
void refreshData(void){
    loopTime = masterTimer.read_ms() - oldTime;
    oldTime = masterTimer.read_ms();
    
    double v48f = 48*(v48/2048);
    double v24f = 24*(v48/2048);
    double v12f = 12*(v48/2048);
    double i48f = i48/40;
    double i24f = i24/40;
    double i12f = i12/40;
    double pwr_out=v12f*i12f;
    double pwr_in=v48f*(i48f-0.25);
    double pwr_eff=100*pwr_out/pwr_in;
    
    sprintf(strbuf, "V48=%2.2f, I48=%2.2f | V24=%2.2f, I24=%2.2f | V12=%2.2f, I12=%2.2f || loop=%d  \r\n", v48f, i48f, v24f, i24f, v12f, i12f, loopTime);
    sendSerial(strbuf);
    sprintf(strbuf, "Power Out = %4.1f W, Power In = %4.1f W, ", pwr_out, pwr_in);
    sendSerial(strbuf);
    sprintf( strbuf, "%c[34m", 27 ); // ESC=27, Blue Text
    sendSerial(strbuf);
    sprintf(strbuf, "Power Efficiency = %2.2f percent\r\n", pwr_eff);
    sendSerial(strbuf);
    sprintf( strbuf, "%c[30m", 27 ); // ESC=27, White Text
    sendSerial(strbuf);
    //sprintf(strbuf, "thermCode=%d, binCode=%d", thermCode[17], binCode[6]);
    sprintf(strbuf, "wr_out_code=%d, en_out_code=%d", wr_out_code, en_out_code);
    sendSerial(strbuf);
}

/*******************************************************************************
 updateTerminal - Save cursor, write data, restore cursor.
*******************************************************************************/
void updateTerminal(void){
    //get cursor pos, store in CUR_POS
    sprintf( strbuf, "%c7", 27 ); // ESC=27, Save cursor position = [s
    sendSerial(strbuf);
    //move cursor to DATA_ROW
    sprintf( strbuf, "%c[2;0f", 27 ); // ESC=27, Move cursor position = [line;columnf
    sendSerial(strbuf);
    //write data
    refreshData();
    //move cursor to CUR_POS
    sprintf( strbuf, "%c8", 27 ); // ESC=27, Save cursor position = [s
    sendSerial(strbuf);
}

/*******************************************************************************
 clrScrn - clears terminal and moves cursor to upper-left corner
*******************************************************************************/
// clears terminal
void clrScrn(void){
    sprintf( strbuf, "%c[2J", 27 ); // ESC=27, Clear screen = [2J
    sendSerial(strbuf);
    sprintf( strbuf, "%c[f", 27 ); // ESC=27, Move cursor to upper-left corner = [f
    sendSerial(strbuf);
}

/*******************************************************************************
 menuRedraw - Write menu to terminal
*******************************************************************************/
// clears terminal and re-draws main menu
void menuRedraw(void){
    clrScrn();
    
    sprintf( strbuf, "%c[34m", 27 ); // ESC=27, Blue Text
    sendSerial(strbuf);
    
    sprintf(strbuf, "Agility Power Systems DCM1 Menu 0.35\r\n");
    sendSerial(strbuf);
    
    sprintf( strbuf, "%c[0m", 27 ); // ESC=27, Normal
    sendSerial(strbuf);
    sprintf( strbuf, "%c[30m", 27 ); // ESC=27, White Text
    sendSerial(strbuf);
    
    refreshData();
    
    sprintf(strbuf, "\r\nMULT");
    sendSerial(strbuf);
    sprintf(strbuf, "\r\nBRDS");
    sendSerial(strbuf);
    sprintf(strbuf, "\r\nALLOFF");
    sendSerial(strbuf);
    sprintf(strbuf, "\r\nRUN [xx]");
    sendSerial(strbuf);
    sprintf(strbuf, "\r\nSTOP");
    sendSerial(strbuf);
    
    menuPrompt(MENU_DCM1);
}

/*******************************************************************************
 menuPrompt - Write menu prompt to terminal
*******************************************************************************/
// sends carriage return and linefeed and prompt character
void menuPrompt(int menuType)
{
 char strMenu[30] ={0};

    switch(menuType)
    {
      case  MENU_DCM1: strcpy(strMenu,"DCM1"); break;
      //case  MENU_DIFFERENTIAL: strcpy(strMenu,"DIFF"); break;
      //case  MENU_SINGLE: strcpy(strMenu,"SINGLE"); break;
      //case  MENU_DUAL: strcpy(strMenu,"DUAL"); break;
      //case  MENU_INVERT: strcpy(strMenu,"INVERT"); break;
      //case  MENU_SETTINGS: strcpy(strMenu,"SET"); break;
      //case  MENU_SETTINGS_CHAN1: strcpy(strMenu,"SET CHAN1"); break;
      //case  MENU_SETTINGS_CHAN2: strcpy(strMenu,"SET CHAN2"); break;
      //case  MENU_SETTINGS_CLOCK: strcpy(strMenu,"SET CLOCK"); break;
      case  MENU_CALIBRATE: strcpy(strMenu,"CAL"); break;
      case  MENU_TEST: strcpy(strMenu,"TEST"); break;
      case  MENU_MAIN: strcpy(strMenu,"MAIN"); break;
      default: strcpy(strMenu,"UNDF"); break;
    }

    // append the CR,LF and the ready character
     if(serialStatus.computer)
     {
         sendSerial("\r");
     }
     else
     {
        // adds a carrot so you know you are talking in terminal
        sprintf(strbuf,"\n\r%s>",strMenu);
        sendSerial(strbuf);
        // sendSerial("\n\r>");
     }
}