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