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