Gpio C++
Classes | |
class | GpioManager |
Allows to interact with a ServiceName service. More... | |
Detailed Description
Gpio API related documentation
A quick overview of basic Gpio API capabilities and examples is given.
If you find Gpio API useful and would like to know more details, please check out further sections of the Gpio API documentation or contact the Deepsy team.
Examples
-
Getting Gpio Status
#include <iostream>#include <cctype>#include <vector>#include <stdlib.h>#include <dpy/logconfigApi.h>void get_log_status_handler(boost::system::error_code ec, std::vector<dpyLogconfig::App> apps){if (ec.value() == 0) {for (unsigned int i = 0; i < apps.size(); i++) {std::cout << "\r\n" << "------------------------------------------------" << std::endl;std::cout << "[ " << apps[i].appname << " ]" << std::endl;std::cout << "------------------------------------------------"<< std::endl;std::cout << "Level: " << apps[i].level << std::endl;std::cout << "Path: " << apps[i].path << "\n" << std::endl;}} else {std::cout << "\rError : " << ec.message() << std::endl;}}int main(int argc, char *argv[]){LogConfig logConfig;std::string appName = "Modem";logConfig.asyncGetStatus(get_log_status_handler, appName);} -
Setting Gpio Value
#include <dpy/gpioApi.h>#include <iostream>volatile bool waiting = true;void result_handler(boost::system::error_code error_code){if (error_code) {std::cout << "\rError : " << error_code.message() << std::endl;} else {std::cout << "\rSynchronous operation performed successfully" << std::endl;}waiting = false;}int main(){GpioManager gpioManager;const std::string& gpioid = "DO2";int value = 0;gpioManager.asyncSetGpioValue(result_handler, gpioid, value);while (waiting == true){}} -
Set and Get Gpio Configuration
#include <dpy/gpioApi.h>#include <iostream>volatile bool waiting = true;void result_handler(boost::system::error_code error_code){if (error_code) {std::cout << "\rError : " << error_code.message() << std::endl;} else {std::cout << "\rSynchronous operation performed successfully" << std::endl;}waiting = false;}void gpio_list_handler(boost::system::error_code error_code, std::map<std::string, dpyGpio::GpioInfoConf> gpioMap){if (error_code) {std::cout << "\rError : " << error_code.message() << std::endl;} else {if (gpioMap.size() > 0) {for (auto g : gpioMap) {std::cout << "Gpio id : " << g.first << std::endl;std::cout << "\tAlias : " << g.second.alias << std::endl;std::cout << "\tType : " << g.second.type << std::endl;std::cout << "\tAddress : " << g.second.address << std::endl;if (g.second.direction == dpyGpio::GpioDirection::INPUT) {std::cout << "\tDirection : INPUT" << std::endl;} else if (g.second.direction == dpyGpio::GpioDirection::OUTPUT) {std::cout << "\tDirection : OUTPUT" << std::endl;}std::cout << "\tDefault Value : " << g.second.default_value << std::endl;std::cout << "\tActive Low : " << g.second.active_low << std::endl;if (g.second.edge_type == dpyGpio::EdgeType::FALLING) {std::cout << "\tEdge Type : FALLING" << std::endl;} else if (g.second.edge_type == dpyGpio::EdgeType::RISING) {std::cout << "\tEdge Type : RISING" << std::endl;} else if (g.second.edge_type == dpyGpio::EdgeType::BOTH) {std::cout << "\tEdge Type : BOTH" << std::endl;}std::cout << "\tPublish : " << g.second.publish << std::endl;std::cout << "\tBouncing Time : " << g.second.bouncing_time << std::endl;}} else {std::cout << "There are no more gpios. " << std::endl;}}waiting = false;}int main(){GpioManager gpioManager;const std::string &gpioid = "DI1";dpyGpio::GpioInfoConf gpioConfiguration;gpioConfiguration.alias = "Test DI1";gpioConfiguration.default_value = 1;gpioConfiguration.active_low = false;gpioConfiguration.edge_type = dpyGpio::EdgeType::BOTH;gpioConfiguration.publish = 4;gpioConfiguration.bouncing_time = 50;gpioManager.asyncSetGpioConfiguration(result_handler, gpioid, gpioConfiguration);while (waiting) {usleep(300000);}waiting = true;gpioManager.asyncGetGpios(gpio_list_handler);while (waiting) {usleep(300000);}} -
Subscribe to Gpio events
#include <dpy/serialportApi.h>#include <iostream>volatile bool waiting = true;void print_serialportevent(boost::system::error_code ec, const std::string &portid, const std::string &data){if (ec.value() == 0) {std::cout << "\nNew message received to serial port: " << portid << std::endl;std::cout << "New message: " << data << std::endl;} else {std::cout << "\rError : " << ec.message() << std::endl;}waiting = false;}void resultHandler_sync(boost::system::error_code ec){if (ec) {std::cout << "\rError : " << ec.message() << std::endl;} else {std::cout << "\rSynchronous operation performed successfully" << std::endl;}}int main(){SerialPortManager serialPort;serialPort.serialPortEvent_S(print_serialportevent);const std::string &serialportid = "COM1";std::string data = "Hello\n";serialPort.syncSend(resultHandler_sync, serialportid, data);while (waiting == true) {}}