source tree changed detached PIConsole and PIScreen* in "pip_console" library
137 lines
3.2 KiB
C++
137 lines
3.2 KiB
C++
#include "pip.h"
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
/*#include <sys/ioctl.h>
|
|
#include <sys/select.h>
|
|
|
|
void print(PIConfig::Entry*e, PIString indent = "") {
|
|
piCout << indent << e->name() << "=" << e->value();
|
|
indent += " ";
|
|
e->children().forEach([=](PIConfig::Entry*e)->PIConfig::Entry*{print(e, indent); return e;});
|
|
}
|
|
|
|
class AsyncIOWatcher: public PIThread {
|
|
PIOBJECT_SUBCLASS(AsyncIOWatcher, PIThread)
|
|
public:
|
|
AsyncIOWatcher() {
|
|
pipe_fd[0] = pipe_fd[1] = 0;
|
|
if (pipe(pipe_fd) != 0) {
|
|
piCoutObj << "Warning: can`t create pipe," << errorString();
|
|
} else {
|
|
fd_list << pipe_fd[0];
|
|
}
|
|
piCout << pipe_fd[0] << pipe_fd[1];
|
|
fd_list_changed = false;
|
|
start();
|
|
}
|
|
~AsyncIOWatcher() {
|
|
stop();
|
|
breakSelect();
|
|
if (!waitForFinish(2000))
|
|
terminate();
|
|
if (pipe_fd[0]) ::close(pipe_fd[0]);
|
|
if (pipe_fd[1]) ::close(pipe_fd[1]);
|
|
}
|
|
|
|
void add(int fd) {
|
|
que_mutex.lock();
|
|
fd_list_changed = true;
|
|
if (!add_que.contains(fd))
|
|
add_que.enqueue(fd);
|
|
que_mutex.unlock();
|
|
breakSelect();
|
|
}
|
|
void remove(int fd) {
|
|
que_mutex.lock();
|
|
fd_list_changed = true;
|
|
if (!remove_que.contains(fd))
|
|
remove_que.enqueue(fd);
|
|
que_mutex.unlock();
|
|
breakSelect();
|
|
}
|
|
|
|
private:
|
|
virtual void run() {
|
|
que_mutex.lock();
|
|
if (fd_list_changed) {
|
|
for (int i = 0; i < add_que.size_s(); ++i) {
|
|
if (!fd_list.contains(add_que[i]))
|
|
fd_list << add_que[i];
|
|
}
|
|
for (int i = 0; i < remove_que.size_s(); ++i) {
|
|
fd_list.removeAll(remove_que[i]);
|
|
}
|
|
add_que.clear();
|
|
remove_que.clear();
|
|
}
|
|
fd_list_changed = false;
|
|
que_mutex.unlock();
|
|
|
|
s_tv.tv_sec = 1;
|
|
s_tv.tv_usec = 0;
|
|
FD_ZERO(&s_set);
|
|
int max_fd = 0;
|
|
piForeachC (int fd, fd_list) {
|
|
FD_SET(fd, &s_set);
|
|
if (max_fd < fd)
|
|
max_fd = fd;
|
|
}
|
|
int ret = select(max_fd + 1, &s_set, 0, 0, 0);
|
|
piCout << "select" << ret;
|
|
if (ret <= 0) return;
|
|
read_buff.resize(1024);
|
|
uint ibuff = 0;
|
|
piForeachC (int fd, fd_list) {
|
|
if (!FD_ISSET(fd, &s_set)) continue;
|
|
if (fd == pipe_fd[0]) {
|
|
read(fd, &ibuff, sizeof(ibuff));
|
|
piCoutObj << "breaked";
|
|
continue;
|
|
}
|
|
int readed = read(fd, read_buff.data(), read_buff.size_s());
|
|
piCout << "readed" << fd << readed;
|
|
}
|
|
}
|
|
void breakSelect() {
|
|
if (pipe_fd[1])
|
|
::write(pipe_fd[1], "\0", 1);
|
|
}
|
|
|
|
PIQueue<int> add_que, remove_que;
|
|
PIDeque<int> fd_list;
|
|
PIByteArray read_buff;
|
|
PIMutex que_mutex;
|
|
bool fd_list_changed;
|
|
int pipe_fd[2];
|
|
|
|
fd_set s_set;
|
|
struct timeval s_tv;
|
|
|
|
};
|
|
*/
|
|
|
|
|
|
//PIKbdListener kbd(0, 0, false);
|
|
|
|
|
|
#include "pigpio.h"
|
|
int main(int argc, char * argv[]) {
|
|
PIObject * o = PIIODevice::createFromFullPath("ser://COM1");
|
|
piCout << o;
|
|
piCout << o->cast<PIIODevice>();
|
|
piCout << o->cast<PISerial>();
|
|
piCout << o->scopeList();
|
|
piCout << "\n\n";
|
|
PIMap<uint, PIObject::__MetaData> & m(PIObject::__meta_data());
|
|
for (auto it = m.constBegin(); it != m.constEnd(); ++it) {
|
|
const PIObject::__MetaData & md(it.value());
|
|
piCout << it.key() << md.scope_list << md.scope_id << ":";
|
|
for (auto j = md.eh_func.constBegin(); j != md.eh_func.constEnd(); ++j) {
|
|
piCout << " " << j.value().fullFormat();
|
|
}
|
|
piCout << "";
|
|
}/**/
|
|
return 0;
|
|
}
|