Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

SerialDiagnostics/SerialDiagnostics.cpp

Committer:
martydd3
Date:
2014-11-07
Revision:
22:fc802e7715f8
Parent:
20:3dfa7e9461a0

File content as of revision 22:fc802e7715f8:

#include "SerialDiagnostics.h"

// Macros for working with the string
// Add newlines, add it to the working buffer 
#define ADD_LINE                  len+=sprintf(buff+len,"%s\r\n",line);                                                    
#define ADD_SPRINTF_LINE          padCenter(line,max_charsPerLine-2,temp,' '); len+=sprintf(buff+len,"%s\r\n",line);

// Print to a string buffer, pad to maxLen chars and center it with char pad, str must be null terminated!
void padCenter(char *buff, int LineLen, char *str, char pad) {
    int len = strlen(str);
    int padL = (LineLen-len)/2;                              // -1 to save room for the null terminator
    for (int i=0; i<padL; i++) buff[i] = pad;                // Fill in the left padding chars
    strcpy(buff+padL, str);
    for (int i = padL+len; i<LineLen; i++) buff[i] = pad;    // Fill remaining with padding chars
    buff[LineLen-1] = '\0';                                  // Add null terminator
}

void SerialDiagnostics::thread_serialOut(void const* args){
    
    const int max_charsPerLine = 81;                 // Max chars per line
    const int max_lines = 30;                        // Max lines that the layout prints out
    pc.printf("\033[2J");                            // Clear the screen to get rid of reset message
 
    char buff[max_charsPerLine*max_lines];           // Giant string to store the printout
    char line[max_charsPerLine];                     // String buffer to work with one line at a time
    char temp[max_charsPerLine];                     // String buffer to sprintf into
    
    while(1){
        
        int len = 0;
        len += sprintf(buff+len, "\033[0;0H");                      // Home the cursor
        padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE     // Generate a line full of 80 -'s      
        padCenter(line, max_charsPerLine-2, " Penn Electric Racing - REV0 System Management Module Serial Dashboard ", '-'); ADD_LINE
        padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE     // Generate a line full of 80 -'s 
        
        
    // Polling Switches -----------------------------------------------------------------------------------------------//
    
        padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE     // Generate blank line 
        padCenter(line, max_charsPerLine-2, " Poll Switches ", '*'); ADD_LINE
        
        char binary_out[12];
        uint16_t poll = pollSwitch.poll();
        
        for(int i = 0; i < 10; i++){
            binary_out[i] = (poll & 0x1) ? '1' : '0';
            poll = poll >> 1;
        }
        binary_out[11] = '\0';
        
        sprintf(temp, "Switch Bits: %s", binary_out); ADD_SPRINTF_LINE
        
    // Reading IMD status and resistance -------------------------------------------------------------------------------//
    
        padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE     // Generate blank line 
        padCenter(line, max_charsPerLine-2, " IMD ", '*'); ADD_LINE
        
        char status = imd.status();
        
        switch(status){
        case OFF:
            sprintf(temp, "IMD Status: OFF"); ADD_SPRINTF_LINE break;
        case NORMAL:
            sprintf(temp, "IMD Status: NORMAL"); ADD_SPRINTF_LINE break;
        case UNDERVOLT:
            sprintf(temp, "IMD Status: UNDERVOLT"); ADD_SPRINTF_LINE break;
        case SPEEDSTART:
            sprintf(temp, "IMD Status: SPEEDSTART"); ADD_SPRINTF_LINE break;
        case ERROR:
            sprintf(temp, "IMD Status: ERROR"); ADD_SPRINTF_LINE break;
        case GROUNDERR:
            sprintf(temp, "IMD Status: GROUNDERR"); ADD_SPRINTF_LINE break;
        case INVALID:
            sprintf(temp, "IMD Status: INVALID"); ADD_SPRINTF_LINE break;
        default:
            sprintf(temp, "IMD Status: UNKNOWN STATUS ERROR"); ADD_SPRINTF_LINE break;
        }
        
    // Reading Coulomb Counter
        padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE     // Generate blank line 
        padCenter(line, max_charsPerLine-2, " CoulombCounter ", '*'); ADD_LINE
        
        sprintf(temp, "Current: %f ampHours: %f ", coulombCounter.current(), coulombCounter.ampHours()); ADD_SPRINTF_LINE
        sprintf(temp, "Capacity: %f SOC: %f ", coulombCounter.capacity(), coulombCounter.SOC()); ADD_SPRINTF_LINE
        
    // Reading DC_DC
        padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE     // Generate blank line 
        padCenter(line, max_charsPerLine-2, " DC-DC Converter ", '*'); ADD_LINE
        
        sprintf(temp, "DC Converter on: %d ", dc.is_on()); ADD_SPRINTF_LINE
        
    // Reading FanPump
        padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE     // Generate blank line 
        padCenter(line, max_charsPerLine-2, "  ", '*'); ADD_LINE
        
        // Write it all at once to output tx buffer
        for (int i = 0; i < strlen(buff); i++) {
            pc.putc(buff[i]);   
        }
        
        Thread::wait(100);
    }
}

/*
    Testing Notes
    
    Oct 25
    Tested PollSwitch:
        
        Wasn't able to access pins p1_x, but tried out pollswitch with pins p0_x
        Logic seems to work
*/