1 | #include "SamClass.h"
|
---|
2 |
|
---|
3 | /****** SAMGAR V2 EXAMPLES******/
|
---|
4 |
|
---|
5 | /*
|
---|
6 | a couple of class's showing how to use SAMGAR, they dont have to be done in header files at all, but it can be better
|
---|
7 | its easer to have a option of compiling them together or seperately, both of which have there advantages in differnt situatoins
|
---|
8 |
|
---|
9 |
|
---|
10 | if you want to send imgs there is a class in yarp exactly for that, you can just send a IPL image.
|
---|
11 | */
|
---|
12 |
|
---|
13 |
|
---|
14 | // This class just sends out some data //
|
---|
15 |
|
---|
16 | class ExampleOneWrite: public SamClass
|
---|
17 | {
|
---|
18 | private:
|
---|
19 | int myint;
|
---|
20 | BufferedPort<Bottle> myfirst,myfirst2; // create buffered ports for bottles like this
|
---|
21 | Network yarp; // make sure the network is ready
|
---|
22 |
|
---|
23 | public:
|
---|
24 | void SamInit(void)
|
---|
25 | {
|
---|
26 | myint=0;
|
---|
27 | RecognisePort("W1"); // name the port to be shown in the gui
|
---|
28 | myfirst.open("/Writer_W1"); // open the port with MODULENAME_PORTNAME
|
---|
29 | myfirst.setReporter(myPortStatus); // set reporter, this is important
|
---|
30 | RecognisePort("W2"); // name the port to be shown in the gui
|
---|
31 | myfirst2.open("/Writer_W2"); // open the port
|
---|
32 | myfirst2.setReporter(myPortStatus); // set reporter
|
---|
33 |
|
---|
34 |
|
---|
35 | StartModule("/Writer"); // we weve addid all ports and are happy then send the module name, this must be done last!!
|
---|
36 | puts("started writer");
|
---|
37 | }
|
---|
38 | void SamIter(void)
|
---|
39 | {
|
---|
40 | Bottle& B = myfirst.prepare(); // prepare the bottle/port
|
---|
41 | B.clear();
|
---|
42 | B.addInt(myint++);
|
---|
43 | myfirst.write(); // add stuff then send
|
---|
44 | Bottle& C = myfirst2.prepare();
|
---|
45 | C.clear();
|
---|
46 | C.addInt(myint+5);
|
---|
47 | myfirst2.write();
|
---|
48 | puts("running writer");
|
---|
49 | }
|
---|
50 | };
|
---|
51 |
|
---|
52 |
|
---|
53 | class ExampleTwoRead: public SamClass
|
---|
54 | {
|
---|
55 | private:
|
---|
56 | int myint;
|
---|
57 | BufferedPort<Bottle> myfirst;
|
---|
58 | Network yarp;
|
---|
59 |
|
---|
60 | public:
|
---|
61 | void SamInit(void)
|
---|
62 | {
|
---|
63 | myint=0;
|
---|
64 | RecognisePort("R1");
|
---|
65 | myfirst.open("/Reader_R1");
|
---|
66 | myfirst.setReporter(myPortStatus);
|
---|
67 | StartModule("/Reader");
|
---|
68 | puts("started reader");
|
---|
69 | }
|
---|
70 | void SamIter(void)
|
---|
71 | {
|
---|
72 | puts("running reader");
|
---|
73 | Bottle *input = myfirst.read(false); // get in the input from the port, if you want it to wait use true, else use false
|
---|
74 | if(input!=NULL) // check theres data
|
---|
75 | {
|
---|
76 | puts("got a msg");
|
---|
77 | puts(input->toString());
|
---|
78 | }
|
---|
79 | else{puts("didn't get a msg");}
|
---|
80 | }
|
---|
81 | };
|
---|
82 |
|
---|
83 | // A very simple data class
|
---|
84 | class DataForm
|
---|
85 | {
|
---|
86 | public:
|
---|
87 | int x;
|
---|
88 | int y;
|
---|
89 | int p;
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | class ExampleThreeSendClass: public SamClass
|
---|
94 | {
|
---|
95 | private:
|
---|
96 | int myint;
|
---|
97 | BufferedPort<BinPortable<DataForm>> myfirst; // use BinPortable to make a class ready to be used on the network
|
---|
98 | Network yarp;
|
---|
99 | public:
|
---|
100 | void SamInit(void)
|
---|
101 | {
|
---|
102 | myint=0;
|
---|
103 | RecognisePort("Out");
|
---|
104 | myfirst.open("/SClass_Out");
|
---|
105 | myfirst.setReporter(myPortStatus);
|
---|
106 | StartModule("/SClass");
|
---|
107 | }
|
---|
108 | void SamIter(void)
|
---|
109 | {
|
---|
110 | myint++;
|
---|
111 | BinPortable<DataForm> &MyData = myfirst.prepare(); // prepare data/port
|
---|
112 | MyData.content().x=myint;
|
---|
113 | MyData.content().y=myint+5;
|
---|
114 | MyData.content().p=myint+10;
|
---|
115 | myfirst.write(); // add stuff and write
|
---|
116 | }
|
---|
117 | };
|
---|
118 |
|
---|
119 |
|
---|
120 | // a Interupt port, when data hits this port it'll do whatever is onread, be carefull, fast firing interupts can cause big problems as in normal code
|
---|
121 |
|
---|
122 | class DataPort : public BufferedPort<BinPortable<DataForm>>
|
---|
123 | {
|
---|
124 | virtual void onRead(BinPortable<DataForm>& b)
|
---|
125 | {
|
---|
126 | printf("X %i Y %i P %i \n",b.content().x,b.content().y,b.content().p);
|
---|
127 | }
|
---|
128 | };
|
---|
129 | class ExampleFourReciveClassInterupt: public SamClass
|
---|
130 | {
|
---|
131 | private:
|
---|
132 | int myint;
|
---|
133 | DataPort myfirst;
|
---|
134 | Network yarp;
|
---|
135 | public:
|
---|
136 | void SamInit(void)
|
---|
137 | {
|
---|
138 | myint=0;
|
---|
139 | RecognisePort("In");
|
---|
140 | myfirst.open("/RClass_In");
|
---|
141 | myfirst.setReporter(myPortStatus);
|
---|
142 | myfirst.useCallback(); // this tells it to use the onread method
|
---|
143 | StartModule("/RClass");
|
---|
144 | }
|
---|
145 |
|
---|
146 | }; |
---|