trabalho final

Dependencies:   X_NUCLEO_IKS01A1-f255a2c75ecb mbed-rtos mbed

Committer:
nlsantos
Date:
Wed May 18 01:01:34 2016 +0000
Revision:
30:b1d50b25d87e
Parent:
25:2197b8bb930c
experimenting with threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jacinta 0:1eaebb55408a 1 #include "userMethods.h"
Jacinta 0:1eaebb55408a 2 #include "sensor.h"
Jacinta 0:1eaebb55408a 3 #include <cstring>
Jacinta 0:1eaebb55408a 4 #include <ctime>
Jacinta 0:1eaebb55408a 5 #include <stdexcept>
Jacinta 2:0b8065489409 6 #include <vector>
Jacinta 7:ed4a10ebe720 7
nlsantos 25:2197b8bb930c 8 // FIFO buffer
Jacinta 7:ed4a10ebe720 9 vector<log_data> * UserMethods::v;
Jacinta 7:ed4a10ebe720 10 int * UserMethods::n;
Jacinta 15:a72b66e1f473 11 ExpansionBoard * UserMethods::e;
Jacinta 21:849e5636076e 12 bool UserMethods::flag;
Jacinta 0:1eaebb55408a 13
nlsantos 25:2197b8bb930c 14 // Method that processes the ReadAll command
Jacinta 21:849e5636076e 15 void UserMethods::readAllData(const void*)
Jacinta 21:849e5636076e 16 {
Jacinta 7:ed4a10ebe720 17 vector<log_data> vector = *v;
Jacinta 21:849e5636076e 18
nlsantos 30:b1d50b25d87e 19 if(flag)printf("Entered in readAllData");
nlsantos 30:b1d50b25d87e 20
Jacinta 2:0b8065489409 21 char buffer[32];
Jacinta 21:849e5636076e 22
Jacinta 2:0b8065489409 23 //.csv header
nlsantos 30:b1d50b25d87e 24 if(flag) printf("Sample_Date,HTS221_Temp,LPS25H_Press,HTS221_Hum\n\r");
nlsantos 30:b1d50b25d87e 25
Jacinta 7:ed4a10ebe720 26 for(int i = 0; i < vector.size(); i++)
Jacinta 2:0b8065489409 27 //TODO Add time here and read n
Jacinta 2:0b8065489409 28 //.csv friendly format for logging
Jacinta 7:ed4a10ebe720 29 printf("%s,%7s°C,%smbar,%s%%\n\r", ctime(&vector[i].date), ExpansionBoard::printDouble(buffer, vector[i].tempCelsius, 2),
Jacinta 21:849e5636076e 30 ExpansionBoard::printDouble(buffer, vector[i].pressure, 2), ExpansionBoard::printDouble(buffer, vector[i].humidity, 2));
nlsantos 30:b1d50b25d87e 31
nlsantos 30:b1d50b25d87e 32 osStatus os = Thread.terminate();
Jacinta 0:1eaebb55408a 33 }
Jacinta 0:1eaebb55408a 34
nlsantos 25:2197b8bb930c 35 // Method to proccess Read <n> command
Jacinta 21:849e5636076e 36 void UserMethods::readNData(const void*)
Jacinta 21:849e5636076e 37 {
Jacinta 7:ed4a10ebe720 38 vector<log_data> vector = *v;
Jacinta 7:ed4a10ebe720 39 int num = *n;
Jacinta 21:849e5636076e 40
Jacinta 2:0b8065489409 41 //Serial pc(USBTX, USBRX);
Jacinta 2:0b8065489409 42 //pc.baud(115200);
Jacinta 21:849e5636076e 43 if(flag) {
Jacinta 21:849e5636076e 44 printf("Sample_Date,HTS221_Temp,LPS25H_Press,HTS221_Hum\n\r");
Jacinta 21:849e5636076e 45 }
Jacinta 2:0b8065489409 46 char buffer[32];
Jacinta 7:ed4a10ebe720 47 for(int i = vector.size()-num-1; i < vector.size(); i++)
Jacinta 7:ed4a10ebe720 48 printf("%s,%7s°C,%smbar,%s%%\n\r", ctime(&vector[i].date), ExpansionBoard::printDouble(buffer, vector[i].tempCelsius, 2),
Jacinta 21:849e5636076e 49 ExpansionBoard::printDouble(buffer, vector[i].pressure, 2), ExpansionBoard::printDouble(buffer, vector[i].humidity, 2));
nlsantos 30:b1d50b25d87e 50 osStatus os = Thread.terminate();
Jacinta 0:1eaebb55408a 51 }
Jacinta 0:1eaebb55408a 52
nlsantos 25:2197b8bb930c 53
nlsantos 25:2197b8bb930c 54 // Method that proccesses the DeleteAll command
Jacinta 21:849e5636076e 55 int UserMethods::deleteAllData(const void*)
Jacinta 21:849e5636076e 56 {
Jacinta 21:849e5636076e 57 vector<log_data> vector = *v;
Jacinta 15:a72b66e1f473 58 int num = vector.size();
Jacinta 15:a72b66e1f473 59 ExpansionBoard sensor = *e;
Jacinta 21:849e5636076e 60
Jacinta 21:849e5636076e 61 for(int i = 0; i < vector.size(); i++) {
Jacinta 15:a72b66e1f473 62 osEvent evt = sensor.mail_box.get();
Jacinta 15:a72b66e1f473 63 if (evt.status == osEventMail) {
Jacinta 21:849e5636076e 64 log_data *log = (log_data*)evt.value.p;
Jacinta 21:849e5636076e 65 sensor.mail_box.free(log);
Jacinta 15:a72b66e1f473 66 }
Jacinta 15:a72b66e1f473 67 }
Jacinta 21:849e5636076e 68
Jacinta 21:849e5636076e 69 vector.clear();
Jacinta 21:849e5636076e 70
Jacinta 15:a72b66e1f473 71 return num;
nlsantos 30:b1d50b25d87e 72 osStatus os = Thread.terminate();
Jacinta 15:a72b66e1f473 73 }
Jacinta 15:a72b66e1f473 74
nlsantos 25:2197b8bb930c 75 // Method thar proccesses the Read<n> command
Jacinta 21:849e5636076e 76 int UserMethods::deleteNData(const void*)
Jacinta 21:849e5636076e 77 {
Jacinta 21:849e5636076e 78 vector<log_data> vector = *v;
Jacinta 21:849e5636076e 79 int num = (int)vector.size();
Jacinta 15:a72b66e1f473 80 int nOfRecords = *n;
Jacinta 15:a72b66e1f473 81 ExpansionBoard sensor = *e;
Jacinta 21:849e5636076e 82
Jacinta 21:849e5636076e 83 if(nOfRecords >= num) {
Jacinta 15:a72b66e1f473 84 //nOfRecords = UserMethods::deleteAllData;
Jacinta 15:a72b66e1f473 85 return nOfRecords;
Jacinta 21:849e5636076e 86 } else {
Jacinta 21:849e5636076e 87 for(int i = 0; i < nOfRecords; i++) {
Jacinta 15:a72b66e1f473 88 osEvent evt = sensor.mail_box.get();
Jacinta 15:a72b66e1f473 89 if (evt.status == osEventMail) {
Jacinta 21:849e5636076e 90 log_data *log = (log_data*)evt.value.p;
Jacinta 21:849e5636076e 91 sensor.mail_box.free(log);
Jacinta 21:849e5636076e 92 }
Jacinta 21:849e5636076e 93 }
Jacinta 15:a72b66e1f473 94 //Erases the first n elements
Jacinta 21:849e5636076e 95 //vector.erase(v.begin(), v.begin() + nOfRecords);
Jacinta 15:a72b66e1f473 96 }
Jacinta 21:849e5636076e 97
Jacinta 15:a72b66e1f473 98 return nOfRecords;
nlsantos 30:b1d50b25d87e 99 osStatus os = Thread.terminate();
Jacinta 15:a72b66e1f473 100 }
Jacinta 15:a72b66e1f473 101
nlsantos 25:2197b8bb930c 102 // Constructor of the class
Jacinta 21:849e5636076e 103 UserMethods::UserMethods(vector<log_data> vector, int num, ExpansionBoard sensor, bool lFlag)
Jacinta 21:849e5636076e 104 {
Jacinta 7:ed4a10ebe720 105 v = &vector;
Jacinta 7:ed4a10ebe720 106 n = &num;
Jacinta 15:a72b66e1f473 107 e = &sensor;
Jacinta 20:014a808b3ea2 108 flag = &lFlag;
Jacinta 7:ed4a10ebe720 109 }