IRsend_test.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2017 David Conran
  2. #ifndef TEST_IRSEND_TEST_H_
  3. #define TEST_IRSEND_TEST_H_
  4. #define __STDC_LIMIT_MACROS
  5. #include <stdint.h>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <string>
  9. #include "IRrecv.h"
  10. #include "IRsend.h"
  11. #include "IRtimer.h"
  12. #define OUTPUT_BUF 10000U
  13. #define RAW_BUF 10000U
  14. #ifdef UNIT_TEST
  15. // Used to help simulate elapsed time in unit tests.
  16. uint32_t _IRtimer_unittest_now = 0;
  17. #endif // UNIT_TEST
  18. class IRsendTest : public IRsend {
  19. public:
  20. uint32_t output[OUTPUT_BUF];
  21. uint16_t last;
  22. uint16_t rawbuf[RAW_BUF];
  23. decode_results capture;
  24. explicit IRsendTest(uint16_t x, bool i = false, bool j = true)
  25. : IRsend(x, i, j) {
  26. reset();
  27. }
  28. void reset() {
  29. last = 0;
  30. for (uint16_t i = 0; i < OUTPUT_BUF; i++) output[i] = 0;
  31. for (uint16_t i = 0; i < RAW_BUF; i++) rawbuf[i] = 0;
  32. }
  33. std::string outputStr() {
  34. std::stringstream result;
  35. if (last == 0 && output[0] == 0) return "";
  36. for (uint16_t i = 0; i <= last; i++) {
  37. if ((i & 1) != outputOff) // Odd XOR outputOff
  38. result << "s";
  39. else
  40. result << "m";
  41. result << output[i];
  42. }
  43. reset();
  44. return result.str();
  45. }
  46. void makeDecodeResult(uint16_t offset = 0) {
  47. capture.decode_type = UNKNOWN;
  48. capture.bits = 0;
  49. capture.rawlen = last + 2 - offset;
  50. capture.overflow = (last - offset >= (int16_t)RAW_BUF);
  51. capture.repeat = false;
  52. capture.address = 0;
  53. capture.command = 0;
  54. capture.value = 0;
  55. capture.rawbuf = rawbuf;
  56. for (uint16_t i = 0; (i < RAW_BUF - 1) && (offset < OUTPUT_BUF);
  57. i++, offset++)
  58. if (output[offset] / kRawTick > UINT16_MAX)
  59. rawbuf[i + 1] = UINT16_MAX;
  60. else
  61. rawbuf[i + 1] = output[offset] / kRawTick;
  62. }
  63. void dumpRawResult() {
  64. std::cout << std::dec;
  65. if (capture.rawlen == 0) return;
  66. std::cout << "uint16_t rawbuf[" << capture.rawlen - 1 << "] = {";
  67. for (uint16_t i = 1; i < capture.rawlen; i++) {
  68. if (i % 8 == 1) std::cout << std::endl << " ";
  69. std::cout << (capture.rawbuf[i] * kRawTick);
  70. // std::cout << "(" << capture.rawbuf[i] << ")";
  71. if (i < capture.rawlen - 1) std::cout << ", ";
  72. }
  73. std::cout << "};" << std::endl;
  74. }
  75. void addGap(uint32_t usecs) { space(usecs); }
  76. uint16_t mark(uint16_t usec) {
  77. IRtimer::add(usec);
  78. if (last >= OUTPUT_BUF) return 0;
  79. if (last & 1) // Is odd? (i.e. last call was a space())
  80. output[++last] = usec;
  81. else
  82. output[last] += usec;
  83. return 0;
  84. }
  85. void space(uint32_t time) {
  86. IRtimer::add(time);
  87. if (last >= OUTPUT_BUF) return;
  88. if (last & 1) { // Is odd? (i.e. last call was a space())
  89. output[last] += time;
  90. } else {
  91. output[++last] = time;
  92. }
  93. }
  94. };
  95. #ifdef UNIT_TEST
  96. class IRsendLowLevelTest : public IRsend {
  97. public:
  98. std::string low_level_sequence;
  99. explicit IRsendLowLevelTest(uint16_t x, bool i = false, bool j = true)
  100. : IRsend(x, i, j) {
  101. reset();
  102. }
  103. void reset() { low_level_sequence = ""; }
  104. protected:
  105. void _delayMicroseconds(uint32_t usec) {
  106. _IRtimer_unittest_now += usec;
  107. std::ostringstream Convert;
  108. Convert << usec;
  109. low_level_sequence += Convert.str() + "usecs";
  110. }
  111. void ledOff() { low_level_sequence += "[Off]"; }
  112. void ledOn() { low_level_sequence += "[On]"; }
  113. };
  114. #endif // UNIT_TEST
  115. #endif // TEST_IRSEND_TEST_H_