From 6a271e8f48a633c53bbd76f50afd52bc943659e4 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sun, 18 Dec 2016 22:19:53 +0200 Subject: [PATCH] Move all strings to hmi_msg --- src/cli_microrl.c | 27 +++++++++------------------ src/hmi_msg.c | 2 ++ src/hmi_msg.h | 19 +++++++++++++++++++ src/main.c | 5 +++-- src/rfid.c | 19 +++++++++---------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/cli_microrl.c b/src/cli_microrl.c index dfc2656..bead7e5 100644 --- a/src/cli_microrl.c +++ b/src/cli_microrl.c @@ -49,7 +49,6 @@ char cli_get_char(void) void cli_print_help(const char *const *argv) { (void) argv; - putc('\n', stdout); printf_P(PSTR(CLI_HELP_MSG "\n")); for (uint8_t i = 0; i < NUM_ELEMS(cli_cmds); i++) { @@ -75,7 +74,6 @@ void print_version(FILE *stream) void cli_print_ver(const char *const *argv) { (void) argv; - putc('\n', stdout); print_version(stdout); } @@ -83,7 +81,6 @@ void cli_print_ver(const char *const *argv) void cli_print_ascii_tbls(const char *const *argv) { (void) argv; - putc('\n', stdout); // ASCII table print print_ascii_tbl(stdout); unsigned char ascii[128]; @@ -96,7 +93,6 @@ void cli_print_ascii_tbls(const char *const *argv) void cli_handle_month(const char *const *argv) { - putc('\n', stdout); lcd_goto(0x40); // Got to the beginning of the next line char spaces_to_print = 16; for (int i = 0; i < 6; i++) { @@ -123,26 +119,24 @@ void cli_rfid_read(const char *const *argv) (void) argv; Uid uid; Uid *uid_ptr = &uid; - putc('\n', stdout); if (PICC_IsNewCardPresent()) { - printf("Card selected!\n"); + printf(CARD_SELECTED_MSG "\n"); PICC_ReadCardSerial(uid_ptr); - printf("UID size: 0x%02X\n", uid.size); - printf("UID sak: 0x%02X\n", uid.sak); - printf("Card UID: "); + printf_P(PSTR(UID_SIZE_MSG "\n"), uid.size); + printf_P(PSTR(UID_SAK_MSG"\n"), uid.sak); + printf_P(PSTR(CARD_UID_MSG)); for (byte i = 0; i < uid.size; i++) { printf("%02X", uid.uidByte[i]); } - printf_P(PSTR("\n")); + putc('\n', stdout); } else { - printf_P((PSTR("Unable to select card.\n"))); + printf_P((PSTR(CARD_NOT_SELECTED))); } } void cli_rfid_add(const char *const *argv) { (void) argv; - putc('\n', stdout); Uid uid; card_t card; if (PICC_IsNewCardPresent()) { @@ -155,34 +149,30 @@ void cli_rfid_add(const char *const *argv) { rfid_add_card(&card); free(user); // card user has ben copied to the linked list } else { - printf_P(PSTR("Unable to detect card.\n")); + printf_P(PSTR(UNABLE_TO_DETECT_CARD_MSG "\n")); } } void cli_rfid_remove(const char *const *argv) { (void) argv; - putc('\n', stdout); rfid_remove_card_by_user(argv[1]); } void cli_rfid_list(const char *const *argv) { (void) argv; - putc('\n', stdout); rfid_list_cards(); } void cli_print_cmd_error(void) { - putc('\n', stdout); printf_P(PSTR(CLI_NO_CMD "\n")); } void cli_print_cmd_arg_error(void) { - putc('\n', stdout); printf_P(PSTR(CLI_ARGS_MSG "\n")); } @@ -191,6 +181,7 @@ int cli_execute(int argc, const char *const *argv) { for (uint8_t i = 0; i < NUM_ELEMS(cli_cmds); i++) { if (!strcmp_P(argv[0], cli_cmds[i].cmd)) { + putc('\n', stdout); // Test do we have correct arguments to run command // Function arguments count shall be defined in struct if ((argc - 1) != cli_cmds[i].func_argc) { @@ -204,7 +195,7 @@ int cli_execute(int argc, const char *const *argv) return 0; } } - + putc('\n', stdout); cli_print_cmd_error(); return 0; } diff --git a/src/hmi_msg.c b/src/hmi_msg.c index 427098a..048a3d4 100644 --- a/src/hmi_msg.c +++ b/src/hmi_msg.c @@ -26,3 +26,5 @@ const char remove_cmd[] PROGMEM = REMOVE_CMD; const char remove_help[] PROGMEM = REMOVE_HELP; const char list_cmd[] PROGMEM = LIST_CMD; const char list_help[] PROGMEM = LIST_HELP; + +const char access_denied_msg[] PROGMEM = ACCESS_DENIED_MSG; diff --git a/src/hmi_msg.h b/src/hmi_msg.h index 6ea81fa..edb4fbc 100644 --- a/src/hmi_msg.h +++ b/src/hmi_msg.h @@ -11,22 +11,41 @@ #define HELP_HELP "Get help" #define VER_CMD "version" #define VER_HELP "Print FW version" + #define ASCII_CMD "ascii" #define ASCII_HELP "print ASCII tables" + #define MONTH_CMD "month" #define MONTH_HELP "Find matching month from lookup list. Usage: month " + #define CLI_HELP_MSG "Implemented commands:" #define CLI_NO_CMD "Command not implemented.\n Use to get help." #define CLI_ARGS_MSG "To few or to many arguments for this command\nUse " + #define READ_CMD "read" #define READ_HELP "Read and print out card info that is currently in proximity of the reader" + #define ADD_CMD "add" #define ADD_HELP "Add a new card to the system. Usage: add " + #define REMOVE_CMD "remove" #define REMOVE_HELP "Remove a card from the system. Usage: remove " + #define LIST_CMD "list" #define LIST_HELP "List all added cards" +#define ACCESS_DENIED_MSG "Access denied!" +#define NOT_ADDING_CARD_MSG1 "Found card \"" +#define NOT_ADDING_CARD_MSG2 "\", not adding it again." +#define OUT_OF_MEMORY_MSG "Out of memory. Please remove cards." +#define NO_CARDS_ADDED_MSG "No cards added" +#define CARD_NOT_FOUND_MSG "Card not found" +#define LINKED_LIST_ERROR_MSG "Invalid situation when removing card" +#define UNABLE_TO_DETECT_CARD_MSG "Unable to detect card." +#define CARD_SELECTED_MSG "Card selected!" +#define UID_SIZE_MSG "UID size: 0x%02X" +#define UID_SAK_MSG "UID sak: 0x%02X" + extern PGM_P const months[]; diff --git a/src/main.c b/src/main.c index 88d6641..81d25bb 100644 --- a/src/main.c +++ b/src/main.c @@ -141,8 +141,8 @@ static inline void handle_door() { } else { DOOR_CLOSE; lcd_goto(0x40); - lcd_puts("Access denied!"); - for (int8_t i=4; i > -1; i--) { + lcd_puts_P(access_denied_msg); + for (int8_t i=16-strlen_P(access_denied_msg); i > -1; i--) { lcd_putc(' '); } } @@ -151,6 +151,7 @@ static inline void handle_door() { } if ((message_start+5) < time_cur) { + message_start = time_cur+5; lcd_goto(0x40); for (int8_t i=16; i > -1; i--) { lcd_putc(' '); diff --git a/src/rfid.c b/src/rfid.c index e4cd58c..d324221 100644 --- a/src/rfid.c +++ b/src/rfid.c @@ -36,9 +36,9 @@ void rfid_add_card(const card_t *card) { card_t *found_card = rfid_find_card(card); if (found_card) { - printf("Found card \""); + printf_P(PSTR(NOT_ADDING_CARD_MSG1)); rfid_print_card(found_card); - printf("\", not adding it again.\n"); + printf_P(PSTR(NOT_ADDING_CARD_MSG2 "\n")); return; } @@ -48,7 +48,7 @@ void rfid_add_card(const card_t *card) new_card = malloc(sizeof(card_t)); new_card_user = malloc(strlen(card->user)+1); if (!new_card || !new_card_user) { - printf_P(PSTR("Out of memory. Please remove cards.\n")); + printf_P(PSTR(OUT_OF_MEMORY_MSG "\n")); return; } @@ -75,17 +75,17 @@ void rfid_add_card(const card_t *card) void rfid_list_cards(void) { if (head == NULL) { - printf_P(PSTR("No cards added\n")); + printf_P(PSTR(NO_CARDS_ADDED_MSG "\n")); } else { card_t *current; current = head; while (current->next != NULL) { rfid_print_card(current); - printf("\n"); + putc('\n', stdout); current = current->next; } rfid_print_card(current); - printf("\n"); + putc('\n', stdout); } } @@ -95,7 +95,7 @@ void rfid_remove_card_by_user(const char *user) { curr = head; prev = NULL; if (head == NULL) { - printf_P(PSTR("No cards added\n")); + printf_P(PSTR(NO_CARDS_ADDED_MSG "\n")); return; } else { while (curr->next != NULL) { @@ -128,10 +128,9 @@ void rfid_remove_card_by_user(const char *user) { free(curr->user); free(curr); } else { - fprintf_P(stderr, PSTR("Invalid situation when removing card\n")); + printf_P(PSTR(LINKED_LIST_ERROR_MSG "\n")); } } else { - - printf_P(PSTR("Card not found\n")); + printf_P(PSTR(CARD_NOT_FOUND_MSG)); } }