4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
419
main.cpp
419
main.cpp
@@ -16,7 +16,7 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
//#define PIP_DEBUG
|
||||
#include "pip.h"
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ private:
|
||||
|
||||
void signalFunc(PISignals::Signal signal) {
|
||||
if (signal != PISignals::Interrupt) return;
|
||||
cout << "Ctrl+C pressed, exiting ..." << endl;
|
||||
cout << endl << "Ctrl+C pressed, exiting ..." << endl;
|
||||
exit(0);
|
||||
};
|
||||
|
||||
@@ -71,293 +71,252 @@ void timerEvent2(void * data, int delim) {
|
||||
|
||||
|
||||
class ObjectTest: public PIObject {
|
||||
PIOBJECT(ObjectTest)
|
||||
public:
|
||||
ObjectTest(const PIString & name = PIString()): PIObject(name) {h = hi = ht = false;}
|
||||
EVENT_HANDLER(ObjectTest, void, handler) {h = true; cout << " handler in \"" << name() << "\"" << endl;}
|
||||
EVENT_HANDLER2(ObjectTest, void, handler_i_s, int, i, PIString, s) {hi = true; cout << " handler_i_s in \"" << name() << "\", i = " << i << ", s = \"" << s << "\"" << endl;}
|
||||
EVENT_HANDLER2(ObjectTest, void, handler_timeout, void * , data, int, delim) {ht = true; cout << " handler_timeout in \"" << name() << "\", data = " << data << ", delim = " << delim << endl;}
|
||||
EVENT_HANDLER(void, handler) {h = true; cout << " handler in \"" << name() << "\"" << endl;}
|
||||
EVENT_HANDLER2(void, handler_i_s, int, i, PIString, s) {hi = true; cout << " handler_i_s in \"" << name() << "\", i = " << i << ", s = \"" << s << "\"" << endl;}
|
||||
EVENT_HANDLER2(void, handler_timeout, void * , data, int, delim) {ht = true; cout << " handler_timeout in \"" << name() << "\", data = " << data << ", delim = " << delim << endl;}
|
||||
bool h, hi, ht;
|
||||
};
|
||||
|
||||
|
||||
class ObjectTest2: public PIObject {
|
||||
PIOBJECT(ObjectTest2)
|
||||
public:
|
||||
ObjectTest2(const PIString & name = PIString()): PIObject(name) {;}
|
||||
void raise0(const PIString & e) {cout << " event \"" << e << "\" from \"" << name() << "\"" << endl; raiseEvent(this, e);}
|
||||
void raise2(const PIString & e, int i, const PIString & s) {cout << " event \"" << e << "\" from \"" << name() << "\"" << endl; raiseEvent<int, PIString>(this, e, i, s);}
|
||||
EVENT(ObjectTest2, event0)
|
||||
EVENT(ObjectTest2, event2)
|
||||
EVENT(event0)
|
||||
EVENT(event2)
|
||||
|
||||
};
|
||||
|
||||
class CA: public PIObject {
|
||||
PIOBJECT(CA)
|
||||
public:
|
||||
CA(const PIString & n): PIObject(n) {;}
|
||||
EVENT_HANDLER(CA, void, handler_ca) {a = true; cout << " handler CA" << endl;}
|
||||
EVENT(CA, event_ca)
|
||||
EVENT_HANDLER(void, handler_ca) {a = true; cout << " handler CA" << endl;}
|
||||
EVENT(event_ca)
|
||||
bool a;
|
||||
};
|
||||
|
||||
class CB: public CA {
|
||||
PIOBJECT(CB)
|
||||
public:
|
||||
CB(const PIString & n): CA(n) {;}
|
||||
EVENT_HANDLER(CB, void, handler_cb) {b = true; cout << " handler CB" << endl;}
|
||||
EVENT_HANDLER(void, handler_cb) {b = true; cout << " handler CB" << endl;}
|
||||
bool b;
|
||||
};
|
||||
|
||||
class CC: public CB {
|
||||
PIOBJECT(CC)
|
||||
public:
|
||||
CC(const PIString & n): CB(n) {;}
|
||||
EVENT_HANDLER(CC, void, handler_cc) {c = true; cout << " handler CC" << endl;}
|
||||
EVENT_HANDLER(void, handler_cc) {c = true; cout << " handler CC" << endl;}
|
||||
bool c;
|
||||
};
|
||||
|
||||
class CD: public CC {
|
||||
PIOBJECT(CD)
|
||||
public:
|
||||
CD(const PIString & n): CC(n) {;}
|
||||
EVENT_HANDLER(CD, void, handler_cd) {d = true; cout << " handler CD" << endl;}
|
||||
EVENT_HANDLER(void, handler_cd) {d = true; cout << " handler CD" << endl;}
|
||||
bool d;
|
||||
};
|
||||
|
||||
PIDiagnostics diag;
|
||||
bool corr = true;
|
||||
void te(void * , int) {
|
||||
diag.received(256, corr);
|
||||
diag.sended(512);
|
||||
}
|
||||
|
||||
struct Packet {
|
||||
int from;
|
||||
int to;
|
||||
float data;
|
||||
int cs;
|
||||
|
||||
class ObjectA: public PIObject {
|
||||
PIOBJECT(ObjectA)
|
||||
public:
|
||||
EVENT_HANDLER1(void, handlerA, const PIString & , str) {piCout << "handler A:" << str;}
|
||||
EVENT2(eventA2, int, i, float, f);
|
||||
EVENT1(eventA1, const PIString & , str);
|
||||
};
|
||||
|
||||
class ObjectB: public PIObject {
|
||||
PIOBJECT(ObjectB)
|
||||
public:
|
||||
EVENT_HANDLER2(void, handlerB, int, i, float, f) {piCout << "handler B:" << i << "," << f;}
|
||||
EVENT1(eventB, PIString, str);
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
struct msgHeader {
|
||||
msgHeader(ushort msg_id_ = 0) {
|
||||
sign[0] = 'B';
|
||||
sign[1] = 'R';
|
||||
sign[2] = 'K';
|
||||
sign[3] = 'D';
|
||||
msg_id = msg_id_;
|
||||
sys_id = 2;
|
||||
subsys_id = 1;
|
||||
fragment = security = cnt = 0;
|
||||
size = sizeof(msgHeader);
|
||||
}
|
||||
char sign[4];
|
||||
ushort size;
|
||||
ushort cnt;
|
||||
uchar fragment;
|
||||
ushort msg_id;
|
||||
uchar sys_id;
|
||||
uchar subsys_id;
|
||||
uchar security;
|
||||
bool verify_sign() {return (sign[0] == 'B' && sign[1] == 'R' && sign[2] == 'K' && sign[3] == 'D');}
|
||||
};
|
||||
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct msg2105base {
|
||||
msgHeader header;
|
||||
union {
|
||||
ullong msgTime;
|
||||
struct {
|
||||
uint time_ns;
|
||||
uint time_s;
|
||||
};
|
||||
};
|
||||
uint id;
|
||||
ushort type1kod;
|
||||
float type1ver;
|
||||
ushort type2kod;
|
||||
float type2ver;
|
||||
ushort type3kod;
|
||||
float type3ver;
|
||||
double azimut;
|
||||
ullong imp_recv_time;
|
||||
double geo_width_PEC;
|
||||
double geo_length_PEC;
|
||||
double geo_width_CP;
|
||||
double geo_length_CP;
|
||||
float height;
|
||||
float kurs_CP;
|
||||
float kren_CP;
|
||||
float tangaj_CP;
|
||||
};
|
||||
|
||||
|
||||
struct InpuData {
|
||||
uint first_number; // Номер первого отсчета
|
||||
struct {
|
||||
uchar packet_number: 7; // Идентификационный код пакета
|
||||
uchar packet_last : 1; // Признак последнего пакета
|
||||
};
|
||||
struct {
|
||||
uchar antenna_number : 3; // Номер антенного канала
|
||||
uchar frequency_number: 3; // Номер частотного канала
|
||||
uchar data_type : 2; // Вид передаваемой информации: 0 – оцифровка, 1 – измеренные параметры
|
||||
};
|
||||
uchar data[122]; // Данные
|
||||
};
|
||||
|
||||
struct msgHeader {
|
||||
char sign[4];
|
||||
unsigned short size;
|
||||
unsigned short cnt;
|
||||
unsigned char fragment;
|
||||
unsigned short msg_id;
|
||||
unsigned char sys_id;
|
||||
unsigned char subsys_id;
|
||||
unsigned char security;
|
||||
bool verify_sign()
|
||||
{
|
||||
bool ok;
|
||||
ok = sign[0] == 'B';
|
||||
ok = ok && sign[1] == 'R';
|
||||
ok = ok && sign[2] == 'K';
|
||||
ok = ok && sign[3] == 'D';
|
||||
return ok;
|
||||
}
|
||||
};
|
||||
struct FilterCommand {
|
||||
FilterCommand() {
|
||||
memset(this, 0, sizeof(FilterCommand));
|
||||
header.sign[0] = 'B'; header.sign[1] = 'R'; header.sign[2] = 'K'; header.sign[3] = 'D';
|
||||
header.size = sizeof(FilterCommand);
|
||||
header.cnt = header.fragment = header.security = 0;
|
||||
header.msg_id = 2102;
|
||||
header.sys_id = header.subsys_id = 2;
|
||||
command = 3;
|
||||
}
|
||||
|
||||
InpuData() {header.msg_id = 2113;}
|
||||
msgHeader header;
|
||||
uchar command; // 3
|
||||
uint first_number;
|
||||
struct {
|
||||
uchar fi_number: 7; // номер частотного участка, 1 - 36
|
||||
uchar bort : 1; // борт: 0 - левый, 1 - правый
|
||||
uchar packet_number: 7;
|
||||
uchar packet_last : 1;
|
||||
};
|
||||
ushort freq_start; // начальная частота, 0 - 6100
|
||||
ushort freq_end; // конечная частота, 0 - 6100
|
||||
uchar filter_bandwith; // полоса проспускания фильтра
|
||||
ushort filter_time_step; // время шага фильтра по времени
|
||||
uchar reserve;
|
||||
ushort record_time_start; // начальное время записи, 1 - 60 мс
|
||||
ushort record_time_end; // конечное время записи, 1 - 60 мс
|
||||
uchar record_channels; // номера каналов для записи, 0 - 5 биты
|
||||
uchar play_channels; // номера каналов для воспроизведения, 0 - 5 биты
|
||||
uchar signal_type; // тип сигнала для выдачи, 0 - С3, 1 - С4, 2 - С4Ф, 3 - выборка
|
||||
ushort play_time_start; // начальное время воспроизведения, 1 - 60 мс
|
||||
ushort play_time_end; // конечное время воспроизведения, 1 - 60 мс
|
||||
uchar checksum;
|
||||
struct {
|
||||
uchar antenna_number : 3;
|
||||
uchar frequency_number: 3;
|
||||
uchar data_type : 2;
|
||||
};
|
||||
uchar data[122];
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
void te(void*, int);
|
||||
PITimer tm_(te);
|
||||
PISerial ser("/dev/ttyS0");
|
||||
bool pins[9];
|
||||
void te(void*, int) {
|
||||
for (int i = 1; i <= 9; ++i)
|
||||
pins[i - 1] = ser.isPin(i);
|
||||
}
|
||||
void ke(char key, void*) {
|
||||
int p = key - '0';
|
||||
if (key >= '1' && key <= '9')
|
||||
ser.setPin(p, !pins[p - 1]);
|
||||
}
|
||||
int main(int argc, char * argv[]) {
|
||||
PIKbdListener kbd;
|
||||
kbd.enableExitCapture();
|
||||
PICLI cli(argc, argv);
|
||||
cli.addArgument("sender");
|
||||
PIEthernet eth;
|
||||
int i = 0;
|
||||
bool _ok;
|
||||
if (cli.hasArgument("sender")) {
|
||||
while (!PIKbdListener::exiting) {
|
||||
_ok = eth.send("234.0.2.1", 10101, &i, sizeof(i));
|
||||
cout << "send " << i << " - " << (_ok ? "ok" : "fail") << endl;
|
||||
i++;
|
||||
msleep(100);
|
||||
}
|
||||
} else {
|
||||
eth.setParameter(PIEthernet::Broadcast);
|
||||
eth.open(":10101");
|
||||
eth.joinMulticastGroup("234.0.2.1");
|
||||
while (!PIKbdListener::exiting) {
|
||||
eth.read(&i, sizeof(i));
|
||||
cout << "receive " << i << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
/*tm_.start(10.);
|
||||
PIConsole con(false, ke);
|
||||
con.enableExitCapture();
|
||||
//ser.setParameter(PISerial::HardwareFlowControl);
|
||||
ser.open();
|
||||
con.addVariable("1 (CAR)", pins);
|
||||
con.addVariable("2 (SR) ", pins + 1);
|
||||
con.addVariable("3 (ST) ", pins + 2);
|
||||
con.addVariable("4 (DTR)", pins + 3);
|
||||
con.addVariable("5 (GND)", pins + 4);
|
||||
con.addVariable("6 (DSR)", pins + 5);
|
||||
con.addVariable("7 (RTS)", pins + 6);
|
||||
con.addVariable("8 (CTS)", pins + 7);
|
||||
con.addVariable("9 (RNG)", pins + 8);
|
||||
con.start();
|
||||
con.waitForFinish();
|
||||
return 0;*/
|
||||
|
||||
/*FilterCommand fc;
|
||||
PIEthernet eth(PIEthernet::TCP_SingleTCP);
|
||||
eth.open("127.0.0.1:10201");
|
||||
while (1) {
|
||||
eth.read(&fc, sizeof(fc));
|
||||
cout << int(fc.fi_number) << ", " << int(fc.bort) << ", " << int(fc.freq_start) << ", " << int(fc.freq_end) << ", " << int(fc.play_channels) << ", " << int(fc.record_channels) << endl;
|
||||
}
|
||||
return 0;*/
|
||||
class RC: public PIObject {
|
||||
PIOBJECT(RC)
|
||||
public:
|
||||
EVENT_HANDLER2(void, re, ullong, id, int, size) {piCout << "written id =" << id << "size =" << size;}
|
||||
};
|
||||
|
||||
/*PIDir dir(argv[1]);
|
||||
FILE*f = popen("cd ../ && pwd", "w");
|
||||
char fd[4096];
|
||||
int ret = fread(fd, 4096, 1, f);
|
||||
pclose(f);
|
||||
cout << PIString(fd, ret);
|
||||
f = popen("cd ../ && pwd", "w");
|
||||
ret = fread(fd, 4096, 1, f);
|
||||
pclose(f);
|
||||
cout << PIString(fd, ret);
|
||||
return 0;
|
||||
cout << dir.path() << endl;
|
||||
dir.up();
|
||||
cout << dir.path() << endl;
|
||||
PIVector<PIDir::DirEntry> ent = dir.entries();
|
||||
piForeachC (PIDir::DirEntry & i, ent)
|
||||
cout << i.mode << " " << PIString::readableSize(i.size).expandRightTo(10, ' ') << " " << i.name << endl;*/
|
||||
|
||||
/*PISystemTime st_time, inc_time;
|
||||
PITimer tm_;
|
||||
tm_.reset();
|
||||
inc_time = PISystemTime::fromMilliseconds(50);
|
||||
st_time = currentSystemTime() + inc_time;
|
||||
int cnt = 100;
|
||||
while (cnt--) {
|
||||
(st_time - currentSystemTime()).sleep();
|
||||
{
|
||||
msleep(49);
|
||||
cout << "tick " << cnt << endl;
|
||||
}
|
||||
st_time += inc_time;
|
||||
}
|
||||
cout << tm_.elapsed_s() - 0.049 << endl;*/
|
||||
|
||||
/*PISystemTime t0, inc_time;
|
||||
PITimer tm_;
|
||||
tm_.reset();
|
||||
inc_time = PISystemTime::fromMilliseconds(50);
|
||||
inc_time.sleep();
|
||||
int cnt = 100;
|
||||
while (cnt--) {
|
||||
t0 = currentSystemTime();
|
||||
{
|
||||
msleep(49);
|
||||
cout << "tick " << cnt << endl;
|
||||
}
|
||||
(inc_time - (currentSystemTime() - t0)).sleep();
|
||||
}
|
||||
cout << tm_.elapsed_s() - 0.049 << endl;*/
|
||||
|
||||
//return 0;
|
||||
|
||||
/*InpuData data;
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
InpuData data;
|
||||
PIEthernet eth(PIEthernet::UDP);
|
||||
data.first_number = 0;
|
||||
data.packet_number = 0;
|
||||
data.packet_last = 0;
|
||||
data.antenna_number = data.frequency_number = data.data_type = 0;
|
||||
PIBitArray ba_;
|
||||
for (int i = 0; i < 97; ++i) {
|
||||
for (int b = 0; b < 10; ++b)
|
||||
ba_.push_back(((i >> b) & 1) == 1);
|
||||
float t = 0.f;
|
||||
int pc = 20, cnt = 0;
|
||||
for (int p = 0; p < pc; ++p) {
|
||||
data.packet_number = p;
|
||||
data.packet_last = (p == pc - 1 ? 1 : 0);
|
||||
data.first_number = p * 97;
|
||||
PIBitArray ba_;
|
||||
int ci = 0;
|
||||
for (int i = 0; i < 97; ++i) {
|
||||
t += M_PI/4;
|
||||
ci = sin(t) * 511 + 511;
|
||||
if (cnt % 500 > 80) ci = 512;
|
||||
cnt++;
|
||||
for (int b = 0; b < 10; ++b)
|
||||
ba_.push_back(((ci >> b) & 1) == 1);
|
||||
}
|
||||
//cout << ba_.byteSize() << ", " << ba_.bitSize() << endl;
|
||||
memcpy(data.data, ba_.data(), 122);
|
||||
eth.send("127.0.0.1:5000", &data, sizeof(data));
|
||||
}
|
||||
//cout << ba_.byteSize() << ", " << ba_.bitSize() << endl;
|
||||
memcpy(data.data, ba_.data(), 122);
|
||||
cout << ba_ << endl;
|
||||
/*cout << ba_ << endl;
|
||||
for (int i = 0; i < 122; ++i)
|
||||
cout << int(data.data[i]) << ", ";
|
||||
cout << endl;
|
||||
eth.send("127.0.0.1:5000", &data, sizeof(data));
|
||||
cout << endl;*/
|
||||
return 0;
|
||||
|
||||
/*PIKbdListener kbd;
|
||||
kbd.enableExitCapture();
|
||||
PIEthernet * _eth;
|
||||
PIByteArray ba_(16);
|
||||
if (PIString(argv[argc - 1]) == "s") {
|
||||
_eth = new PIEthernet(PIEthernet::TCP_Server);
|
||||
piCout << "listen" << _eth->listen("127.0.0.1:1111");
|
||||
int cc = 0;
|
||||
while (!PIKbdListener::exiting) {
|
||||
if (cc != _eth->clientsCount()) {
|
||||
piCout << "new client";
|
||||
_eth->clients().back()->startThreadedRead();
|
||||
cc++;
|
||||
}
|
||||
msleep(1);
|
||||
}
|
||||
} else {
|
||||
_eth = new PIEthernet(PIEthernet::TCP_Client);
|
||||
piCout << "connection" << _eth->connect("127.0.0.1:1111");
|
||||
_eth->send(PIString("0123456789101112").data(), 16);
|
||||
while (!PIKbdListener::exiting) {
|
||||
msleep(1);
|
||||
}
|
||||
}
|
||||
delete _eth;
|
||||
return 0;
|
||||
|
||||
msg2105base msg;
|
||||
msg.header.size = sizeof(msg);
|
||||
msg.header.msg_id = 2105;
|
||||
msg.msgTime = 1;
|
||||
//PIKbdListener kbd;
|
||||
//kbd.enableExitCapture();
|
||||
RC rc_;
|
||||
PIEthernet eth;
|
||||
CONNECT2(void, ullong, int, ð, threadedWriteEvent, &rc_, re);
|
||||
eth.setParameter(PIEthernet::Broadcast);
|
||||
eth.open("127.0.0.1:10211");
|
||||
eth.setSendAddress("234.0.2.1:10211");
|
||||
eth.joinMulticastGroup("234.0.2.1");
|
||||
eth.startThreadedWrite();
|
||||
for (int i = 0; i < 100000; ++i) {
|
||||
PISystemTime tm = currentSystemTime();
|
||||
msg.type1kod = (i + 1) % 34;
|
||||
msg.id = piRoundd(double(i) / 5. + 50);
|
||||
if (i % 15 >= 10) msg.id += 10000;
|
||||
msg.time_ns = tm.seconds;
|
||||
msg.time_s = tm.nanoseconds;
|
||||
msg.azimut = sin(float(i) / 20.) * 90;
|
||||
msg.geo_width_PEC = sin(float(i) / 50.) * 90;
|
||||
msg.geo_length_PEC = cos(float(i) / 50.) * 90;
|
||||
piCout << "push to queue with id =" << eth.writeThreaded(&msg, sizeof(msg));
|
||||
piMSleep(50);
|
||||
if (PIKbdListener::exiting) break;
|
||||
}
|
||||
return 0;*/
|
||||
|
||||
/*QApplication app(argc, argv);
|
||||
PIFFT fft;
|
||||
PIVector<complexd> in;
|
||||
in.resize(50*1000);
|
||||
for (int i=400; i<404; i++) in[i] = complexd(1,0);
|
||||
PITimer timer_;
|
||||
fft.prepareFFT(in.size());
|
||||
timer_.reset();
|
||||
PIVector<complexd> * out = fft.calcFFT(in);
|
||||
cout << timer_.elapsed_m() << endl;
|
||||
Graphic * g = new Graphic();
|
||||
QVector<QPointF> res;
|
||||
fft.getAmplitude();
|
||||
for (int i=0; i<out->size(); i++) res.append(QPointF(i,abs(out->at(i))));
|
||||
g->setGraphicData(res);
|
||||
g->addGraphic("arg", Qt::darkBlue);
|
||||
res.clear();
|
||||
for (int i=0; i< out->size(); i++) res.append(QPointF(i,arg(out->at(i))));
|
||||
g->setGraphicData(res, 1);
|
||||
g->show();
|
||||
|
||||
return app.exec();*/
|
||||
|
||||
PISignals::setSlot(signalFunc);
|
||||
//PISignals::grabSignals(PISignals::Interrupt);
|
||||
bool r_string = true, r_thread = true, r_mutex = true, r_timer = true, r_file = true, r_eval = true, r_event = true;
|
||||
bool succ = true;
|
||||
cout << "== PIP test program ==" << endl;
|
||||
|
||||
Reference in New Issue
Block a user