example for the ssWi library

Dependencies:   ssWi

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Thu Oct 11 06:49:40 2012 +0000
Parent:
3:a0d27c04263e
Child:
5:502371e99bdb
Commit message:
it seems to work

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	Sun Sep 09 20:03:53 2012 +0000
+++ b/main.cpp	Thu Oct 11 06:49:40 2012 +0000
@@ -4,8 +4,9 @@
 #include "xbee.hpp"
 #include "ssWiSocket.hpp"
 
-#define WRITING_PERIOD_MS 1000
-#define READING_PERIOD_MS 1000
+#define READ_FUNCTION_MS 1000
+#define WRITE_FUNCTION_MS 1000
+
 
 struct Task {
     PortID _id;
@@ -21,10 +22,8 @@
     }
 };
 
-Task *t1;
-Task *t2;
-Task *t3;
-Task *t4;
+#define N_TASKS 4
+Task *task_array[N_TASKS];
 
 
 void readingFunction(const void* arg);
@@ -32,9 +31,10 @@
 void writingFunction(const void* arg);
 
 
+
+
 int main()
 {
-
     printf("\n\r************* CONFIG *************\n\r");
 
     //radio module
@@ -44,17 +44,24 @@
     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());
+//    printf("XBEE: channel: %d\n\r", xbee.getChannel());
+//    printf("XBEE: pan id: %d\n\r", xbee.getPanID());
 
     //wireless network
-    xbee.init(1.0, 1.0);
+    xbee.init(1, 2);
+
+    srand(time(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);
+    printf("\n\r************* READ *************\n\r");
+
+
+    //tasks
+    task_array[0] = new Task(10, 3, ((double)rand()/RAND_MAX)*20);
+    task_array[1] = new Task(15, 5, ((double)rand()/RAND_MAX)*30);
+    task_array[2] = new Task(100, 7, ((double)rand()/RAND_MAX)*25);
+    task_array[3] = new Task(120, 4, ((double)rand()/RAND_MAX)*65);
 
     //thread
     Thread readingThread(readingFunction);
@@ -65,13 +72,21 @@
     Thread::wait(osWaitForever);
 }
 
+
+
 void readingFunction(const void* arg)
 {
+    int vals[N_TASKS];
+    for (int i=0; i<N_TASKS; i++)
+        vals[i] = -1;
     while(1) {
-        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);
+        for (int i=0; i<N_TASKS; i++)
+            if (vals[i]!=task_array[i]->_s->read()) {
+                vals[i] = task_array[i]->_s->read();
+                printf("Read[%d] = %d\n\r", task_array[i]->_id, 
+                                                    task_array[i]->_s->read());
+            }
+        Thread::wait(READ_FUNCTION_MS);
     }
 }
 
@@ -79,15 +94,13 @@
 {
     int x = 1;
     while(1) {
-        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++);
+        for (int i=0; i<N_TASKS; i++)
+            if (x%(task_array[i]->_ratio)==0) {
+                task_array[i]->_s->write(task_array[i]->_val++);
+                printf("Write[%d] = %d\n\r", task_array[i]->_id, 
+                                                    task_array[i]->_val);
+            }
         x++;
-        Thread::wait(WRITING_PERIOD_MS);
+        Thread::wait(WRITE_FUNCTION_MS);
     }
 }
--- a/ssWi.lib	Sun Sep 09 20:03:53 2012 +0000
+++ b/ssWi.lib	Thu Oct 11 06:49:40 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mariob/code/ssWi/#354a0e3087c1
+http://mbed.org/users/mariob/code/ssWi/#4ad44d62d510