example for the ssWi library

Dependencies:   ssWi

Committer:
mariob
Date:
Wed Mar 06 23:14:11 2013 +0000
Revision:
6:0d434f4c83bd
Parent:
5:502371e99bdb
Child:
7:f3ba13c6d56e
MB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mariob 1:0c9911bd5715 1 #include "mbed.h"
mariob 1:0c9911bd5715 2 #include "rtos.h"
mariob 1:0c9911bd5715 3
mariob 1:0c9911bd5715 4 #include "xbee.hpp"
mariob 1:0c9911bd5715 5 #include "ssWiSocket.hpp"
mariob 1:0c9911bd5715 6
mariob 4:989ea19b8fd2 7 #define READ_FUNCTION_MS 1000
mariob 4:989ea19b8fd2 8 #define WRITE_FUNCTION_MS 1000
mariob 4:989ea19b8fd2 9
mariob 1:0c9911bd5715 10
mariob 6:0d434f4c83bd 11
mariob 1:0c9911bd5715 12 struct Task {
mariob 1:0c9911bd5715 13 PortID _id;
mariob 3:a0d27c04263e 14 int _ratio;
mariob 1:0c9911bd5715 15 ssWiSocket* _s;
mariob 1:0c9911bd5715 16 int _val;
mariob 1:0c9911bd5715 17
mariob 3:a0d27c04263e 18 Task (PortID id, int ratio, int value) {
mariob 1:0c9911bd5715 19 _id = id;
mariob 3:a0d27c04263e 20 _ratio = ratio;
mariob 1:0c9911bd5715 21 _s = ssWiSocket::createSocket(id);
mariob 1:0c9911bd5715 22 _val = value;
mariob 1:0c9911bd5715 23 }
mariob 1:0c9911bd5715 24 };
mariob 1:0c9911bd5715 25
mariob 4:989ea19b8fd2 26 #define N_TASKS 4
mariob 4:989ea19b8fd2 27 Task *task_array[N_TASKS];
mariob 3:a0d27c04263e 28
mariob 1:0c9911bd5715 29
mariob 1:0c9911bd5715 30 void readingFunction(const void* arg);
mariob 1:0c9911bd5715 31
mariob 1:0c9911bd5715 32 void writingFunction(const void* arg);
mariob 1:0c9911bd5715 33
mariob 1:0c9911bd5715 34
mariob 4:989ea19b8fd2 35
mariob 4:989ea19b8fd2 36
mariob 1:0c9911bd5715 37 int main()
mariob 1:0c9911bd5715 38 {
mariob 1:0c9911bd5715 39 printf("\n\r************* CONFIG *************\n\r");
mariob 1:0c9911bd5715 40
mariob 1:0c9911bd5715 41 //radio module
mariob 1:0c9911bd5715 42 XBeeModule xbee(p9, p10, 102, 14);
mariob 1:0c9911bd5715 43 XBeeAddress addr = xbee.getLocalAddress();
mariob 1:0c9911bd5715 44 printf("XBEE: src addr: %s,%s\n\r", addr.getHighAddr().c_str(), addr.getLowAddr().c_str());
mariob 1:0c9911bd5715 45 xbee.setDstAddress(XBeeBroadcastAddress());
mariob 1:0c9911bd5715 46 XBeeAddress addr2 = xbee.getDstAddress();
mariob 1:0c9911bd5715 47 printf("XBEE: dts addr: %s,%s\n\r", addr2.getHighAddr().c_str(), addr2.getLowAddr().c_str());
mariob 5:502371e99bdb 48 printf("XBEE: channel: %d\n\r", xbee.getChannel());
mariob 5:502371e99bdb 49 printf("XBEE: pan id: %d\n\r", xbee.getPanID());
mariob 1:0c9911bd5715 50
mariob 1:0c9911bd5715 51 //wireless network
mariob 6:0d434f4c83bd 52 xbee.init(5, 10);
mariob 4:989ea19b8fd2 53
mariob 4:989ea19b8fd2 54 srand(time(0));
mariob 4:989ea19b8fd2 55
mariob 4:989ea19b8fd2 56 printf("\n\r************* READ *************\n\r");
mariob 4:989ea19b8fd2 57
mariob 4:989ea19b8fd2 58 //tasks
mariob 4:989ea19b8fd2 59 task_array[0] = new Task(10, 3, ((double)rand()/RAND_MAX)*20);
mariob 4:989ea19b8fd2 60 task_array[1] = new Task(15, 5, ((double)rand()/RAND_MAX)*30);
mariob 4:989ea19b8fd2 61 task_array[2] = new Task(100, 7, ((double)rand()/RAND_MAX)*25);
mariob 4:989ea19b8fd2 62 task_array[3] = new Task(120, 4, ((double)rand()/RAND_MAX)*65);
mariob 1:0c9911bd5715 63
mariob 1:0c9911bd5715 64 //thread
mariob 3:a0d27c04263e 65 Thread readingThread(readingFunction);
mariob 3:a0d27c04263e 66 Thread writingThread(writingFunction);
mariob 1:0c9911bd5715 67
mariob 1:0c9911bd5715 68 printf("\n\r************* START *************\n\r");
mariob 1:0c9911bd5715 69
mariob 3:a0d27c04263e 70 Thread::wait(osWaitForever);
mariob 1:0c9911bd5715 71 }
mariob 1:0c9911bd5715 72
mariob 4:989ea19b8fd2 73
mariob 4:989ea19b8fd2 74
mariob 1:0c9911bd5715 75 void readingFunction(const void* arg)
mariob 1:0c9911bd5715 76 {
mariob 4:989ea19b8fd2 77 int vals[N_TASKS];
mariob 4:989ea19b8fd2 78 for (int i=0; i<N_TASKS; i++)
mariob 4:989ea19b8fd2 79 vals[i] = -1;
mariob 1:0c9911bd5715 80 while(1) {
mariob 6:0d434f4c83bd 81 for (int i=0; i<N_TASKS; i++) {
mariob 4:989ea19b8fd2 82 if (vals[i]!=task_array[i]->_s->read()) {
mariob 4:989ea19b8fd2 83 vals[i] = task_array[i]->_s->read();
mariob 6:0d434f4c83bd 84 printf("%ld: Read[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_s->read());
mariob 4:989ea19b8fd2 85 }
mariob 6:0d434f4c83bd 86 }
mariob 4:989ea19b8fd2 87 Thread::wait(READ_FUNCTION_MS);
mariob 1:0c9911bd5715 88 }
mariob 1:0c9911bd5715 89 }
mariob 1:0c9911bd5715 90
mariob 1:0c9911bd5715 91 void writingFunction(const void* arg)
mariob 1:0c9911bd5715 92 {
mariob 3:a0d27c04263e 93 int x = 1;
mariob 1:0c9911bd5715 94 while(1) {
mariob 4:989ea19b8fd2 95 for (int i=0; i<N_TASKS; i++)
mariob 4:989ea19b8fd2 96 if (x%(task_array[i]->_ratio)==0) {
mariob 4:989ea19b8fd2 97 task_array[i]->_s->write(task_array[i]->_val++);
mariob 6:0d434f4c83bd 98 printf("%ld: Write[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_val);
mariob 4:989ea19b8fd2 99 }
mariob 3:a0d27c04263e 100 x++;
mariob 4:989ea19b8fd2 101 Thread::wait(WRITE_FUNCTION_MS);
mariob 1:0c9911bd5715 102 }
mariob 1:0c9911bd5715 103 }