Eurobot2012_Secondary

Fork of Eurobot_2012_Secondary by Shuto Naruse

Committer:
narshu
Date:
Wed Oct 17 22:25:31 2012 +0000
Revision:
1:cc2a9eb0bd55
Commit before publishing

Who changed what in which revision?

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