gpio_explorer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "gpio_explorer.hpp"
  2. #include <stdexcept>
  3. #include <thread>
  4. #include <iostream>
  5. int waveLine[5] = {1,1,1,1,0};
  6. GPIOExplorer* g_gpioExplorer = nullptr;
  7. GPIOExplorer::GPIOExplorer(const char *chipname, int line_offset)
  8. {
  9. // Initialize the status queue with 0
  10. m_StatusList.push_back(1);
  11. m_StatusList.push_back(1);
  12. m_StatusList.push_back(1);
  13. m_StatusList.push_back(1);
  14. m_StatusList.push_back(1);
  15. chip = gpiod_chip_open_by_name(chipname);
  16. if (!chip)
  17. {
  18. throw std::runtime_error("Error opening chip");
  19. }
  20. line = gpiod_chip_get_line(chip, line_offset);
  21. if (!line)
  22. {
  23. gpiod_chip_close(chip);
  24. throw std::runtime_error("Error getting line");
  25. }
  26. int ret = gpiod_line_request_input(line, "gpio_explorer");
  27. if (ret < 0)
  28. {
  29. gpiod_chip_close(chip);
  30. throw std::runtime_error("Error requesting rising edge events");
  31. }
  32. std::thread([this]
  33. {
  34. while(true)
  35. {
  36. int ret;
  37. ret = gpiod_line_get_value(line);
  38. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  39. m_StatusList.push_back(ret);
  40. m_StatusList.pop_front();
  41. if(ret == 0)
  42. {
  43. if(detectZWave())
  44. {
  45. std::lock_guard<std::mutex> lock(m_mutex);
  46. FailingStatus = 0x07;
  47. //std::cout << "Status changed!!" << std::endl;
  48. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  49. }
  50. StatusKeep = ret;
  51. }
  52. } })
  53. .detach();
  54. g_gpioExplorer = this;
  55. }
  56. GPIOExplorer::~GPIOExplorer()
  57. {
  58. gpiod_line_release(line);
  59. gpiod_chip_close(chip);
  60. g_gpioExplorer = nullptr;
  61. }
  62. int GPIOExplorer::read()
  63. {
  64. return gpiod_line_get_value(line);
  65. }
  66. int GPIOExplorer::monitorRisingEdge()
  67. {
  68. struct gpiod_line_event event;
  69. int ret;
  70. ret = gpiod_line_event_wait(line, NULL);
  71. if (ret < 0)
  72. {
  73. throw std::runtime_error("Error waiting for event");
  74. }
  75. ret = gpiod_line_event_read(line, &event);
  76. if (ret < 0)
  77. {
  78. throw std::runtime_error("Error reading event");
  79. }
  80. if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
  81. {
  82. // std::cout << "Rising edge detected!" << std::endl;
  83. FailingStatus = false;
  84. return 1;
  85. }
  86. if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE)
  87. {
  88. std::cout << "Failing edge detected!" << std::endl;
  89. FailingStatus = true;
  90. return 0;
  91. }
  92. }
  93. void GPIOExplorer::close()
  94. {
  95. gpiod_line_release(line);
  96. }
  97. bool GPIOExplorer::detectZWave()
  98. {
  99. // check the m_StatusQueue for the last 5 values is 11100
  100. if (m_StatusList.size() != 5)
  101. {
  102. return false;
  103. }
  104. int i = 0;
  105. std::cout << "wave check : ";
  106. bool check = true;
  107. std::list<int>::iterator iter = m_StatusList.begin();
  108. while(iter!=m_StatusList.end())
  109. {
  110. std::cout << "-" <<*iter;
  111. if(*iter != waveLine[i])
  112. {
  113. check = false;
  114. }
  115. iter++;
  116. i++;
  117. }
  118. std::cout <<std::endl;
  119. return check;
  120. }