123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include "gpio_explorer.hpp"
- #include <stdexcept>
- #include <thread>
- #include <iostream>
- int waveLine[5] = {1,1,1,1,0};
- GPIOExplorer* g_gpioExplorer = nullptr;
- GPIOExplorer::GPIOExplorer(const char *chipname, int line_offset)
- {
- // Initialize the status queue with 0
- m_StatusList.push_back(1);
- m_StatusList.push_back(1);
- m_StatusList.push_back(1);
- m_StatusList.push_back(1);
- m_StatusList.push_back(1);
- chip = gpiod_chip_open_by_name(chipname);
- if (!chip)
- {
- throw std::runtime_error("Error opening chip");
- }
- line = gpiod_chip_get_line(chip, line_offset);
- if (!line)
- {
- gpiod_chip_close(chip);
- throw std::runtime_error("Error getting line");
- }
- int ret = gpiod_line_request_input(line, "gpio_explorer");
- if (ret < 0)
- {
- gpiod_chip_close(chip);
- throw std::runtime_error("Error requesting rising edge events");
- }
- std::thread([this]
- {
- while(true)
- {
- int ret;
- ret = gpiod_line_get_value(line);
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- m_StatusList.push_back(ret);
- m_StatusList.pop_front();
- if(ret == 0)
- {
- if(detectZWave())
- {
- std::lock_guard<std::mutex> lock(m_mutex);
- FailingStatus = 0x07;
- //std::cout << "Status changed!!" << std::endl;
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- StatusKeep = ret;
- }
-
- } })
- .detach();
- g_gpioExplorer = this;
- }
- GPIOExplorer::~GPIOExplorer()
- {
- gpiod_line_release(line);
- gpiod_chip_close(chip);
- g_gpioExplorer = nullptr;
- }
- int GPIOExplorer::read()
- {
- return gpiod_line_get_value(line);
- }
- int GPIOExplorer::monitorRisingEdge()
- {
- struct gpiod_line_event event;
- int ret;
- ret = gpiod_line_event_wait(line, NULL);
- if (ret < 0)
- {
- throw std::runtime_error("Error waiting for event");
- }
- ret = gpiod_line_event_read(line, &event);
- if (ret < 0)
- {
- throw std::runtime_error("Error reading event");
- }
- if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE)
- {
- // std::cout << "Rising edge detected!" << std::endl;
- FailingStatus = false;
- return 1;
- }
- if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE)
- {
- std::cout << "Failing edge detected!" << std::endl;
- FailingStatus = true;
- return 0;
- }
- }
- void GPIOExplorer::close()
- {
- gpiod_line_release(line);
- }
- bool GPIOExplorer::detectZWave()
- {
- // check the m_StatusQueue for the last 5 values is 11100
- if (m_StatusList.size() != 5)
- {
- return false;
- }
- int i = 0;
- std::cout << "wave check : ";
- bool check = true;
- std::list<int>::iterator iter = m_StatusList.begin();
- while(iter!=m_StatusList.end())
- {
- std::cout << "-" <<*iter;
- if(*iter != waveLine[i])
- {
- check = false;
- }
- iter++;
- i++;
- }
- std::cout <<std::endl;
- return check;
- }
|