On Using hashcode
Using hashcode makes simple decode command but needs some preparation. A hashcode is a code that a series of characters produce always the same code. So a number of commands have unique the number of hashcodes. If we don't use those codes, we have to use a bunch of string compare and if...else if...else if blocks of code. The if...else if...else if blocks are not all that bad, but they have less readability and you might not see the program again if there are 20 or more commands.
Write the following 2 subroutines to get hash table.
#define hashSeed 0x65559
uint32 getHash(char *Command) {
uint32 hash = 0;
uint8 size = strlen(Command);
uint8 i;
for(i = 0; i <= size - 1; i++) {
hash = hash * hashSeed + Command[i];
}
return hash;
}
void createHashTable(void) {
/* Output to debug */
/* Convert command string to hashcode and making table */
uint32 hashcode;
uint8 i;
uint8 cmdSize = sizeof(CmdArray) / 4; /* 4 is the per item count */
debugPrint("\r\n", 2);
sprintf(buf, "/* Hash code table --------------------- */\r\n");
debugPrint(buf, strlen(buf));
for(i = 0; i <= cmdSize - 1; i++) {
hashcode = getHash(CmdArray[i]);
/* Output each code to SerialCommander */
sprintf(buf, "#define hs%s \t 0x%08X \r\n", CmdArray[i], hashcode);
debugPrint(buf, strlen(buf));
}
sprintf(buf, "/* End of Hash code table -------------- */\r\n");
debugPrint(buf, strlen(buf));
sprintf(buf, "sizeof(CmdArray) = %d\r\n", cmdSize);
debugPrint(buf, strlen(buf));
while(1);
}
Now prepare your own command, for example,
/* Const Command array */
const char *CmdArray[]
= {
/* Command class *********************************/
"OPR", "AUTO", "MAN", "CCL", "SD",
"SYS", "ERR", "ENQ", "DBG", "PTNNUM",
/* System operation ******************************/
"RESET", "ABORT",
/* Auto mode operation ***************************/
"RUN", "RESUME", "STOP", "SET", "TIMEMODE",
/* Manual mode operation *************************/
"AGLRST", "SHTDN", "CANPREST", "SENTPREST", "ALLPREST",
"CANAGL", "SENTAGL", "PTNTEST", "PTNSTART", "PTNABORT",
/* Cycle operation *******************************/
"CYCLE", "CCLSPAN", "RPTCCL", "RSMCCL",
/* SD memory settings ****************************/
"INTVL", "PTNRD", "PTNCLR", "RSMCCL", "CDFILE",
/* System settings operation *********************/
"VTGMAX", "CURMAX", "TMOUT", "FREQ", "AGLMODE",
"SENTCODE", "PRESET", "TIME", "DATE", "RTCOFS",
"USEACC", "ACCTM", "PIDKP", "PIDKI", "PIDKD",
"XMITDATA", "POSTOL", "CANDEVSEL", "SERIAL",
/* Error override operation **********************/
"SDOVD",
/* System enquiry operation **********************/
"STATUS", "ASCDATA", "ASCSTATUS", "SYSID", "PTNDAT",
"SETTINGS", "SRPAGE", "DATA",
/* Debug operation *******************************/
"LOGON", "MONAGL", "RUNTEST", "CDFILE", "CHGIMG",
"MOVPOS",
/* Device IDs ************************************/
"CAN", "SENT", "CTV", "CBV",
/* Auxiliary commands ******************************/
"LOW", "HIGH", "ON", "OFF", "INCLD", "EXCLD",
"UNIPOL", "BIPOL",
};
In main(), insert the following.
int main(void) {
createHashTable(); /* Comment out this line after getting the codes */
/* Super loop */
while(true) {
/* Do Things here */
}
}
Using debug tool, we might have
/* Hash code table ---------------------- */
#define hsOPR 0xD76C3E81
#define hsAUTO 0x0FFCEC91
#define hsMAN 0x9DCCCC64
#define hsCCL 0x7F973DAA
#define hsSD 0x020DAC1F
#define hsSYS 0x4A262267
#define hsERR 0xB936AFC9
#define hsENQ 0xB91D5A64
#define hsDBG 0x9C31213D
#define hsPTNNUM 0xDC56212A
#define hsRESET 0x447349B3
#define hsABORT 0x4CEFC828
#define hsRUN 0x2D6C940D
#define hsRESUME 0x97964821
........
/* End of Hash code table --------------- */
Now copy the table from the debugger and paste it to the program. It will be used in the command interpreter.
가장 많이 본 글
-
PIC microprocessor에 대한 경험을 알려 드리고 선택에 도움이 되었으면 합니다. 저는 PIC을 접한지 꽤 되었는데 아직도 완벽 하다고는 생각지 않습니다. 너무나 종류도 많고 memory크기, interface도 다양해서 어떤...
-
PIC Peripheral Pin Select(PPS: 주변장치 Pin선택기능) PIC24 또는 32 Series 중에는 주변장치 pin을 program에서 select 할 수 있는 기능이 있는 chip들이 있습니다. HW design시 rou...
-
Development environment is more important than others. This is the stepping stone for success. 개발환경 구축이 무엇보다 중요 합니다. 첫단추를 잘 끼워야 하니까요. 개발...
댓글 없음:
댓글 쓰기