ui

Files at this revision

API Documentation at this revision

Comitter:
narshu
Date:
Thu Apr 26 19:58:44 2012 +0000
Commit message:

Changed in this revision

ui.cpp Show annotated file Show diff for this revision Revisions of this file
ui.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui.cpp	Thu Apr 26 19:58:44 2012 +0000
@@ -0,0 +1,91 @@
+
+#include "ui.h"
+#include <iostream>
+
+UI::UI() :
+        tUI(printtw,this,osPriorityNormal,1024) {
+    newdataflags = 0;
+    for (int i = 0; i < NUMIDS; i++) {
+        idlist[i] = 0;
+        buffarr[i] = 0;
+    }
+    
+    //char* sync = "ABCD";
+    //std::cout.write(sync, 4);
+}
+
+bool UI::regid(char id, unsigned int length) {
+
+    //check if the id is already taken
+    if (id < NUMIDS && !idlist[id]) {
+        idlist[id] = length;
+        buffarr[id] = new float[length];
+        return true;
+    } else
+        return false;
+}
+
+bool UI::updateval(char id, float* buffer, unsigned int length) {
+
+    //check if the id is registered, and has buffer of correct length
+    if (id < NUMIDS && idlist[id] == length && buffarr[id] && !(newdataflags & (1<<id))) {
+        for (int i = 0; i < length; i++)
+            buffarr[id][i] = buffer[i];
+        newdataflags |= (1<<id);
+        return true;
+    } else
+        return false;
+}
+
+bool UI::updateval(char id, float value) {
+
+    //check if the id is registered, and the old value has been written
+    if (id < NUMIDS && idlist[id] == 1 && buffarr[id] && !(newdataflags & (1<<id))) {
+        buffarr[id][0] = value;
+        newdataflags |= (1<<id);
+        return true;
+    } else
+        return false;
+
+}
+
+bool UI::unregid(char id) {
+    if (id < NUMIDS) {
+        idlist[id] = 0;
+        if (buffarr[id])
+            delete buffarr[id];
+        return true;
+    } else 
+        return false;
+}
+
+void UI::printloop() {
+
+    while(1) {
+        //char* stuff = "asdf";
+        char* sync = "ABCD";
+        std::cout.write(sync, 4);
+        
+        //output num packets
+        char numidstosend = 0;
+        for (int id = 0; id < NUMIDS; id++)
+            if (newdataflags & (1<<id))
+                numidstosend++;
+        std::cout.put(numidstosend);
+        
+        //send packets
+        for (int id = 0; id < NUMIDS; id++) {
+            if (newdataflags & (1<<id)) {
+                std::cout.put(id);
+                std::cout.write((char*)buffarr[id], 4*idlist[id]);
+                newdataflags &= ~(1<<id);
+            }
+        }
+        
+        std::cout.flush();
+        Thread::wait(200);
+        
+    }
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui.h	Thu Apr 26 19:58:44 2012 +0000
@@ -0,0 +1,29 @@
+
+#ifndef UI_H
+#define UI_H
+
+#include "rtos.h"
+
+#define NUMIDS 32
+
+class UI {
+public:
+    Thread tUI;
+    
+    UI();
+    
+    bool regid(char id, unsigned int length);
+    bool updateval(char id, float* buffer, unsigned int length);
+    bool updateval(char id, float value);
+    bool unregid(char id);
+    
+private:
+    char idlist[NUMIDS];
+    float* buffarr[NUMIDS];
+    int newdataflags; //Only works for NUMID = 32
+    
+    void printloop();
+    static void printtw(void const *arg){ ((UI*)arg)->printloop(); }
+};
+
+#endif //UI_H