Download the file
  1. #include "speaker.h"
  2. #include "protocol.h"
  3. #include "heater_controller_master.h"
  4.  
  5. Speaker::Speaker():
  6. buffer_len(0),
  7. with_ack(false),
  8. retry_count(0),
  9. next_retry_millis(0)
  10. {}
  11.  
  12. void Speaker::set_protocol(Protocol* _protocol) {
  13. protocol = _protocol;
  14. }
  15.  
  16. bool Speaker::canRun(uint32_t now) {
  17. if (buffer_len > 0 && retry_count > 0 && millis() > next_retry_millis)
  18. return rf12_canSend();
  19. return false;
  20. }
  21.  
  22. void Speaker::run(uint32_t now) {
  23. if (with_ack && retry_count == 1) {
  24. buffer_len = 0;
  25. protocol->got_nack();
  26. }
  27. uint8_t header = (with_ack ? RF12_HDR_ACK : 0) | RF12_HDR_DST | SLAVE_ID;
  28. //SHOW_BUFFER("SEND: ", header, buffer, buffer_len);
  29. rf12_sendStart(header, buffer, buffer_len);
  30. rf12_sendWait(0);
  31. if (with_ack) {
  32. //Serial.println("with_ack");
  33. retry_count --;
  34. next_retry_millis = millis() + SEND_RETRY_TIMEOUT;
  35. }
  36. else {
  37. //Serial.println("no_ack");
  38. buffer_len = 0;
  39. }
  40. }
  41.  
  42. uint8_t* Speaker::get_buffer() {
  43. return buffer;
  44. }
  45.  
  46. void Speaker::send(uint8_t len, bool ack) {
  47. with_ack = ack;
  48. buffer_len = len;
  49. retry_count = SEND_RETRY_COUNT + 1;
  50. next_retry_millis = millis();
  51. }
  52.  
  53. void Speaker::got_ack() {
  54. //Serial.println("got_ack");
  55. buffer_len = 0;
  56. }
  57. // vim:ft=arduino
  58.