example for the ssWi library

Dependencies:   ssWi

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Fri Apr 17 15:23:08 2020 +0000
Parent:
7:f3ba13c6d56e
Commit message:
updated

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
ssWi.lib Show annotated file Show diff for this revision Revisions of this file
diff -r f3ba13c6d56e -r ffe1815b6deb main.cpp
--- a/main.cpp	Thu Mar 07 15:44:53 2013 +0000
+++ b/main.cpp	Fri Apr 17 15:23:08 2020 +0000
@@ -1,108 +1,105 @@
 #include "mbed.h"
-#include "rtos.h"
 
 #include "xbee.hpp"
 #include "ssWiSocket.hpp"
 
-#define READ_FUNCTION_MS 1000
-#define WRITE_FUNCTION_MS 1000
-
-
+#define TARGET_ID               1
 
-struct Task {
-    PortID _id;
-    int _ratio;
-    ssWiSocket* _s;
-    int _val;
+int readValue = 0;
+int writtenValue = 0;
+
+void code_state(int);
 
-    Task (PortID id, int ratio, int value) {
-        _id = id;
-        _ratio = ratio;
-        _s = ssWiSocket::createSocket(id);
-        _val = value;
-    }
-};
+Thread readingThread;
+Thread writingThread;
 
-#define N_TASKS 4
-Task *task_array[N_TASKS];
-
+void readingFunction();
+void writingFunction();
 
-void readingFunction(const void* arg);
+//Serial pc(USBTX, USBRX, "pc", 9600);
 
-void writingFunction(const void* arg);
-
-
-
+#define UPDATE_RATE_MS    1000
 
 int main()
 {
-    printf("\n\r************* CONFIG *************\n\r");
+
+    //pc.printf("\n\r************* CONFIG *************\n\r");
+
+code_state(0);
 
     //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());
+    //XBeeAddress addr = xbee.getLocalAddress();
+
+code_state(1);
+
     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());
+
+code_state(2);
+
+   //XBeeAddress addr2(0, 0);
+   //xbee.getDstAddress(addr2);
+   //xbee.getChannel();
+   //xbee.getPanID();
 
     //wireless network
     xbee.init(5, 10);
 
     srand(time(0));
 
-    printf("\n\r************* READ *************\n\r");
+code_state(4);
 
-    //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_sleep_for(UPDATE_RATE_MS);
 
-/*
     //thread
-    Thread readingThread(readingFunction);
-    Thread writingThread(writingFunction);
-*/
-    RtosTimer reader (readingFunction, osTimerPeriodic, NULL);
-    RtosTimer writer (writingFunction, osTimerPeriodic, NULL);
+    readingThread.start(readingFunction);
+    writingThread.start(writingFunction);
 
-    reader.start(1000);
-    writer.start(1000);
+    while (true) {
+        //code_state(writtenValue);
+        //thread_sleep_for(UPDATE_RATE_MS);
+        code_state(readValue);
+        thread_sleep_for(UPDATE_RATE_MS);
+    }
 
-    printf("\n\r************* START *************\n\r");
-
-    Thread::wait(osWaitForever);
+    return 0;
 }
 
-
+#define READ_FUNCTION_MS 1000
+#define WRITE_FUNCTION_MS 3000
 
-void readingFunction(const void* arg)
+#define PORT_ID_TARGET_1   2
+#define PORT_ID_TARGET_2   10
+
+void readingFunction()
 {
-    static int vals[N_TASKS];
-//    while(1) {
-    for (int i=0; i<N_TASKS; i++) {
-        if (vals[i]!=task_array[i]->_s->read()) {
-            vals[i] = task_array[i]->_s->read();
-            printf("%ld: Read[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_s->read());
-        }
+    PortID id = TARGET_ID == 1 ? PORT_ID_TARGET_1 : PORT_ID_TARGET_2;
+    ssWiSocket* s = ssWiSocket::createSocket(id);
+
+    while (1) {
+        readValue = s->read();
+        thread_sleep_for(READ_FUNCTION_MS);
     }
-//        Thread::wait(READ_FUNCTION_MS);
-//    }
 }
 
-void writingFunction(const void* arg)
-{
-    static int x = 1;
-//    while(1) {
-    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("%ld: Write[%d] = %d\n\r", time(0), task_array[i]->_id, task_array[i]->_val);
-        }
-    x++;
-//       Thread::wait(WRITE_FUNCTION_MS);
-//    }
+void writingFunction() {
+    PortID id = TARGET_ID == 1 ? PORT_ID_TARGET_2 : PORT_ID_TARGET_1;
+    ssWiSocket* s = ssWiSocket::createSocket(id);
+
+    writtenValue = TARGET_ID == 1 ? 3 : 0;
+    
+    while (1) {
+        s->write(writtenValue++);
+        thread_sleep_for(WRITE_FUNCTION_MS);
+  }
 }
+
+void code_state(int val) {
+    static DigitalOut led1(LED1);
+    static DigitalOut led2(LED2);
+    static DigitalOut led3(LED3);
+
+    led1 = val % 2;
+    led2 = (val >> 1) % 2;
+    led3 = (val >> 2) % 2;
+}
diff -r f3ba13c6d56e -r ffe1815b6deb mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Apr 17 15:23:08 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#565ab149819481224ab43f878c3921b14b11d180
diff -r f3ba13c6d56e -r ffe1815b6deb mbed-rtos.lib
--- a/mbed-rtos.lib	Thu Mar 07 15:44:53 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#9654a71f5a90
diff -r f3ba13c6d56e -r ffe1815b6deb mbed.bld
--- a/mbed.bld	Thu Mar 07 15:44:53 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/cd19af002ccc
\ No newline at end of file
diff -r f3ba13c6d56e -r ffe1815b6deb ssWi.lib
--- a/ssWi.lib	Thu Mar 07 15:44:53 2013 +0000
+++ b/ssWi.lib	Fri Apr 17 15:23:08 2020 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mariob/code/ssWi/#b50c3b3b241c
+http://mbed.org/users/mariob/code/ssWi/#36931ee9af85