gpio_explorer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. std::this_thread::sleep_for(std::chrono::milliseconds(m_nWaitingTime));
  47. FailingStatus = 0x07;
  48. //std::cout << "Status changed!!" << std::endl;
  49. //std::cout << "Waiting time is " << m_nWaitingTime <<std::endl;
  50. }
  51. StatusKeep = ret;
  52. }
  53. } })
  54. .detach();
  55. g_gpioExplorer = this;
  56. }
  57. GPIOExplorer::~GPIOExplorer()
  58. {
  59. gpiod_line_release(line);
  60. gpiod_chip_close(chip);
  61. g_gpioExplorer = nullptr;
  62. }
  63. int GPIOExplorer::read()
  64. {
  65. return gpiod_line_get_value(line);
  66. }
  67. int GPIOExplorer::monitorRisingEdge()
  68. {
  69. struct gpiod_line_event event;
  70. int ret;
  71. ret = gpiod_line_event_wait(line, NULL);
  72. if (ret < 0)
  73. {
  74. throw std::runtime_error("Error waiting for event");
  75. }
  76. ret = gpiod_line_event_read(line, &event);
  77. if (ret < 0)
  78. {
  79. throw std::runtime_error("Error reading event");
  80. }
  81. if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
  82. {
  83. // std::cout << "Rising edge detected!" << std::endl;
  84. FailingStatus = false;
  85. return 1;
  86. }
  87. if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE)
  88. {
  89. std::cout << "Failing edge detected!" << std::endl;
  90. FailingStatus = true;
  91. return 0;
  92. }
  93. }
  94. void GPIOExplorer::close()
  95. {
  96. gpiod_line_release(line);
  97. }
  98. bool GPIOExplorer::detectZWave()
  99. {
  100. // check the m_StatusQueue for the last 5 values is 11100
  101. if (m_StatusList.size() != 5)
  102. {
  103. return false;
  104. }
  105. int i = 0;
  106. // std::cout << "wave check : ";
  107. bool check = true;
  108. std::list<int>::iterator iter = m_StatusList.begin();
  109. while (iter != m_StatusList.end())
  110. {
  111. // std::cout << "-" <<*iter;
  112. if (*iter != waveLine[i])
  113. {
  114. check = false;
  115. }
  116. iter++;
  117. i++;
  118. }
  119. // std::cout <<std::endl;
  120. return check;
  121. }