Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- mariob
- Date:
- 2012-10-11
- Revision:
- 4:989ea19b8fd2
- Parent:
- 3:a0d27c04263e
- Child:
- 5:502371e99bdb
File content as of revision 4:989ea19b8fd2:
#include "mbed.h"
#include "rtos.h"
#include "xbee.hpp"
#include "ssWiSocket.hpp"
#define READ_FUNCTION_MS 1000
#define WRITE_FUNCTION_MS 1000
struct Task {
PortID _id;
int _ratio;
ssWiSocket* _s;
int _val;
Task (PortID id, int ratio, int value) {
_id = id;
_ratio = ratio;
_s = ssWiSocket::createSocket(id);
_val = value;
}
};
#define N_TASKS 4
Task *task_array[N_TASKS];
void readingFunction(const void* arg);
void writingFunction(const void* arg);
int main()
{
printf("\n\r************* CONFIG *************\n\r");
//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());
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());
//wireless network
xbee.init(1, 2);
srand(time(0));
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);
Thread writingThread(writingFunction);
printf("\n\r************* START *************\n\r");
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) {
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);
}
}
void writingFunction(const void* arg)
{
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("Write[%d] = %d\n\r", task_array[i]->_id,
task_array[i]->_val);
}
x++;
Thread::wait(WRITE_FUNCTION_MS);
}
}