相关参考

野火linux开发板GPIO

libgpiod库注意事项

库源码git地址——目前还在更新

库参考手册地址

编译源码参考http://hohaitonydiao.site/archives/libgpiodyuan-ma-bian-yi

gpioTest

/*
    运行GPIO程序不许使用Ctrl+z退出
    否则不能释放驱动,导致一直gpioLine占用
*/

#include <iostream>
#include <thread>
#include <chrono>
#include <iomanip>
#include "./include/libgpiod/include/gpiod.hpp"

const char *edge_event_type_str(const ::gpiod::edge_event &event)
{
	switch (event.type()) {
	case ::gpiod::edge_event::event_type::RISING_EDGE:
		return "Rising";
	case ::gpiod::edge_event::event_type::FALLING_EDGE:
		return "Falling";
	default:
		return "Unknown";
	}
}

void pin3_Out(gpiod::chip& chip, int& offset){
    
}

void pin5_In(gpiod::chip& chip, int& offset){
    
}

int main(int argn, char** argv){
    gpiod::chip chip("/dev/gpiochip1");     //先打开对应的gpiochip
   
    int offset1 = chip.get_line_offset_from_name("PIN_3");  //从名字获取offset偏移值
    int offset2 = chip.get_line_offset_from_name("PIN_5");
    
    /*  
        链式编程    
    */
    gpiod::line_request request_out = chip  .prepare_request()  
                                        .set_consumer("test")
                                        .add_line_settings(
                                            offset1, 
                                            gpiod::line_settings()
                                                .set_direction(gpiod::line::direction::OUTPUT)
                                        )
                                        .do_request();
    
    gpiod::line_request request_in = chip  .prepare_request()
                                        .set_consumer("test")
                                        .add_line_settings(
                                            offset2, 
                                            gpiod::line_settings()
                                                .set_direction(gpiod::line::direction::INPUT)
                                                // .set_edge_detection(gpiod::line::edge::BOTH)     //边界检测
                                                .set_bias(gpiod::line::bias::PULL_DOWN)             //下拉输入
                                                // .set_debounce_period(std::chrono::milliseconds(20))  //软件消抖
                                        )
                                        .do_request();
                                                

                                            
    // if (offset>=0)
    // {
    //     std::cout<<"Chip Name:"<<chip.get_info().name()<<std::endl;
    //     std::cout<<"Line Name:"<<"PIN_3"<<std::endl;
    //     std::cout<<"Line Offset:"<<offset<<std::endl;
    // }
    // else return -1;


    //gpiod::edge_event_buffer edgeBuffer(1);

    int i = 5;
    while (i)
    {
        // request_in.read_edge_events(edgeBuffer);

        request_out.set_value(offset1, gpiod::line::value::ACTIVE);
        std::cout<<"PIN_5 State" <<request_in.get_value(offset2)<<std::endl;
        
        
        std::this_thread::sleep_for(std::chrono::seconds(1));
        
        request_out.set_value(offset1, gpiod::line::value::INACTIVE);
        std::cout<<"PIN_5 State" <<request_in.get_value(offset2)<<std::endl;
        
        std::this_thread::sleep_for(std::chrono::seconds(1));
        i--;
    }

    // gpiod::request_config requesrt_config;
    // requesrt_config

    // gpiod::line PIN_3 = chip.find_line("PIN_3");

    // gpiod::line_request lr;
    // lr.consumer = "test";
    // lr.request_type = gpiod::line_request::DIRECTION_OUTPUT;
    // lr.flags=gpiod::line_request::FLAG_ACTIVE_LOW;
    // PIN_3.request(lr);
    

    //std::cout<<gpiod::api_version();
    return 0;
}

山和山不相遇,人与人要相逢