Download the file
  1. #include "protocol.h"
  2. #include "heater_controller_slave.h"
  3.  
  4. Protocol::Protocol(Speaker* speaker):
  5. speaker(speaker)
  6. { }
  7.  
  8. void Protocol::handle(uint8_t * buffer, uint8_t buffer_len) {
  9. switch(buffer[0]) {
  10. case 0x00:
  11. status();
  12. break;
  13. case 0x01:
  14. relay.close();
  15. break;
  16. case 0x02:
  17. relay.open();
  18. break;
  19. default:
  20. error(buffer[0]);
  21. break;
  22. }
  23. }
  24.  
  25. void Protocol::status() {
  26. uint8_t* buf = speaker->get_buffer();
  27. temperature.start_reading();
  28. uint16_t cur = current.get_value();
  29. uint16_t temp = temperature.get_value();
  30. uint8_t i = 0;
  31. buf[i++] = 0x80; // status reply
  32. buf[i++] = 4; // this value is the lenth of the data below
  33. buf[i++] = (relay.get_state() << 7) | ((temp & 0x0100) >> 8); // take only bit 8, since all bits >= 8 have the same value
  34. buf[i++] = temp & 0xFF;
  35. buf[i++] = (cur >> 8);
  36. buf[i++] = cur & 0xFF;
  37. speaker->send(i, true);
  38. }
  39.  
  40. void Protocol::error(uint8_t faulty_command) {
  41. uint8_t* buf = speaker->get_buffer();
  42. uint8_t i = 0;
  43. buf[i++] = 0xFF; // error reply
  44. buf[i++] = 1;
  45. buf[i++] = faulty_command;
  46. speaker->send(i, true);
  47. }
  48.  
  49. void Protocol::got_ack() {
  50. speaker->got_ack();
  51. }
  52.  
  53.  
  54. // vim:ft=arduino
  55.  
  56.  
  57.