You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
7.2KB

  1. /*
  2. * LegoPowerFunctionsTest: LEGO Power Functions Tests
  3. * Copyright (c) 2016, 2017 Philipp Henkel
  4. */
  5. #include <ir_Lego_PF_BitStreamEncoder.h>
  6. void setup() {
  7. Serial.begin(9600);
  8. delay(1000); // wait for reset triggered by serial connection
  9. runBitStreamEncoderTests();
  10. }
  11. void loop() {
  12. }
  13. void runBitStreamEncoderTests() {
  14. Serial.println();
  15. Serial.println("BitStreamEncoder Tests");
  16. static LegoPfBitStreamEncoder bitStreamEncoder;
  17. testStartBit(bitStreamEncoder);
  18. testLowBit(bitStreamEncoder);
  19. testHighBit(bitStreamEncoder);
  20. testMessageBitCount(bitStreamEncoder);
  21. testMessageBitCountRepeat(bitStreamEncoder);
  22. testMessage407(bitStreamEncoder);
  23. testMessage407Repeated(bitStreamEncoder);
  24. testGetChannelId1(bitStreamEncoder);
  25. testGetChannelId2(bitStreamEncoder);
  26. testGetChannelId3(bitStreamEncoder);
  27. testGetChannelId4(bitStreamEncoder);
  28. testGetMessageLengthAllHigh(bitStreamEncoder);
  29. testGetMessageLengthAllLow(bitStreamEncoder);
  30. }
  31. void logTestResult(bool testPassed) {
  32. if (testPassed) {
  33. Serial.println("OK");
  34. }
  35. else {
  36. Serial.println("FAIL ############");
  37. }
  38. }
  39. void testStartBit(LegoPfBitStreamEncoder& bitStreamEncoder) {
  40. Serial.print(" testStartBit ");
  41. bitStreamEncoder.reset(0, false);
  42. int startMark = bitStreamEncoder.getMarkDuration();
  43. int startPause = bitStreamEncoder.getPauseDuration();
  44. logTestResult(startMark == 158 && startPause == 1184-158);
  45. }
  46. void testLowBit(LegoPfBitStreamEncoder& bitStreamEncoder) {
  47. Serial.print(" testLowBit ");
  48. bitStreamEncoder.reset(0, false);
  49. bitStreamEncoder.next();
  50. int lowMark = bitStreamEncoder.getMarkDuration();
  51. int lowPause = bitStreamEncoder.getPauseDuration();
  52. logTestResult(lowMark == 158 && lowPause == 421-158);
  53. }
  54. void testHighBit(LegoPfBitStreamEncoder& bitStreamEncoder) {
  55. Serial.print(" testHighBit ");
  56. bitStreamEncoder.reset(0xFFFF, false);
  57. bitStreamEncoder.next();
  58. int highMark = bitStreamEncoder.getMarkDuration();
  59. int highPause = bitStreamEncoder.getPauseDuration();
  60. logTestResult(highMark == 158 && highPause == 711-158);
  61. }
  62. void testMessageBitCount(LegoPfBitStreamEncoder& bitStreamEncoder) {
  63. Serial.print(" testMessageBitCount ");
  64. bitStreamEncoder.reset(0xFFFF, false);
  65. int bitCount = 1;
  66. while (bitStreamEncoder.next()) {
  67. bitCount++;
  68. }
  69. logTestResult(bitCount == 18);
  70. }
  71. boolean check(LegoPfBitStreamEncoder& bitStreamEncoder, unsigned long markDuration, unsigned long pauseDuration) {
  72. bool result = true;
  73. result = result && bitStreamEncoder.getMarkDuration() == markDuration;
  74. result = result && bitStreamEncoder.getPauseDuration() == pauseDuration;
  75. return result;
  76. }
  77. boolean checkNext(LegoPfBitStreamEncoder& bitStreamEncoder, unsigned long markDuration, unsigned long pauseDuration) {
  78. bool result = bitStreamEncoder.next();
  79. result = result && check(bitStreamEncoder, markDuration, pauseDuration);
  80. return result;
  81. }
  82. boolean checkDataBitsOfMessage407(LegoPfBitStreamEncoder& bitStreamEncoder) {
  83. bool result = true;
  84. result = result && checkNext(bitStreamEncoder, 158, 263);
  85. result = result && checkNext(bitStreamEncoder, 158, 263);
  86. result = result && checkNext(bitStreamEncoder, 158, 263);
  87. result = result && checkNext(bitStreamEncoder, 158, 263);
  88. result = result && checkNext(bitStreamEncoder, 158, 263);
  89. result = result && checkNext(bitStreamEncoder, 158, 263);
  90. result = result && checkNext(bitStreamEncoder, 158, 263);
  91. result = result && checkNext(bitStreamEncoder, 158, 553);
  92. result = result && checkNext(bitStreamEncoder, 158, 553);
  93. result = result && checkNext(bitStreamEncoder, 158, 263);
  94. result = result && checkNext(bitStreamEncoder, 158, 263);
  95. result = result && checkNext(bitStreamEncoder, 158, 553);
  96. result = result && checkNext(bitStreamEncoder, 158, 263);
  97. result = result && checkNext(bitStreamEncoder, 158, 553);
  98. result = result && checkNext(bitStreamEncoder, 158, 553);
  99. result = result && checkNext(bitStreamEncoder, 158, 553);
  100. return result;
  101. }
  102. void testMessage407(LegoPfBitStreamEncoder& bitStreamEncoder) {
  103. Serial.print(" testMessage407 ");
  104. bitStreamEncoder.reset(407, false);
  105. bool result = true;
  106. result = result && check(bitStreamEncoder, 158, 1026);
  107. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  108. result = result && checkNext(bitStreamEncoder, 158, 1026);
  109. result = result && !bitStreamEncoder.next();
  110. logTestResult(result);
  111. }
  112. void testMessage407Repeated(LegoPfBitStreamEncoder& bitStreamEncoder) {
  113. Serial.print(" testMessage407Repeated ");
  114. bitStreamEncoder.reset(407, true);
  115. bool result = true;
  116. result = result && check(bitStreamEncoder, 158, 1026);
  117. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  118. result = result && checkNext(bitStreamEncoder, 158, 1026L + 5L * 16000L - 10844L);
  119. result = result && checkNext(bitStreamEncoder, 158, 1026);
  120. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  121. result = result && checkNext(bitStreamEncoder, 158, 1026L + 5L * 16000L - 10844L);
  122. result = result && checkNext(bitStreamEncoder, 158, 1026);
  123. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  124. result = result && checkNext(bitStreamEncoder, 158, 1026L + 8L * 16000L - 10844L);
  125. result = result && checkNext(bitStreamEncoder, 158, 1026);
  126. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  127. result = result && checkNext(bitStreamEncoder, 158, 1026L + 8L * 16000L - 10844L);
  128. result = result && checkNext(bitStreamEncoder, 158, 1026);
  129. result = result && checkDataBitsOfMessage407(bitStreamEncoder);
  130. result = result && checkNext(bitStreamEncoder, 158, 1026);
  131. result = result && !bitStreamEncoder.next();
  132. logTestResult(result);
  133. }
  134. void testMessageBitCountRepeat(LegoPfBitStreamEncoder& bitStreamEncoder) {
  135. Serial.print(" testMessageBitCountRepeat ");
  136. bitStreamEncoder.reset(0xFFFF, true);
  137. int bitCount = 1;
  138. while (bitStreamEncoder.next()) {
  139. bitCount++;
  140. }
  141. logTestResult(bitCount == 5*18);
  142. }
  143. void testGetChannelId1(LegoPfBitStreamEncoder& bitStreamEncoder) {
  144. Serial.print(" testGetChannelId1 ");
  145. bitStreamEncoder.reset(407, false);
  146. logTestResult(bitStreamEncoder.getChannelId() == 1);
  147. }
  148. void testGetChannelId2(LegoPfBitStreamEncoder& bitStreamEncoder) {
  149. Serial.print(" testGetChannelId2 ");
  150. bitStreamEncoder.reset(4502, false);
  151. logTestResult(bitStreamEncoder.getChannelId() == 2);
  152. }
  153. void testGetChannelId3(LegoPfBitStreamEncoder& bitStreamEncoder) {
  154. Serial.print(" testGetChannelId3 ");
  155. bitStreamEncoder.reset(8597, false);
  156. logTestResult(bitStreamEncoder.getChannelId() == 3);
  157. }
  158. void testGetChannelId4(LegoPfBitStreamEncoder& bitStreamEncoder) {
  159. Serial.print(" testGetChannelId4 ");
  160. bitStreamEncoder.reset(12692, false);
  161. logTestResult(bitStreamEncoder.getChannelId() == 4);
  162. }
  163. void testGetMessageLengthAllHigh(LegoPfBitStreamEncoder& bitStreamEncoder) {
  164. Serial.print(" testGetMessageLengthAllHigh ");
  165. bitStreamEncoder.reset(0xFFFF, false);
  166. logTestResult(bitStreamEncoder.getMessageLength() == 13744);
  167. }
  168. void testGetMessageLengthAllLow(LegoPfBitStreamEncoder& bitStreamEncoder) {
  169. Serial.print(" testGetMessageLengthAllLow ");
  170. bitStreamEncoder.reset(0x0, false);
  171. logTestResult(bitStreamEncoder.getMessageLength() == 9104);
  172. }