example for the ssWi library

Dependencies:   ssWi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "xbee.hpp"
00004 #include "ssWiSocket.hpp"
00005 
00006 #define TARGET_ID               1
00007 
00008 int readValue = 0;
00009 int writtenValue = 0;
00010 
00011 void code_state(int);
00012 
00013 Thread readingThread;
00014 Thread writingThread;
00015 
00016 void readingFunction();
00017 void writingFunction();
00018 
00019 //Serial pc(USBTX, USBRX, "pc", 9600);
00020 
00021 #define UPDATE_RATE_MS    1000
00022 
00023 int main()
00024 {
00025 
00026     //pc.printf("\n\r************* CONFIG *************\n\r");
00027 
00028 code_state(0);
00029 
00030     //radio module
00031     XBeeModule xbee(p9, p10, 102, 14);
00032     //XBeeAddress addr = xbee.getLocalAddress();
00033 
00034 code_state(1);
00035 
00036     xbee.setDstAddress(XBeeBroadcastAddress());
00037 
00038 code_state(2);
00039 
00040    //XBeeAddress addr2(0, 0);
00041    //xbee.getDstAddress(addr2);
00042    //xbee.getChannel();
00043    //xbee.getPanID();
00044 
00045     //wireless network
00046     xbee.init(5, 10);
00047 
00048     srand(time(0));
00049 
00050 code_state(4);
00051 
00052     thread_sleep_for(UPDATE_RATE_MS);
00053 
00054     //thread
00055     readingThread.start(readingFunction);
00056     writingThread.start(writingFunction);
00057 
00058     while (true) {
00059         //code_state(writtenValue);
00060         //thread_sleep_for(UPDATE_RATE_MS);
00061         code_state(readValue);
00062         thread_sleep_for(UPDATE_RATE_MS);
00063     }
00064 
00065     return 0;
00066 }
00067 
00068 #define READ_FUNCTION_MS 1000
00069 #define WRITE_FUNCTION_MS 3000
00070 
00071 #define PORT_ID_TARGET_1   2
00072 #define PORT_ID_TARGET_2   10
00073 
00074 void readingFunction()
00075 {
00076     PortID id = TARGET_ID == 1 ? PORT_ID_TARGET_1 : PORT_ID_TARGET_2;
00077     ssWiSocket* s = ssWiSocket::createSocket(id);
00078 
00079     while (1) {
00080         readValue = s->read();
00081         thread_sleep_for(READ_FUNCTION_MS);
00082     }
00083 }
00084 
00085 void writingFunction() {
00086     PortID id = TARGET_ID == 1 ? PORT_ID_TARGET_2 : PORT_ID_TARGET_1;
00087     ssWiSocket* s = ssWiSocket::createSocket(id);
00088 
00089     writtenValue = TARGET_ID == 1 ? 3 : 0;
00090     
00091     while (1) {
00092         s->write(writtenValue++);
00093         thread_sleep_for(WRITE_FUNCTION_MS);
00094   }
00095 }
00096 
00097 void code_state(int val) {
00098     static DigitalOut led1(LED1);
00099     static DigitalOut led2(LED2);
00100     static DigitalOut led3(LED3);
00101 
00102     led1 = val % 2;
00103     led2 = (val >> 1) % 2;
00104     led3 = (val >> 2) % 2;
00105 }