Eli Hughes
/
RobotPowerLogger-V2
For Terrance
Code/System.c@0:085749c8446f, 2012-06-13 (annotated)
- Committer:
- emh203
- Date:
- Wed Jun 13 15:10:06 2012 +0000
- Revision:
- 0:085749c8446f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:085749c8446f | 1 | #include "System.h" |
emh203 | 0:085749c8446f | 2 | |
emh203 | 0:085749c8446f | 3 | DigitalOut EXT_LED0(LED2); |
emh203 | 0:085749c8446f | 4 | DigitalOut ETH_LED_GREEN(p29); |
emh203 | 0:085749c8446f | 5 | DigitalOut ETH_LED_YELLOW(p30); |
emh203 | 0:085749c8446f | 6 | DigitalIn ActionButton(p28); |
emh203 | 0:085749c8446f | 7 | DigitalIn SDCardDetect(p9); |
emh203 | 0:085749c8446f | 8 | Ticker SystemTick; |
emh203 | 0:085749c8446f | 9 | |
emh203 | 0:085749c8446f | 10 | WORD UpdateHostTerminalTick; |
emh203 | 0:085749c8446f | 11 | WORD DisplayUpdateTick; |
emh203 | 0:085749c8446f | 12 | WORD ButtonCheckTick; |
emh203 | 0:085749c8446f | 13 | WORD DisplayShutOffTick; |
emh203 | 0:085749c8446f | 14 | |
emh203 | 0:085749c8446f | 15 | BYTE SystemState; |
emh203 | 0:085749c8446f | 16 | CHAR DTBuf[32]; |
emh203 | 0:085749c8446f | 17 | CHAR DataLineBuf[256]; |
emh203 | 0:085749c8446f | 18 | |
emh203 | 0:085749c8446f | 19 | //***************************************************************** |
emh203 | 0:085749c8446f | 20 | //smart Display related stuff |
emh203 | 0:085749c8446f | 21 | //***************************************************************** |
emh203 | 0:085749c8446f | 22 | |
emh203 | 0:085749c8446f | 23 | #define DISPLAY_MEASUREMENTS 0x00 |
emh203 | 0:085749c8446f | 24 | #define DISPLAY_BATTERY 0x01 |
emh203 | 0:085749c8446f | 25 | #define DISPLAY_DATE_TIME 0x02 |
emh203 | 0:085749c8446f | 26 | #define DISPLAY_CARD_STATUS 0x03 |
emh203 | 0:085749c8446f | 27 | #define DISPLAY_BUFFER_STATS 0x04 |
emh203 | 0:085749c8446f | 28 | #define DISPLAY_FIRMWARE_VERSION 0x05 |
emh203 | 0:085749c8446f | 29 | #define DISPLAY_ID 0x06 |
emh203 | 0:085749c8446f | 30 | #define DISPLAY_OFF 0xFF |
emh203 | 0:085749c8446f | 31 | |
emh203 | 0:085749c8446f | 32 | #define NUM_DISPLAYS 7 |
emh203 | 0:085749c8446f | 33 | #define DISPLAY_SHUTOFF_TIME 10000 |
emh203 | 0:085749c8446f | 34 | |
emh203 | 0:085749c8446f | 35 | BYTE DisplayMode; |
emh203 | 0:085749c8446f | 36 | BYTE LastBackLightColor = 0; |
emh203 | 0:085749c8446f | 37 | |
emh203 | 0:085749c8446f | 38 | //***************************************************************** |
emh203 | 0:085749c8446f | 39 | //Action Button Related stuff |
emh203 | 0:085749c8446f | 40 | //***************************************************************** |
emh203 | 0:085749c8446f | 41 | #define WAITING_FOR_PRESS 0x00 |
emh203 | 0:085749c8446f | 42 | #define WAITING_FOR_RELEASE 0x01 |
emh203 | 0:085749c8446f | 43 | |
emh203 | 0:085749c8446f | 44 | BYTE PreviousButtonState; |
emh203 | 0:085749c8446f | 45 | BYTE CurrentButtonState; |
emh203 | 0:085749c8446f | 46 | WORD ButtonHoldTime; |
emh203 | 0:085749c8446f | 47 | BYTE ButtonCheckState; |
emh203 | 0:085749c8446f | 48 | void ActionButtonHandler(WORD ButtonPressTime); |
emh203 | 0:085749c8446f | 49 | |
emh203 | 0:085749c8446f | 50 | //***************************************************************** |
emh203 | 0:085749c8446f | 51 | //Terminal Related stuff |
emh203 | 0:085749c8446f | 52 | //***************************************************************** |
emh203 | 0:085749c8446f | 53 | |
emh203 | 0:085749c8446f | 54 | //#define MAX_TERMINAL_LINE_CHARS 128 |
emh203 | 0:085749c8446f | 55 | //CHAR TerminalLineBuf[MAX_TERMINAL_LINE_CHARS]; |
emh203 | 0:085749c8446f | 56 | //BYTE TerminalPos; |
emh203 | 0:085749c8446f | 57 | time_t CurrentTime_InSec; |
emh203 | 0:085749c8446f | 58 | tm *CurrentTime; |
emh203 | 0:085749c8446f | 59 | BOOL ProcessTerminalLine(CHAR * LineIn); |
emh203 | 0:085749c8446f | 60 | BOOL CheckForTime(CHAR *LineIn); |
emh203 | 0:085749c8446f | 61 | BOOL CheckForDate(CHAR *LineIn); |
emh203 | 0:085749c8446f | 62 | BOOL CheckForTimeInit(CHAR * LineIn); |
emh203 | 0:085749c8446f | 63 | BOOL CheckforConfig(CHAR *LineIn); |
emh203 | 0:085749c8446f | 64 | |
emh203 | 0:085749c8446f | 65 | //***************************************************************** |
emh203 | 0:085749c8446f | 66 | //Logging Related Stuff |
emh203 | 0:085749c8446f | 67 | //***************************************************************** |
emh203 | 0:085749c8446f | 68 | CHAR CurrentLogFileName[256]; |
emh203 | 0:085749c8446f | 69 | BOOL InitDataLog(); |
emh203 | 0:085749c8446f | 70 | BOOL DataLogError; |
emh203 | 0:085749c8446f | 71 | CHAR *ErrorMsg; |
emh203 | 0:085749c8446f | 72 | |
emh203 | 0:085749c8446f | 73 | void CreateLogFileName(); |
emh203 | 0:085749c8446f | 74 | |
emh203 | 0:085749c8446f | 75 | FATFS MyFileSystem; |
emh203 | 0:085749c8446f | 76 | FIL CurrentLogFileHandle; |
emh203 | 0:085749c8446f | 77 | |
emh203 | 0:085749c8446f | 78 | //DataBlock MyDataBlock[2]; |
emh203 | 0:085749c8446f | 79 | DataBlock MyDataBlock; |
emh203 | 0:085749c8446f | 80 | |
emh203 | 0:085749c8446f | 81 | BYTE ActiveDataBlock; |
emh203 | 0:085749c8446f | 82 | DWORD DataBlocksWritten; |
emh203 | 0:085749c8446f | 83 | DWORD DataPointIndex; |
emh203 | 0:085749c8446f | 84 | SIGNED_DWORD ReadWriteDifferential; |
emh203 | 0:085749c8446f | 85 | |
emh203 | 0:085749c8446f | 86 | DWORD WritesSinceLastFlush; |
emh203 | 0:085749c8446f | 87 | |
emh203 | 0:085749c8446f | 88 | #define BINARY_WRITE_CACHE_THRESHOLD 512 |
emh203 | 0:085749c8446f | 89 | BYTE BinaryDataCache[BINARY_WRITE_CACHE_THRESHOLD + 64]; |
emh203 | 0:085749c8446f | 90 | DWORD BinaryDataCachePtr; |
emh203 | 0:085749c8446f | 91 | |
emh203 | 0:085749c8446f | 92 | #define NUM_WRITES_BEFORE_FLUSH (10) |
emh203 | 0:085749c8446f | 93 | |
emh203 | 0:085749c8446f | 94 | //***************************************************************** |
emh203 | 0:085749c8446f | 95 | //Misc System Related Stuff |
emh203 | 0:085749c8446f | 96 | //***************************************************************** |
emh203 | 0:085749c8446f | 97 | void SystemTickIrq(); |
emh203 | 0:085749c8446f | 98 | void AdjustIOffset(); |
emh203 | 0:085749c8446f | 99 | |
emh203 | 0:085749c8446f | 100 | void InitRobotPowerMeasurementSystem() |
emh203 | 0:085749c8446f | 101 | { |
emh203 | 0:085749c8446f | 102 | InitDataBlocks(&MyDataBlock); |
emh203 | 0:085749c8446f | 103 | |
emh203 | 0:085749c8446f | 104 | ActiveDataBlock = 0; |
emh203 | 0:085749c8446f | 105 | DataBlocksWritten = 0; |
emh203 | 0:085749c8446f | 106 | DataPointIndex = 0; |
emh203 | 0:085749c8446f | 107 | |
emh203 | 0:085749c8446f | 108 | SystemTick.attach_us(&SystemTickIrq,10000); |
emh203 | 0:085749c8446f | 109 | CurrentTime_InSec = time(NULL); |
emh203 | 0:085749c8446f | 110 | |
emh203 | 0:085749c8446f | 111 | DataLogError = FALSE; |
emh203 | 0:085749c8446f | 112 | } |
emh203 | 0:085749c8446f | 113 | |
emh203 | 0:085749c8446f | 114 | |
emh203 | 0:085749c8446f | 115 | void InitDataBlocks(DataBlock * DB) |
emh203 | 0:085749c8446f | 116 | { |
emh203 | 0:085749c8446f | 117 | int i; |
emh203 | 0:085749c8446f | 118 | |
emh203 | 0:085749c8446f | 119 | for(i=0;i<DATA_BLOCK_SIZE;i++) |
emh203 | 0:085749c8446f | 120 | { |
emh203 | 0:085749c8446f | 121 | DB->Voltage[i] = 0; |
emh203 | 0:085749c8446f | 122 | DB->Current[i] = 0; |
emh203 | 0:085749c8446f | 123 | } |
emh203 | 0:085749c8446f | 124 | |
emh203 | 0:085749c8446f | 125 | DB->WriteOutPtr = 0; |
emh203 | 0:085749c8446f | 126 | DB->ReadInPtr = 0; |
emh203 | 0:085749c8446f | 127 | } |
emh203 | 0:085749c8446f | 128 | |
emh203 | 0:085749c8446f | 129 | |
emh203 | 0:085749c8446f | 130 | void EnterSystemState(BYTE NextState) |
emh203 | 0:085749c8446f | 131 | { |
emh203 | 0:085749c8446f | 132 | switch(NextState) |
emh203 | 0:085749c8446f | 133 | { |
emh203 | 0:085749c8446f | 134 | default: |
emh203 | 0:085749c8446f | 135 | case SYSTEM_STATE_INIT: |
emh203 | 0:085749c8446f | 136 | DisplayMode = DISPLAY_MEASUREMENTS; |
emh203 | 0:085749c8446f | 137 | SystemState = NextState; |
emh203 | 0:085749c8446f | 138 | break; |
emh203 | 0:085749c8446f | 139 | |
emh203 | 0:085749c8446f | 140 | case SYSTEM_STATE_LOGGING: |
emh203 | 0:085749c8446f | 141 | DisplayMode = DISPLAY_CARD_STATUS; |
emh203 | 0:085749c8446f | 142 | |
emh203 | 0:085749c8446f | 143 | if(InitDataLog() == FALSE) |
emh203 | 0:085749c8446f | 144 | { |
emh203 | 0:085749c8446f | 145 | SystemState = NextState; |
emh203 | 0:085749c8446f | 146 | |
emh203 | 0:085749c8446f | 147 | } |
emh203 | 0:085749c8446f | 148 | else |
emh203 | 0:085749c8446f | 149 | { |
emh203 | 0:085749c8446f | 150 | SystemState = SYSTEM_STATE_IDLE; |
emh203 | 0:085749c8446f | 151 | } |
emh203 | 0:085749c8446f | 152 | |
emh203 | 0:085749c8446f | 153 | break; |
emh203 | 0:085749c8446f | 154 | |
emh203 | 0:085749c8446f | 155 | case SYSTEM_STATE_IDLE: |
emh203 | 0:085749c8446f | 156 | DisplayMode = DISPLAY_CARD_STATUS; |
emh203 | 0:085749c8446f | 157 | if(SystemState == SYSTEM_STATE_LOGGING) |
emh203 | 0:085749c8446f | 158 | { |
emh203 | 0:085749c8446f | 159 | f_close(&CurrentLogFileHandle); |
emh203 | 0:085749c8446f | 160 | f_mount(0,NULL); |
emh203 | 0:085749c8446f | 161 | PrintfEnqueue(&PCBackDoorTx,"Logging terminated on file %s\r\n>",CurrentLogFileName); |
emh203 | 0:085749c8446f | 162 | } |
emh203 | 0:085749c8446f | 163 | |
emh203 | 0:085749c8446f | 164 | SystemState = NextState; |
emh203 | 0:085749c8446f | 165 | break; |
emh203 | 0:085749c8446f | 166 | } |
emh203 | 0:085749c8446f | 167 | } |
emh203 | 0:085749c8446f | 168 | |
emh203 | 0:085749c8446f | 169 | BOOL InitDataLog() |
emh203 | 0:085749c8446f | 170 | { |
emh203 | 0:085749c8446f | 171 | UINT BytesWritten; |
emh203 | 0:085749c8446f | 172 | |
emh203 | 0:085749c8446f | 173 | PrintfEnqueue(&PCBackDoorTx,"\r\n\r\bInitializing Data log....\r\n"); |
emh203 | 0:085749c8446f | 174 | CreateLogFileName(); |
emh203 | 0:085749c8446f | 175 | PrintfEnqueue(&PCBackDoorTx,"Filename: %s\r\n",CurrentLogFileName); |
emh203 | 0:085749c8446f | 176 | PrintfEnqueue(&PCBackDoorTx,"Attempting File Open....\r\n"); |
emh203 | 0:085749c8446f | 177 | |
emh203 | 0:085749c8446f | 178 | BinaryDataCachePtr = 0; |
emh203 | 0:085749c8446f | 179 | WritesSinceLastFlush = 0; |
emh203 | 0:085749c8446f | 180 | |
emh203 | 0:085749c8446f | 181 | f_mount(0,&MyFileSystem); |
emh203 | 0:085749c8446f | 182 | if(f_open(&CurrentLogFileHandle,&CurrentLogFileName[0],FA_WRITE | FA_OPEN_ALWAYS) != FR_OK) |
emh203 | 0:085749c8446f | 183 | { |
emh203 | 0:085749c8446f | 184 | DataLogError = TRUE; |
emh203 | 0:085749c8446f | 185 | PrintfEnqueue(&PCBackDoorTx,"Could not open file!\r\n>"); |
emh203 | 0:085749c8446f | 186 | ErrorMsg = "Write Error!"; |
emh203 | 0:085749c8446f | 187 | return TRUE; |
emh203 | 0:085749c8446f | 188 | } |
emh203 | 0:085749c8446f | 189 | |
emh203 | 0:085749c8446f | 190 | DataBlocksWritten = 0; |
emh203 | 0:085749c8446f | 191 | DataPointIndex = 0; |
emh203 | 0:085749c8446f | 192 | |
emh203 | 0:085749c8446f | 193 | DataLogError = FALSE; |
emh203 | 0:085749c8446f | 194 | PrintfEnqueue(&PCBackDoorTx,"Writing Headers....\r\n"); |
emh203 | 0:085749c8446f | 195 | time(&CurrentTime_InSec); |
emh203 | 0:085749c8446f | 196 | strftime(DTBuf,128, "%Y.%m.%d", localtime(&CurrentTime_InSec)); |
emh203 | 0:085749c8446f | 197 | f_printf(&CurrentLogFileHandle, "Date %s\r\n",DTBuf); |
emh203 | 0:085749c8446f | 198 | strftime(DTBuf,128, "%H:%M:%S", localtime(&CurrentTime_InSec)); |
emh203 | 0:085749c8446f | 199 | f_printf(&CurrentLogFileHandle, "Time %s\r\n\n",DTBuf); |
emh203 | 0:085749c8446f | 200 | sprintf(DTBuf, "Sample Rate %.1f Hz\r\n\r\n",SAMPLE_RATE); |
emh203 | 0:085749c8446f | 201 | f_write(&CurrentLogFileHandle,DTBuf,strlen(DTBuf),&BytesWritten); |
emh203 | 0:085749c8446f | 202 | PrintfEnqueue(&PCBackDoorTx,"Headers Written.... Starting log\r\n"); |
emh203 | 0:085749c8446f | 203 | |
emh203 | 0:085749c8446f | 204 | |
emh203 | 0:085749c8446f | 205 | PrintfEnqueue(&PCBackDoorTx,"\r\n>"); |
emh203 | 0:085749c8446f | 206 | |
emh203 | 0:085749c8446f | 207 | return FALSE; |
emh203 | 0:085749c8446f | 208 | } |
emh203 | 0:085749c8446f | 209 | |
emh203 | 0:085749c8446f | 210 | |
emh203 | 0:085749c8446f | 211 | void CreateLogFileName() |
emh203 | 0:085749c8446f | 212 | { |
emh203 | 0:085749c8446f | 213 | time(&CurrentTime_InSec); |
emh203 | 0:085749c8446f | 214 | strftime(CurrentLogFileName,256, "F%Y.%m.%d.%H.%M.%S.csv", localtime(&CurrentTime_InSec)); |
emh203 | 0:085749c8446f | 215 | } |
emh203 | 0:085749c8446f | 216 | |
emh203 | 0:085749c8446f | 217 | |
emh203 | 0:085749c8446f | 218 | |
emh203 | 0:085749c8446f | 219 | void SystemTickIrq() |
emh203 | 0:085749c8446f | 220 | { |
emh203 | 0:085749c8446f | 221 | if(UpdateHostTerminalTick<0xFFFF) |
emh203 | 0:085749c8446f | 222 | UpdateHostTerminalTick++; |
emh203 | 0:085749c8446f | 223 | |
emh203 | 0:085749c8446f | 224 | if(DisplayUpdateTick<0xFFFF) |
emh203 | 0:085749c8446f | 225 | DisplayUpdateTick++; |
emh203 | 0:085749c8446f | 226 | |
emh203 | 0:085749c8446f | 227 | if(ButtonCheckTick<0xFFFF) |
emh203 | 0:085749c8446f | 228 | ButtonCheckTick++; |
emh203 | 0:085749c8446f | 229 | |
emh203 | 0:085749c8446f | 230 | if(DisplayShutOffTick<0xFFFF) |
emh203 | 0:085749c8446f | 231 | DisplayShutOffTick++; |
emh203 | 0:085749c8446f | 232 | } |
emh203 | 0:085749c8446f | 233 | |
emh203 | 0:085749c8446f | 234 | void InitButton() |
emh203 | 0:085749c8446f | 235 | { |
emh203 | 0:085749c8446f | 236 | ButtonCheckState = WAITING_FOR_PRESS; |
emh203 | 0:085749c8446f | 237 | ButtonCheckTick = 0; |
emh203 | 0:085749c8446f | 238 | PreviousButtonState = FALSE; |
emh203 | 0:085749c8446f | 239 | CurrentButtonState = FALSE; |
emh203 | 0:085749c8446f | 240 | |
emh203 | 0:085749c8446f | 241 | } |
emh203 | 0:085749c8446f | 242 | void CheckButton() |
emh203 | 0:085749c8446f | 243 | { |
emh203 | 0:085749c8446f | 244 | if(ButtonCheckTick>0) |
emh203 | 0:085749c8446f | 245 | { |
emh203 | 0:085749c8446f | 246 | ButtonCheckTick = 0; |
emh203 | 0:085749c8446f | 247 | |
emh203 | 0:085749c8446f | 248 | PreviousButtonState = CurrentButtonState; |
emh203 | 0:085749c8446f | 249 | CurrentButtonState = ActionButton.read(); |
emh203 | 0:085749c8446f | 250 | |
emh203 | 0:085749c8446f | 251 | switch(ButtonCheckState) |
emh203 | 0:085749c8446f | 252 | { |
emh203 | 0:085749c8446f | 253 | default: |
emh203 | 0:085749c8446f | 254 | case WAITING_FOR_PRESS: |
emh203 | 0:085749c8446f | 255 | |
emh203 | 0:085749c8446f | 256 | if(CurrentButtonState == TRUE && PreviousButtonState == FALSE) |
emh203 | 0:085749c8446f | 257 | { |
emh203 | 0:085749c8446f | 258 | ButtonCheckState = WAITING_FOR_RELEASE; |
emh203 | 0:085749c8446f | 259 | } |
emh203 | 0:085749c8446f | 260 | |
emh203 | 0:085749c8446f | 261 | ButtonHoldTime = 0; |
emh203 | 0:085749c8446f | 262 | |
emh203 | 0:085749c8446f | 263 | break; |
emh203 | 0:085749c8446f | 264 | |
emh203 | 0:085749c8446f | 265 | case WAITING_FOR_RELEASE: |
emh203 | 0:085749c8446f | 266 | |
emh203 | 0:085749c8446f | 267 | if(CurrentButtonState == TRUE && PreviousButtonState == TRUE) |
emh203 | 0:085749c8446f | 268 | { |
emh203 | 0:085749c8446f | 269 | ButtonHoldTime++; |
emh203 | 0:085749c8446f | 270 | } |
emh203 | 0:085749c8446f | 271 | else if(CurrentButtonState == FALSE && PreviousButtonState == TRUE) |
emh203 | 0:085749c8446f | 272 | { |
emh203 | 0:085749c8446f | 273 | ActionButtonHandler(ButtonHoldTime); |
emh203 | 0:085749c8446f | 274 | ButtonCheckState = WAITING_FOR_PRESS; |
emh203 | 0:085749c8446f | 275 | } |
emh203 | 0:085749c8446f | 276 | else |
emh203 | 0:085749c8446f | 277 | { |
emh203 | 0:085749c8446f | 278 | ButtonCheckState = WAITING_FOR_PRESS; |
emh203 | 0:085749c8446f | 279 | } |
emh203 | 0:085749c8446f | 280 | break; |
emh203 | 0:085749c8446f | 281 | } |
emh203 | 0:085749c8446f | 282 | } |
emh203 | 0:085749c8446f | 283 | } |
emh203 | 0:085749c8446f | 284 | |
emh203 | 0:085749c8446f | 285 | |
emh203 | 0:085749c8446f | 286 | void ActionButtonHandler(WORD ButtonPressTime) |
emh203 | 0:085749c8446f | 287 | { |
emh203 | 0:085749c8446f | 288 | DisplayShutOffTick = 0; |
emh203 | 0:085749c8446f | 289 | |
emh203 | 0:085749c8446f | 290 | if(ButtonPressTime<50) |
emh203 | 0:085749c8446f | 291 | { |
emh203 | 0:085749c8446f | 292 | if(DisplayMode == DISPLAY_OFF) |
emh203 | 0:085749c8446f | 293 | { |
emh203 | 0:085749c8446f | 294 | PowerUpSmartSwitch(); |
emh203 | 0:085749c8446f | 295 | SmartSwitch_Reset(); |
emh203 | 0:085749c8446f | 296 | SmartSwitchClear(); |
emh203 | 0:085749c8446f | 297 | DisplayMode = DISPLAY_MEASUREMENTS; |
emh203 | 0:085749c8446f | 298 | } |
emh203 | 0:085749c8446f | 299 | else |
emh203 | 0:085749c8446f | 300 | { |
emh203 | 0:085749c8446f | 301 | |
emh203 | 0:085749c8446f | 302 | if(DataLogError == TRUE) |
emh203 | 0:085749c8446f | 303 | { |
emh203 | 0:085749c8446f | 304 | DataLogError = FALSE; |
emh203 | 0:085749c8446f | 305 | } |
emh203 | 0:085749c8446f | 306 | else |
emh203 | 0:085749c8446f | 307 | { |
emh203 | 0:085749c8446f | 308 | DisplayMode++; |
emh203 | 0:085749c8446f | 309 | |
emh203 | 0:085749c8446f | 310 | if(DisplayMode >= NUM_DISPLAYS) |
emh203 | 0:085749c8446f | 311 | { |
emh203 | 0:085749c8446f | 312 | DisplayMode = 0; |
emh203 | 0:085749c8446f | 313 | } |
emh203 | 0:085749c8446f | 314 | } |
emh203 | 0:085749c8446f | 315 | } |
emh203 | 0:085749c8446f | 316 | } |
emh203 | 0:085749c8446f | 317 | |
emh203 | 0:085749c8446f | 318 | else if(ButtonPressTime>100 && ButtonPressTime<500) |
emh203 | 0:085749c8446f | 319 | { |
emh203 | 0:085749c8446f | 320 | switch(SystemState) |
emh203 | 0:085749c8446f | 321 | { |
emh203 | 0:085749c8446f | 322 | default: |
emh203 | 0:085749c8446f | 323 | case SYSTEM_STATE_IDLE: |
emh203 | 0:085749c8446f | 324 | switch(DisplayMode) |
emh203 | 0:085749c8446f | 325 | { |
emh203 | 0:085749c8446f | 326 | default: |
emh203 | 0:085749c8446f | 327 | |
emh203 | 0:085749c8446f | 328 | break; |
emh203 | 0:085749c8446f | 329 | |
emh203 | 0:085749c8446f | 330 | case DISPLAY_MEASUREMENTS: |
emh203 | 0:085749c8446f | 331 | AdjustIOffset(); |
emh203 | 0:085749c8446f | 332 | break; |
emh203 | 0:085749c8446f | 333 | |
emh203 | 0:085749c8446f | 334 | |
emh203 | 0:085749c8446f | 335 | case DISPLAY_FIRMWARE_VERSION: |
emh203 | 0:085749c8446f | 336 | LoadConfiguration(); |
emh203 | 0:085749c8446f | 337 | break; |
emh203 | 0:085749c8446f | 338 | |
emh203 | 0:085749c8446f | 339 | |
emh203 | 0:085749c8446f | 340 | case DISPLAY_CARD_STATUS: |
emh203 | 0:085749c8446f | 341 | if(SDCardDetect == 1) |
emh203 | 0:085749c8446f | 342 | { |
emh203 | 0:085749c8446f | 343 | EnterSystemState(SYSTEM_STATE_LOGGING); |
emh203 | 0:085749c8446f | 344 | } |
emh203 | 0:085749c8446f | 345 | break; |
emh203 | 0:085749c8446f | 346 | } |
emh203 | 0:085749c8446f | 347 | break; |
emh203 | 0:085749c8446f | 348 | |
emh203 | 0:085749c8446f | 349 | |
emh203 | 0:085749c8446f | 350 | case SYSTEM_STATE_LOGGING: |
emh203 | 0:085749c8446f | 351 | EnterSystemState(SYSTEM_STATE_IDLE); |
emh203 | 0:085749c8446f | 352 | // unmount the file system |
emh203 | 0:085749c8446f | 353 | f_mount(0,NULL); |
emh203 | 0:085749c8446f | 354 | break; |
emh203 | 0:085749c8446f | 355 | } |
emh203 | 0:085749c8446f | 356 | } |
emh203 | 0:085749c8446f | 357 | } |
emh203 | 0:085749c8446f | 358 | |
emh203 | 0:085749c8446f | 359 | void AdjustIOffset() |
emh203 | 0:085749c8446f | 360 | { |
emh203 | 0:085749c8446f | 361 | DWORD SamplesToTake; |
emh203 | 0:085749c8446f | 362 | DWORD i; |
emh203 | 0:085749c8446f | 363 | float RunningSum = 0; |
emh203 | 0:085749c8446f | 364 | |
emh203 | 0:085749c8446f | 365 | PrintfEnqueue(&PCBackDoorTx,"Zeroing ACS576.....\r\n"); |
emh203 | 0:085749c8446f | 366 | |
emh203 | 0:085749c8446f | 367 | ADCDataRdy=0; |
emh203 | 0:085749c8446f | 368 | |
emh203 | 0:085749c8446f | 369 | SamplesToTake = (DWORD)(SAMPLE_RATE)*2; |
emh203 | 0:085749c8446f | 370 | |
emh203 | 0:085749c8446f | 371 | for(i=0;i<SamplesToTake;i++) |
emh203 | 0:085749c8446f | 372 | { |
emh203 | 0:085749c8446f | 373 | SmartSwitch_SetBackLightColor2((BYTE)((float)i/(float)SamplesToTake * 64)); |
emh203 | 0:085749c8446f | 374 | |
emh203 | 0:085749c8446f | 375 | while(ADCDataRdy == FALSE) |
emh203 | 0:085749c8446f | 376 | { |
emh203 | 0:085749c8446f | 377 | |
emh203 | 0:085749c8446f | 378 | } |
emh203 | 0:085749c8446f | 379 | ADCDataRdy = FALSE; |
emh203 | 0:085749c8446f | 380 | RunningSum += RobotBusCurrentHR; |
emh203 | 0:085749c8446f | 381 | } |
emh203 | 0:085749c8446f | 382 | |
emh203 | 0:085749c8446f | 383 | ACS576_IOFFSET_TRIM += RunningSum / SamplesToTake; |
emh203 | 0:085749c8446f | 384 | |
emh203 | 0:085749c8446f | 385 | SmartSwitch_SetBackLightColor(3,3,3); |
emh203 | 0:085749c8446f | 386 | |
emh203 | 0:085749c8446f | 387 | PrintfEnqueue(&PCBackDoorTx,"Exporting new configuration file.....\r\n>"); |
emh203 | 0:085749c8446f | 388 | |
emh203 | 0:085749c8446f | 389 | ExportConfigurationSettings(); |
emh203 | 0:085749c8446f | 390 | } |
emh203 | 0:085749c8446f | 391 | |
emh203 | 0:085749c8446f | 392 | |
emh203 | 0:085749c8446f | 393 | /*void UpdateHostTerminal() |
emh203 | 0:085749c8446f | 394 | { |
emh203 | 0:085749c8446f | 395 | BYTE NextCharIn; |
emh203 | 0:085749c8446f | 396 | |
emh203 | 0:085749c8446f | 397 | if(BytesInQueue(&PCBackDoorRx)>0) |
emh203 | 0:085749c8446f | 398 | { |
emh203 | 0:085749c8446f | 399 | ByteDequeue(&PCBackDoorRx,&NextCharIn); |
emh203 | 0:085749c8446f | 400 | |
emh203 | 0:085749c8446f | 401 | switch(NextCharIn) |
emh203 | 0:085749c8446f | 402 | { |
emh203 | 0:085749c8446f | 403 | case '\r': |
emh203 | 0:085749c8446f | 404 | |
emh203 | 0:085749c8446f | 405 | TerminalLineBuf[TerminalPos++] = 0x0; |
emh203 | 0:085749c8446f | 406 | ByteEnqueue(&PCBackDoorTx,NextCharIn); |
emh203 | 0:085749c8446f | 407 | ProcessTerminalLine(TerminalLineBuf); |
emh203 | 0:085749c8446f | 408 | ByteEnqueue(&PCBackDoorTx,'\n'); |
emh203 | 0:085749c8446f | 409 | ByteEnqueue(&PCBackDoorTx,'>'); |
emh203 | 0:085749c8446f | 410 | TerminalPos = 0; |
emh203 | 0:085749c8446f | 411 | |
emh203 | 0:085749c8446f | 412 | break; |
emh203 | 0:085749c8446f | 413 | |
emh203 | 0:085749c8446f | 414 | case '\b': |
emh203 | 0:085749c8446f | 415 | if(TerminalPos > 0) |
emh203 | 0:085749c8446f | 416 | { |
emh203 | 0:085749c8446f | 417 | TerminalPos--; |
emh203 | 0:085749c8446f | 418 | ByteEnqueue(&PCBackDoorTx,NextCharIn); |
emh203 | 0:085749c8446f | 419 | } |
emh203 | 0:085749c8446f | 420 | break; |
emh203 | 0:085749c8446f | 421 | |
emh203 | 0:085749c8446f | 422 | default: |
emh203 | 0:085749c8446f | 423 | |
emh203 | 0:085749c8446f | 424 | if(NextCharIn > 32) |
emh203 | 0:085749c8446f | 425 | { |
emh203 | 0:085749c8446f | 426 | if(TerminalPos < MAX_TERMINAL_LINE_CHARS-1) |
emh203 | 0:085749c8446f | 427 | { |
emh203 | 0:085749c8446f | 428 | TerminalLineBuf[TerminalPos++] = NextCharIn; |
emh203 | 0:085749c8446f | 429 | ByteEnqueue(&PCBackDoorTx,NextCharIn); |
emh203 | 0:085749c8446f | 430 | } |
emh203 | 0:085749c8446f | 431 | } |
emh203 | 0:085749c8446f | 432 | |
emh203 | 0:085749c8446f | 433 | break; |
emh203 | 0:085749c8446f | 434 | |
emh203 | 0:085749c8446f | 435 | } |
emh203 | 0:085749c8446f | 436 | } |
emh203 | 0:085749c8446f | 437 | |
emh203 | 0:085749c8446f | 438 | } |
emh203 | 0:085749c8446f | 439 | */ |
emh203 | 0:085749c8446f | 440 | |
emh203 | 0:085749c8446f | 441 | BOOL ProcessTerminalLine(CHAR * LineIn) |
emh203 | 0:085749c8446f | 442 | { |
emh203 | 0:085749c8446f | 443 | CheckForTime(LineIn); |
emh203 | 0:085749c8446f | 444 | CheckForDate(LineIn); |
emh203 | 0:085749c8446f | 445 | CheckForTimeInit(LineIn); |
emh203 | 0:085749c8446f | 446 | CheckforConfig(LineIn); |
emh203 | 0:085749c8446f | 447 | return TRUE; |
emh203 | 0:085749c8446f | 448 | } |
emh203 | 0:085749c8446f | 449 | |
emh203 | 0:085749c8446f | 450 | BOOL CheckforConfig(CHAR *LineIn) |
emh203 | 0:085749c8446f | 451 | { |
emh203 | 0:085749c8446f | 452 | if(!strcmp(LineIn,"config")) |
emh203 | 0:085749c8446f | 453 | { |
emh203 | 0:085749c8446f | 454 | LoadConfiguration(); |
emh203 | 0:085749c8446f | 455 | } |
emh203 | 0:085749c8446f | 456 | return TRUE; |
emh203 | 0:085749c8446f | 457 | } |
emh203 | 0:085749c8446f | 458 | |
emh203 | 0:085749c8446f | 459 | |
emh203 | 0:085749c8446f | 460 | BOOL CheckForTimeInit(CHAR * LineIn) |
emh203 | 0:085749c8446f | 461 | { |
emh203 | 0:085749c8446f | 462 | if(!strcmp(LineIn,"tinit")) |
emh203 | 0:085749c8446f | 463 | { |
emh203 | 0:085749c8446f | 464 | set_time(1256729737); |
emh203 | 0:085749c8446f | 465 | PrintfEnqueue(&PCBackDoorTx,"\r\nTime Reset\r\n"); |
emh203 | 0:085749c8446f | 466 | } |
emh203 | 0:085749c8446f | 467 | |
emh203 | 0:085749c8446f | 468 | return TRUE; |
emh203 | 0:085749c8446f | 469 | |
emh203 | 0:085749c8446f | 470 | } |
emh203 | 0:085749c8446f | 471 | |
emh203 | 0:085749c8446f | 472 | BOOL CheckForTime(CHAR *LineIn) |
emh203 | 0:085749c8446f | 473 | { |
emh203 | 0:085749c8446f | 474 | int Hour,Minute,Second=0; |
emh203 | 0:085749c8446f | 475 | int Items; |
emh203 | 0:085749c8446f | 476 | BOOL Error = FALSE; |
emh203 | 0:085749c8446f | 477 | time_t TimeStampTemp; |
emh203 | 0:085749c8446f | 478 | |
emh203 | 0:085749c8446f | 479 | |
emh203 | 0:085749c8446f | 480 | Items = sscanf(LineIn, "time=%d : %d : %d", &Hour, &Minute, &Second); |
emh203 | 0:085749c8446f | 481 | if(Items == 3) |
emh203 | 0:085749c8446f | 482 | { |
emh203 | 0:085749c8446f | 483 | PrintfEnqueue(&PCBackDoorTx,"\r\n"); |
emh203 | 0:085749c8446f | 484 | if(Hour>23) |
emh203 | 0:085749c8446f | 485 | { |
emh203 | 0:085749c8446f | 486 | PrintfEnqueue(&PCBackDoorTx,"Hour entry must be between 0 and 24\r\n"); |
emh203 | 0:085749c8446f | 487 | Error = TRUE; |
emh203 | 0:085749c8446f | 488 | } |
emh203 | 0:085749c8446f | 489 | if(Minute>60) |
emh203 | 0:085749c8446f | 490 | { |
emh203 | 0:085749c8446f | 491 | PrintfEnqueue(&PCBackDoorTx,"Minute entry must be between 0 and 60\r\n"); |
emh203 | 0:085749c8446f | 492 | Error = TRUE; |
emh203 | 0:085749c8446f | 493 | } |
emh203 | 0:085749c8446f | 494 | if(Second>60) |
emh203 | 0:085749c8446f | 495 | { |
emh203 | 0:085749c8446f | 496 | PrintfEnqueue(&PCBackDoorTx,"Second entry must be between 0 and 60\r\n"); |
emh203 | 0:085749c8446f | 497 | Error = TRUE; |
emh203 | 0:085749c8446f | 498 | } |
emh203 | 0:085749c8446f | 499 | |
emh203 | 0:085749c8446f | 500 | if(Error == TRUE) |
emh203 | 0:085749c8446f | 501 | { |
emh203 | 0:085749c8446f | 502 | PrintfEnqueue(&PCBackDoorTx,"Error in time format. Time not changed.\r\n"); |
emh203 | 0:085749c8446f | 503 | } |
emh203 | 0:085749c8446f | 504 | else |
emh203 | 0:085749c8446f | 505 | { |
emh203 | 0:085749c8446f | 506 | TimeStampTemp = time(NULL); |
emh203 | 0:085749c8446f | 507 | //convert to tm struct |
emh203 | 0:085749c8446f | 508 | CurrentTime = localtime(&TimeStampTemp); |
emh203 | 0:085749c8446f | 509 | //dump in our new valus |
emh203 | 0:085749c8446f | 510 | CurrentTime->tm_sec = Second; |
emh203 | 0:085749c8446f | 511 | CurrentTime->tm_min = Minute; |
emh203 | 0:085749c8446f | 512 | CurrentTime->tm_hour = Hour; |
emh203 | 0:085749c8446f | 513 | //set the new time |
emh203 | 0:085749c8446f | 514 | set_time(mktime(CurrentTime)); |
emh203 | 0:085749c8446f | 515 | PrintfEnqueue(&PCBackDoorTx,"Time set to %d:%d:%d\r\n",Hour,Minute,Second); |
emh203 | 0:085749c8446f | 516 | } |
emh203 | 0:085749c8446f | 517 | |
emh203 | 0:085749c8446f | 518 | } |
emh203 | 0:085749c8446f | 519 | return FALSE; |
emh203 | 0:085749c8446f | 520 | } |
emh203 | 0:085749c8446f | 521 | |
emh203 | 0:085749c8446f | 522 | BOOL CheckForDate(CHAR *LineIn) |
emh203 | 0:085749c8446f | 523 | { |
emh203 | 0:085749c8446f | 524 | int Day,Month,Year=0; |
emh203 | 0:085749c8446f | 525 | int Items; |
emh203 | 0:085749c8446f | 526 | BOOL Error = FALSE; |
emh203 | 0:085749c8446f | 527 | time_t TimeStampTemp; |
emh203 | 0:085749c8446f | 528 | |
emh203 | 0:085749c8446f | 529 | Items = sscanf(LineIn, "date=%d / %d / %d", &Month, &Day, &Year); |
emh203 | 0:085749c8446f | 530 | if(Items == 3) |
emh203 | 0:085749c8446f | 531 | { |
emh203 | 0:085749c8446f | 532 | PrintfEnqueue(&PCBackDoorTx,"\r\n"); |
emh203 | 0:085749c8446f | 533 | if(Month>12 || Month < 1) |
emh203 | 0:085749c8446f | 534 | { |
emh203 | 0:085749c8446f | 535 | PrintfEnqueue(&PCBackDoorTx,"Month entry must be between 1 and 12\r\n"); |
emh203 | 0:085749c8446f | 536 | Error = TRUE; |
emh203 | 0:085749c8446f | 537 | } |
emh203 | 0:085749c8446f | 538 | if(Day>31 || Day<1) |
emh203 | 0:085749c8446f | 539 | { |
emh203 | 0:085749c8446f | 540 | PrintfEnqueue(&PCBackDoorTx,"Day entry must be between 1 and 31\r\n"); |
emh203 | 0:085749c8446f | 541 | Error = TRUE; |
emh203 | 0:085749c8446f | 542 | } |
emh203 | 0:085749c8446f | 543 | if(Year<1900) |
emh203 | 0:085749c8446f | 544 | { |
emh203 | 0:085749c8446f | 545 | PrintfEnqueue(&PCBackDoorTx,"Year entry must be greater than 1900\r\n"); |
emh203 | 0:085749c8446f | 546 | Error = TRUE; |
emh203 | 0:085749c8446f | 547 | } |
emh203 | 0:085749c8446f | 548 | |
emh203 | 0:085749c8446f | 549 | if(Error == TRUE) |
emh203 | 0:085749c8446f | 550 | { |
emh203 | 0:085749c8446f | 551 | PrintfEnqueue(&PCBackDoorTx,"Error in time format. Date not changed.\r\n"); |
emh203 | 0:085749c8446f | 552 | } |
emh203 | 0:085749c8446f | 553 | else |
emh203 | 0:085749c8446f | 554 | { |
emh203 | 0:085749c8446f | 555 | //Get the current time in seconds since unix epoch |
emh203 | 0:085749c8446f | 556 | TimeStampTemp = time(NULL); |
emh203 | 0:085749c8446f | 557 | //convert to tm struct fom |
emh203 | 0:085749c8446f | 558 | CurrentTime = localtime(&TimeStampTemp); |
emh203 | 0:085749c8446f | 559 | //dump in our new valus |
emh203 | 0:085749c8446f | 560 | CurrentTime->tm_mon = Month-1; |
emh203 | 0:085749c8446f | 561 | CurrentTime->tm_mday = Day; |
emh203 | 0:085749c8446f | 562 | CurrentTime->tm_year = Year - 1900; |
emh203 | 0:085749c8446f | 563 | //set the new time |
emh203 | 0:085749c8446f | 564 | set_time(mktime(CurrentTime)); |
emh203 | 0:085749c8446f | 565 | PrintfEnqueue(&PCBackDoorTx,"Date set to %d/%d/%d\r\n",Month,Day,Year); |
emh203 | 0:085749c8446f | 566 | } |
emh203 | 0:085749c8446f | 567 | |
emh203 | 0:085749c8446f | 568 | } |
emh203 | 0:085749c8446f | 569 | return FALSE; |
emh203 | 0:085749c8446f | 570 | } |
emh203 | 0:085749c8446f | 571 | |
emh203 | 0:085749c8446f | 572 | |
emh203 | 0:085749c8446f | 573 | |
emh203 | 0:085749c8446f | 574 | void UpdateDisplay() |
emh203 | 0:085749c8446f | 575 | { |
emh203 | 0:085749c8446f | 576 | |
emh203 | 0:085749c8446f | 577 | if(DisplayShutOffTick > DISPLAY_SHUTOFF_TIME) |
emh203 | 0:085749c8446f | 578 | { |
emh203 | 0:085749c8446f | 579 | DisplayMode = DISPLAY_OFF; |
emh203 | 0:085749c8446f | 580 | SmartSwitchClear(); |
emh203 | 0:085749c8446f | 581 | PowerDownSmartSwitch(); |
emh203 | 0:085749c8446f | 582 | } |
emh203 | 0:085749c8446f | 583 | else |
emh203 | 0:085749c8446f | 584 | { |
emh203 | 0:085749c8446f | 585 | if(DisplayUpdateTick > 25) |
emh203 | 0:085749c8446f | 586 | { |
emh203 | 0:085749c8446f | 587 | GFX_FullDisplayBufferClear(&BackBuffer); |
emh203 | 0:085749c8446f | 588 | DisplayUpdateTick = 0; |
emh203 | 0:085749c8446f | 589 | switch(DisplayMode) |
emh203 | 0:085749c8446f | 590 | { |
emh203 | 0:085749c8446f | 591 | default: |
emh203 | 0:085749c8446f | 592 | case DISPLAY_MEASUREMENTS: |
emh203 | 0:085749c8446f | 593 | SmartSwitch_SetBackLightColor(3,3,3); |
emh203 | 0:085749c8446f | 594 | GFX_DrawString(&BackBuffer,"Robot Bus",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 595 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 596 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 597 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 598 | |
emh203 | 0:085749c8446f | 599 | GFX_printf(&BackBuffer,0,16,&Font5x7,"V: %.2fv",RobotBusVoltageHR); |
emh203 | 0:085749c8446f | 600 | GFX_printf(&BackBuffer,0,24,&Font5x7,"I: %.2fa",RobotBusCurrentHR); |
emh203 | 0:085749c8446f | 601 | break; |
emh203 | 0:085749c8446f | 602 | |
emh203 | 0:085749c8446f | 603 | |
emh203 | 0:085749c8446f | 604 | case DISPLAY_BATTERY: |
emh203 | 0:085749c8446f | 605 | SmartSwitch_SetBackLightColor(3,3,0); |
emh203 | 0:085749c8446f | 606 | GFX_DrawString(&BackBuffer,"Battery",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 607 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 608 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 609 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 610 | |
emh203 | 0:085749c8446f | 611 | GFX_printf(&BackBuffer,0,16,&Font5x7,"V: %.1fv",BatteryVoltage); |
emh203 | 0:085749c8446f | 612 | |
emh203 | 0:085749c8446f | 613 | break; |
emh203 | 0:085749c8446f | 614 | |
emh203 | 0:085749c8446f | 615 | case DISPLAY_DATE_TIME: |
emh203 | 0:085749c8446f | 616 | SmartSwitch_SetBackLightColor(1,3,0); |
emh203 | 0:085749c8446f | 617 | GFX_DrawString(&BackBuffer,"Date/Time",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 618 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 619 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 620 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 621 | |
emh203 | 0:085749c8446f | 622 | time(&CurrentTime_InSec); |
emh203 | 0:085749c8446f | 623 | strftime(DTBuf, 32, "%m.%d.%Y", localtime(&CurrentTime_InSec)); |
emh203 | 0:085749c8446f | 624 | GFX_printf(&BackBuffer,0,16,&Font5x7,"%s",DTBuf); |
emh203 | 0:085749c8446f | 625 | |
emh203 | 0:085749c8446f | 626 | strftime(DTBuf, 32, "%H:%M:%S", localtime(&CurrentTime_InSec)); |
emh203 | 0:085749c8446f | 627 | GFX_printf(&BackBuffer,0,24,&Font5x7,"%s",DTBuf); |
emh203 | 0:085749c8446f | 628 | break; |
emh203 | 0:085749c8446f | 629 | |
emh203 | 0:085749c8446f | 630 | case DISPLAY_BUFFER_STATS: |
emh203 | 0:085749c8446f | 631 | SmartSwitch_SetBackLightColor(3,3,3); |
emh203 | 0:085749c8446f | 632 | GFX_DrawString(&BackBuffer,"Buf Status",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 633 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 634 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 635 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 636 | GFX_printf(&BackBuffer,0,16,&Font3x5,"R/W Diff:"); |
emh203 | 0:085749c8446f | 637 | GFX_printf(&BackBuffer,0,24,&Font3x5,"%04d/%d",ReadWriteDifferential,DATA_BLOCK_SIZE); |
emh203 | 0:085749c8446f | 638 | break; |
emh203 | 0:085749c8446f | 639 | |
emh203 | 0:085749c8446f | 640 | case DISPLAY_FIRMWARE_VERSION: |
emh203 | 0:085749c8446f | 641 | SmartSwitch_SetBackLightColor(3,3,3); |
emh203 | 0:085749c8446f | 642 | GFX_DrawString(&BackBuffer,"Firmware Version",0,0,&Font3x5); |
emh203 | 0:085749c8446f | 643 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 644 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 645 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 646 | GFX_printf(&BackBuffer,19,20,&Font5x7,"%d.%d",FIRMWARE_VERSION_MAJOR,FIRMWARE_VERSION_MINOR); |
emh203 | 0:085749c8446f | 647 | break; |
emh203 | 0:085749c8446f | 648 | |
emh203 | 0:085749c8446f | 649 | case DISPLAY_ID: |
emh203 | 0:085749c8446f | 650 | SmartSwitch_SetBackLightColor(3,3,3); |
emh203 | 0:085749c8446f | 651 | GFX_DrawString(&BackBuffer,"ID",0,0,&Font3x5); |
emh203 | 0:085749c8446f | 652 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 653 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 654 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 655 | GFX_printf(&BackBuffer,0,20,&Font5x7,"%s",ID); |
emh203 | 0:085749c8446f | 656 | break; |
emh203 | 0:085749c8446f | 657 | |
emh203 | 0:085749c8446f | 658 | case DISPLAY_CARD_STATUS: |
emh203 | 0:085749c8446f | 659 | |
emh203 | 0:085749c8446f | 660 | if(SDCardDetect == 1) |
emh203 | 0:085749c8446f | 661 | { |
emh203 | 0:085749c8446f | 662 | |
emh203 | 0:085749c8446f | 663 | switch(SystemState) |
emh203 | 0:085749c8446f | 664 | { |
emh203 | 0:085749c8446f | 665 | default: |
emh203 | 0:085749c8446f | 666 | case SYSTEM_STATE_IDLE: |
emh203 | 0:085749c8446f | 667 | switch(LastBackLightColor) |
emh203 | 0:085749c8446f | 668 | { |
emh203 | 0:085749c8446f | 669 | default: |
emh203 | 0:085749c8446f | 670 | case SMART_SWITCH_BACKLIGHT_GREEN: |
emh203 | 0:085749c8446f | 671 | LastBackLightColor = SMART_SWITCH_BACKLIGHT_YELLOW; |
emh203 | 0:085749c8446f | 672 | SmartSwitch_SetBackLightColor2(LastBackLightColor); |
emh203 | 0:085749c8446f | 673 | break; |
emh203 | 0:085749c8446f | 674 | |
emh203 | 0:085749c8446f | 675 | case SMART_SWITCH_BACKLIGHT_YELLOW: |
emh203 | 0:085749c8446f | 676 | LastBackLightColor = SMART_SWITCH_BACKLIGHT_GREEN; |
emh203 | 0:085749c8446f | 677 | SmartSwitch_SetBackLightColor2(LastBackLightColor); |
emh203 | 0:085749c8446f | 678 | break; |
emh203 | 0:085749c8446f | 679 | } |
emh203 | 0:085749c8446f | 680 | |
emh203 | 0:085749c8446f | 681 | |
emh203 | 0:085749c8446f | 682 | if(DataLogError == TRUE) |
emh203 | 0:085749c8446f | 683 | { |
emh203 | 0:085749c8446f | 684 | GFX_DrawString(&BackBuffer,"Error!",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 685 | GFX_DrawString(&BackBuffer,ErrorMsg,0,16,&Font3x5); |
emh203 | 0:085749c8446f | 686 | } |
emh203 | 0:085749c8446f | 687 | else |
emh203 | 0:085749c8446f | 688 | { |
emh203 | 0:085749c8446f | 689 | GFX_DrawString(&BackBuffer,"SD Detected",0,16,&Font5x7); |
emh203 | 0:085749c8446f | 690 | GFX_DrawString(&BackBuffer,"Idle....",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 691 | GFX_DrawString(&BackBuffer,"Not Logging",0,24,&Font5x7); |
emh203 | 0:085749c8446f | 692 | } |
emh203 | 0:085749c8446f | 693 | break; |
emh203 | 0:085749c8446f | 694 | |
emh203 | 0:085749c8446f | 695 | |
emh203 | 0:085749c8446f | 696 | case SYSTEM_STATE_LOGGING: |
emh203 | 0:085749c8446f | 697 | SmartSwitch_SetBackLightColor2(SMART_SWITCH_BACKLIGHT_GREEN); |
emh203 | 0:085749c8446f | 698 | |
emh203 | 0:085749c8446f | 699 | GFX_DrawString(&BackBuffer,"Logging Data....",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 700 | GFX_DrawString(&BackBuffer,&CurrentLogFileName[4],0,16,&Font5x7); |
emh203 | 0:085749c8446f | 701 | GFX_printf(&BackBuffer,0,26,&Font3x5,"Block: %d",DataBlocksWritten); |
emh203 | 0:085749c8446f | 702 | |
emh203 | 0:085749c8446f | 703 | break; |
emh203 | 0:085749c8446f | 704 | } |
emh203 | 0:085749c8446f | 705 | } |
emh203 | 0:085749c8446f | 706 | else |
emh203 | 0:085749c8446f | 707 | { |
emh203 | 0:085749c8446f | 708 | GFX_DrawString(&BackBuffer,"No SD Card!",0,0,&Font5x7); |
emh203 | 0:085749c8446f | 709 | |
emh203 | 0:085749c8446f | 710 | switch(LastBackLightColor) |
emh203 | 0:085749c8446f | 711 | { |
emh203 | 0:085749c8446f | 712 | default: |
emh203 | 0:085749c8446f | 713 | case SMART_SWITCH_BACKLIGHT_RED: |
emh203 | 0:085749c8446f | 714 | LastBackLightColor = SMART_SWITCH_BACKLIGHT_YELLOW; |
emh203 | 0:085749c8446f | 715 | SmartSwitch_SetBackLightColor2(LastBackLightColor); |
emh203 | 0:085749c8446f | 716 | break; |
emh203 | 0:085749c8446f | 717 | |
emh203 | 0:085749c8446f | 718 | case SMART_SWITCH_BACKLIGHT_YELLOW: |
emh203 | 0:085749c8446f | 719 | LastBackLightColor = SMART_SWITCH_BACKLIGHT_RED; |
emh203 | 0:085749c8446f | 720 | SmartSwitch_SetBackLightColor2(LastBackLightColor); |
emh203 | 0:085749c8446f | 721 | break; |
emh203 | 0:085749c8446f | 722 | } |
emh203 | 0:085749c8446f | 723 | } |
emh203 | 0:085749c8446f | 724 | |
emh203 | 0:085749c8446f | 725 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,10); |
emh203 | 0:085749c8446f | 726 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,11); |
emh203 | 0:085749c8446f | 727 | GFX_DrawHline(&BackBuffer,0,PHYSICAL_DISPLAY_XRES-1,12); |
emh203 | 0:085749c8446f | 728 | |
emh203 | 0:085749c8446f | 729 | break; |
emh203 | 0:085749c8446f | 730 | } |
emh203 | 0:085749c8446f | 731 | GFX_DumpRenderContextToPhysicalScreen(&BackBuffer); |
emh203 | 0:085749c8446f | 732 | } |
emh203 | 0:085749c8446f | 733 | } |
emh203 | 0:085749c8446f | 734 | } |
emh203 | 0:085749c8446f | 735 | |
emh203 | 0:085749c8446f | 736 | |
emh203 | 0:085749c8446f | 737 | void CheckSDCardStatus() |
emh203 | 0:085749c8446f | 738 | { |
emh203 | 0:085749c8446f | 739 | //Make sure that the SD card stays in while logging |
emh203 | 0:085749c8446f | 740 | switch(SystemState) |
emh203 | 0:085749c8446f | 741 | { |
emh203 | 0:085749c8446f | 742 | case SYSTEM_STATE_LOGGING: |
emh203 | 0:085749c8446f | 743 | if(SDCardDetect == 0) |
emh203 | 0:085749c8446f | 744 | { |
emh203 | 0:085749c8446f | 745 | //Gracefully shut the logging system down..... |
emh203 | 0:085749c8446f | 746 | EnterSystemState(SYSTEM_STATE_IDLE); |
emh203 | 0:085749c8446f | 747 | } |
emh203 | 0:085749c8446f | 748 | break; |
emh203 | 0:085749c8446f | 749 | |
emh203 | 0:085749c8446f | 750 | default: |
emh203 | 0:085749c8446f | 751 | break; |
emh203 | 0:085749c8446f | 752 | } |
emh203 | 0:085749c8446f | 753 | } |
emh203 | 0:085749c8446f | 754 | |
emh203 | 0:085749c8446f | 755 | |
emh203 | 0:085749c8446f | 756 | void LogData() |
emh203 | 0:085749c8446f | 757 | { |
emh203 | 0:085749c8446f | 758 | WORD i; |
emh203 | 0:085749c8446f | 759 | int ElementsToWrite; |
emh203 | 0:085749c8446f | 760 | UINT BytesWritten; |
emh203 | 0:085749c8446f | 761 | |
emh203 | 0:085749c8446f | 762 | if(RobotBusVoltageHR < 9) |
emh203 | 0:085749c8446f | 763 | { |
emh203 | 0:085749c8446f | 764 | EnterSystemState(SYSTEM_STATE_IDLE); |
emh203 | 0:085749c8446f | 765 | return; |
emh203 | 0:085749c8446f | 766 | } |
emh203 | 0:085749c8446f | 767 | |
emh203 | 0:085749c8446f | 768 | if(DataLogError == TRUE) |
emh203 | 0:085749c8446f | 769 | { |
emh203 | 0:085749c8446f | 770 | EnterSystemState(SYSTEM_STATE_IDLE); |
emh203 | 0:085749c8446f | 771 | } |
emh203 | 0:085749c8446f | 772 | else |
emh203 | 0:085749c8446f | 773 | { |
emh203 | 0:085749c8446f | 774 | if(MyDataBlock.ReadInPtr < MyDataBlock.WriteOutPtr) |
emh203 | 0:085749c8446f | 775 | { |
emh203 | 0:085749c8446f | 776 | ElementsToWrite = MyDataBlock.ReadInPtr + (DATA_BLOCK_SIZE - MyDataBlock.WriteOutPtr); |
emh203 | 0:085749c8446f | 777 | } |
emh203 | 0:085749c8446f | 778 | else |
emh203 | 0:085749c8446f | 779 | { |
emh203 | 0:085749c8446f | 780 | ElementsToWrite = MyDataBlock.ReadInPtr - MyDataBlock.WriteOutPtr; |
emh203 | 0:085749c8446f | 781 | } |
emh203 | 0:085749c8446f | 782 | |
emh203 | 0:085749c8446f | 783 | if(ElementsToWrite > WRITE_BLOCK_THRESH) |
emh203 | 0:085749c8446f | 784 | { |
emh203 | 0:085749c8446f | 785 | for(i=0;i<ElementsToWrite;i++) |
emh203 | 0:085749c8446f | 786 | { |
emh203 | 0:085749c8446f | 787 | BinaryDataCachePtr += sprintf((char *)&BinaryDataCache[BinaryDataCachePtr],"%d,%.2f,%.2f\r\n",DataPointIndex,MyDataBlock.Voltage[MyDataBlock.WriteOutPtr],MyDataBlock.Current[MyDataBlock.WriteOutPtr]); |
emh203 | 0:085749c8446f | 788 | if(BinaryDataCachePtr>=BINARY_WRITE_CACHE_THRESHOLD) |
emh203 | 0:085749c8446f | 789 | { |
emh203 | 0:085749c8446f | 790 | EXT_LED0 = 1; |
emh203 | 0:085749c8446f | 791 | f_write(&CurrentLogFileHandle,BinaryDataCache,BinaryDataCachePtr,&BytesWritten); |
emh203 | 0:085749c8446f | 792 | |
emh203 | 0:085749c8446f | 793 | BinaryDataCachePtr = 0; |
emh203 | 0:085749c8446f | 794 | |
emh203 | 0:085749c8446f | 795 | EXT_LED0=0; |
emh203 | 0:085749c8446f | 796 | } |
emh203 | 0:085749c8446f | 797 | DataPointIndex++; |
emh203 | 0:085749c8446f | 798 | MyDataBlock.WriteOutPtr++; |
emh203 | 0:085749c8446f | 799 | if(MyDataBlock.WriteOutPtr == DATA_BLOCK_SIZE) |
emh203 | 0:085749c8446f | 800 | { |
emh203 | 0:085749c8446f | 801 | MyDataBlock.WriteOutPtr = 0; |
emh203 | 0:085749c8446f | 802 | } |
emh203 | 0:085749c8446f | 803 | } |
emh203 | 0:085749c8446f | 804 | if(WritesSinceLastFlush > NUM_WRITES_BEFORE_FLUSH) |
emh203 | 0:085749c8446f | 805 | { |
emh203 | 0:085749c8446f | 806 | f_close(&CurrentLogFileHandle); |
emh203 | 0:085749c8446f | 807 | WritesSinceLastFlush = 0; |
emh203 | 0:085749c8446f | 808 | f_open(&CurrentLogFileHandle,&CurrentLogFileName[0],FA_WRITE | FA_OPEN_ALWAYS); |
emh203 | 0:085749c8446f | 809 | f_lseek(&CurrentLogFileHandle, CurrentLogFileHandle.fsize); |
emh203 | 0:085749c8446f | 810 | } |
emh203 | 0:085749c8446f | 811 | DataBlocksWritten++; |
emh203 | 0:085749c8446f | 812 | } |
emh203 | 0:085749c8446f | 813 | } |
emh203 | 0:085749c8446f | 814 | } |
emh203 | 0:085749c8446f | 815 | |
emh203 | 0:085749c8446f | 816 | |
emh203 | 0:085749c8446f | 817 | |
emh203 | 0:085749c8446f | 818 | void DisplayBootMsg() |
emh203 | 0:085749c8446f | 819 | { |
emh203 | 0:085749c8446f | 820 | PrintfEnqueue(&PCBackDoorTx,"\r\n\r\n"); |
emh203 | 0:085749c8446f | 821 | PrintfEnqueue(&PCBackDoorTx,".______ _______. __ __ ___ .______ __ \r\n"); |
emh203 | 0:085749c8446f | 822 | PrintfEnqueue(&PCBackDoorTx,"| _ \\ / || | | | / \\ | _ \\ | | \r\n"); |
emh203 | 0:085749c8446f | 823 | PrintfEnqueue(&PCBackDoorTx,"| |_) | | (----`| | | | ______ / ^ \\ | |_) | | | \r\n"); |
emh203 | 0:085749c8446f | 824 | PrintfEnqueue(&PCBackDoorTx,"| ___/ \\ \\ | | | | |______/ /_\\ \\ | / | | \r\n"); |
emh203 | 0:085749c8446f | 825 | PrintfEnqueue(&PCBackDoorTx,"| | .----) | | `--' | / _____ \\ | |\\ \\----.| `----.\r\n"); |
emh203 | 0:085749c8446f | 826 | PrintfEnqueue(&PCBackDoorTx,"| _| |_______/ \\______/ /__/ \\__\\ | _| `._____||_______|\r\n"); |
emh203 | 0:085749c8446f | 827 | PrintfEnqueue(&PCBackDoorTx,"---------------------------------------------------------------------------\r\n"); |
emh203 | 0:085749c8446f | 828 | PrintfEnqueue(&PCBackDoorTx,"HyPER MPMC Control Terminal\r\n\r\n>"); |
emh203 | 0:085749c8446f | 829 | } |