가장 많이 본 글

이 블로그 검색

2018년 6월 5일 화요일

Making hashcode to interpret commands

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.

댓글 없음:

댓글 쓰기