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.
 
 
 
 
 
 

139 lines
4.4 KiB

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