Download the file
  1. #include "protocol.h"
  2. #include "ui.h"
  3. #include "heater_controller_master.h"
  4.  
  5. Protocol::Protocol(Speaker* speaker):
  6. TimedTask(0), speaker(speaker), timer_armed(false)
  7. { }
  8.  
  9. bool Protocol::canRun(uint32_t now) {
  10. return timer_armed && TimedTask::canRun(now);
  11. }
  12.  
  13. void Protocol::run(uint32_t now) {
  14. timer_armed = false;
  15. ui->write_error(NO_REPLY_ERROR);
  16. }
  17.  
  18. void Protocol::set_ui(UI* _ui) {
  19. ui = _ui;
  20. }
  21.  
  22. void Protocol::handle(uint8_t * buffer, uint8_t buffer_len) {
  23. switch(buffer[0]) {
  24. case 0x80:
  25. process_status_reply(buffer+1, buffer_len-1);
  26. break;
  27. }
  28. }
  29.  
  30. void Protocol::status_request() {
  31. uint8_t* buf = speaker->get_buffer();
  32. uint8_t i = 0;
  33. buf[i++] = 0x00; // status request
  34. buf[i++] = 0;
  35. speaker->send(i, true);
  36. setRunTime(millis() + REPLY_TIMEOUT);
  37. timer_armed = true;
  38. }
  39.  
  40. void Protocol::relay_close_request() {
  41. uint8_t* buf = speaker->get_buffer();
  42. uint8_t i = 0;
  43. buf[i++] = 0x01; // relay close request
  44. buf[i++] = 0;
  45. speaker->send(i, true);
  46. }
  47.  
  48. void Protocol::relay_open_request() {
  49. uint8_t* buf = speaker->get_buffer();
  50. uint8_t i = 0;
  51. buf[i++] = 0x02; // relay open request
  52. buf[i++] = 0;
  53. speaker->send(i, true);
  54. }
  55.  
  56. void Protocol::process_status_reply(uint8_t * buffer, uint8_t buffer_len) {
  57. timer_armed = false;
  58. uint8_t data_len = buffer[0];
  59. if (data_len == buffer_len - 1) {
  60. uint8_t relay_status = buffer[1] >> 7;
  61. unsigned short temp = 0x00FF & buffer[2];
  62. if (buffer[1] & 0x01) temp |= 0xFF00;
  63. float temperature = (signed short)temp/2.0;
  64. uint16_t current = ((buffer[3] << 8) | (buffer[4]));
  65. ui->write_status(relay_status, temperature, current/1000.0);
  66. }
  67. else {
  68. ui->write_error(BAD_MESSAGE_ERROR);
  69. }
  70. }
  71.  
  72. void Protocol::got_ack() {
  73. speaker->got_ack();
  74. ui->write_OK();
  75. }
  76.  
  77. void Protocol::got_nack() {
  78. ui->write_error(NACK_ERROR);
  79. timer_armed = false;
  80. }
  81. // vim:ft=arduino
  82.  
  83.  
  84.