
example for the ssWi library
Revision 1:0c9911bd5715, committed 2012-09-07
- Comitter:
- mariob
- Date:
- Fri Sep 07 23:43:24 2012 +0000
- Parent:
- 0:8145d0de8bdc
- Child:
- 2:2ce4cca21387
- Commit message:
- to be tested
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 Thu Sep 06 12:18:30 2012 +0000 +++ b/main.cpp Fri Sep 07 23:43:24 2012 +0000 @@ -1,78 +1,87 @@ -#include "mbed.h" - -#include "xbee.hpp" -#include "ssWi.hpp" -#include "ssWiSocket.hpp" - -#define READING_PORTID 10 -#define WRITING_PORTID 10 - -#define WRITING_PERIOD 1.0 -#define READING_PERIOD 0.9 - -ssWiSocket* readingSocket; -ssWiSocket* writingSocket; - - -void checkSocket(ssWiSocket* sok, char* name); - -void readingFunction(const void* arg); - -void writingFunction(const void* arg); - -int main() -{ - - printf("\n\r************* CONFIG *************\n\r"); - - //radio module - XBeeModule xbee(p9, p10, 102, 14); - XBeeAddress addr = xbee.getLocalAddress(); - printf("XBEE: src addr: %s,%s\n\r", addr.getHighAddr().c_str(), addr.getLowAddr().c_str()); - xbee.setDstAddress(XBeeBroadcastAddress()); - XBeeAddress addr2 = xbee.getDstAddress(); - printf("XBEE: dts addr: %s,%s\n\r", addr2.getHighAddr().c_str(), addr2.getLowAddr().c_str()); - printf("XBEE: channel: %d\n\r", xbee.getChannel()); - printf("XBEE: pan id: %d\n\r", xbee.getPanID()); - - //wireless protocol - ssWi channel(&xbee, 10, 20); - readingSocket = channel.createSocket(READING_PORTID); - checkSocket(readingSocket, "reading"); - writingSocket = channel.createSocket(WRITING_PORTID); - checkSocket(writingSocket, "writing"); - - //thread - Thread readingThread(readingFunction); - Thread writingThread(writingFunction); - - printf("\n\r************* START *************\n\r"); - - while(1); -} - - -void checkSocket(ssWiSocket* socket, char* name) -{ - if (socket==NULL) - printf("CHANNEL: error on %s socket\n\r", name); - else - printf("CHANNEL: %s socket ok\n\r", name); -} - -void readingFunction(const void* arg) -{ - while(1) { - printf("Read value: %d\n\r", readingSocket->read()); - wait(READING_PERIOD); - } -} - -void writingFunction(const void* arg) -{ - static int value = 0; - while(1) { - writingSocket->write(value++); - wait(WRITING_PERIOD); - } -} +#include "mbed.h" +#include "rtos.h" + +#include "xbee.hpp" +#include "ssWiSocket.hpp" + + +struct Task { + PortID _id; + float _period; + ssWiSocket* _s; + int _val; + + Task (PortID id, float period, int value) { + _id = id; + _period = period; + _s = ssWiSocket::createSocket(id); + _val = value; + } +}; + + +void readingFunction(const void* arg); + +void writingFunction(const void* arg); + + +int main() +{ + + printf("\n\r************* CONFIG *************\n\r"); + + //radio module + XBeeModule xbee(p9, p10, 102, 14); + XBeeAddress addr = xbee.getLocalAddress(); + printf("XBEE: src addr: %s,%s\n\r", addr.getHighAddr().c_str(), addr.getLowAddr().c_str()); + xbee.setDstAddress(XBeeBroadcastAddress()); + XBeeAddress addr2 = xbee.getDstAddress(); + printf("XBEE: dts addr: %s,%s\n\r", addr2.getHighAddr().c_str(), addr2.getLowAddr().c_str()); + printf("XBEE: channel: %d\n\r", xbee.getChannel()); + 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); + + //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); + + printf("\n\r************* START *************\n\r"); + + while(1); +} + +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); + } +} + +void writingFunction(const void* arg) +{ + Task* task = (Task*)arg; + while(1) { + if (task->_s==NULL) + printf("SOCKET: error\n\n"); + else + task->_s->write(task->_val++); + wait(task->_period); + } +}
--- a/ssWi.lib Thu Sep 06 12:18:30 2012 +0000 +++ b/ssWi.lib Fri Sep 07 23:43:24 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mariob/code/ssWi/#1adc077d4906 +http://mbed.org/users/mariob/code/ssWi/#f98855efa37e