{"id":514,"date":"2020-06-21T23:25:11","date_gmt":"2020-06-21T21:25:11","guid":{"rendered":"http:\/\/paguilar.org\/?p=514"},"modified":"2020-06-21T23:25:13","modified_gmt":"2020-06-21T21:25:13","slug":"accessing-gpio-from-qt5","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=514","title":{"rendered":"Accessing GPIO from Qt5"},"content":{"rendered":"<p>Qt5 is has many useful classes for handling the filesystem, but I didn&#8217;t find anything for handling the GPIOs in an embedded system. You need to handle the GPIO configuration by opening a standard file descriptor and reading\/writing from\/to with the standard <em>QFile<\/em> methods.<\/p>\n<p>This is just a snippet that shows briefly how to change the direction of a GPIO. In my case I have for buttons that must be configured as inputs:<\/p>\n<pre><span style=\"color: #5fd7ff;\">#define GPIO_INPUT_UP <\/span><span style=\"color: #ad7fa8;\">83<\/span>\n<span style=\"color: #5fd7ff;\">#define GPIO_INPUT_DOWN <\/span><span style=\"color: #ad7fa8;\">84<\/span>\n<span style=\"color: #5fd7ff;\">#define GPIO_INPUT_LEFT <\/span><span style=\"color: #ad7fa8;\">6<\/span>\n<span style=\"color: #5fd7ff;\">#define GPIO_INPUT_RIGHT <\/span><span style=\"color: #ad7fa8;\">1<\/span>\n\n<span style=\"color: #87ffaf;\">static<\/span> <span style=\"color: #87ffaf;\">bool<\/span> setup_gpio_input()\n{\n\u00a0\u00a0\u00a0 QString     gpio_file,\n                gpio_num;\n\u00a0\u00a0\u00a0 QFile       exportFile,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0directionFile;\n\u00a0\u00a0\u00a0 QByteArray  ba; \n\n\u00a0\u00a0\u00a0 <span style=\"color: #fce94f;\">for<\/span> (<span style=\"color: #87ffaf;\">int<\/span> i = <span style=\"color: #ad7fa8;\">0<\/span>; i &lt; <span style=\"color: #ad7fa8;\">4<\/span>; i++) {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #fce94f;\">if<\/span> (i == <span style=\"color: #ad7fa8;\">0<\/span>)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0     gpio_num.setNum(GPIO_INPUT_UP);\n        <span style=\"color: #fce94f;\">else<\/span> <span style=\"color: #fce94f;\">if<\/span> (i == <span style=\"color: #ad7fa8;\">1<\/span>)\n            gpio_num.setNum(GPIO_INPUT_DOWN);\n        <span style=\"color: #fce94f;\">else<\/span> <span style=\"color: #fce94f;\">if<\/span> (i == <span style=\"color: #ad7fa8;\">2<\/span>)\n            gpio_num.setNum(GPIO_INPUT_LEFT);\n        <span style=\"color: #fce94f;\">else<\/span> <span style=\"color: #fce94f;\">if<\/span> (i == <span style=\"color: #ad7fa8;\">3<\/span>)\n            gpio_num.setNum(GPIO_INPUT_RIGHT);\n\n        <span style=\"color: #34e2e2;\">\/\/ Export GPIO<\/span>\n        gpio_file = <span style=\"color: #ad7fa8;\">\"\/sys\/class\/gpio\/export\"<\/span>;\n        exportFile.setFileName(gpio_file);\n        exportFile.open(QIODevice::WriteOnly);\n        ba = gpio_num.toLatin1();\n        exportFile.write(ba); \n        exportFile.close();\n\n        <span style=\"color: #34e2e2;\">\/\/ Set GPIO direction<\/span>\n        gpio_file = <span style=\"color: #ad7fa8;\">\"\/sys\/class\/gpio\/gpio\"<\/span> + gpio_num + <span style=\"color: #ad7fa8;\">\"\/direction\"<\/span>;\n        directionFile.setFileName(gpio_file);\n        directionFile.open(QIODevice::WriteOnly);\n        directionFile.write(<span style=\"color: #ad7fa8;\">\"in\"<\/span>);\n        directionFile.close(); \n    }   \n\n    <span style=\"color: #fce94f;\">return<\/span> <span style=\"color: #ad7fa8;\">true<\/span>;\n}\n\n<\/pre>\n<pre><span style=\"color: #87ffaf;\">int<\/span> main(<span style=\"color: #87ffaf;\">int<\/span> argc, <span style=\"color: #87ffaf;\">char<\/span> *argv[])\n{\n    QApplication app(argc, argv);\n\n    <span style=\"color: #fce94f;\">if<\/span> (!setup_gpio_input())\n        qDebug() &lt;&lt; <span style=\"color: #ad7fa8;\">\"Fail to set GPIOs input\"<\/span>;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Qt5 is has many useful classes for handling the filesystem, but I didn&#8217;t find anything for handling the GPIOs in an embedded system. You need to handle the GPIO configuration by opening a standard file descriptor and reading\/writing from\/to with the standard QFile methods. This is just a snippet that shows briefly how to change\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=514\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,45],"tags":[64,46,63],"class_list":["post-514","post","type-post","status-publish","format-standard","hentry","category-compiling","category-linux","tag-gpio","tag-linux","tag-qt5"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=514"}],"version-history":[{"count":4,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/514\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/514\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}