Reduce the size of the AVRDriver code.
Colorize the changed parameter. Warning reduction across all files.
This commit is contained in:
@@ -15,13 +15,12 @@
|
||||
//#include "AVRCommandDecoder.h"
|
||||
#include "ConsoleHandler.h"
|
||||
|
||||
enum
|
||||
{
|
||||
enum {
|
||||
COM_NO_PORT = -1,
|
||||
COM_MIN_PORT = 1,
|
||||
COM_MAX_PORT = 99
|
||||
};
|
||||
CSerialPort avrPort;
|
||||
CSerialPort rs232Port;
|
||||
int avrOnPort = COM_NO_PORT; // Serial Port 1, 2, ... taking note that some USB adapters can be up toward channel 11, 12, ...
|
||||
unsigned avrBaud = 9600;
|
||||
|
||||
@@ -30,6 +29,7 @@ const uint32_t retryInterval = 1000; // ms
|
||||
|
||||
AVRInterface *avr;
|
||||
|
||||
#if 0
|
||||
// Each DT is the hex-character from the stream
|
||||
//
|
||||
//
|
||||
@@ -198,7 +198,7 @@ typedef struct {
|
||||
//AVR_StatusHeader_T avrStatusHeader;
|
||||
//AVR_Configuration_T avrConfigData;
|
||||
AVR_Status_T avrStatus;
|
||||
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
EXIT_OK = 0,
|
||||
@@ -224,7 +224,7 @@ int DetachSerialPort();
|
||||
// @param[in] len is the count of bytes in the message
|
||||
// @returns true if the serial interface accepted it.
|
||||
//
|
||||
bool SerialSend(const uint8_t *p, uint16_t len);
|
||||
bool SerialSend(const uint8_t *message, uint32_t len);
|
||||
|
||||
// Just big enough to hold an OSD message which is a 1 message command, 4 messages with text
|
||||
#define SERIALQUEUESIZE 5
|
||||
@@ -237,7 +237,7 @@ static SerialQueue_T serialQueue[SERIALQUEUESIZE];
|
||||
static int serialQueueCount = 0;
|
||||
|
||||
|
||||
// ProcessSerialQueue
|
||||
// SendMessageToSerial
|
||||
//
|
||||
// If there are parameters passed, insert a message into the queue.
|
||||
// Process the queue (with zero or more messages) to send
|
||||
@@ -246,7 +246,7 @@ static int serialQueueCount = 0;
|
||||
// @param[in] len is the count of bytes in the message
|
||||
// @returns false if the queue (which is a fixed size) is full.
|
||||
//
|
||||
//bool ProcessSerialQueue(const uint8_t *p = NULL, uint16_t len = 0);
|
||||
//bool SendMessageToSerial(const uint8_t *p = NULL, uint16_t len = 0);
|
||||
|
||||
|
||||
// ProcessSerialReceive
|
||||
@@ -256,7 +256,7 @@ static int serialQueueCount = 0;
|
||||
// @returns true if a message was processed.
|
||||
//
|
||||
SerialQueue_T ProcessSerialReceive(void);
|
||||
void HandleMessage(uint8_t *szBuffer, uint32_t num);
|
||||
//void HandleMessage(uint8_t *szBuffer, uint32_t num);
|
||||
|
||||
void EnumerateComPorts();
|
||||
unsigned long Hex2Dec(uint8_t *p, int dig);
|
||||
@@ -268,6 +268,7 @@ void GetAndSendOSDMessage();
|
||||
bool SendExtendedMessage(char *buf);
|
||||
|
||||
void EmitRuntimeHelp();
|
||||
DWORD AppTime();
|
||||
|
||||
bool UserExitRequested = false;
|
||||
bool SystemExitRequested = false;
|
||||
@@ -313,6 +314,7 @@ BOOL WINAPI ControlIntercept(DWORD CtrlType) {
|
||||
void ProcessWindowsMessage(void) {
|
||||
MSG msgx;
|
||||
bool msgReturn;
|
||||
|
||||
msgReturn = PeekMessage(&msgx, NULL, WM_QUIT, WM_QUIT, PM_NOREMOVE);
|
||||
if (msgReturn == true && msgx.message == WM_QUIT) {
|
||||
SystemExitRequested = true;
|
||||
@@ -320,12 +322,10 @@ void ProcessWindowsMessage(void) {
|
||||
}
|
||||
|
||||
|
||||
bool SerialSend(const uint8_t *p, uint16_t len) {
|
||||
bool SerialSend(const uint8_t *message, uint32_t len) {
|
||||
bool retVal = false;
|
||||
//Console_SetCursor(0, -1);
|
||||
//EmitBuffer("> ", p, len);
|
||||
//Console_ScrollBottomRegion();
|
||||
if (avrPort.Write((const LPVOID)p, len) == len) {
|
||||
|
||||
if (rs232Port.Write((const LPVOID)message, len) == len) {
|
||||
retVal = true;
|
||||
} else {
|
||||
Console_WriteAt(0, -1, "***** Failed to send. Port not open?");
|
||||
@@ -337,6 +337,7 @@ bool SerialSend(const uint8_t *p, uint16_t len) {
|
||||
void EmitSpinner() {
|
||||
static int x = 0;
|
||||
const char arrow[] = "|/-\\|/-\\";
|
||||
|
||||
printf("%c\b", arrow[x++]);
|
||||
if (x == 8) x = 0;
|
||||
}
|
||||
@@ -407,10 +408,10 @@ void ProcessKeyboard(void) {
|
||||
int c = CS_GetChar();
|
||||
switch (c) {
|
||||
case 'P':
|
||||
avr->AVRCommand(AVRInterface::subMain, AVRInterface::fncPower, AVRInterface::eOn);
|
||||
avr->AVRCommand(AVRSubsystem_T::subMain, AVRFunction_T::fncPower, AVRArg_T::eOn);
|
||||
break;
|
||||
case 'p':
|
||||
avr->AVRCommand(AVRInterface::subMain, AVRInterface::fncPower, AVRInterface::eOff);
|
||||
avr->AVRCommand(AVRSubsystem_T::subMain, AVRFunction_T::fncPower, AVRArg_T::eOff);
|
||||
break;
|
||||
case 'O':
|
||||
GetAndSendOSDMessage();
|
||||
@@ -426,19 +427,18 @@ void ProcessKeyboard(void) {
|
||||
break;
|
||||
case '/':
|
||||
avr->ReportAllStatus();
|
||||
//ShowAllStatusInfo();
|
||||
break;
|
||||
case '\x1B':
|
||||
UserExitRequested = true;
|
||||
break;
|
||||
case 'S':
|
||||
avrPort.Set_RTS_State(TRUE);
|
||||
rs232Port.Set_RTS_State(TRUE);
|
||||
Console_SetCursor(0, -1);
|
||||
printf("RTS set ON");
|
||||
Console_ScrollBottomRegion();
|
||||
break;
|
||||
case 's':
|
||||
avrPort.Set_RTS_State(FALSE);
|
||||
rs232Port.Set_RTS_State(FALSE);
|
||||
Console_SetCursor(0, -1);
|
||||
printf("RTS set OFF");
|
||||
Console_ScrollBottomRegion();
|
||||
@@ -446,8 +446,8 @@ void ProcessKeyboard(void) {
|
||||
default:
|
||||
for (int i = 0; i < sizeof(UserCommands) / sizeof(UserCmd_T); i++) {
|
||||
if (UserCommands[i].Char == c) {
|
||||
//ProcessSerialQueue((const uint8_t *)UserCommands[i].TxMsg.pMsg, UserCommands[i].TxMsg.MsgLen);
|
||||
avr->ProcessSerialQueue((const uint8_t *)UserCommands[i].TxMsg.pMsg, UserCommands[i].TxMsg.MsgLen);
|
||||
//SendMessageToSerial((const uint8_t *)UserCommands[i].TxMsg.pMsg, UserCommands[i].TxMsg.MsgLen);
|
||||
avr->SendMessageToSerial(UserCommands[i].TxMsg.pMsg, UserCommands[i].TxMsg.MsgLen);
|
||||
cmdFound = true;
|
||||
break;
|
||||
}
|
||||
@@ -499,19 +499,58 @@ void EmitRuntimeHelp() {
|
||||
Console_AdvanceToNextLineIfNotRoomFor((short)strlen(buf), 1);
|
||||
Console_Write(buf);
|
||||
}
|
||||
if (avrPort.IsOpen()) {
|
||||
if (rs232Port.IsOpen()) {
|
||||
Console_ScrollBottomRegion();
|
||||
Console_SetCursor(0, -1);
|
||||
printf(" Com Status: RTS: %-3s ", avrPort.Get_RTS_State() ? "ON" : "OFF");
|
||||
printf("DTR: %-3s ", avrPort.Get_DTR_State() ? "ON" : "OFF");
|
||||
printf("CTS: %-3s ", avrPort.Get_CTS_State() ? "ON" : "OFF");
|
||||
printf("DSR: %-3s ", avrPort.Get_DSR_State() ? "ON" : "OFF");
|
||||
printf("RI: %-3s", avrPort.Get_RI_State() ? "ON" : "OFF");
|
||||
printf(" Com Status: RTS: %-3s ", rs232Port.Get_RTS_State() ? "ON" : "OFF");
|
||||
printf("DTR: %-3s ", rs232Port.Get_DTR_State() ? "ON" : "OFF");
|
||||
printf("CTS: %-3s ", rs232Port.Get_CTS_State() ? "ON" : "OFF");
|
||||
printf("DSR: %-3s ", rs232Port.Get_DSR_State() ? "ON" : "OFF");
|
||||
printf("RI: %-3s", rs232Port.Get_RI_State() ? "ON" : "OFF");
|
||||
Console_ScrollBottomRegion();
|
||||
Console_SetCursor(0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
void EmitBuffer(const char *prefix, const uint8_t *buf, size_t len = 0, bool appendReturn = false) {
|
||||
int i = 0;
|
||||
const char *p = (const char *)buf;
|
||||
char txtBuf[MAXTEXTLEN] = "";
|
||||
|
||||
if (len == 0) len = strlen((const char *)buf);
|
||||
sprintf_s(txtBuf, MAXTEXTLEN, "%7.3f: [%3d]%s", (float)(AppTime())/1000.0, (int)strlen(p), prefix);
|
||||
Console_Write(txtBuf);
|
||||
while (*p && ((unsigned)(p - (const char *)buf) < len)) {
|
||||
if (isprint((char)*p)) {
|
||||
putchar(*p);
|
||||
i++;
|
||||
} else if (*p == '\r') {
|
||||
putchar('\n');
|
||||
Console_ScrollBottomRegion();
|
||||
} else if (*p == '\n') {
|
||||
// skip it
|
||||
} else {
|
||||
printf("[%02X]", (unsigned char)*p);
|
||||
}
|
||||
if ((i & 3) == 0) {
|
||||
if (Console_AdvanceToNextLineIfNotRoomFor(12, 1)) {
|
||||
//Console_ScrollBottomRegion();
|
||||
printf(" "); // sized to get past the timestamp, length, and prefix
|
||||
i = 0;
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (appendReturn) {
|
||||
Console_ScrollBottomRegion(); // putch('\r');
|
||||
} else {
|
||||
//putch('\n');
|
||||
//Console_ScrollBottomRegion();
|
||||
}
|
||||
}
|
||||
|
||||
void HexUppercase(char *p) {
|
||||
const char *hex = "0123456789ABCDEF";
|
||||
while (*p) {
|
||||
@@ -561,7 +600,7 @@ void GetAndSendCustomMessage() {
|
||||
} else {
|
||||
Console_ScrollBottomRegion();
|
||||
sprintf_s(msg, sizeof(msg), "\x02%s\x03", buf);
|
||||
avr->ProcessSerialQueue(msg, (uint16_t)strlen(msg));
|
||||
avr->SendMessageToSerial(msg, (uint16_t)strlen(msg));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -579,7 +618,7 @@ bool SendExtendedMessage(char * buf) {
|
||||
} while (*p && (p < msg + 70)); // 70 < 75 to generously reserve space for SUM0,SUM1,ETX,\0
|
||||
sprintf_s(p, 75 - strlen(msg), "%02X\x03", checksum);
|
||||
Console_ScrollBottomRegion();
|
||||
avr->ProcessSerialQueue(msg, (uint16_t)strlen(msg));
|
||||
avr->SendMessageToSerial(msg, (uint16_t)strlen(msg));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -612,32 +651,78 @@ void GetProgName(char *name) {
|
||||
}
|
||||
|
||||
|
||||
void InformationUpdate(AVRInterface::AVRMessageType_T type, const char *msg) {
|
||||
bool InformationUpdate(AVRMessageType_T type, const void *message, uint32_t attrib) {
|
||||
const char *msg = (const char *)message;
|
||||
char buf[MAXTEXTLEN] = "";
|
||||
|
||||
if (verboseMode) {
|
||||
switch (type) {
|
||||
case AVRInterface::AVRMessageType_T::mtStatus:
|
||||
sprintf_s(buf, MAXTEXTLEN, "AVR Status: %-60s", msg);
|
||||
case mtState:
|
||||
sprintf_s(buf, MAXTEXTLEN, "AVR Status: %-60s", avr->GetStateLabel(attrib));
|
||||
Console_SetColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
|
||||
Console_WriteAt(0, 1, buf);
|
||||
Console_RestoreColor();
|
||||
break;
|
||||
case AVRInterface::AVRMessageType_T::mtInfo:
|
||||
case mtInfo:
|
||||
Console_WriteAt(0, -1, msg);
|
||||
Console_ScrollBottomRegion();
|
||||
break;
|
||||
case AVRInterface::AVRMessageType_T::mtModelInfo:
|
||||
case mtModelInfo:
|
||||
Console_WriteAt(0, 2, msg);
|
||||
break;
|
||||
case AVRInterface::AVRMessageType_T::mtStreamStart:
|
||||
case mtStreamStart:
|
||||
Console_SetCursor(0, 3);
|
||||
// break; // fall through
|
||||
case AVRInterface::AVRMessageType_T::mtStream:
|
||||
case mtStream:
|
||||
Console_AdvanceToNextLineIfNotRoomFor(40); // @TODO hard-coded - ick
|
||||
if (attrib) {
|
||||
Console_SetColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
|
||||
}
|
||||
Console_Write(msg);
|
||||
if (attrib) {
|
||||
Console_RestoreColor();
|
||||
}
|
||||
break;
|
||||
case mtXmtdMsg:
|
||||
Console_SetCursor(0, -1);
|
||||
EmitBuffer(">", (const uint8_t *)msg, attrib, true);
|
||||
break;
|
||||
case mtRcvdMsg:
|
||||
Console_SetCursor(0, -1);
|
||||
EmitBuffer("<", (const uint8_t *)msg, attrib, true);
|
||||
break;
|
||||
case mtExtendedResp:
|
||||
Console_SetCursor(0, -1);
|
||||
EmitBuffer("[", (const uint8_t *)msg, attrib, true);
|
||||
break;
|
||||
case mtStatusMsg:
|
||||
Console_SetCursor(0, -1);
|
||||
EmitBuffer("}", (const uint8_t *)msg, attrib, true);
|
||||
break;
|
||||
case mtOperationResp:
|
||||
// This is just the echo of the command
|
||||
// so there is nothing to report that wouldn't be a duplicate
|
||||
//Console_SetCursor(0, -1);
|
||||
//EmitBuffer("{", (const uint8_t *)msg, attrib, true);
|
||||
break;
|
||||
case mtParamUpdated:
|
||||
message = "x";
|
||||
break;
|
||||
case mtErrNotFound:
|
||||
sprintf_s(buf, MAXTEXTLEN, "AVRCommand(%02X,%02X,%02X) -- not found", (attrib >> 16), (attrib >> 8) & 0xFF, attrib & 0xFF);
|
||||
Console_WriteAt(0, -1, buf);
|
||||
break;
|
||||
case mtErrRxLength:
|
||||
break;
|
||||
case mtErrMalloc:
|
||||
break;
|
||||
case mtFailed:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void EmitCommandLineHelp() {
|
||||
@@ -668,7 +753,7 @@ void EmitCommandLineHelp() {
|
||||
printf("\n");
|
||||
printf("Options:\n");
|
||||
printf("\n");
|
||||
printf(" -SerialPort=X[,yyyy] Set to Com port X to baud rate yyyy.\n");
|
||||
printf(" -SerialPort=X[,yyyy] Set to Serial Port X to baud rate yyyy.\n");
|
||||
printf(" Defaults baud is %d\n", avrBaud);
|
||||
printf(" NOTE: At least for the RX-V2400, 9600,8,N,1 is required.\n");
|
||||
printf(" NOTE: At least for the RX-V2400, the host must assert RTS.\n");
|
||||
@@ -779,7 +864,7 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
short consoleWidth = 122;
|
||||
short consoleHeight = 80;
|
||||
short consoleScrollHeight = 30;
|
||||
short consoleLeft = 15;
|
||||
short consoleLeft = 50;
|
||||
short consoleTop = 15;
|
||||
int CommandToExecute[4] = { -1, -1, -1, -1 };
|
||||
float db;
|
||||
@@ -790,8 +875,7 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
GetProgName(argv[0]);
|
||||
UserCommandsSanityCheck(); // If the table is bad, we exit here
|
||||
|
||||
avr = new AVRInterface(SerialSend);
|
||||
avr->RegisterInformationCallback(InformationUpdate);
|
||||
avr = new AVRInterface(InformationUpdate, SerialSend);
|
||||
|
||||
ReadIniFile();
|
||||
|
||||
@@ -822,10 +906,10 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
//
|
||||
switch (parseCount) {
|
||||
case 1:
|
||||
CommandToExecute[1] = AVRInterface::AVRFunction_E::fncFunctionCount;
|
||||
CommandToExecute[1] = AVRFunction_T::fncFunctionCount;
|
||||
// break; // fall thru on purpose
|
||||
case 2:
|
||||
CommandToExecute[2] = AVRInterface::AVRArg_T::eARGCount;
|
||||
CommandToExecute[2] = AVRArg_T::eARGCount;
|
||||
// break; // fall thru on purpose
|
||||
default:
|
||||
// nothing to do
|
||||
@@ -833,32 +917,32 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
}
|
||||
bool saveVerbose = verboseMode;
|
||||
verboseMode = true;
|
||||
avr->ExportInformation((AVRInterface::AVRSubsystem_T)CommandToExecute[0],
|
||||
(AVRInterface::AVRFunction_E)CommandToExecute[1],
|
||||
(AVRInterface::AVRArg_T)CommandToExecute[2]);
|
||||
avr->ExportInformation((AVRSubsystem_T)CommandToExecute[0],
|
||||
(AVRFunction_T)CommandToExecute[1],
|
||||
(AVRArg_T)CommandToExecute[2]);
|
||||
verboseMode = saveVerbose;
|
||||
exit(EXIT_OK);
|
||||
} else if (0 == strcmp(argv[i], "-Power=On")) {
|
||||
CommandToExecute[0] = AVRInterface::subMain;
|
||||
CommandToExecute[1] = AVRInterface::fncPower;
|
||||
CommandToExecute[2] = AVRInterface::eOn;
|
||||
CommandToExecute[0] = subMain;
|
||||
CommandToExecute[1] = fncPower;
|
||||
CommandToExecute[2] = eOn;
|
||||
} else if (0 == strcmp(argv[i], "-Power=Off")) {
|
||||
CommandToExecute[0] = AVRInterface::subMain;
|
||||
CommandToExecute[1] = AVRInterface::fncPower;
|
||||
CommandToExecute[2] = AVRInterface::eOff;
|
||||
CommandToExecute[0] = subMain;
|
||||
CommandToExecute[1] = fncPower;
|
||||
CommandToExecute[2] = eOff;
|
||||
} else if (0 == strcmp(argv[i], "-Mute=On")) {
|
||||
CommandToExecute[0] = AVRInterface::subMain;
|
||||
CommandToExecute[1] = AVRInterface::fncMute;
|
||||
CommandToExecute[2] = AVRInterface::eOn;
|
||||
CommandToExecute[0] = subMain;
|
||||
CommandToExecute[1] = fncMute;
|
||||
CommandToExecute[2] = eOn;
|
||||
} else if (0 == strcmp(argv[i], "-Mute=Off")) {
|
||||
CommandToExecute[0] = AVRInterface::subMain;
|
||||
CommandToExecute[1] = AVRInterface::fncMute;
|
||||
CommandToExecute[2] = AVRInterface::eOff;
|
||||
CommandToExecute[0] = subMain;
|
||||
CommandToExecute[1] = fncMute;
|
||||
CommandToExecute[2] = eOff;
|
||||
} else if (1 == sscanf_s(argv[i], "-Zone1Volume=%4f", &db)) {
|
||||
if (db >= -80.0f && db <= +16.5f) {
|
||||
CommandToExecute[0] = AVRInterface::sysCommand;
|
||||
CommandToExecute[1] = AVRInterface::fncSetValue;
|
||||
CommandToExecute[2] = AVRInterface::eMasterVol;
|
||||
CommandToExecute[0] = sysCommand;
|
||||
CommandToExecute[1] = fncSetValue;
|
||||
CommandToExecute[2] = eMasterVol;
|
||||
CommandToExecute[3] = avr->VolumeDBtoAPIValue(db);
|
||||
} else {
|
||||
printf("***** Volume %+4.1f not in the range '-80.0 <= value <= 16.5' *****\n", db);
|
||||
@@ -866,9 +950,9 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
}
|
||||
} else if (1 == sscanf_s(argv[i], "-Zone2Volume=%4f", &db)) {
|
||||
if (db >= -80.0f && db <= +16.5f) {
|
||||
CommandToExecute[0] = AVRInterface::sysCommand;
|
||||
CommandToExecute[1] = AVRInterface::fncSetValue;
|
||||
CommandToExecute[2] = AVRInterface::eZone2Vol;
|
||||
CommandToExecute[0] = sysCommand;
|
||||
CommandToExecute[1] = fncSetValue;
|
||||
CommandToExecute[2] = eZone2Vol;
|
||||
CommandToExecute[3] = avr->VolumeDBtoAPIValue(db);
|
||||
} else {
|
||||
printf("***** Volume %+4.1f not in the range '-80.0 <= value <= 16.5' *****\n", db);
|
||||
@@ -876,9 +960,9 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
}
|
||||
} else if (1 == sscanf_s(argv[i], "-Zone3Volume=%4f", &db)) {
|
||||
if (db >= -80.0f && db <= +16.5f) {
|
||||
CommandToExecute[0] = AVRInterface::sysCommand;
|
||||
CommandToExecute[1] = AVRInterface::fncSetValue;
|
||||
CommandToExecute[2] = AVRInterface::eZone3Vol;
|
||||
CommandToExecute[0] = sysCommand;
|
||||
CommandToExecute[1] = fncSetValue;
|
||||
CommandToExecute[2] = eZone3Vol;
|
||||
CommandToExecute[3] = avr->VolumeDBtoAPIValue(db);
|
||||
} else {
|
||||
printf("***** Volume %+4.1f not in the range '-80.0 <= value <= 16.5' *****\n", db);
|
||||
@@ -900,7 +984,7 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
DWORD refTime = AppTime();
|
||||
DWORD nowTime = AppTime();
|
||||
DWORD elapsedTime = nowTime - refTime;
|
||||
AVRInterface::AVRState_T state = AVRInterface::stPoweringUp;
|
||||
AVRInterface::AVRState_T state = AVRInterface::stMaxStates;
|
||||
do {
|
||||
state = avr->Tick(nowTime);
|
||||
Sleep(10); // @TODO how long to wait should not be based on lucky timing
|
||||
@@ -916,9 +1000,9 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
if (pExtendedMessage) {
|
||||
SendExtendedMessage(pExtendedMessage);
|
||||
} else {
|
||||
avr->AVRCommand((AVRInterface::AVRSubsystem_T)CommandToExecute[0],
|
||||
(AVRInterface::AVRFunction_E)CommandToExecute[1],
|
||||
(AVRInterface::AVRArg_T)CommandToExecute[2],
|
||||
avr->AVRCommand((AVRSubsystem_T)CommandToExecute[0],
|
||||
(AVRFunction_T)CommandToExecute[1],
|
||||
(AVRArg_T)CommandToExecute[2],
|
||||
(uint8_t)CommandToExecute[3]);
|
||||
}
|
||||
refTime = nowTime = AppTime();
|
||||
@@ -952,14 +1036,15 @@ int __cdecl main(int argc, char *argv[]) {
|
||||
ProcessKeyboard();
|
||||
SerialQueue_T anyRcvdMsg = ProcessSerialReceive();
|
||||
if (anyRcvdMsg.len) {
|
||||
bool showAllFlag = avr->HandleMessage(anyRcvdMsg.message, anyRcvdMsg.len);
|
||||
uint8_t offsetChanged = avr->HandleMessage(anyRcvdMsg.message, anyRcvdMsg.len);
|
||||
|
||||
if (showAllFlag) {
|
||||
if (offsetChanged) {
|
||||
bool priorState = Console_SetCursorVisibility(false);
|
||||
avr->ReportAllStatus();
|
||||
avr->ReportAllStatus(offsetChanged);
|
||||
Console_SetCursorVisibility(priorState);
|
||||
}
|
||||
}
|
||||
Sleep(10);
|
||||
ProcessWindowsMessage();
|
||||
} while (!UserExitRequested && !SystemExitRequested);
|
||||
DetachSerialPort();
|
||||
@@ -1024,57 +1109,6 @@ bool CheckTheChecksum(uint8_t *szBuffer, uint32_t num) {
|
||||
}
|
||||
|
||||
|
||||
// HandleMessage
|
||||
//
|
||||
// Given a response string, typically of the form:
|
||||
// [11] .... [03]
|
||||
// [12] .... [03]
|
||||
// ... etc
|
||||
//
|
||||
//
|
||||
void HandleMessage(uint8_t *szBuffer, uint32_t num) {
|
||||
switch (szBuffer[0]) {
|
||||
case 0x02: // STX
|
||||
//ProcessReportResponse(szBuffer, num);
|
||||
break;
|
||||
case 0x11: // DC1
|
||||
break;
|
||||
case 0x12: // DC2
|
||||
if (CheckTheChecksum(szBuffer, num)) {
|
||||
if (num == 21) {
|
||||
memcpy(&avrStatus.header, &szBuffer[1], sizeof(AVR_StatusHeader_T));
|
||||
num = Hex2Dec(&avrStatus.header.length[0], 2);
|
||||
memcpy(&avrStatus.config.DT0, &szBuffer[9], num); // Copy bits of the config
|
||||
avrStatus.headerValid = true;
|
||||
} else if (num == 150) {
|
||||
memcpy(&avrStatus.header, &szBuffer[1], sizeof(AVR_StatusHeader_T));
|
||||
num = Hex2Dec(&avrStatus.header.length[0], 2);
|
||||
memcpy(&avrStatus.config.DT0, &szBuffer[9], num); // Copy the config
|
||||
|
||||
//memcpy(&avrStatus.header, &szBuffer[1], sizeof(AVR_StatusHeader_T) + sizeof(AVR_Configuration_T));
|
||||
avrStatus.headerValid = true;
|
||||
avrStatus.configValid = true;
|
||||
} else {
|
||||
printf("***** Received message of unexpected length [%u]\n", num);
|
||||
}
|
||||
} else {
|
||||
Console_WriteAt(0, -1, "Checksum failure on Status Header");
|
||||
}
|
||||
//PrintConfiguration(szBuffer);
|
||||
break;
|
||||
case 0x14: // DC4 Extended Response
|
||||
//DecodeExtended(szBuffer);
|
||||
// Decode Extended response
|
||||
break;
|
||||
case '0':
|
||||
//rcmd = Hex2Dec(&szBuffer[0], 2);
|
||||
//DecodeString(rcmd, &szBuffer[2]);
|
||||
break;
|
||||
default:
|
||||
//printf("[%d] %s\n", i, szBuffer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ProcessSerialReceive
|
||||
//
|
||||
@@ -1089,7 +1123,8 @@ SerialQueue_T ProcessSerialReceive() {
|
||||
static uint8_t *p = messageBuf[bufInUse]; // used to fill the partialRx as data comes in
|
||||
SerialQueue_T retInfo = { NULL, 0 };
|
||||
|
||||
uint32_t num = avrPort.Read(partialRx, MAXTEXTLEN);
|
||||
#include <stdio.h>
|
||||
uint32_t num = rs232Port.Read(partialRx, MAXTEXTLEN);
|
||||
if (num) {
|
||||
for (uint32_t i = 0; i < num; i++) {
|
||||
*p = partialRx[i];
|
||||
@@ -1102,10 +1137,12 @@ SerialQueue_T ProcessSerialReceive() {
|
||||
bufInUse = (++bufInUse & 1);
|
||||
p = messageBuf[bufInUse]; // Reset the buffer for the next message, which might be in partialRx
|
||||
*p = '\0';
|
||||
} else {
|
||||
*p = '\0'; // keep it null terminated
|
||||
}
|
||||
}
|
||||
if (messageBuf[0]) {
|
||||
//EmitBuffer("~", messageBuf, 0, true); // Show them the partial receipt if anything is there
|
||||
//EmitBuffer("~", (const uint8_t *)messageBuf, 0, true); // Show them the partial receipt if anything is there
|
||||
}
|
||||
}
|
||||
return retInfo;
|
||||
@@ -1114,7 +1151,7 @@ SerialQueue_T ProcessSerialReceive() {
|
||||
|
||||
|
||||
void EnumerateComPorts() {
|
||||
printf("Com Port Scan:\n");
|
||||
printf("Serial Port Scan:\n");
|
||||
int foundPorts = 0;
|
||||
for (int pNum = COM_MIN_PORT; pNum < COM_MAX_PORT; pNum++) {
|
||||
char cBuf[20]; // generously sized
|
||||
@@ -1135,18 +1172,18 @@ void EnumerateComPorts() {
|
||||
CloseHandle(port);
|
||||
}
|
||||
if (portFound) {
|
||||
printf(" Com Port %d found.\n", pNum);
|
||||
printf(" Serial Port %d found.\n", pNum);
|
||||
}
|
||||
}
|
||||
if (foundPorts == 0) {
|
||||
printf(" No Com Ports found, perhaps you need to plug in an adapter?\n");
|
||||
printf(" No Serial Ports found, perhaps you need to plug in an adapter?\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int DetachSerialPort() {
|
||||
if (avrPort.IsOpen()) {
|
||||
avrPort.Close();
|
||||
if (rs232Port.IsOpen()) {
|
||||
rs232Port.Close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -1158,11 +1195,11 @@ int AttachToSerialPort() {
|
||||
|
||||
sprintf_s(buf, sizeof(buf), "\\\\.\\COM%d", avrOnPort);
|
||||
uint32_t Access = GENERIC_WRITE | GENERIC_READ;
|
||||
if (avrPort.Open(buf, avrBaud, 8, NOPARITY, ONESTOPBIT, Access)) {
|
||||
if (rs232Port.Open(buf, avrBaud, 8, NOPARITY, ONESTOPBIT, Access)) {
|
||||
success = true;
|
||||
avrPort.Set_RTS_State(false);
|
||||
rs232Port.Set_RTS_State(false);
|
||||
Sleep(50);
|
||||
avrPort.Set_RTS_State(true);
|
||||
rs232Port.Set_RTS_State(true);
|
||||
Sleep(50);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user