Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Fri Apr 27 14:29:56 2012 +0000
Revision:
6:324946320c6d
Parent:
2:cffa347bb943
Child:
7:f9c59a3e4155
changed ui protocol (removed numpackets)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 2:cffa347bb943 1
narshu 2:cffa347bb943 2 #include "ui.h"
narshu 2:cffa347bb943 3 #include <iostream>
narshu 2:cffa347bb943 4
narshu 2:cffa347bb943 5 UI::UI() :
narshu 2:cffa347bb943 6 tUI(printtw,this,osPriorityNormal,1024) {
narshu 2:cffa347bb943 7 newdataflags = 0;
narshu 2:cffa347bb943 8 for (int i = 0; i < NUMIDS; i++) {
narshu 2:cffa347bb943 9 idlist[i] = 0;
narshu 2:cffa347bb943 10 buffarr[i] = 0;
narshu 2:cffa347bb943 11 }
narshu 2:cffa347bb943 12
narshu 2:cffa347bb943 13 //char* sync = "ABCD";
narshu 2:cffa347bb943 14 //std::cout.write(sync, 4);
narshu 2:cffa347bb943 15 }
narshu 2:cffa347bb943 16
narshu 2:cffa347bb943 17 bool UI::regid(char id, unsigned int length) {
narshu 2:cffa347bb943 18
narshu 2:cffa347bb943 19 //check if the id is already taken
narshu 2:cffa347bb943 20 if (id < NUMIDS && !idlist[id]) {
narshu 2:cffa347bb943 21 idlist[id] = length;
narshu 2:cffa347bb943 22 buffarr[id] = new float[length];
narshu 2:cffa347bb943 23 return true;
narshu 2:cffa347bb943 24 } else
narshu 2:cffa347bb943 25 return false;
narshu 2:cffa347bb943 26 }
narshu 2:cffa347bb943 27
narshu 2:cffa347bb943 28 bool UI::updateval(char id, float* buffer, unsigned int length) {
narshu 2:cffa347bb943 29
narshu 2:cffa347bb943 30 //check if the id is registered, and has buffer of correct length
narshu 2:cffa347bb943 31 if (id < NUMIDS && idlist[id] == length && buffarr[id] && !(newdataflags & (1<<id))) {
narshu 2:cffa347bb943 32 for (int i = 0; i < length; i++)
narshu 2:cffa347bb943 33 buffarr[id][i] = buffer[i];
narshu 2:cffa347bb943 34 newdataflags |= (1<<id);
narshu 2:cffa347bb943 35 return true;
narshu 2:cffa347bb943 36 } else
narshu 2:cffa347bb943 37 return false;
narshu 2:cffa347bb943 38 }
narshu 2:cffa347bb943 39
narshu 2:cffa347bb943 40 bool UI::updateval(char id, float value) {
narshu 2:cffa347bb943 41
narshu 2:cffa347bb943 42 //check if the id is registered, and the old value has been written
narshu 2:cffa347bb943 43 if (id < NUMIDS && idlist[id] == 1 && buffarr[id] && !(newdataflags & (1<<id))) {
narshu 2:cffa347bb943 44 buffarr[id][0] = value;
narshu 2:cffa347bb943 45 newdataflags |= (1<<id);
narshu 2:cffa347bb943 46 return true;
narshu 2:cffa347bb943 47 } else
narshu 2:cffa347bb943 48 return false;
narshu 2:cffa347bb943 49
narshu 2:cffa347bb943 50 }
narshu 2:cffa347bb943 51
narshu 2:cffa347bb943 52 bool UI::unregid(char id) {
narshu 2:cffa347bb943 53 if (id < NUMIDS) {
narshu 2:cffa347bb943 54 idlist[id] = 0;
narshu 2:cffa347bb943 55 if (buffarr[id])
narshu 2:cffa347bb943 56 delete buffarr[id];
narshu 2:cffa347bb943 57 return true;
narshu 2:cffa347bb943 58 } else
narshu 2:cffa347bb943 59 return false;
narshu 2:cffa347bb943 60 }
narshu 2:cffa347bb943 61
narshu 2:cffa347bb943 62 void UI::printloop() {
narshu 2:cffa347bb943 63
narshu 2:cffa347bb943 64 char* sync = "ABCD";
narshu 6:324946320c6d 65 std::cout.write(sync, 4);
narshu 2:cffa347bb943 66
narshu 2:cffa347bb943 67 while (1) {
narshu 2:cffa347bb943 68
narshu 6:324946320c6d 69 //send packets
narshu 6:324946320c6d 70 for (int id = 0; id < NUMIDS; id++) {
narshu 6:324946320c6d 71 if (newdataflags & (1<<id)) {
narshu 6:324946320c6d 72 std::cout.put(id);
narshu 6:324946320c6d 73 std::cout.write((char*)buffarr[id], 4*idlist[id]);
narshu 6:324946320c6d 74 newdataflags &= ~(1<<id);
narshu 2:cffa347bb943 75 }
narshu 2:cffa347bb943 76 }
narshu 2:cffa347bb943 77
narshu 6:324946320c6d 78 std::cout.flush();
narshu 2:cffa347bb943 79 Thread::wait(200);
narshu 2:cffa347bb943 80 }
narshu 2:cffa347bb943 81
narshu 2:cffa347bb943 82 }
narshu 2:cffa347bb943 83