I237 Riistvaralähedane programmerimine 2016
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.1KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <avr/pgmspace.h>
  4. #include "../lib/hd44780_111/hd44780.h"
  5. #include "../lib/andygock_avr-uart/uart.h"
  6. #include "hmi_msg.h"
  7. #include "print_helper.h"
  8. #include "cli_microrl.h"
  9. #include "../lib/matejx_avr_lib/mfrc522.h"
  10. typedef struct cli_cmd {
  11. PGM_P cmd;
  12. PGM_P help;
  13. void (*func_p)();
  14. const uint8_t func_argc;
  15. } cli_cmd_t;
  16. const cli_cmd_t cli_cmds[] = {
  17. {help_cmd, help_help, cli_print_help, 0},
  18. {ver_cmd, ver_help, cli_print_ver, 0},
  19. {ascii_cmd, ascii_help, cli_print_ascii_tbls, 0},
  20. {month_cmd, month_help, cli_handle_month, 1},
  21. {read_cmd, read_help, cli_rfid_read, 0}
  22. };
  23. void cli_print(const char *str)
  24. {
  25. printf("%s", str);
  26. }
  27. char cli_get_char(void)
  28. {
  29. if (uart0_peek() != UART_NO_DATA) {
  30. return uart0_getc() & UART_STATUS_MASK;
  31. } else {
  32. return 0x00;
  33. }
  34. }
  35. void cli_print_help(const char *const *argv)
  36. {
  37. (void) argv;
  38. putc('\n', stdout);
  39. printf_P(PSTR(CLI_HELP_MSG "\n"));
  40. for (uint8_t i = 0; i < NUM_ELEMS(cli_cmds); i++) {
  41. printf_P(cli_cmds[i].cmd);
  42. printf_P(PSTR(" : "));
  43. printf_P(cli_cmds[i].help);
  44. putc('\n', stdout);
  45. }
  46. }
  47. void print_version(FILE *stream)
  48. {
  49. // Print program and libc versions
  50. fprintf_P(stream, PSTR(PROG_VERSION "\n"),
  51. PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
  52. fprintf_P(stream, PSTR(LIBC_VERSION "\n"),
  53. PSTR(__AVR_LIBC_VERSION_STRING__),
  54. PSTR(__VERSION__));
  55. }
  56. void cli_print_ver(const char *const *argv)
  57. {
  58. (void) argv;
  59. putc('\n', stdout);
  60. print_version(stdout);
  61. }
  62. void cli_print_ascii_tbls(const char *const *argv)
  63. {
  64. (void) argv;
  65. putc('\n', stdout);
  66. // ASCII table print
  67. print_ascii_tbl(stdout);
  68. unsigned char ascii[128];
  69. for (unsigned char i = 0; i < sizeof(ascii); i++) {
  70. ascii[i] = i;
  71. }
  72. print_for_human(stdout, ascii, sizeof(ascii));
  73. }
  74. void cli_handle_month(const char *const *argv)
  75. {
  76. putc('\n', stdout);
  77. lcd_goto(0x40); // Got to the beginning of the next line
  78. char spaces_to_print = 16;
  79. for (int i = 0; i < 6; i++) {
  80. if (!strncmp_P(argv[1],
  81. (PGM_P)pgm_read_word(&months[i]),
  82. strlen(argv[1]))) {
  83. char printed_count;
  84. printed_count = fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i]));
  85. fputc('\n', stdout);
  86. lcd_puts_P((PGM_P)pgm_read_word(&months[i]));
  87. lcd_putc(' ');
  88. spaces_to_print -= (printed_count + 1);
  89. }
  90. }
  91. // Clear the end of the line
  92. for (; spaces_to_print > -1; spaces_to_print--) {
  93. lcd_putc(' ');
  94. }
  95. }
  96. void cli_rfid_read(const char *const *argv)
  97. {
  98. (void) argv;
  99. Uid uid;
  100. Uid *uid_ptr = &uid;
  101. printf_P(PSTR("\n"));
  102. if (PICC_IsNewCardPresent()) {
  103. printf("Card selected!\n");
  104. PICC_ReadCardSerial(uid_ptr);
  105. printf("UID size: 0x%02X\n", uid.size);
  106. printf("UID sak: 0x%02X\n", uid.sak);
  107. printf("Card UID: ");
  108. for (byte i = 0; i < uid.size; i++) {
  109. printf("%02X", uid.uidByte[i]);
  110. }
  111. printf_P(PSTR("\n"));
  112. } else {
  113. printf_P((PSTR("Unable to select card.\n")));
  114. }
  115. }
  116. void cli_print_cmd_error(void)
  117. {
  118. putc('\n', stdout);
  119. printf_P(PSTR(CLI_NO_CMD "\n"));
  120. }
  121. void cli_print_cmd_arg_error(void)
  122. {
  123. putc('\n', stdout);
  124. printf_P(PSTR(CLI_ARGS_MSG "\n"));
  125. }
  126. int cli_execute(int argc, const char *const *argv)
  127. {
  128. for (uint8_t i = 0; i < NUM_ELEMS(cli_cmds); i++) {
  129. if (!strcmp_P(argv[0], cli_cmds[i].cmd)) {
  130. // Test do we have correct arguments to run command
  131. // Function arguments count shall be defined in struct
  132. if ((argc - 1) != cli_cmds[i].func_argc) {
  133. cli_print_cmd_arg_error();
  134. return 0;
  135. }
  136. // Hand argv over to function pointer,
  137. // cross fingers and hope that funcion handles it properly
  138. cli_cmds[i].func_p (argv);
  139. return 0;
  140. }
  141. }
  142. cli_print_cmd_error();
  143. return 0;
  144. }