18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.
This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
34
piiodevice.h
Executable file → Normal file
34
piiodevice.h
Executable file → Normal file
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Abstract input/output device
|
||||
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -25,6 +25,18 @@
|
||||
// function executed from threaded read, pass ThreadedReadData, readedData, sizeOfData
|
||||
typedef bool (*ReadRetFunc)(void * , uchar * , int );
|
||||
|
||||
/// events:
|
||||
/// void opened()
|
||||
/// void closed()
|
||||
///
|
||||
/// handlers:
|
||||
/// bool open()
|
||||
/// bool open(const PIString & path)
|
||||
/// bool open(const DeviceMode & type)
|
||||
/// bool open(const PIString & path, const DeviceMode & type)
|
||||
/// bool close()
|
||||
/// bool initialize()
|
||||
/// void flush()
|
||||
class PIIODevice: public PIThread {
|
||||
public:
|
||||
PIIODevice();
|
||||
@@ -32,7 +44,7 @@ public:
|
||||
enum DeviceMode {ReadOnly = 0x01, WriteOnly = 0x02, ReadWrite = 0x03};
|
||||
|
||||
PIIODevice(const PIString & path, DeviceMode type = ReadWrite, bool initNow = true);
|
||||
~PIIODevice() {if (opened_) closeDevice();}
|
||||
virtual ~PIIODevice() {if (opened_) {closeDevice(); if (!opened_) closed();}}
|
||||
|
||||
|
||||
DeviceMode mode() const {return mode_;}
|
||||
@@ -66,15 +78,19 @@ public:
|
||||
void startThreadedRead(ReadRetFunc func) {ret_func_ = func; if (!isRunning()) start();}
|
||||
|
||||
|
||||
EVENT_HANDLER(PIIODevice, bool, open) {if (!init_) init(); opened_ = openDevice(); return opened_;}
|
||||
EVENT_HANDLER1(PIIODevice, bool, open, const PIString &, _path) {path_ = _path; if (!init_) init(); opened_ = openDevice(); return opened_;}
|
||||
EVENT_HANDLER1(PIIODevice, bool, open, const DeviceMode &, _type) {mode_ = _type; if (!init_) init(); opened_ = openDevice(); return opened_;}
|
||||
EVENT_HANDLER2(PIIODevice, bool, open, const PIString &, _path, const DeviceMode &, _type) {path_ = _path; mode_ = _type; if (!init_) init(); opened_ = openDevice(); return opened_;}
|
||||
EVENT_HANDLER(PIIODevice, bool, close) {opened_ = !closeDevice(); return !opened_;}
|
||||
EVENT_HANDLER(PIIODevice, bool, open) {if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER1(PIIODevice, bool, open, const PIString &, _path) {path_ = _path; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER1(PIIODevice, bool, open, const DeviceMode &, _type) {mode_ = _type; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER2(PIIODevice, bool, open, const PIString &, _path, const DeviceMode &, _type) {path_ = _path; mode_ = _type; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER(PIIODevice, bool, close) {opened_ = !closeDevice(); if (!opened_) closed(); return !opened_;}
|
||||
EVENT_HANDLER(PIIODevice, bool, initialize) {init_ = init(); return init_;}
|
||||
|
||||
// Flush device
|
||||
EVENT_VHANDLER(PIIODevice, void, flush) {;}
|
||||
|
||||
EVENT(PIIODevice, opened)
|
||||
EVENT(PIIODevice, closed)
|
||||
|
||||
|
||||
// Read from device to "read_to" maximum "max_size" bytes, return readed bytes count
|
||||
virtual int read(void * read_to, int max_size) {piCout << "[PIIODevice] \"read\" not implemented!" << endl; return -2;}
|
||||
@@ -84,6 +100,7 @@ public:
|
||||
|
||||
// Read from device maximum "max_size" bytes and return them as PIByteArray
|
||||
PIByteArray read(int max_size) {buffer_in.resize(max_size); int ret = read(buffer_in.data(), max_size); if (ret < 0) return PIByteArray(); return buffer_in.resized(ret);}
|
||||
int write(const PIByteArray & data) {return write(data.data(), data.size_s());}
|
||||
|
||||
|
||||
protected:
|
||||
@@ -97,6 +114,8 @@ protected:
|
||||
// Function executed when thread read some data, default implementation execute external slot "ret_func_"
|
||||
virtual bool threadedRead(uchar * readed, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, readed, size); return true;}
|
||||
|
||||
void terminate();
|
||||
|
||||
PIString path_;
|
||||
DeviceMode mode_;
|
||||
ReadRetFunc ret_func_;
|
||||
@@ -106,7 +125,6 @@ protected:
|
||||
|
||||
private:
|
||||
EVENT_HANDLER2(PIIODevice, void, check_start, void * , data, int, delim);
|
||||
void terminate();
|
||||
|
||||
void begin();
|
||||
void run();
|
||||
|
||||
Reference in New Issue
Block a user