const 以及 errcode
別緊張
這兩個都沒有很難
本周就輕鬆一下吧~
const.h
這裡主要是定義結構用
#ifndef __CONST_H #define __CONST_H #define NUM_SERVO (17) #define NUM_RBVer (7) #define NUM_SKEY (17) typedef struct _strint_{ const char* name; unsigned int value; }STRINT; typedef struct _servo_{ const char* name; unsigned int value; bool feedback; }_SERVO; typedef _SERVO SERVO[NUM_SERVO]; typedef STRINT SKEY[NUM_SKEY]; typedef STRINT RBVER[NUM_RBVer]; extern SERVO Servo; extern RBVER RBVer; extern SKEY SKey; #endif
const.cpp
剛剛定義結構的內容 有servo,RBver等
#include "rcservo.h" #include "userio.h" #include "common.h" #include "const.h" _SERVO Servo[] = { {"DEFAULT", RCSERVO_SERVO_DEFAULT, true}, {"DEFAULT_NOFB", RCSERVO_SERVO_DEFAULT_NOFB, false}, {"KONDO_KRS786", RCSERVO_KONDO_KRS786, true}, {"KONDO_KRS788", RCSERVO_KONDO_KRS788, true}, {"KONDO_KRS78X", RCSERVO_KONDO_KRS78X, true}, {"KONDO_KRS4014", RCSERVO_KONDO_KRS4014, true}, {"KONDO_KRS4024", RCSERVO_KONDO_KRS4024, true}, {"HITEC_HSR8498", RCSERVO_HITEC_HSR8498, true}, {"FUTABA_S3003", RCSERVO_FUTABA_S3003, false}, {"SHAYYE_SYS214050", RCSERVO_SHAYYE_SYS214050, false}, {"TOWERPRO_MG995", RCSERVO_TOWERPRO_MG995, false}, {"TOWERPRO_MG996", RCSERVO_TOWERPRO_MG996, false}, {"DMP_RS0263", RCSERVO_DMP_RS0263, false}, {"DMP_RS1270", RCSERVO_DMP_RS1270, false}, {"GWS_S777", RCSERVO_GWS_S777, false}, {"GWS_S03T", RCSERVO_GWS_S03T, false}, {"GWS_MICRO", RCSERVO_GWS_MICRO, false} }; STRINT RBVer[] = { {"RB_100b1", RB_100b1}, {"RB_100b2", RB_100b2}, {"RB_100b3", RB_100b3}, {"RB_100", RB_100}, {"RB_100RD", RB_100RD}, {"RB_110", RB_110}, {"RB_050", RB_050} }; STRINT SKey[] = { {"F1", KBF1}, {"F2", KBF2}, {"F3", KBF3}, {"F4", KBF4}, {"F5", KBF5}, {"F6", KBF6}, {"F7", KBF7}, {"F8", KBF8}, {"F9", KBF9}, {"F10", KBF10}, {"F11", KBF11}, {"F12", KBF12}, {"ENTER", KBENTER}, // {"SPACE", KBSPACE}, {"UP", KBUP}, {"DOWN", KBDOWN}, {"LEFT", KBLEFT}, {"RIGHT", KBRIGHT} // {"PGUP", KBPGUP}, // {"PGDN", KBPGDN} };
errcode.h
顧名思義,error code用 將一些錯誤條件define為值
#ifndef __ERRCODE_H #define __ERRCODE_H #define SUCCESS 0 // ================= demofile error cede ================== // for general case #define OFFSET 0 #define OPEN_FILE_FAIL (OFFSET+0) #define UNKNOWN_LINE (OFFSET+1) //not [XXX] , XX = YY #define INVALID_GROUP (OFFSET+2) #define NOGROUP_ITEM (OFFSET+3) #define INVALID_NAME (OFFSET+4) #define INVALID_VALUE (OFFSET+5) // for def file #define DEF_OFFSET 100 // in [setting] group #define DEF_SET_OFFSET 200 #define DEF_SET_INVALID_FRMNO (DEF_SET_OFFSET+6) #define DEF_SET_INVALID_MODE (DEF_SET_OFFSET+7) // in [channel] group #define DEF_CH_OFFSET 300 #define DEF_CH_INVALID_CH (DEF_CH_OFFSET+6) #define DEF_CH_REPEAT_CH (DEF_CH_OFFSET+7) #define DEF_CH_INVALID_PIN (DEF_CH_OFFSET+8) #define DEF_CH_REPEAT_PIN (DEF_CH_OFFSET+9) #define DEF_CH_INVALID_MODE (DEF_CH_OFFSET+10) // in [Files] group #define DEF_FIL_OFFSET 400 // for cmd file #define CMD_OFFSET 500 #define CMD_NO_ACTNO (CMD_OFFSET+6) #define CMD_NO_CMD (CMD_OFFSET+7) #define CMD_NO_NAME (CMD_OFFSET+8) // for act file #define ACT_OFFSET 600 #define ACT_INVALID_FRMNO (ACT_OFFSET+6) #define ACT_INVALID_TIME (ACT_OFFSET+7) // for frm file #define FRM_OFFSET 700 #define FRM_REPEAT_CH (FRM_OFFSET+6) // for offset file #define TXT_OFFSET 800 #define TXT_REPEAT_CH (TXT_OFFSET+6) // +++++++++++++++++++++++ end +++++++++++++++++++++++ // code for fileio #define FILEIO_OFFSET 900 #define INI_FILEEND (FILEIO_OFFSET+0) #define INI_INVALID_CH (FILEIO_OFFSET+1) #define INI_INVALID_VALUE (FILEIO_OFFSET+2) #define INI_DIVIDE_FAIL (FILEIO_OFFSET+3) //string doesn't have separate char #define INI_DIVIDE_STR1 (FILEIO_OFFSET+4) #define INI_DIVIDE_STR2 (FILEIO_OFFSET+5) #define INI_INVALID_NO (FILEIO_OFFSET+6) #define INI_INVALIDCHANNEL (FILEIO_OFFSET+7) #define INI_NUMPRE_NULL (FILEIO_OFFSET+8) #define INI_NUMPRE_INVALID (FILEIO_OFFSET+9) #define INI_NUMPRE_FAIL (FILEIO_OFFSET+10) #define INI_NUMPOST_NULL (FILEIO_OFFSET+11) // str = NULL #define INI_NUMPOST_INVALID (FILEIO_OFFSET+12) // unit error #define INI_NUMPOST_FAIL (FILEIO_OFFSET+13) // value error //memory error #define MEMORY_ERROR (1000) // fode for map #define MAP_OFFSET 1100 #define MAP_NOFINDINITFRM (MAP_OFFSET+0) #define MAP_NOFINDFRM (MAP_OFFSET+1) #define MAP_REPEAT_FRMNO (MAP_OFFSET+2) #define MAP_NOFINEINITACT (MAP_OFFSET+3) #define MAP_NOFINDACT (MAP_OFFSET+4) #define MAP_REPEAT_ACTNO (MAP_OFFSET+5) #define MAP_REPEAT_CMD (MAP_OFFSET+6) #ifdef __cplusplus extern "C" { #endif void errcode_ShowErr(int code); void errcode_SetErrStr(const char *str); void errcode_SetErrGroup(const char *str); void errcode_SetErrFRM(const int frmno); void errcode_SetErrACT(const int actno); void errcode_SetErrCMD(const int cmd); #ifdef __cplusplus } #endif #endif
errcode.cpp
error code輸出錯誤訊息用
#include <string.h> #include "errcode.h" #include "userio.h" #include "const.h" #define DIVISOR 100 #define DEF (DEF_OFFSET / DIVISOR) #define DEF_SET (DEF_SET_OFFSET / DIVISOR) #define DEF_CH (DEF_CH_OFFSET / DIVISOR) #define DEF_FILE (DEF_FIL_OFFSET / DIVISOR) #define CMD (CMD_OFFSET / DIVISOR) #define ACT (ACT_OFFSET / DIVISOR) #define FRM (FRM_OFFSET / DIVISOR) #define TXT (TXT_OFFSET / DIVISOR) #define MAP (MAP_OFFSET / DIVISOR) static char ErrLine[1024] = ""; static char ErrFile[256] = ""; static char ErrGroup[256] = ""; static int errfrm; static int erract; static char errcmd[10]; static void def_ErrShow(int code); static void def_SetErrShow(int code); static void def_CHErrShow(int code); static void def_FileErrShow(int code); static void cmd_ErrShow(int code); static void act_ErrShow(int code); static void frm_ErrShow(int code); static void txt_ErrShow(int code); static void map_ErrShow(int code); static void map_ErrShow(int code) { int i = code % DIVISOR; const char *str[] = { "map : can't find initial frame : %d\n", "map : an act has an undefined frame : %d\n", "map : repeat frame number : %d\n", "map : can't find initial action : %d\n", "map : a command point to an undefined act : %d\n", "map : repeat action number : %d\n", "map : repeat command : %s\n" }; switch(i){ case 0: case 1: case 2: showmsg(str[i], errfrm); break; case 3: case 4: case 5: showmsg(str[i], erract); break; case 6: showmsg(str[i], errcmd); break; } } static void def_SetErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"def"); strcpy(ErrGroup, "Setting"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s", "%s file : invalid frame number appears in group %s : %s", "%s file : invalid initial frame mode appears in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } static void def_CHErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"def"); strcpy(ErrGroup, "Channels"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s", "%s file : invalid channel appears in group %s : %s", "%s file : repeat channel appears in group %s : %s", "%s file : invalid pin appears in group %s : %s", "%s file : repeat pin appears in group %s : %s", "%s file : invalid servo mode appears in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } static void def_FileErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"def"); strcpy(ErrGroup, "Files"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } static void cmd_ErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"cmd"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears : %s\n", "%s file : invalid item value appears : %s\n", "%s file : a command don't define active number\n", "%s file : a command don't define command key\n", "%s file : a command don't define command name\n", }; switch(i){ case 0: case 6: case 7: case 8: case 9: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: case 4: case 5: showmsg(str[i], ErrFile, ErrLine); break; } } static void act_ErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"act"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s", "%s file : invalid frame number in group %s : %s", "%s file : incalid time appears in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } static void frm_ErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"frm"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s", "%s file : repeat channal in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } static void txt_ErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"offset"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears : %s", "%s file : invalid item value appears : %s", "%s file : repeat channal : %s", }; switch(i){ case 0: showmsg(str[i], ErrFile); break; default: showmsg(str[i], ErrFile, ErrLine); break; } } static void def_ErrShow(int code) { int i = code % DIVISOR; strcpy(ErrFile,"def"); const char *str[] = { "Open %s file fail\n", "%s file : unknown line : %s\n", "%s file : invalid group : %s\n", "%s file : no group item : %s\n", "%s file : invalid item name appears in group %s : %s", "%s file : invalid item value appears in group %s : %s" }; switch(i){ case 0: showmsg(str[i], ErrFile); break; case 1: case 2: case 3: showmsg(str[i], ErrFile, ErrLine); break; default: showmsg(str[i], ErrFile, ErrGroup, ErrLine); break; } } void errcode_ShowErr(int code) { int i; i = code / DIVISOR; switch(i){ case DEF: def_ErrShow(code); break; case DEF_SET: def_SetErrShow(code); break; case DEF_CH: def_CHErrShow(code); break; case DEF_FILE: def_FileErrShow(code); break; case CMD: cmd_ErrShow(code); break; case ACT: act_ErrShow(code); break; case FRM: frm_ErrShow(code); break; case TXT: txt_ErrShow(code); break; case MAP: map_ErrShow(code); } showmsg("\nPress any key to quit\n"); keyboard_waitkey(); } void errcode_SetErrFRM(const int frmno) { errfrm = frmno; } void errcode_SetErrACT(const int actno) { erract = actno; } void errcode_SetErrGroup(const char *str) { strcpy(ErrGroup, str); } void errcode_SetErrStr(const char *str) { strcpy(ErrLine, str); } void errcode_SetErrCMD(const int cmd) { int i; if((cmd > 'A') && (cmd < 'Z')){ errcmd[0] = cmd; errcmd[1] = '\0'; } else{ for(i = 0; i < NUM_SKEY; i++){ if(cmd == SKey[i].value) strcpy(errcmd, SKey[i].name); } } }
基本上這邊講的都蠻簡單的
也可以當作複習基本功囉~
下次開始就要進入較麻煩的部分了
敬請期待
0 留言:
張貼留言