example for the ssWi library

Dependencies:   ssWi

Committer:
mariob
Date:
Thu Sep 06 12:18:30 2012 +0000
Revision:
0:8145d0de8bdc
Child:
1:0c9911bd5715
usage example of the ssWi library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 0:8145d0de8bdc 1 #include "mbed.h"
mariob 0:8145d0de8bdc 2
mariob 0:8145d0de8bdc 3 #include "xbee.hpp"
mariob 0:8145d0de8bdc 4 #include "ssWi.hpp"
mariob 0:8145d0de8bdc 5 #include "ssWiSocket.hpp"
mariob 0:8145d0de8bdc 6
mariob 0:8145d0de8bdc 7 #define READING_PORTID 10
mariob 0:8145d0de8bdc 8 #define WRITING_PORTID 10
mariob 0:8145d0de8bdc 9
mariob 0:8145d0de8bdc 10 #define WRITING_PERIOD 1.0
mariob 0:8145d0de8bdc 11 #define READING_PERIOD 0.9
mariob 0:8145d0de8bdc 12
mariob 0:8145d0de8bdc 13 ssWiSocket* readingSocket;
mariob 0:8145d0de8bdc 14 ssWiSocket* writingSocket;
mariob 0:8145d0de8bdc 15
mariob 0:8145d0de8bdc 16
mariob 0:8145d0de8bdc 17 void checkSocket(ssWiSocket* sok, char* name);
mariob 0:8145d0de8bdc 18
mariob 0:8145d0de8bdc 19 void readingFunction(const void* arg);
mariob 0:8145d0de8bdc 20
mariob 0:8145d0de8bdc 21 void writingFunction(const void* arg);
mariob 0:8145d0de8bdc 22
mariob 0:8145d0de8bdc 23 int main()
mariob 0:8145d0de8bdc 24 {
mariob 0:8145d0de8bdc 25
mariob 0:8145d0de8bdc 26 printf("\n\r************* CONFIG *************\n\r");
mariob 0:8145d0de8bdc 27
mariob 0:8145d0de8bdc 28 //radio module
mariob 0:8145d0de8bdc 29 XBeeModule xbee(p9, p10, 102, 14);
mariob 0:8145d0de8bdc 30 XBeeAddress addr = xbee.getLocalAddress();
mariob 0:8145d0de8bdc 31 printf("XBEE: src addr: %s,%s\n\r", addr.getHighAddr().c_str(), addr.getLowAddr().c_str());
mariob 0:8145d0de8bdc 32 xbee.setDstAddress(XBeeBroadcastAddress());
mariob 0:8145d0de8bdc 33 XBeeAddress addr2 = xbee.getDstAddress();
mariob 0:8145d0de8bdc 34 printf("XBEE: dts addr: %s,%s\n\r", addr2.getHighAddr().c_str(), addr2.getLowAddr().c_str());
mariob 0:8145d0de8bdc 35 printf("XBEE: channel: %d\n\r", xbee.getChannel());
mariob 0:8145d0de8bdc 36 printf("XBEE: pan id: %d\n\r", xbee.getPanID());
mariob 0:8145d0de8bdc 37
mariob 0:8145d0de8bdc 38 //wireless protocol
mariob 0:8145d0de8bdc 39 ssWi channel(&xbee, 10, 20);
mariob 0:8145d0de8bdc 40 readingSocket = channel.createSocket(READING_PORTID);
mariob 0:8145d0de8bdc 41 checkSocket(readingSocket, "reading");
mariob 0:8145d0de8bdc 42 writingSocket = channel.createSocket(WRITING_PORTID);
mariob 0:8145d0de8bdc 43 checkSocket(writingSocket, "writing");
mariob 0:8145d0de8bdc 44
mariob 0:8145d0de8bdc 45 //thread
mariob 0:8145d0de8bdc 46 Thread readingThread(readingFunction);
mariob 0:8145d0de8bdc 47 Thread writingThread(writingFunction);
mariob 0:8145d0de8bdc 48
mariob 0:8145d0de8bdc 49 printf("\n\r************* START *************\n\r");
mariob 0:8145d0de8bdc 50
mariob 0:8145d0de8bdc 51 while(1);
mariob 0:8145d0de8bdc 52 }
mariob 0:8145d0de8bdc 53
mariob 0:8145d0de8bdc 54
mariob 0:8145d0de8bdc 55 void checkSocket(ssWiSocket* socket, char* name)
mariob 0:8145d0de8bdc 56 {
mariob 0:8145d0de8bdc 57 if (socket==NULL)
mariob 0:8145d0de8bdc 58 printf("CHANNEL: error on %s socket\n\r", name);
mariob 0:8145d0de8bdc 59 else
mariob 0:8145d0de8bdc 60 printf("CHANNEL: %s socket ok\n\r", name);
mariob 0:8145d0de8bdc 61 }
mariob 0:8145d0de8bdc 62
mariob 0:8145d0de8bdc 63 void readingFunction(const void* arg)
mariob 0:8145d0de8bdc 64 {
mariob 0:8145d0de8bdc 65 while(1) {
mariob 0:8145d0de8bdc 66 printf("Read value: %d\n\r", readingSocket->read());
mariob 0:8145d0de8bdc 67 wait(READING_PERIOD);
mariob 0:8145d0de8bdc 68 }
mariob 0:8145d0de8bdc 69 }
mariob 0:8145d0de8bdc 70
mariob 0:8145d0de8bdc 71 void writingFunction(const void* arg)
mariob 0:8145d0de8bdc 72 {
mariob 0:8145d0de8bdc 73 static int value = 0;
mariob 0:8145d0de8bdc 74 while(1) {
mariob 0:8145d0de8bdc 75 writingSocket->write(value++);
mariob 0:8145d0de8bdc 76 wait(WRITING_PERIOD);
mariob 0:8145d0de8bdc 77 }
mariob 0:8145d0de8bdc 78 }