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: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
SerialDiagnostics/SerialDiagnostics.cpp@22:fc802e7715f8, 2014-11-07 (annotated)
- Committer:
- martydd3
- Date:
- Fri Nov 07 21:09:50 2014 +0000
- Revision:
- 22:fc802e7715f8
- Parent:
- 20:3dfa7e9461a0
CANController to link DC_DC and FanPump, as well as listen to CAN Messages (since only these 2 functions act based on incoming messages)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martydd3 | 19:3a817d2cef11 | 1 | #include "SerialDiagnostics.h" |
| martydd3 | 19:3a817d2cef11 | 2 | |
| martydd3 | 19:3a817d2cef11 | 3 | // Macros for working with the string |
| martydd3 | 19:3a817d2cef11 | 4 | // Add newlines, add it to the working buffer |
| martydd3 | 19:3a817d2cef11 | 5 | #define ADD_LINE len+=sprintf(buff+len,"%s\r\n",line); |
| martydd3 | 19:3a817d2cef11 | 6 | #define ADD_SPRINTF_LINE padCenter(line,max_charsPerLine-2,temp,' '); len+=sprintf(buff+len,"%s\r\n",line); |
| martydd3 | 19:3a817d2cef11 | 7 | |
| martydd3 | 19:3a817d2cef11 | 8 | // Print to a string buffer, pad to maxLen chars and center it with char pad, str must be null terminated! |
| martydd3 | 19:3a817d2cef11 | 9 | void padCenter(char *buff, int LineLen, char *str, char pad) { |
| martydd3 | 19:3a817d2cef11 | 10 | int len = strlen(str); |
| martydd3 | 19:3a817d2cef11 | 11 | int padL = (LineLen-len)/2; // -1 to save room for the null terminator |
| martydd3 | 19:3a817d2cef11 | 12 | for (int i=0; i<padL; i++) buff[i] = pad; // Fill in the left padding chars |
| martydd3 | 19:3a817d2cef11 | 13 | strcpy(buff+padL, str); |
| martydd3 | 19:3a817d2cef11 | 14 | for (int i = padL+len; i<LineLen; i++) buff[i] = pad; // Fill remaining with padding chars |
| martydd3 | 19:3a817d2cef11 | 15 | buff[LineLen-1] = '\0'; // Add null terminator |
| martydd3 | 19:3a817d2cef11 | 16 | } |
| martydd3 | 19:3a817d2cef11 | 17 | |
| martydd3 | 19:3a817d2cef11 | 18 | void SerialDiagnostics::thread_serialOut(void const* args){ |
| martydd3 | 20:3dfa7e9461a0 | 19 | |
| martydd3 | 19:3a817d2cef11 | 20 | const int max_charsPerLine = 81; // Max chars per line |
| martydd3 | 19:3a817d2cef11 | 21 | const int max_lines = 30; // Max lines that the layout prints out |
| martydd3 | 19:3a817d2cef11 | 22 | pc.printf("\033[2J"); // Clear the screen to get rid of reset message |
| martydd3 | 19:3a817d2cef11 | 23 | |
| martydd3 | 19:3a817d2cef11 | 24 | char buff[max_charsPerLine*max_lines]; // Giant string to store the printout |
| martydd3 | 19:3a817d2cef11 | 25 | char line[max_charsPerLine]; // String buffer to work with one line at a time |
| martydd3 | 19:3a817d2cef11 | 26 | char temp[max_charsPerLine]; // String buffer to sprintf into |
| martydd3 | 19:3a817d2cef11 | 27 | |
| martydd3 | 19:3a817d2cef11 | 28 | while(1){ |
| martydd3 | 19:3a817d2cef11 | 29 | |
| martydd3 | 19:3a817d2cef11 | 30 | int len = 0; |
| martydd3 | 19:3a817d2cef11 | 31 | len += sprintf(buff+len, "\033[0;0H"); // Home the cursor |
| martydd3 | 19:3a817d2cef11 | 32 | padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE // Generate a line full of 80 -'s |
| martydd3 | 19:3a817d2cef11 | 33 | padCenter(line, max_charsPerLine-2, " Penn Electric Racing - REV0 System Management Module Serial Dashboard ", '-'); ADD_LINE |
| martydd3 | 19:3a817d2cef11 | 34 | padCenter(line, max_charsPerLine-2, "-", '-'); ADD_LINE // Generate a line full of 80 -'s |
| martydd3 | 19:3a817d2cef11 | 35 | |
| martydd3 | 20:3dfa7e9461a0 | 36 | |
| martydd3 | 20:3dfa7e9461a0 | 37 | // Polling Switches -----------------------------------------------------------------------------------------------// |
| martydd3 | 20:3dfa7e9461a0 | 38 | |
| martydd3 | 20:3dfa7e9461a0 | 39 | padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line |
| martydd3 | 20:3dfa7e9461a0 | 40 | padCenter(line, max_charsPerLine-2, " Poll Switches ", '*'); ADD_LINE |
| martydd3 | 20:3dfa7e9461a0 | 41 | |
| martydd3 | 20:3dfa7e9461a0 | 42 | char binary_out[12]; |
| martydd3 | 20:3dfa7e9461a0 | 43 | uint16_t poll = pollSwitch.poll(); |
| martydd3 | 20:3dfa7e9461a0 | 44 | |
| martydd3 | 20:3dfa7e9461a0 | 45 | for(int i = 0; i < 10; i++){ |
| martydd3 | 20:3dfa7e9461a0 | 46 | binary_out[i] = (poll & 0x1) ? '1' : '0'; |
| martydd3 | 20:3dfa7e9461a0 | 47 | poll = poll >> 1; |
| martydd3 | 20:3dfa7e9461a0 | 48 | } |
| martydd3 | 20:3dfa7e9461a0 | 49 | binary_out[11] = '\0'; |
| martydd3 | 20:3dfa7e9461a0 | 50 | |
| martydd3 | 20:3dfa7e9461a0 | 51 | sprintf(temp, "Switch Bits: %s", binary_out); ADD_SPRINTF_LINE |
| martydd3 | 20:3dfa7e9461a0 | 52 | |
| martydd3 | 20:3dfa7e9461a0 | 53 | // Reading IMD status and resistance -------------------------------------------------------------------------------// |
| martydd3 | 20:3dfa7e9461a0 | 54 | |
| martydd3 | 20:3dfa7e9461a0 | 55 | padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line |
| martydd3 | 20:3dfa7e9461a0 | 56 | padCenter(line, max_charsPerLine-2, " IMD ", '*'); ADD_LINE |
| martydd3 | 20:3dfa7e9461a0 | 57 | |
| martydd3 | 20:3dfa7e9461a0 | 58 | char status = imd.status(); |
| martydd3 | 20:3dfa7e9461a0 | 59 | |
| martydd3 | 20:3dfa7e9461a0 | 60 | switch(status){ |
| martydd3 | 20:3dfa7e9461a0 | 61 | case OFF: |
| martydd3 | 20:3dfa7e9461a0 | 62 | sprintf(temp, "IMD Status: OFF"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 63 | case NORMAL: |
| martydd3 | 20:3dfa7e9461a0 | 64 | sprintf(temp, "IMD Status: NORMAL"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 65 | case UNDERVOLT: |
| martydd3 | 20:3dfa7e9461a0 | 66 | sprintf(temp, "IMD Status: UNDERVOLT"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 67 | case SPEEDSTART: |
| martydd3 | 20:3dfa7e9461a0 | 68 | sprintf(temp, "IMD Status: SPEEDSTART"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 69 | case ERROR: |
| martydd3 | 20:3dfa7e9461a0 | 70 | sprintf(temp, "IMD Status: ERROR"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 71 | case GROUNDERR: |
| martydd3 | 20:3dfa7e9461a0 | 72 | sprintf(temp, "IMD Status: GROUNDERR"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 73 | case INVALID: |
| martydd3 | 20:3dfa7e9461a0 | 74 | sprintf(temp, "IMD Status: INVALID"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 75 | default: |
| martydd3 | 20:3dfa7e9461a0 | 76 | sprintf(temp, "IMD Status: UNKNOWN STATUS ERROR"); ADD_SPRINTF_LINE break; |
| martydd3 | 20:3dfa7e9461a0 | 77 | } |
| martydd3 | 20:3dfa7e9461a0 | 78 | |
| martydd3 | 20:3dfa7e9461a0 | 79 | // Reading Coulomb Counter |
| martydd3 | 20:3dfa7e9461a0 | 80 | padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line |
| martydd3 | 20:3dfa7e9461a0 | 81 | padCenter(line, max_charsPerLine-2, " CoulombCounter ", '*'); ADD_LINE |
| martydd3 | 20:3dfa7e9461a0 | 82 | |
| martydd3 | 22:fc802e7715f8 | 83 | sprintf(temp, "Current: %f ampHours: %f ", coulombCounter.current(), coulombCounter.ampHours()); ADD_SPRINTF_LINE |
| martydd3 | 22:fc802e7715f8 | 84 | sprintf(temp, "Capacity: %f SOC: %f ", coulombCounter.capacity(), coulombCounter.SOC()); ADD_SPRINTF_LINE |
| martydd3 | 22:fc802e7715f8 | 85 | |
| martydd3 | 22:fc802e7715f8 | 86 | // Reading DC_DC |
| martydd3 | 22:fc802e7715f8 | 87 | padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line |
| martydd3 | 22:fc802e7715f8 | 88 | padCenter(line, max_charsPerLine-2, " DC-DC Converter ", '*'); ADD_LINE |
| martydd3 | 22:fc802e7715f8 | 89 | |
| martydd3 | 22:fc802e7715f8 | 90 | sprintf(temp, "DC Converter on: %d ", dc.is_on()); ADD_SPRINTF_LINE |
| martydd3 | 22:fc802e7715f8 | 91 | |
| martydd3 | 22:fc802e7715f8 | 92 | // Reading FanPump |
| martydd3 | 22:fc802e7715f8 | 93 | padCenter(line, max_charsPerLine-2, " ", ' '); ADD_LINE // Generate blank line |
| martydd3 | 22:fc802e7715f8 | 94 | padCenter(line, max_charsPerLine-2, " ", '*'); ADD_LINE |
| martydd3 | 20:3dfa7e9461a0 | 95 | |
| martydd3 | 19:3a817d2cef11 | 96 | // Write it all at once to output tx buffer |
| martydd3 | 19:3a817d2cef11 | 97 | for (int i = 0; i < strlen(buff); i++) { |
| martydd3 | 19:3a817d2cef11 | 98 | pc.putc(buff[i]); |
| martydd3 | 19:3a817d2cef11 | 99 | } |
| martydd3 | 19:3a817d2cef11 | 100 | |
| martydd3 | 19:3a817d2cef11 | 101 | Thread::wait(100); |
| martydd3 | 19:3a817d2cef11 | 102 | } |
| martydd3 | 19:3a817d2cef11 | 103 | } |
| martydd3 | 20:3dfa7e9461a0 | 104 | |
| martydd3 | 20:3dfa7e9461a0 | 105 | /* |
| martydd3 | 20:3dfa7e9461a0 | 106 | Testing Notes |
| martydd3 | 20:3dfa7e9461a0 | 107 | |
| martydd3 | 20:3dfa7e9461a0 | 108 | Oct 25 |
| martydd3 | 20:3dfa7e9461a0 | 109 | Tested PollSwitch: |
| martydd3 | 20:3dfa7e9461a0 | 110 | |
| martydd3 | 20:3dfa7e9461a0 | 111 | Wasn't able to access pins p1_x, but tried out pollswitch with pins p0_x |
| martydd3 | 20:3dfa7e9461a0 | 112 | Logic seems to work |
| martydd3 | 20:3dfa7e9461a0 | 113 | */ |
