Microprocessor and peripheral 2 assignments for AUTH
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.
 
 
 
 
 
 

131 lines
4.2 KiB

  1. /*
  2. * console.c
  3. *
  4. * Created on: Jun 28, 2020
  5. * Author: hoo2
  6. */
  7. #include "console.h"
  8. static char _buffer[COM_BUFFER_SIZE];
  9. static int _cursor;
  10. static char _ans[512];
  11. void parse (char* buffer) {
  12. int i;
  13. float_t s;
  14. if ((strncmp((const void*)buffer, "help", 4) == 0) ||
  15. (strncmp((const void*)buffer, "?", 1) == 0) ) {
  16. i = sprintf (_ans, "\r\nThermostat commands:\r\n");
  17. i += sprintf (&_ans[i], "info : Get information\r\n");
  18. i += sprintf (&_ans[i], "set Tset <val> : Set Thermostat\'s temperature\r\n");
  19. i += sprintf (&_ans[i], "set Thyst <val> : Set Thermostat\'s hysteresis\r\n");
  20. i += sprintf (&_ans[i], "set Pset <val> : Set Proximity\r\n");
  21. i += sprintf (&_ans[i], "set Physt <val> : Set Proximity\'s hysteresis\r\n");
  22. i += sprintf (&_ans[i], "set mode heating : Set Thermostat to heating mode\r\n");
  23. i += sprintf (&_ans[i], "set mode cooling : Set Thermostat to cooling mode\r\n");
  24. i += sprintf (&_ans[i], "\r\n");
  25. _ans[i] =0;
  26. }
  27. else if (strncmp((const void*)buffer, "info", 4) == 0) {
  28. i = sprintf (_ans, "\r\nThermostat data:\r\n");
  29. i += sprintf (&_ans[i], "Tcur = %4.1f\r\n", temp_get_current ());
  30. i += sprintf (&_ans[i], "Tav = %4.1f\r\n", app_data.Tav);
  31. i += sprintf (&_ans[i], "Mode = %s\r\n", (settings.mode == HEATING) ? "Heating" : "Cooling");
  32. i += sprintf (&_ans[i], "Output = %s\r\n", (app_data.flag_output) ? "On" : "Off");
  33. i += sprintf (&_ans[i], "Tset = %4.1f, Hyst = %4.1f\r\n", settings.Tset, settings.Thyst);
  34. i += sprintf (&_ans[i], "Pset = %4.1f, Hyst = %4.1f\r\n", settings.Pset, settings.Physt);
  35. i += sprintf (&_ans[i], "\r\n");
  36. _ans[i] =0;
  37. }
  38. else if (strncmp((const void*)buffer, "set ", 4) == 0) {
  39. if (strncmp((const void*)&buffer[4], "Tset ", 5) == 0) {
  40. sscanf((const void*)&buffer[9], "%f", &s);
  41. if (s > -20 && s < 80) {
  42. settings.Tset = s;
  43. i = sprintf (_ans, "OK\r\n");
  44. }
  45. else
  46. i = sprintf (_ans, "Error\r\n");
  47. _ans[i] =0;
  48. }
  49. else if (strncmp((const void*)&buffer[4], "Thyst ", 6) == 0) {
  50. sscanf((const void*)&buffer[10], "%f", &s);
  51. if (s > 0 && s < 20) {
  52. settings.Thyst = s;
  53. i = sprintf (_ans, "OK\r\n");
  54. }
  55. else
  56. i = sprintf (_ans, "Error\r\n");
  57. _ans[i] =0;
  58. }
  59. else if (strncmp((const void*)&buffer[4], "Pset ", 5) == 0) {
  60. sscanf((const void*)&buffer[9], "%f", &s);
  61. if (s > 0 && s < 100)
  62. settings.Pset = s;
  63. i = sprintf (_ans, "OK\r\n");
  64. _ans[i] =0;
  65. }
  66. else if (strncmp((const void*)&buffer[4], "Physt ", 6) == 0) {
  67. sscanf((const void*)&buffer[10], "%f", &s);
  68. if (s > 0 && s < 50) {
  69. settings.Physt = s;
  70. i = sprintf (_ans, "OK\r\n");
  71. }
  72. else
  73. i = sprintf (_ans, "Error\r\n");
  74. _ans[i] =0;
  75. }
  76. else if (strncmp((const void*)&buffer[4], "mode ", 5) == 0) {
  77. if (strncmp((const void*)&buffer[9], "heating", 7) == 0) {
  78. settings.mode = HEATING;
  79. i = sprintf (_ans, "OK\r\n");
  80. _ans[i] =0;
  81. }
  82. else if (strncmp((const void*)&buffer[9], "cooling", 7) == 0) {
  83. settings.mode = COOLING;
  84. i = sprintf (_ans, "OK\r\n");
  85. _ans[i] =0;
  86. }
  87. }
  88. else {
  89. i = sprintf (_ans, "Error\r\n");
  90. _ans[i] =0;
  91. }
  92. }
  93. else {
  94. i = sprintf (_ans, "Error\r\n");
  95. _ans[i] =0;
  96. }
  97. COM_Transmit((uint8_t*)_ans, i);
  98. }
  99. void console (void) {
  100. int ch;
  101. if (COM_CheckReceive()) {
  102. ch = COM_getchar(); // get character
  103. COM_putchar((char)ch); // Echo back
  104. _buffer[_cursor++] = ch; // store
  105. // Backspace, delete
  106. if (ch == 0x08 || ch == 0x7F)
  107. --_cursor;
  108. if (ch == 0 || ch == '\n' || ch == '\r') {
  109. COM_putchar('\n'); // Echo an extra '\n'
  110. _buffer[_cursor -1] = 0;
  111. _cursor =0;
  112. parse(_buffer);
  113. }
  114. if (_cursor >= COM_BUFFER_SIZE) {
  115. _buffer[_cursor -1] = 0;
  116. _cursor =0;
  117. parse(_buffer);
  118. }
  119. }
  120. }