Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Sat Apr 28 17:21:24 2012 +0000
Revision:
9:377560539b74
Parent:
ui/ui.cpp@8:ffc7d8af2d5a
Child:
11:ea2112ae3c4a
Restructured project to have a single shared lib; Also raised the RF baud rate

Who changed what in which revision?

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