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.
 
 
 
 
 
 

188 lines
4.5 KiB

  1. /*!
  2. * \file
  3. * main.c
  4. * \brief
  5. * Main application file
  6. *
  7. * Author: Christos Choutouridis AEM: 8997
  8. * email : <cchoutou@ece.auth.gr>
  9. */
  10. #include <hal.h>
  11. #include "thermostat.h"
  12. #include "console.h"
  13. /*
  14. * Zero initialized globals
  15. */
  16. app_data_t app_data;
  17. state_en state;
  18. // Value initialized globals
  19. settings_t _Init_settings(settings);
  20. /*
  21. * helper api
  22. */
  23. float_t temp_get_current (void) {
  24. return (app_data.cur) ? app_data.T[app_data.cur-1] : app_data.T[MEASUREMENTS-1];
  25. }
  26. void control (void) {
  27. static time_t sample =0;
  28. time_t now = time(0); // get time
  29. // Proximity
  30. if (proximity(&prox) < settings.Pset)
  31. app_data.flag_proximity = 1;
  32. else if (proximity(&prox) < settings.Pset + settings.Physt)
  33. ;
  34. else
  35. app_data.flag_proximity = 0;
  36. // Temperature calculation
  37. if (now - sample >= MEAS_INTERVAL) {
  38. sample = now;
  39. app_data.T[app_data.cur] = temp_read();
  40. if (++app_data.cur >= MEASUREMENTS) {
  41. app_data.cur =0;
  42. float_t f =0;
  43. for (int i=0 ; i<MEASUREMENTS ; ++i) {
  44. f += app_data.T[i];
  45. }
  46. app_data.Tav = f / MEASUREMENTS;
  47. app_data.signal_cycle = 1;
  48. app_data.flag_init = 0;
  49. }
  50. }
  51. // Output handling
  52. if (!app_data.flag_init) {
  53. switch (settings.mode) {
  54. case HEATING: {
  55. if (app_data.Tav <= settings.Tset)
  56. app_data.flag_output = 1;
  57. else if (app_data.Tav < settings.Tset + settings.Thyst)
  58. ;
  59. else
  60. app_data.flag_output = 0;
  61. }
  62. break;
  63. case COOLING: {
  64. if (app_data.Tav <= settings.Tset - settings.Thyst)
  65. app_data.flag_output = 0;
  66. else if (app_data.Tav < settings.Tset)
  67. ;
  68. else
  69. app_data.flag_output = 1;
  70. }
  71. break;
  72. }
  73. }
  74. }
  75. void display (void) {
  76. static time_t mark =0;
  77. char line1[18];
  78. char line2[18];
  79. time_t now = time(0);
  80. switch (state) {
  81. case ST_INIT:
  82. sprintf(line1, "\fInitializing %c ", (time(0)%2) ? '*' : ' ');
  83. sprintf(line2, "\n ");
  84. // escapes
  85. if (app_data.flag_proximity)
  86. state = ST_USER;
  87. else if (app_data.signal_cycle) {
  88. app_data.signal_cycle =0;
  89. mark = now;
  90. state = ST_AVERAGE;
  91. }
  92. break;
  93. case ST_COUNT:
  94. sprintf(line1, "\fMeasuring %c ", (time(0)%2) ? '*' : ' ');
  95. sprintf(line2, "\n%s: %s ",
  96. (settings.mode == HEATING) ? "Heating" : "Cooling",
  97. (app_data.flag_output) ? "On " : "Off"
  98. );
  99. // escapes
  100. if (app_data.flag_proximity)
  101. state = ST_USER;
  102. else if (app_data.signal_cycle) {
  103. app_data.signal_cycle =0;
  104. mark = now;
  105. state = ST_AVERAGE;
  106. }
  107. break;
  108. case ST_AVERAGE:
  109. sprintf(line1, "\fTav=%4.1f ", app_data.Tav);
  110. sprintf(line2, "\n%s: %s ",
  111. (settings.mode == HEATING) ? "Heating" : "Cooling",
  112. (app_data.flag_output) ? "On " : "Off"
  113. );
  114. // escapes
  115. if (app_data.flag_proximity)
  116. state = ST_USER;
  117. else if (now - mark >= UI_AVERAGE_TIME)
  118. state = ST_COUNT;
  119. break;
  120. case ST_USER:
  121. sprintf(line1, "\fTav=%4.1f T=%4.1f", app_data.Tav, temp_get_current());
  122. sprintf(line2, "\n%s: %s ",
  123. (settings.mode == HEATING) ? "Heating" : "Cooling",
  124. (app_data.flag_output) ? "On " : "Off"
  125. );
  126. // escapes
  127. if (!app_data.flag_proximity)
  128. state = ST_COUNT;
  129. break;
  130. }
  131. lcd_puts(line1);
  132. lcd_puts(line2);
  133. }
  134. void outputs (void) {
  135. static uint8_t pr_output =0;
  136. if (app_data.flag_init)
  137. return;
  138. led_green(app_data.flag_output && settings.mode == COOLING);
  139. led_red (app_data.flag_output && settings.mode == HEATING);
  140. if (app_data.flag_output != pr_output) {
  141. relay(app_data.flag_output);
  142. pr_output = app_data.flag_output;
  143. }
  144. }
  145. int main(void) {
  146. hal_hw_init ();
  147. lcd_init ();
  148. proximity_init(&prox);
  149. temp_init (TEMP_11_BIT);
  150. lcd_enable(1);
  151. app_data.flag_init = 1;
  152. while (1) {
  153. control ();
  154. display ();
  155. outputs();
  156. console ();
  157. HAL_Delay(10);
  158. }
  159. }