
example for the ssWi library
main.cpp@7:f3ba13c6d56e, 2013-03-07 (annotated)
- Committer:
- mariob
- Date:
- Thu Mar 07 15:44:53 2013 +0000
- Revision:
- 7:f3ba13c6d56e
- Parent:
- 6:0d434f4c83bd
- Child:
- 8:ffe1815b6deb
MB
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:f3ba13c6d56e | 64 | /* |
mariob | 1:0c9911bd5715 | 65 | //thread |
mariob | 3:a0d27c04263e | 66 | Thread readingThread(readingFunction); |
mariob | 3:a0d27c04263e | 67 | Thread writingThread(writingFunction); |
mariob | 7:f3ba13c6d56e | 68 | */ |
mariob | 7:f3ba13c6d56e | 69 | RtosTimer reader (readingFunction, osTimerPeriodic, NULL); |
mariob | 7:f3ba13c6d56e | 70 | RtosTimer writer (writingFunction, osTimerPeriodic, NULL); |
mariob | 7:f3ba13c6d56e | 71 | |
mariob | 7:f3ba13c6d56e | 72 | reader.start(1000); |
mariob | 7:f3ba13c6d56e | 73 | writer.start(1000); |
mariob | 1:0c9911bd5715 | 74 | |
mariob | 1:0c9911bd5715 | 75 | printf("\n\r************* START *************\n\r"); |
mariob | 1:0c9911bd5715 | 76 | |
mariob | 3:a0d27c04263e | 77 | Thread::wait(osWaitForever); |
mariob | 1:0c9911bd5715 | 78 | } |
mariob | 1:0c9911bd5715 | 79 | |
mariob | 4:989ea19b8fd2 | 80 | |
mariob | 4:989ea19b8fd2 | 81 | |
mariob | 1:0c9911bd5715 | 82 | void readingFunction(const void* arg) |
mariob | 1:0c9911bd5715 | 83 | { |
mariob | 7:f3ba13c6d56e | 84 | static int vals[N_TASKS]; |
mariob | 7:f3ba13c6d56e | 85 | // while(1) { |
mariob | 7:f3ba13c6d56e | 86 | for (int i=0; i<N_TASKS; i++) { |
mariob | 7:f3ba13c6d56e | 87 | if (vals[i]!=task_array[i]->_s->read()) { |
mariob | 7:f3ba13c6d56e | 88 | vals[i] = task_array[i]->_s->read(); |
mariob | 7:f3ba13c6d56e | 89 | printf("%ld: Read[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_s->read()); |
mariob | 6:0d434f4c83bd | 90 | } |
mariob | 1:0c9911bd5715 | 91 | } |
mariob | 7:f3ba13c6d56e | 92 | // Thread::wait(READ_FUNCTION_MS); |
mariob | 7:f3ba13c6d56e | 93 | // } |
mariob | 1:0c9911bd5715 | 94 | } |
mariob | 1:0c9911bd5715 | 95 | |
mariob | 1:0c9911bd5715 | 96 | void writingFunction(const void* arg) |
mariob | 1:0c9911bd5715 | 97 | { |
mariob | 7:f3ba13c6d56e | 98 | static int x = 1; |
mariob | 7:f3ba13c6d56e | 99 | // while(1) { |
mariob | 7:f3ba13c6d56e | 100 | for (int i=0; i<N_TASKS; i++) |
mariob | 7:f3ba13c6d56e | 101 | if (x%(task_array[i]->_ratio)==0) { |
mariob | 7:f3ba13c6d56e | 102 | task_array[i]->_s->write(task_array[i]->_val++); |
mariob | 7:f3ba13c6d56e | 103 | printf("%ld: Write[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_val); |
mariob | 7:f3ba13c6d56e | 104 | } |
mariob | 7:f3ba13c6d56e | 105 | x++; |
mariob | 7:f3ba13c6d56e | 106 | // Thread::wait(WRITE_FUNCTION_MS); |
mariob | 7:f3ba13c6d56e | 107 | // } |
mariob | 1:0c9911bd5715 | 108 | } |