ICRS Eurobot 2013

Dependencies:   mbed mbed-rtos Servo QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Printing.cpp Source File

Printing.cpp

00001 #include "Printing.h"
00002 #ifdef PRINTINGOFF
00003 void printingThread(void const*){Thread::wait(osWaitForever);}
00004 bool registerID(char, size_t){return true;}
00005 bool unregisterID(char) {return true;}
00006 bool updateval(char, float*, size_t){return true;}
00007 bool updateval(char id, float value){return true;}
00008 #else
00009 #include <iostream>
00010 using namespace std;
00011 
00012 size_t idlist[NUMIDS]; // Stores length of buffer 0 => unassigned
00013 float* buffarr[NUMIDS];
00014 volatile unsigned int newdataflags;
00015 
00016 bool registerID(char id, size_t length) {   
00017     if (id < NUMIDS && !idlist[id]) {//check if the id is already taken
00018         idlist[id] = length;
00019         buffarr[id] = new float[length];
00020         return true;
00021     } else
00022         return false;
00023 }
00024 bool unregisterID(char id) {
00025     if (id < NUMIDS) {
00026         idlist[id] = 0;
00027         if (buffarr[id])
00028             delete buffarr[id];
00029         return true;
00030     } else
00031         return false;
00032 }
00033 
00034 bool updateval(char id, float* buffer, size_t length) {
00035     //check if the id is registered, and has buffer of correct length
00036     if (id < NUMIDS && idlist[id] == length && buffarr[id] && !(newdataflags & (1<<id))) {
00037         for (size_t i = 0; i < length; i++)
00038             buffarr[id][i] = buffer[i];
00039         newdataflags |= (1<<id);
00040         return true;
00041     } else
00042         return false;
00043 }
00044 
00045 bool updateval(char id, float value){
00046     //check if the id is registered, and the old value has been written
00047     if (id < NUMIDS && idlist[id] == 1 && buffarr[id] && !(newdataflags & (1<<id))) {
00048         buffarr[id][0] = value;
00049         newdataflags |= (1<<id);
00050         return true;
00051     } else
00052         return false;
00053 }
00054 
00055 void printingThread(void const*){
00056     newdataflags = 0;
00057     for (int i = 0; i < NUMIDS; i++) {
00058         idlist[i] = 0;
00059         buffarr[i] = 0;
00060     }
00061 
00062 
00063     Thread::wait(3500);
00064     while(true){   
00065         // Send number of packets
00066         char numtosend = 0;
00067         for (unsigned int v = newdataflags; v; numtosend++){v &= v - 1;}        
00068         cout.put(numtosend);
00069 
00070         // Send packets
00071         for (char id = 0; id < NUMIDS; id++) {
00072             if (newdataflags & (1<<id)) {
00073                 cout.put(id);
00074                 cout.write((char*)buffarr[id], idlist[id] * sizeof(float));
00075                 newdataflags &= ~(1<<id);
00076             }
00077         }
00078         cout << endl;
00079         Thread::wait(200);
00080     }
00081 }
00082 #endif
00083 
00084 
00085