ui

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ui.cpp Source File

ui.cpp

00001 
00002 #include "ui.h"
00003 #include <iostream>
00004 
00005 UI::UI() :
00006         tUI(printtw,this,osPriorityNormal,1024) {
00007     newdataflags = 0;
00008     for (int i = 0; i < NUMIDS; i++) {
00009         idlist[i] = 0;
00010         buffarr[i] = 0;
00011     }
00012     
00013     //char* sync = "ABCD";
00014     //std::cout.write(sync, 4);
00015 }
00016 
00017 bool UI::regid(char id, unsigned int length) {
00018 
00019     //check if the id is already taken
00020     if (id < NUMIDS && !idlist[id]) {
00021         idlist[id] = length;
00022         buffarr[id] = new float[length];
00023         return true;
00024     } else
00025         return false;
00026 }
00027 
00028 bool UI::updateval(char id, float* buffer, unsigned int length) {
00029 
00030     //check if the id is registered, and has buffer of correct length
00031     if (id < NUMIDS && idlist[id] == length && buffarr[id] && !(newdataflags & (1<<id))) {
00032         for (int i = 0; i < length; i++)
00033             buffarr[id][i] = buffer[i];
00034         newdataflags |= (1<<id);
00035         return true;
00036     } else
00037         return false;
00038 }
00039 
00040 bool UI::updateval(char id, float value) {
00041 
00042     //check if the id is registered, and the old value has been written
00043     if (id < NUMIDS && idlist[id] == 1 && buffarr[id] && !(newdataflags & (1<<id))) {
00044         buffarr[id][0] = value;
00045         newdataflags |= (1<<id);
00046         return true;
00047     } else
00048         return false;
00049 
00050 }
00051 
00052 bool UI::unregid(char id) {
00053     if (id < NUMIDS) {
00054         idlist[id] = 0;
00055         if (buffarr[id])
00056             delete buffarr[id];
00057         return true;
00058     } else 
00059         return false;
00060 }
00061 
00062 void UI::printloop() {
00063 
00064     while(1) {
00065         //char* stuff = "asdf";
00066         char* sync = "ABCD";
00067         std::cout.write(sync, 4);
00068         
00069         //output num packets
00070         char numidstosend = 0;
00071         for (int id = 0; id < NUMIDS; id++)
00072             if (newdataflags & (1<<id))
00073                 numidstosend++;
00074         std::cout.put(numidstosend);
00075         
00076         //send packets
00077         for (int id = 0; id < NUMIDS; id++) {
00078             if (newdataflags & (1<<id)) {
00079                 std::cout.put(id);
00080                 std::cout.write((char*)buffarr[id], 4*idlist[id]);
00081                 newdataflags &= ~(1<<id);
00082             }
00083         }
00084         
00085         std::cout.flush();
00086         Thread::wait(200);
00087         
00088     }
00089 
00090 }
00091