code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,20 +1,20 @@
/*
PIP - Platform Independent Primitives
GPIO
Andrey Bychkov work.a.b@yandex.ru, Ivan Pelipenko peri4ko@yandex.ru
PIP - Platform Independent Primitives
GPIO
Andrey Bychkov work.a.b@yandex.ru, Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pigpio.h"
@@ -22,9 +22,9 @@
# define GPIO_SYS_CLASS
#endif
#ifdef GPIO_SYS_CLASS
# include <cstdlib>
# include <fcntl.h>
# include <unistd.h>
# include <cstdlib>
#endif
@@ -64,8 +64,7 @@
//!
PIGPIO::PIGPIO(): PIThread() {
}
PIGPIO::PIGPIO(): PIThread() {}
PIGPIO::~PIGPIO() {
@@ -74,7 +73,7 @@ PIGPIO::~PIGPIO() {
PIMutexLocker ml(mutex);
#ifdef GPIO_SYS_CLASS
PIVector<int> ids = gpio_.keys();
for(int i = 0; i < ids.size_s(); i++) {
for (int i = 0; i < ids.size_s(); i++) {
GPIOData & g(gpio_[ids[i]]);
if (g.fd != -1) {
::close(g.fd);
@@ -92,7 +91,6 @@ PIGPIO * PIGPIO::instance() {
}
PIString PIGPIO::GPIOName(int gpio_num) {
return PIStringAscii("gpio") + PIString::fromNumber(gpio_num);
}
@@ -101,13 +99,13 @@ PIString PIGPIO::GPIOName(int gpio_num) {
void PIGPIO::exportGPIO(int gpio_num) {
#ifdef GPIO_SYS_CLASS
PIString valfile = "/sys/class/gpio/" + GPIOName(gpio_num) + "/value";
int fd = ::open(valfile.dataAscii(), O_RDONLY);
int fd = ::open(valfile.dataAscii(), O_RDONLY);
if (fd != -1) {
::close(fd);
return;
}
int ret = 0;
ret = system(PIString("echo " + PIString::fromNumber(gpio_num) + " >> /sys/class/gpio/export").dataAscii());
ret = system(PIString("echo " + PIString::fromNumber(gpio_num) + " >> /sys/class/gpio/export").dataAscii());
if (ret == 0) {
PITimeMeasurer tm;
while (tm.elapsed_s() < 1.) {
@@ -130,8 +128,8 @@ void PIGPIO::openGPIO(GPIOData & g) {
g.fd = -1;
}
PIString fp = "/sys/class/gpio/" + g.name + "/value";
g.fd = ::open(fp.dataAscii(), O_RDWR);
//piCoutObj << "initGPIO" << g.num << ":" << fp << g.fd << errorString();
g.fd = ::open(fp.dataAscii(), O_RDWR);
// piCoutObj << "initGPIO" << g.num << ":" << fp << g.fd << errorString();
#endif
}
@@ -139,7 +137,7 @@ void PIGPIO::openGPIO(GPIOData & g) {
bool PIGPIO::getPinState(int gpio_num) {
#ifdef GPIO_SYS_CLASS
GPIOData & g(gpio_[gpio_num]);
char r = 0;
char r = 0;
int ret = 0;
if (g.fd != -1) {
::lseek(g.fd, 0, SEEK_SET);
@@ -149,7 +147,7 @@ bool PIGPIO::getPinState(int gpio_num) {
if (r == '0') return false;
}
}
//piCoutObj << "pinState" << gpio_num << ":" << ret << (int)r << errorString();
// piCoutObj << "pinState" << gpio_num << ":" << ret << (int)r << errorString();
#endif
return false;
}
@@ -159,7 +157,7 @@ void PIGPIO::begin() {
PIMutexLocker ml(mutex);
if (watch_state.isEmpty()) return;
PIVector<int> ids = watch_state.keys();
for(int i = 0; i < ids.size_s(); i++) {
for (int i = 0; i < ids.size_s(); i++) {
GPIOData & g(gpio_[ids[i]]);
if (g.num != -1 && !g.name.isEmpty()) {
openGPIO(g);
@@ -181,7 +179,7 @@ void PIGPIO::run() {
GPIOData & g(gpio_[it.key()]);
if (g.num == -1 || g.name.isEmpty()) continue;
bool v = getPinState(g.num);
//piCoutObj << "*** pin state ***" << ids[i] << "=" << v;
// piCoutObj << "*** pin state ***" << ids[i] << "=" << v;
if (watch_state[g.num] != v) {
watch_state[g.num] = v;
changed.push_back({g.num, v});
@@ -197,7 +195,7 @@ void PIGPIO::end() {
PIMutexLocker ml(mutex);
if (watch_state.isEmpty()) return;
PIVector<int> ids = watch_state.keys();
for(int i = 0; i < ids.size_s(); i++) {
for (int i = 0; i < ids.size_s(); i++) {
GPIOData & g(gpio_[ids[i]]);
if (g.fd != -1) {
#ifdef GPIO_SYS_CLASS
@@ -214,20 +212,16 @@ void PIGPIO::initPin(int gpio_num, Direction dir) {
PIMutexLocker ml(mutex);
GPIOData & g(gpio_[gpio_num]);
if (g.num == -1) {
g.num = gpio_num;
g.num = gpio_num;
g.name = GPIOName(gpio_num);
exportGPIO(gpio_num);
}
g.dir = dir;
g.dir = dir;
int ret = 0;
NO_UNUSED(ret);
switch(dir) {
case In:
ret = system(("echo in >> /sys/class/gpio/" + g.name + "/direction").dataAscii());
break;
case Out:
ret = system(("echo out >> /sys/class/gpio/" + g.name + "/direction").dataAscii());
break;
switch (dir) {
case In: ret = system(("echo in >> /sys/class/gpio/" + g.name + "/direction").dataAscii()); break;
case Out: ret = system(("echo out >> /sys/class/gpio/" + g.name + "/direction").dataAscii()); break;
default: break;
}
openGPIO(g);
@@ -242,10 +236,12 @@ void PIGPIO::pinSet(int gpio_num, bool value) {
int ret = 0;
NO_UNUSED(ret);
if (g.fd != -1) {
if (value) ret = ::write(g.fd, "1", 1);
else ret = ::write(g.fd, "0", 1);
if (value)
ret = ::write(g.fd, "1", 1);
else
ret = ::write(g.fd, "0", 1);
}
//piCoutObj << "pinSet" << gpio_num << ":" << ret << errorString();
// piCoutObj << "pinSet" << gpio_num << ":" << ret << errorString();
#endif
}
@@ -306,5 +302,5 @@ void PIGPIO::clearWatch() {
#ifdef __GNUC__
//# pragma GCC diagnostic pop
// # pragma GCC diagnostic pop
#endif