Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Thu May 03 14:20:04 2012 +0000
Revision:
22:7ba09c0af0d0
Parent:
17:bafcef1c3579
added 90sec timer and tigger

Who changed what in which revision?

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