test_gpio.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <atomic>
  2. #include "kbf.h"
  3. #include "kbf/gpio.h"
  4. #include <unity.h>
  5. // TODO use Kconfig
  6. #define INPUT_PIN 27
  7. #define INPUT_PIN_2 34
  8. #define OUTPUT_PIN 13
  9. using namespace std;
  10. using namespace kbf;
  11. static atomic<int> counter = {0};
  12. TEST_CASE("GPIO", "[kbf_gpio]") {
  13. auto input = new gpio::Input(INPUT_PIN);
  14. auto notUsed = new gpio::Input(INPUT_PIN_2);
  15. TEST_ASSERT_TRUE(notUsed->isLow())
  16. auto output = gpio::Output(OUTPUT_PIN);
  17. TEST_ASSERT_EQUAL(OUTPUT_PIN, output.pin);
  18. kbf::sleep(100);
  19. TEST_ASSERT_EQUAL(0, counter);
  20. output.high();
  21. kbf::sleep(100);
  22. TEST_ASSERT_EQUAL(0, counter);
  23. output.low();
  24. kbf::sleep(100);
  25. TEST_ASSERT_EQUAL(0, counter);
  26. input->onLow = {[](gpio::Input &input) {
  27. TEST_ASSERT_EQUAL(INPUT_PIN, input.pin);
  28. counter += 1;
  29. }};
  30. input->onHigh = {[](gpio::Input &input) {
  31. TEST_ASSERT_EQUAL(INPUT_PIN, input.pin);
  32. counter += 100;
  33. }};
  34. output.high();
  35. kbf::sleep(20);
  36. TEST_ASSERT_EQUAL(100, counter);
  37. kbf::sleep(300);
  38. TEST_ASSERT_EQUAL(100, counter);
  39. output.low();
  40. kbf::sleep(20);
  41. TEST_ASSERT_EQUAL(101, counter);
  42. kbf::sleep(300);
  43. TEST_ASSERT_EQUAL(101, counter);
  44. delete input;
  45. output.high();
  46. kbf::sleep(20);
  47. TEST_ASSERT_EQUAL(101, counter);
  48. output.low();
  49. kbf::sleep(20);
  50. TEST_ASSERT_EQUAL(101, counter);
  51. }