
example for the ssWi library
Revision 3:a0d27c04263e, committed 2012-09-09
- Comitter:
- mariob
- Date:
- Sun Sep 09 20:03:53 2012 +0000
- Parent:
- 2:2ce4cca21387
- Child:
- 4:989ea19b8fd2
- Commit message:
- ok; ; MB
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
ssWi.lib | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Sep 07 23:46:10 2012 +0000 +++ b/main.cpp Sun Sep 09 20:03:53 2012 +0000 @@ -4,21 +4,28 @@ #include "xbee.hpp" #include "ssWiSocket.hpp" +#define WRITING_PERIOD_MS 1000 +#define READING_PERIOD_MS 1000 struct Task { PortID _id; - float _period; + int _ratio; ssWiSocket* _s; int _val; - Task (PortID id, float period, int value) { + Task (PortID id, int ratio, int value) { _id = id; - _period = period; + _ratio = ratio; _s = ssWiSocket::createSocket(id); _val = value; } }; +Task *t1; +Task *t2; +Task *t3; +Task *t4; + void readingFunction(const void* arg); @@ -41,47 +48,46 @@ printf("XBEE: pan id: %d\n\r", xbee.getPanID()); //wireless network - xbee.init(10, 20); - Task t1(10, 1.0, 20); - Task t2(15, 1.2, 30); - Task t3(100, 0.9, 25); - Task t4(120, 1.1, 65); + xbee.init(1.0, 1.0); + + + t1 = new Task(10, 3, 20); + t2 = new Task(15, 5, 30); + t3 = new Task(100, 7, 25); + t4 = new Task(120, 4, 65); //thread - Thread readingThread1(readingFunction, (void*)&t1); - Thread readingThread2(readingFunction, (void*)&t2); - Thread readingThread3(readingFunction, (void*)&t3); - Thread readingThread4(readingFunction, (void*)&t4); - Thread writingThread1(writingFunction, (void*)&t1); - Thread writingThread2(writingFunction, (void*)&t2); - Thread writingThread3(writingFunction, (void*)&t3); - Thread writingThread4(writingFunction, (void*)&t4); + Thread readingThread(readingFunction); + Thread writingThread(writingFunction); printf("\n\r************* START *************\n\r"); - while(1); + Thread::wait(osWaitForever); } void readingFunction(const void* arg) { - Task* task = (Task*)arg; while(1) { - if (task->_s==NULL) - printf("SOCKET: error\n\n"); - else - printf("Read value: %d\n\r", task->_s->read()); - wait(task->_period); + printf("Read value: (%d, %d), (%d, %d), (%d, %d), (%d, %d)\n\r", + t1->_id, t1->_s->read(), t2->_id, t2->_s->read(), + t3->_id, t3->_s->read(), t4->_id, t4->_s->read()); + Thread::wait(READING_PERIOD_MS); } } void writingFunction(const void* arg) { - Task* task = (Task*)arg; + int x = 1; while(1) { - if (task->_s==NULL) - printf("SOCKET: error\n\n"); - else - task->_s->write(task->_val++); - wait(task->_period); + if ((x%(t1->_ratio))==0) + t1->_s->write(t1->_val++); + if ((x%(t2->_ratio))==0) + t2->_s->write(t2->_val++); + if ((x%(t3->_ratio))==0) + t3->_s->write(t3->_val++); + if ((x%(t4->_ratio))==0) + t4->_s->write(t4->_val++); + x++; + Thread::wait(WRITING_PERIOD_MS); } }
--- a/ssWi.lib Fri Sep 07 23:46:10 2012 +0000 +++ b/ssWi.lib Sun Sep 09 20:03:53 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mariob/code/ssWi/#ca87b7287af9 +http://mbed.org/users/mariob/code/ssWi/#354a0e3087c1