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