Contains necessary classes and functions for ELEC351
Embed:
(wiki syntax)
Show/hide line numbers
recordsMaster.cpp
00001 #include "mbed.h" 00002 #include <iostream> 00003 #include "recordsMaster.hpp" 00004 00005 circularBuffer::circularBuffer() 00006 { 00007 // Check allocated buffer size 00008 if (sizeof(record)/sizeof(TDS_record) == 120) { 00009 bufferLoopBack = 0; 00010 cerr <<"\n\rBuffer aquired\n\r\n"<<endl; 00011 } else { 00012 cerr << "Could not aquire buffer - bad length\n\r" << endl; 00013 cerr << "Attempted to aquire buffer of length 120\n\r" << endl; 00014 cerr << "Got buffer of " << sizeof(record)/sizeof(TDS_record) << "\n\r\n" << endl; 00015 } 00016 00017 // Create blank record with all data = 0 00018 blankRecord.record_Data.temp = 0; 00019 blankRecord.record_Data.pres = 0; 00020 blankRecord.record_Data.ligt = 0; 00021 blankRecord.record_DT.day = 0; 00022 blankRecord.record_DT.mnt = 0; 00023 blankRecord.record_DT.yr = 0; 00024 blankRecord.record_DT.sec = 0; 00025 blankRecord.record_DT.min = 0; 00026 blankRecord.record_DT.hr = 0; 00027 00028 } 00029 00030 int circularBuffer::write(TDS_record recordData) 00031 { 00032 if (bufferLoopBack > 119) { 00033 bufferLoopBack = 0; // Make circular 00034 } 00035 recordLock.lock(); // Lock for writing 00036 record[bufferLoopBack] = recordData; // Store data temporarely 00037 bufferLoopBack++; // Set next writing space 00038 recordLock.unlock(); // Unlock 00039 return 0; 00040 } 00041 00042 00043 int circularBuffer::read(int index) 00044 { 00045 if (index == 0) { // If index is 0 00046 index = bufferLoopBack-1; // Read last record 00047 } else { 00048 index--; // Scale form 1->120 to 0->119 00049 } 00050 if ((index <0)||(index >119)) { // If reading outside of buffer range 00051 return -1; // Return fail 00052 00053 } else { // Else 00054 recordLock.lock(); // Prevent unsynchronised reads 00055 TDS_record tempRecord = record[index]; // Store data temporarely 00056 recordLock.unlock(); // Unlock 00057 cout <<"\n\n\n\r Record number "<< index <<endl; 00058 cout <<"\n\r Date:" <<endl; 00059 cout <<"\r "<< tempRecord.record_DT.day <<"."<< tempRecord.record_DT.mnt <<"."<< tempRecord.record_DT.yr <<endl; 00060 cout <<"\n\r Time:" <<endl; 00061 cout <<"\r "<< tempRecord.record_DT.hr <<":"<< tempRecord.record_DT.min <<":"<< tempRecord.record_DT.sec <<endl; 00062 cout <<"\n\r Temperature = " << tempRecord.record_Data.temp <<endl; 00063 cout <<"\r Preassure = " << tempRecord.record_Data.pres <<endl; 00064 cout <<"\r Light Level = " << tempRecord.record_Data.ligt <<endl; 00065 return 0; // Return sucess 00066 00067 } 00068 } 00069 00070 00071 int circularBuffer::del(int index) 00072 { 00073 index--; // Scale form 1->120 to 0->119 00074 if ((index <0)||(index >119)) { // If reading outside of buffer range 00075 return -1; // Return fail 00076 } else { 00077 recordLock.lock(); // Lock for writing 00078 record[index] = blankRecord; // Erase actual data, not just refference 00079 recordLock.unlock(); // Unlock 00080 return 0; // Return sucess 00081 } 00082 } 00083 00084 00085 int circularBuffer::readAll() 00086 { 00087 TDS_record tempRecord; 00088 00089 for (int i=bufferLoopBack; i <120; i++) { // Read top part of buffer 00090 recordLock.lock(); // Prevent unsynchronised reads 00091 tempRecord = record[i]; // Store data temporarely 00092 recordLock.unlock(); // Unlock 00093 cout <<"\n\r Record number "<< i <<endl; 00094 cout <<"\n\r Date:" <<endl; 00095 cout <<"\n\r"<< tempRecord.record_DT.day <<"."<< tempRecord.record_DT.mnt <<"."<< tempRecord.record_DT.yr <<endl; 00096 cout <<"\n\r Time:" <<endl; 00097 cout <<"\n\r"<< tempRecord.record_DT.hr <<":"<< tempRecord.record_DT.min <<":"<< tempRecord.record_DT.sec <<endl; 00098 cout <<"\n\r Temperature = " << tempRecord.record_Data.temp <<endl; 00099 cout <<"\n\r Preassure = " << tempRecord.record_Data.pres <<endl; 00100 cout <<"\n\r Light Level = " << tempRecord.record_Data.ligt <<endl; 00101 } 00102 00103 for (int j=0; j <bufferLoopBack; j++) { // Read bottom part of buffer 00104 recordLock.lock(); // Prevent unsynchronised reads 00105 tempRecord = record[j]; // Store data temporarely 00106 recordLock.unlock(); // Unlock 00107 cout <<"\n\r Record number "<< j <<endl; 00108 cout <<"\n\r Date:" <<endl; 00109 cout <<"\n\r"<< tempRecord.record_DT.day <<"."<< tempRecord.record_DT.mnt <<"."<< tempRecord.record_DT.yr <<endl; 00110 cout <<"\n\r Time:" <<endl; 00111 cout <<"\n\r"<< tempRecord.record_DT.hr <<":"<< tempRecord.record_DT.min <<":"<< tempRecord.record_DT.sec <<endl; 00112 cout <<"\n\r Temperature = " << tempRecord.record_Data.temp <<endl; 00113 cout <<"\n\r Preassure = " << tempRecord.record_Data.pres <<endl; 00114 cout <<"\n\r Light Level = " << tempRecord.record_Data.ligt <<endl; 00115 } 00116 return 0; // Return sucess 00117 } 00118 00119 00120 int circularBuffer::delAll() 00121 { 00122 for (int i=0; i <120; i++) { 00123 recordLock.lock(); // Lock for writing 00124 record[i] = blankRecord; // Erase actual data, not just refference 00125 recordLock.unlock(); // Unlock 00126 bufferLoopBack = 0; // Reset buffer 00127 } 00128 return 0; // Return sucess 00129 } 00130 00131 00132 int circularBuffer::recNum() 00133 { 00134 return bufferLoopBack+1; // Return last record number 00135 } 00136 00137 circularBuffer::~circularBuffer() 00138 { 00139 cerr << "Buffer Destroyed\n\r\n" << endl; 00140 }
Generated on Tue Jul 12 2022 18:37:11 by
1.7.2