update code
Dependencies: X_NUCLEO_IKS01A1 mbed-rtos mbed
main.cpp@8:aec689bd13c0, 2016-05-11 (annotated)
- Committer:
- acarter2
- Date:
- Wed May 11 11:27:41 2016 +0000
- Revision:
- 8:aec689bd13c0
- Parent:
- 7:cb29c20009e8
production and conduction;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acarter2 | 0:4a8b751dbe7c | 1 | #include <stdlib.h> |
acarter2 | 1:4cd93767691a | 2 | //#include <pthread.h> |
acarter2 | 1:4cd93767691a | 3 | //#include <semaphore.h> |
acarter2 | 0:4a8b751dbe7c | 4 | #include "mbed.h" |
acarter2 | 0:4a8b751dbe7c | 5 | //#include "string.h" |
acarter2 | 0:4a8b751dbe7c | 6 | #include <stdio.h> |
acarter2 | 0:4a8b751dbe7c | 7 | #include <ctype.h> |
acarter2 | 0:4a8b751dbe7c | 8 | #include "x_nucleo_iks01a1.h" |
acarter2 | 0:4a8b751dbe7c | 9 | #include "rtos.h" |
acarter2 | 7:cb29c20009e8 | 10 | |
acarter2 | 0:4a8b751dbe7c | 11 | #define RAND_DIVISOR 100000000 |
acarter2 | 0:4a8b751dbe7c | 12 | #define TRUE 1 |
acarter2 | 7:cb29c20009e8 | 13 | |
acarter2 | 8:aec689bd13c0 | 14 | |
acarter2 | 8:aec689bd13c0 | 15 | void consumer(void const *arg); |
acarter2 | 8:aec689bd13c0 | 16 | |
acarter2 | 7:cb29c20009e8 | 17 | void thread1(void *param); |
acarter2 | 8:aec689bd13c0 | 18 | |
acarter2 | 8:aec689bd13c0 | 19 | //Thread* printSampleThread = new Thread(printCurrentRecord); |
acarter2 | 7:cb29c20009e8 | 20 | |
acarter2 | 0:4a8b751dbe7c | 21 | //setting out variables |
acarter2 | 7:cb29c20009e8 | 22 | |
acarter2 | 0:4a8b751dbe7c | 23 | /** |
acarter2 | 0:4a8b751dbe7c | 24 | / this is setting up the connection from the board |
acarter2 | 0:4a8b751dbe7c | 25 | **/ |
acarter2 | 7:cb29c20009e8 | 26 | |
acarter2 | 0:4a8b751dbe7c | 27 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); |
acarter2 | 0:4a8b751dbe7c | 28 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
acarter2 | 0:4a8b751dbe7c | 29 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
acarter2 | 0:4a8b751dbe7c | 30 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
acarter2 | 3:f61ae69ed662 | 31 | //static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
acarter2 | 7:cb29c20009e8 | 32 | |
acarter2 | 1:4cd93767691a | 33 | /* |
acarter2 | 1:4cd93767691a | 34 | / this is will make sure the contant valid of how much data will be sorted or not as this |
acarter2 | 1:4cd93767691a | 35 | /could help when the data range. |
acarter2 | 1:4cd93767691a | 36 | */ |
acarter2 | 7:cb29c20009e8 | 37 | const int AMOUNT_OF_SAMPLE = 150; |
acarter2 | 7:cb29c20009e8 | 38 | |
acarter2 | 1:4cd93767691a | 39 | const int BUFFER_SIZE = 75; |
acarter2 | 0:4a8b751dbe7c | 40 | |
acarter2 | 7:cb29c20009e8 | 41 | int secondsOfsample = 0; |
acarter2 | 8:aec689bd13c0 | 42 | |
acarter2 | 8:aec689bd13c0 | 43 | /* |
acarter2 | 8:aec689bd13c0 | 44 | this is storing the current array of the sample |
acarter2 | 8:aec689bd13c0 | 45 | */ |
acarter2 | 8:aec689bd13c0 | 46 | char outputSample[200]; |
acarter2 | 7:cb29c20009e8 | 47 | |
acarter2 | 7:cb29c20009e8 | 48 | // Mutex lock |
acarter2 | 7:cb29c20009e8 | 49 | Mutex *bufferLock = new Mutex; |
acarter2 | 7:cb29c20009e8 | 50 | |
acarter2 | 0:4a8b751dbe7c | 51 | //buffer |
acarter2 | 3:f61ae69ed662 | 52 | char buffer_item[BUFFER_SIZE]; |
acarter2 | 7:cb29c20009e8 | 53 | |
acarter2 | 7:cb29c20009e8 | 54 | |
acarter2 | 7:cb29c20009e8 | 55 | |
acarter2 | 7:cb29c20009e8 | 56 | Thread *tid; //thread id |
acarter2 | 7:cb29c20009e8 | 57 | //pthread_attr_t attr; //set of thread attributes |
acarter2 | 7:cb29c20009e8 | 58 | |
acarter2 | 0:4a8b751dbe7c | 59 | void *producer(void *param); //producer thread |
acarter2 | 0:4a8b751dbe7c | 60 | void *consumer(void *param); //consumer thread |
acarter2 | 7:cb29c20009e8 | 61 | |
acarter2 | 7:cb29c20009e8 | 62 | |
acarter2 | 7:cb29c20009e8 | 63 | |
acarter2 | 7:cb29c20009e8 | 64 | volatile char memorySample[AMOUNT_OF_SAMPLE]; |
acarter2 | 7:cb29c20009e8 | 65 | |
acarter2 | 0:4a8b751dbe7c | 66 | char values[20][20]; |
acarter2 | 7:cb29c20009e8 | 67 | |
acarter2 | 7:cb29c20009e8 | 68 | |
acarter2 | 7:cb29c20009e8 | 69 | |
acarter2 | 7:cb29c20009e8 | 70 | |
acarter2 | 0:4a8b751dbe7c | 71 | uint8_t id; |
acarter2 | 7:cb29c20009e8 | 72 | |
acarter2 | 7:cb29c20009e8 | 73 | |
acarter2 | 0:4a8b751dbe7c | 74 | //class |
acarter2 | 7:cb29c20009e8 | 75 | |
acarter2 | 0:4a8b751dbe7c | 76 | class KeyLocks { |
acarter2 | 0:4a8b751dbe7c | 77 | |
acarter2 | 0:4a8b751dbe7c | 78 | public: |
acarter2 | 0:4a8b751dbe7c | 79 | int key; |
acarter2 | 0:4a8b751dbe7c | 80 | |
acarter2 | 0:4a8b751dbe7c | 81 | KeyLocks(){ |
acarter2 | 0:4a8b751dbe7c | 82 | |
acarter2 | 0:4a8b751dbe7c | 83 | key = 0; |
acarter2 | 0:4a8b751dbe7c | 84 | |
acarter2 | 0:4a8b751dbe7c | 85 | } |
acarter2 | 0:4a8b751dbe7c | 86 | |
acarter2 | 0:4a8b751dbe7c | 87 | bool unlock(void) |
acarter2 | 0:4a8b751dbe7c | 88 | { |
acarter2 | 0:4a8b751dbe7c | 89 | if(key == 0){ |
acarter2 | 0:4a8b751dbe7c | 90 | |
acarter2 | 0:4a8b751dbe7c | 91 | key = 1; |
acarter2 | 0:4a8b751dbe7c | 92 | return true; |
acarter2 | 0:4a8b751dbe7c | 93 | |
acarter2 | 0:4a8b751dbe7c | 94 | }else{ |
acarter2 | 0:4a8b751dbe7c | 95 | return false; |
acarter2 | 0:4a8b751dbe7c | 96 | |
acarter2 | 0:4a8b751dbe7c | 97 | } |
acarter2 | 0:4a8b751dbe7c | 98 | } |
acarter2 | 0:4a8b751dbe7c | 99 | |
acarter2 | 0:4a8b751dbe7c | 100 | bool lock(void) |
acarter2 | 0:4a8b751dbe7c | 101 | { |
acarter2 | 0:4a8b751dbe7c | 102 | if(key == 1){ |
acarter2 | 0:4a8b751dbe7c | 103 | |
acarter2 | 0:4a8b751dbe7c | 104 | key = 0; |
acarter2 | 0:4a8b751dbe7c | 105 | return true; |
acarter2 | 0:4a8b751dbe7c | 106 | |
acarter2 | 0:4a8b751dbe7c | 107 | }else{ |
acarter2 | 0:4a8b751dbe7c | 108 | return false; |
acarter2 | 0:4a8b751dbe7c | 109 | |
acarter2 | 0:4a8b751dbe7c | 110 | } |
acarter2 | 0:4a8b751dbe7c | 111 | } |
acarter2 | 0:4a8b751dbe7c | 112 | |
acarter2 | 0:4a8b751dbe7c | 113 | }; |
acarter2 | 7:cb29c20009e8 | 114 | |
acarter2 | 0:4a8b751dbe7c | 115 | class SampleRate : public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 116 | { |
acarter2 | 0:4a8b751dbe7c | 117 | public: |
acarter2 | 0:4a8b751dbe7c | 118 | /* |
acarter2 | 0:4a8b751dbe7c | 119 | /this is going the amount of time which the data sample will be taking and this will have |
acarter2 | 0:4a8b751dbe7c | 120 | /a default stating of every 15 second but can be changed |
acarter2 | 0:4a8b751dbe7c | 121 | */ |
acarter2 | 0:4a8b751dbe7c | 122 | int sampleRate; |
acarter2 | 0:4a8b751dbe7c | 123 | |
acarter2 | 0:4a8b751dbe7c | 124 | SampleRate(){ |
acarter2 | 0:4a8b751dbe7c | 125 | |
acarter2 | 0:4a8b751dbe7c | 126 | sampleRate = 15; |
acarter2 | 0:4a8b751dbe7c | 127 | |
acarter2 | 0:4a8b751dbe7c | 128 | } |
acarter2 | 0:4a8b751dbe7c | 129 | |
acarter2 | 0:4a8b751dbe7c | 130 | int getSampleRate(void) |
acarter2 | 0:4a8b751dbe7c | 131 | { |
acarter2 | 0:4a8b751dbe7c | 132 | return sampleRate; |
acarter2 | 0:4a8b751dbe7c | 133 | } |
acarter2 | 0:4a8b751dbe7c | 134 | void setSampleRate(double newSampleRate){ |
acarter2 | 0:4a8b751dbe7c | 135 | |
acarter2 | 0:4a8b751dbe7c | 136 | sampleRate = newSampleRate; |
acarter2 | 0:4a8b751dbe7c | 137 | |
acarter2 | 0:4a8b751dbe7c | 138 | } |
acarter2 | 0:4a8b751dbe7c | 139 | |
acarter2 | 0:4a8b751dbe7c | 140 | |
acarter2 | 0:4a8b751dbe7c | 141 | |
acarter2 | 0:4a8b751dbe7c | 142 | }; |
acarter2 | 7:cb29c20009e8 | 143 | |
acarter2 | 7:cb29c20009e8 | 144 | |
acarter2 | 0:4a8b751dbe7c | 145 | class TakeSample : public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 146 | { |
acarter2 | 0:4a8b751dbe7c | 147 | public: |
acarter2 | 0:4a8b751dbe7c | 148 | /* |
acarter2 | 0:4a8b751dbe7c | 149 | /this is going to take the sample or not as this can change if the person thing it could happen |
acarter2 | 0:4a8b751dbe7c | 150 | /which will see if the data is stored |
acarter2 | 0:4a8b751dbe7c | 151 | */ |
acarter2 | 0:4a8b751dbe7c | 152 | bool takeSample; |
acarter2 | 0:4a8b751dbe7c | 153 | |
acarter2 | 0:4a8b751dbe7c | 154 | TakeSample(){ |
acarter2 | 0:4a8b751dbe7c | 155 | takeSample = true; |
acarter2 | 0:4a8b751dbe7c | 156 | } |
acarter2 | 0:4a8b751dbe7c | 157 | |
acarter2 | 0:4a8b751dbe7c | 158 | int getTakeSample(void) |
acarter2 | 0:4a8b751dbe7c | 159 | { |
acarter2 | 0:4a8b751dbe7c | 160 | return takeSample; |
acarter2 | 0:4a8b751dbe7c | 161 | } |
acarter2 | 0:4a8b751dbe7c | 162 | void setTakeSample(bool newTakeSample){ |
acarter2 | 0:4a8b751dbe7c | 163 | |
acarter2 | 0:4a8b751dbe7c | 164 | takeSample = newTakeSample; |
acarter2 | 0:4a8b751dbe7c | 165 | |
acarter2 | 0:4a8b751dbe7c | 166 | } |
acarter2 | 0:4a8b751dbe7c | 167 | }; |
acarter2 | 7:cb29c20009e8 | 168 | |
acarter2 | 7:cb29c20009e8 | 169 | |
acarter2 | 7:cb29c20009e8 | 170 | |
acarter2 | 0:4a8b751dbe7c | 171 | class SampleTemp: public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 172 | { |
acarter2 | 0:4a8b751dbe7c | 173 | public: |
acarter2 | 0:4a8b751dbe7c | 174 | |
acarter2 | 0:4a8b751dbe7c | 175 | SampleTemp(){} |
acarter2 | 0:4a8b751dbe7c | 176 | float currentTemp; |
acarter2 | 0:4a8b751dbe7c | 177 | /* |
acarter2 | 0:4a8b751dbe7c | 178 | /this is the fuction which will get the humidity for the external devices |
acarter2 | 0:4a8b751dbe7c | 179 | */ |
acarter2 | 0:4a8b751dbe7c | 180 | float getTemp(void) |
acarter2 | 0:4a8b751dbe7c | 181 | { |
acarter2 | 0:4a8b751dbe7c | 182 | temp_sensor1->GetTemperature(¤tTemp); |
acarter2 | 0:4a8b751dbe7c | 183 | return currentTemp; |
acarter2 | 0:4a8b751dbe7c | 184 | } |
acarter2 | 0:4a8b751dbe7c | 185 | }; |
acarter2 | 7:cb29c20009e8 | 186 | |
acarter2 | 0:4a8b751dbe7c | 187 | class SampleHumitaty: public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 188 | { |
acarter2 | 0:4a8b751dbe7c | 189 | public: |
acarter2 | 0:4a8b751dbe7c | 190 | |
acarter2 | 0:4a8b751dbe7c | 191 | SampleHumitaty(){} |
acarter2 | 0:4a8b751dbe7c | 192 | |
acarter2 | 0:4a8b751dbe7c | 193 | float currentHumitaty; |
acarter2 | 0:4a8b751dbe7c | 194 | /* |
acarter2 | 0:4a8b751dbe7c | 195 | /this is the fuction which will get the humidity for the external devices |
acarter2 | 0:4a8b751dbe7c | 196 | */ |
acarter2 | 0:4a8b751dbe7c | 197 | float getHumidity() |
acarter2 | 0:4a8b751dbe7c | 198 | { |
acarter2 | 0:4a8b751dbe7c | 199 | humidity_sensor->GetHumidity(¤tHumitaty); |
acarter2 | 0:4a8b751dbe7c | 200 | return currentHumitaty; |
acarter2 | 0:4a8b751dbe7c | 201 | |
acarter2 | 0:4a8b751dbe7c | 202 | } |
acarter2 | 0:4a8b751dbe7c | 203 | }; |
acarter2 | 7:cb29c20009e8 | 204 | |
acarter2 | 7:cb29c20009e8 | 205 | |
acarter2 | 0:4a8b751dbe7c | 206 | class SamplePressure: public KeyLocks |
acarter2 | 7:cb29c20009e8 | 207 | |
acarter2 | 0:4a8b751dbe7c | 208 | { |
acarter2 | 0:4a8b751dbe7c | 209 | public: |
acarter2 | 0:4a8b751dbe7c | 210 | |
acarter2 | 0:4a8b751dbe7c | 211 | SamplePressure(){} |
acarter2 | 0:4a8b751dbe7c | 212 | |
acarter2 | 0:4a8b751dbe7c | 213 | float currentPressure; |
acarter2 | 0:4a8b751dbe7c | 214 | /* |
acarter2 | 0:4a8b751dbe7c | 215 | /this is the fuction which will get the pressure for the external devices |
acarter2 | 0:4a8b751dbe7c | 216 | */ |
acarter2 | 0:4a8b751dbe7c | 217 | float getPressure() |
acarter2 | 0:4a8b751dbe7c | 218 | { |
acarter2 | 0:4a8b751dbe7c | 219 | pressure_sensor->GetPressure(¤tPressure); |
acarter2 | 0:4a8b751dbe7c | 220 | return currentPressure; |
acarter2 | 0:4a8b751dbe7c | 221 | } |
acarter2 | 0:4a8b751dbe7c | 222 | }; |
acarter2 | 7:cb29c20009e8 | 223 | |
acarter2 | 0:4a8b751dbe7c | 224 | class SampleDisplay: public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 225 | { |
acarter2 | 0:4a8b751dbe7c | 226 | public: |
acarter2 | 0:4a8b751dbe7c | 227 | /* |
acarter2 | 0:4a8b751dbe7c | 228 | /this is the variable to see if the data will be display or not after the data being sorted |
acarter2 | 0:4a8b751dbe7c | 229 | /this can change by the user input |
acarter2 | 0:4a8b751dbe7c | 230 | */ |
acarter2 | 7:cb29c20009e8 | 231 | |
acarter2 | 0:4a8b751dbe7c | 232 | bool display; |
acarter2 | 0:4a8b751dbe7c | 233 | |
acarter2 | 0:4a8b751dbe7c | 234 | SampleDisplay(){ |
acarter2 | 0:4a8b751dbe7c | 235 | display = false; |
acarter2 | 0:4a8b751dbe7c | 236 | } |
acarter2 | 0:4a8b751dbe7c | 237 | |
acarter2 | 0:4a8b751dbe7c | 238 | bool getTakeDisplay(void) |
acarter2 | 0:4a8b751dbe7c | 239 | { |
acarter2 | 0:4a8b751dbe7c | 240 | return display; |
acarter2 | 0:4a8b751dbe7c | 241 | } |
acarter2 | 0:4a8b751dbe7c | 242 | void setDisplay(bool newSampleDisplay){ |
acarter2 | 0:4a8b751dbe7c | 243 | |
acarter2 | 0:4a8b751dbe7c | 244 | display = newSampleDisplay; |
acarter2 | 0:4a8b751dbe7c | 245 | |
acarter2 | 0:4a8b751dbe7c | 246 | } |
acarter2 | 0:4a8b751dbe7c | 247 | }; |
acarter2 | 7:cb29c20009e8 | 248 | |
acarter2 | 0:4a8b751dbe7c | 249 | |
acarter2 | 0:4a8b751dbe7c | 250 | |
acarter2 | 0:4a8b751dbe7c | 251 | |
acarter2 | 0:4a8b751dbe7c | 252 | class BufferStartPointer: public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 253 | { |
acarter2 | 0:4a8b751dbe7c | 254 | public: |
acarter2 | 0:4a8b751dbe7c | 255 | /* |
acarter2 | 0:4a8b751dbe7c | 256 | /this is the pointer to start of the buffer as this will make the buffer be cycler |
acarter2 | 0:4a8b751dbe7c | 257 | */ |
acarter2 | 0:4a8b751dbe7c | 258 | int bufferStartPointer; |
acarter2 | 7:cb29c20009e8 | 259 | int bufferSize; |
acarter2 | 0:4a8b751dbe7c | 260 | |
acarter2 | 7:cb29c20009e8 | 261 | BufferStartPointer(int newBufferSize){ |
acarter2 | 0:4a8b751dbe7c | 262 | bufferStartPointer = 0; |
acarter2 | 7:cb29c20009e8 | 263 | bufferSize = newBufferSize; |
acarter2 | 0:4a8b751dbe7c | 264 | } |
acarter2 | 0:4a8b751dbe7c | 265 | |
acarter2 | 0:4a8b751dbe7c | 266 | int getBufferStartPointer (void) |
acarter2 | 0:4a8b751dbe7c | 267 | { |
acarter2 | 3:f61ae69ed662 | 268 | key = 1; |
acarter2 | 0:4a8b751dbe7c | 269 | return bufferStartPointer; |
acarter2 | 3:f61ae69ed662 | 270 | |
acarter2 | 0:4a8b751dbe7c | 271 | } |
acarter2 | 0:4a8b751dbe7c | 272 | void setBufferStartPointer (int newBufferStartPointer ){ |
acarter2 | 0:4a8b751dbe7c | 273 | |
acarter2 | 0:4a8b751dbe7c | 274 | bufferStartPointer = newBufferStartPointer; |
acarter2 | 0:4a8b751dbe7c | 275 | |
acarter2 | 1:4cd93767691a | 276 | } |
acarter2 | 1:4cd93767691a | 277 | void plusOne(void){ |
acarter2 | 7:cb29c20009e8 | 278 | if(bufferStartPointer + 1 == bufferSize){ |
acarter2 | 1:4cd93767691a | 279 | bufferStartPointer = 0; |
acarter2 | 1:4cd93767691a | 280 | }else{ |
acarter2 | 1:4cd93767691a | 281 | bufferStartPointer = bufferStartPointer + 1; |
acarter2 | 1:4cd93767691a | 282 | } |
acarter2 | 1:4cd93767691a | 283 | } |
acarter2 | 0:4a8b751dbe7c | 284 | }; |
acarter2 | 7:cb29c20009e8 | 285 | |
acarter2 | 7:cb29c20009e8 | 286 | |
acarter2 | 0:4a8b751dbe7c | 287 | class BufferEndPointer: public KeyLocks |
acarter2 | 0:4a8b751dbe7c | 288 | { |
acarter2 | 0:4a8b751dbe7c | 289 | public: |
acarter2 | 0:4a8b751dbe7c | 290 | /* |
acarter2 | 0:4a8b751dbe7c | 291 | /this is the pointer to End of the buffer as this will make the buffer be cycler |
acarter2 | 0:4a8b751dbe7c | 292 | */ |
acarter2 | 0:4a8b751dbe7c | 293 | int bufferEndPointer; |
acarter2 | 0:4a8b751dbe7c | 294 | |
acarter2 | 7:cb29c20009e8 | 295 | int bufferSize; |
acarter2 | 7:cb29c20009e8 | 296 | |
acarter2 | 7:cb29c20009e8 | 297 | BufferEndPointer(int newBufferSize){ |
acarter2 | 7:cb29c20009e8 | 298 | bufferEndPointer = 0; |
acarter2 | 7:cb29c20009e8 | 299 | bufferSize = newBufferSize; |
acarter2 | 7:cb29c20009e8 | 300 | } |
acarter2 | 7:cb29c20009e8 | 301 | |
acarter2 | 1:4cd93767691a | 302 | |
acarter2 | 0:4a8b751dbe7c | 303 | BufferEndPointer(){ |
acarter2 | 0:4a8b751dbe7c | 304 | bufferEndPointer = 0; |
acarter2 | 0:4a8b751dbe7c | 305 | } |
acarter2 | 0:4a8b751dbe7c | 306 | |
acarter2 | 0:4a8b751dbe7c | 307 | int getBufferEndPointer (void) |
acarter2 | 0:4a8b751dbe7c | 308 | { |
acarter2 | 0:4a8b751dbe7c | 309 | return bufferEndPointer; |
acarter2 | 0:4a8b751dbe7c | 310 | } |
acarter2 | 0:4a8b751dbe7c | 311 | void setBufferEndPointer (int newBufferEndPointer ){ |
acarter2 | 0:4a8b751dbe7c | 312 | |
acarter2 | 0:4a8b751dbe7c | 313 | bufferEndPointer = newBufferEndPointer; |
acarter2 | 0:4a8b751dbe7c | 314 | |
acarter2 | 1:4cd93767691a | 315 | } |
acarter2 | 1:4cd93767691a | 316 | |
acarter2 | 1:4cd93767691a | 317 | void plusOne(void){ |
acarter2 | 7:cb29c20009e8 | 318 | if(bufferEndPointer + 1 == bufferSize){ |
acarter2 | 1:4cd93767691a | 319 | bufferEndPointer = 0; |
acarter2 | 1:4cd93767691a | 320 | }else{ |
acarter2 | 1:4cd93767691a | 321 | bufferEndPointer = bufferEndPointer + 1; |
acarter2 | 1:4cd93767691a | 322 | } |
acarter2 | 1:4cd93767691a | 323 | } |
acarter2 | 1:4cd93767691a | 324 | }; |
acarter2 | 7:cb29c20009e8 | 325 | |
acarter2 | 1:4cd93767691a | 326 | class BufferSize: public KeyLocks |
acarter2 | 1:4cd93767691a | 327 | { |
acarter2 | 1:4cd93767691a | 328 | |
acarter2 | 1:4cd93767691a | 329 | public: |
acarter2 | 1:4cd93767691a | 330 | int bufferSize; |
acarter2 | 1:4cd93767691a | 331 | Semaphore *EmptyBuffer; |
acarter2 | 1:4cd93767691a | 332 | Semaphore *fullBuffer; |
acarter2 | 1:4cd93767691a | 333 | |
acarter2 | 1:4cd93767691a | 334 | BufferSize(){ |
acarter2 | 1:4cd93767691a | 335 | EmptyBuffer = new Semaphore(0); |
acarter2 | 3:f61ae69ed662 | 336 | fullBuffer = new Semaphore(BUFFER_SIZE); |
acarter2 | 1:4cd93767691a | 337 | bufferSize = 0; |
acarter2 | 1:4cd93767691a | 338 | |
acarter2 | 1:4cd93767691a | 339 | } |
acarter2 | 1:4cd93767691a | 340 | |
acarter2 | 1:4cd93767691a | 341 | void increaseBufferSize(){ |
acarter2 | 1:4cd93767691a | 342 | bufferSize = bufferSize + 1; |
acarter2 | 1:4cd93767691a | 343 | } |
acarter2 | 1:4cd93767691a | 344 | |
acarter2 | 1:4cd93767691a | 345 | void deincreaseBufferSize(){ |
acarter2 | 1:4cd93767691a | 346 | bufferSize = bufferSize - 1; |
acarter2 | 1:4cd93767691a | 347 | } |
acarter2 | 1:4cd93767691a | 348 | |
acarter2 | 1:4cd93767691a | 349 | int getBufferSize(){ |
acarter2 | 1:4cd93767691a | 350 | return bufferSize; |
acarter2 | 1:4cd93767691a | 351 | } |
acarter2 | 1:4cd93767691a | 352 | |
acarter2 | 1:4cd93767691a | 353 | int getEmptyBuffer(){ |
acarter2 | 1:4cd93767691a | 354 | return EmptyBuffer -> wait(); |
acarter2 | 1:4cd93767691a | 355 | } |
acarter2 | 1:4cd93767691a | 356 | |
acarter2 | 1:4cd93767691a | 357 | int getFullBuffer(){ |
acarter2 | 1:4cd93767691a | 358 | return fullBuffer -> wait(); |
acarter2 | 1:4cd93767691a | 359 | } |
acarter2 | 1:4cd93767691a | 360 | |
acarter2 | 1:4cd93767691a | 361 | void releaseEmptyBuffer(){ |
acarter2 | 3:f61ae69ed662 | 362 | EmptyBuffer -> release(); |
acarter2 | 1:4cd93767691a | 363 | } |
acarter2 | 1:4cd93767691a | 364 | |
acarter2 | 1:4cd93767691a | 365 | void releaseFullBuffer(){ |
acarter2 | 3:f61ae69ed662 | 366 | fullBuffer -> release(); |
acarter2 | 1:4cd93767691a | 367 | } |
acarter2 | 1:4cd93767691a | 368 | |
acarter2 | 1:4cd93767691a | 369 | |
acarter2 | 0:4a8b751dbe7c | 370 | }; |
acarter2 | 0:4a8b751dbe7c | 371 | |
acarter2 | 7:cb29c20009e8 | 372 | |
acarter2 | 7:cb29c20009e8 | 373 | class SampleString: public KeyLocks |
acarter2 | 7:cb29c20009e8 | 374 | { |
acarter2 | 7:cb29c20009e8 | 375 | public: |
acarter2 | 8:aec689bd13c0 | 376 | char word[100]; |
acarter2 | 7:cb29c20009e8 | 377 | |
acarter2 | 7:cb29c20009e8 | 378 | SampleString(){} |
acarter2 | 7:cb29c20009e8 | 379 | |
acarter2 | 7:cb29c20009e8 | 380 | char* getWord(){ |
acarter2 | 8:aec689bd13c0 | 381 | return word; |
acarter2 | 7:cb29c20009e8 | 382 | } |
acarter2 | 7:cb29c20009e8 | 383 | |
acarter2 | 8:aec689bd13c0 | 384 | void setWord(char* newWord){ |
acarter2 | 7:cb29c20009e8 | 385 | |
acarter2 | 8:aec689bd13c0 | 386 | //word = newWord; |
acarter2 | 7:cb29c20009e8 | 387 | |
acarter2 | 7:cb29c20009e8 | 388 | } |
acarter2 | 7:cb29c20009e8 | 389 | |
acarter2 | 7:cb29c20009e8 | 390 | }; |
acarter2 | 7:cb29c20009e8 | 391 | |
acarter2 | 7:cb29c20009e8 | 392 | |
acarter2 | 7:cb29c20009e8 | 393 | SampleTemp sampleTemp = SampleTemp(); |
acarter2 | 7:cb29c20009e8 | 394 | SampleHumitaty sampleHumitaty = SampleHumitaty(); |
acarter2 | 7:cb29c20009e8 | 395 | SamplePressure samplePressure = SamplePressure(); |
acarter2 | 7:cb29c20009e8 | 396 | SampleString samplString = SampleString(); |
acarter2 | 7:cb29c20009e8 | 397 | |
acarter2 | 7:cb29c20009e8 | 398 | SampleRate sampleRate = SampleRate(); |
acarter2 | 7:cb29c20009e8 | 399 | TakeSample takeSample = TakeSample(); |
acarter2 | 7:cb29c20009e8 | 400 | SampleDisplay sampleDisplay = SampleDisplay(); |
acarter2 | 7:cb29c20009e8 | 401 | |
acarter2 | 7:cb29c20009e8 | 402 | BufferStartPointer bufferStartPointer = BufferStartPointer(BUFFER_SIZE); |
acarter2 | 7:cb29c20009e8 | 403 | BufferEndPointer ramEndPointer = BufferEndPointer(AMOUNT_OF_SAMPLE); |
acarter2 | 7:cb29c20009e8 | 404 | BufferEndPointer bufferEndPointer = BufferEndPointer(BUFFER_SIZE); |
acarter2 | 7:cb29c20009e8 | 405 | BufferSize bufferSize = BufferSize(); |
acarter2 | 7:cb29c20009e8 | 406 | |
acarter2 | 2:c5fb78148ccc | 407 | /**void initialiseBufferData(){ |
acarter2 | 0:4a8b751dbe7c | 408 | //create mutex lock |
acarter2 | 0:4a8b751dbe7c | 409 | pthread_mutex_init(&mutex, NULL); |
acarter2 | 0:4a8b751dbe7c | 410 | |
acarter2 | 0:4a8b751dbe7c | 411 | //create full semaphore and initialise to 0 |
acarter2 | 0:4a8b751dbe7c | 412 | sem_init(&full, 0, 0); |
acarter2 | 0:4a8b751dbe7c | 413 | |
acarter2 | 0:4a8b751dbe7c | 414 | //create empty semaphore and initialise to BUFFER_SIZE |
acarter2 | 0:4a8b751dbe7c | 415 | sem_init(&empty, 0, BUFFER_SIZE); |
acarter2 | 0:4a8b751dbe7c | 416 | |
acarter2 | 0:4a8b751dbe7c | 417 | //get default attributes |
acarter2 | 0:4a8b751dbe7c | 418 | pthread_attr_init(&attr); |
acarter2 | 0:4a8b751dbe7c | 419 | |
acarter2 | 0:4a8b751dbe7c | 420 | //init buffer |
acarter2 | 0:4a8b751dbe7c | 421 | counter = 0; |
acarter2 | 2:c5fb78148ccc | 422 | }**/ |
acarter2 | 7:cb29c20009e8 | 423 | |
acarter2 | 7:cb29c20009e8 | 424 | |
acarter2 | 7:cb29c20009e8 | 425 | |
acarter2 | 7:cb29c20009e8 | 426 | /* |
acarter2 | 7:cb29c20009e8 | 427 | /read Record is the selected data which be passed in as a a string |
acarter2 | 7:cb29c20009e8 | 428 | */ |
acarter2 | 7:cb29c20009e8 | 429 | void printCurrentString(char* displayString, int length) |
acarter2 | 7:cb29c20009e8 | 430 | { |
acarter2 | 7:cb29c20009e8 | 431 | printf("%s\n", displayString); |
acarter2 | 7:cb29c20009e8 | 432 | |
acarter2 | 7:cb29c20009e8 | 433 | |
acarter2 | 7:cb29c20009e8 | 434 | } |
acarter2 | 0:4a8b751dbe7c | 435 | |
acarter2 | 7:cb29c20009e8 | 436 | /* |
acarter2 | 7:cb29c20009e8 | 437 | /read Record is the selected data which be passed in as a a string |
acarter2 | 7:cb29c20009e8 | 438 | */ |
acarter2 | 8:aec689bd13c0 | 439 | void printCurrentRecord(void const *arg) |
acarter2 | 7:cb29c20009e8 | 440 | { |
acarter2 | 7:cb29c20009e8 | 441 | //strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &time); |
acarter2 | 8:aec689bd13c0 | 442 | printf("\n%s\n", outputSample); |
acarter2 | 7:cb29c20009e8 | 443 | |
acarter2 | 7:cb29c20009e8 | 444 | |
acarter2 | 7:cb29c20009e8 | 445 | } |
acarter2 | 0:4a8b751dbe7c | 446 | //producer thread |
acarter2 | 8:aec689bd13c0 | 447 | void producer(void const *arg){ |
acarter2 | 1:4cd93767691a | 448 | |
acarter2 | 7:cb29c20009e8 | 449 | while(!bufferSize.lock()){}; |
acarter2 | 0:4a8b751dbe7c | 450 | |
acarter2 | 7:cb29c20009e8 | 451 | int size = bufferSize.getBufferSize(); |
acarter2 | 1:4cd93767691a | 452 | |
acarter2 | 7:cb29c20009e8 | 453 | if(size < BUFFER_SIZE){ |
acarter2 | 0:4a8b751dbe7c | 454 | //sleep for random amount of time |
acarter2 | 0:4a8b751dbe7c | 455 | |
acarter2 | 1:4cd93767691a | 456 | //get the buffer |
acarter2 | 1:4cd93767691a | 457 | bufferLock->lock(); |
acarter2 | 7:cb29c20009e8 | 458 | |
acarter2 | 1:4cd93767691a | 459 | |
acarter2 | 7:cb29c20009e8 | 460 | while(!bufferEndPointer.lock()){}; |
acarter2 | 7:cb29c20009e8 | 461 | bufferEndPointer.plusOne(); |
acarter2 | 7:cb29c20009e8 | 462 | const int bufferIndex = bufferEndPointer.getBufferEndPointer(); |
acarter2 | 7:cb29c20009e8 | 463 | bufferEndPointer.unlock(); |
acarter2 | 7:cb29c20009e8 | 464 | |
acarter2 | 8:aec689bd13c0 | 465 | //while(!samplString.lock()){}; |
acarter2 | 8:aec689bd13c0 | 466 | buffer_item[bufferIndex] = *outputSample; |
acarter2 | 8:aec689bd13c0 | 467 | //samplString.unlock(); |
acarter2 | 1:4cd93767691a | 468 | bufferLock->unlock(); |
acarter2 | 7:cb29c20009e8 | 469 | |
acarter2 | 7:cb29c20009e8 | 470 | while(!bufferSize.lock()){}; |
acarter2 | 7:cb29c20009e8 | 471 | bufferSize.increaseBufferSize(); |
acarter2 | 8:aec689bd13c0 | 472 | bufferSize.unlock(); |
acarter2 | 1:4cd93767691a | 473 | |
acarter2 | 0:4a8b751dbe7c | 474 | } |
acarter2 | 8:aec689bd13c0 | 475 | //Thread consumerTread(consumer); |
acarter2 | 8:aec689bd13c0 | 476 | |
acarter2 | 8:aec689bd13c0 | 477 | //Thread::terminate; |
acarter2 | 0:4a8b751dbe7c | 478 | } |
acarter2 | 7:cb29c20009e8 | 479 | |
acarter2 | 0:4a8b751dbe7c | 480 | //consumer thread |
acarter2 | 8:aec689bd13c0 | 481 | void consumer(void const *arg){ |
acarter2 | 2:c5fb78148ccc | 482 | |
acarter2 | 7:cb29c20009e8 | 483 | while(!bufferSize.lock()){}; |
acarter2 | 2:c5fb78148ccc | 484 | |
acarter2 | 7:cb29c20009e8 | 485 | int size = bufferSize.getBufferSize(); |
acarter2 | 2:c5fb78148ccc | 486 | |
acarter2 | 2:c5fb78148ccc | 487 | while(size > 0){ |
acarter2 | 2:c5fb78148ccc | 488 | //sleep for random amount of time |
acarter2 | 2:c5fb78148ccc | 489 | |
acarter2 | 2:c5fb78148ccc | 490 | //get the buffer |
acarter2 | 2:c5fb78148ccc | 491 | bufferLock->lock(); |
acarter2 | 2:c5fb78148ccc | 492 | |
acarter2 | 7:cb29c20009e8 | 493 | while(!ramEndPointer.lock()){}; |
acarter2 | 2:c5fb78148ccc | 494 | |
acarter2 | 7:cb29c20009e8 | 495 | int ramIndex = ramEndPointer.getBufferEndPointer(); |
acarter2 | 2:c5fb78148ccc | 496 | |
acarter2 | 7:cb29c20009e8 | 497 | ramEndPointer.unlock(); |
acarter2 | 2:c5fb78148ccc | 498 | |
acarter2 | 7:cb29c20009e8 | 499 | while(!bufferStartPointer.lock()){}; |
acarter2 | 2:c5fb78148ccc | 500 | |
acarter2 | 7:cb29c20009e8 | 501 | bufferStartPointer.plusOne(); |
acarter2 | 3:f61ae69ed662 | 502 | |
acarter2 | 3:f61ae69ed662 | 503 | |
acarter2 | 7:cb29c20009e8 | 504 | memorySample[ramIndex] = buffer_item[bufferStartPointer.getBufferStartPointer()]; |
acarter2 | 3:f61ae69ed662 | 505 | |
acarter2 | 3:f61ae69ed662 | 506 | |
acarter2 | 3:f61ae69ed662 | 507 | |
acarter2 | 7:cb29c20009e8 | 508 | bufferStartPointer.unlock(); |
acarter2 | 2:c5fb78148ccc | 509 | bufferLock->unlock(); |
acarter2 | 2:c5fb78148ccc | 510 | |
acarter2 | 2:c5fb78148ccc | 511 | |
acarter2 | 7:cb29c20009e8 | 512 | while(!bufferSize.lock()){}; |
acarter2 | 7:cb29c20009e8 | 513 | bufferSize.deincreaseBufferSize(); |
acarter2 | 7:cb29c20009e8 | 514 | size = bufferSize.getBufferSize(); |
acarter2 | 2:c5fb78148ccc | 515 | |
acarter2 | 2:c5fb78148ccc | 516 | |
acarter2 | 8:aec689bd13c0 | 517 | } |
acarter2 | 0:4a8b751dbe7c | 518 | } |
acarter2 | 0:4a8b751dbe7c | 519 | |
acarter2 | 0:4a8b751dbe7c | 520 | |
acarter2 | 8:aec689bd13c0 | 521 | |
acarter2 | 7:cb29c20009e8 | 522 | static char *printDouble(char* str, double v, int decimalDigits=2) |
acarter2 | 7:cb29c20009e8 | 523 | { |
acarter2 | 7:cb29c20009e8 | 524 | int i = 1; |
acarter2 | 7:cb29c20009e8 | 525 | int intPart, fractPart; |
acarter2 | 7:cb29c20009e8 | 526 | int len; |
acarter2 | 7:cb29c20009e8 | 527 | char *ptr; |
acarter2 | 7:cb29c20009e8 | 528 | |
acarter2 | 7:cb29c20009e8 | 529 | /* prepare decimal digits multiplicator */ |
acarter2 | 7:cb29c20009e8 | 530 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
acarter2 | 7:cb29c20009e8 | 531 | |
acarter2 | 7:cb29c20009e8 | 532 | /* calculate integer & fractinal parts */ |
acarter2 | 7:cb29c20009e8 | 533 | intPart = (int)v; |
acarter2 | 7:cb29c20009e8 | 534 | fractPart = (int)((v-(double)(int)v)*i); |
acarter2 | 7:cb29c20009e8 | 535 | |
acarter2 | 7:cb29c20009e8 | 536 | /* fill in integer part */ |
acarter2 | 7:cb29c20009e8 | 537 | sprintf(str, "%i.", intPart); |
acarter2 | 7:cb29c20009e8 | 538 | |
acarter2 | 7:cb29c20009e8 | 539 | /* prepare fill in of fractional part */ |
acarter2 | 7:cb29c20009e8 | 540 | len = strlen(str); |
acarter2 | 7:cb29c20009e8 | 541 | ptr = &str[len]; |
acarter2 | 7:cb29c20009e8 | 542 | |
acarter2 | 7:cb29c20009e8 | 543 | /* fill in leading fractional zeros */ |
acarter2 | 7:cb29c20009e8 | 544 | for (i/=10;i>1; i/=10, ptr++) { |
acarter2 | 7:cb29c20009e8 | 545 | if(fractPart >= i) break; |
acarter2 | 7:cb29c20009e8 | 546 | *ptr = '0'; |
acarter2 | 7:cb29c20009e8 | 547 | } |
acarter2 | 7:cb29c20009e8 | 548 | |
acarter2 | 7:cb29c20009e8 | 549 | /* fill in (rest of) fractional part */ |
acarter2 | 7:cb29c20009e8 | 550 | sprintf(ptr, "%i", fractPart); |
acarter2 | 7:cb29c20009e8 | 551 | |
acarter2 | 7:cb29c20009e8 | 552 | return str; |
acarter2 | 7:cb29c20009e8 | 553 | } |
acarter2 | 7:cb29c20009e8 | 554 | |
acarter2 | 7:cb29c20009e8 | 555 | /* |
acarter2 | 7:cb29c20009e8 | 556 | /this is going to sort the string to see the opertion which will picked the opertion which |
acarter2 | 7:cb29c20009e8 | 557 | /need the be carried out and the put the data in to the tread to then which the tread will |
acarter2 | 7:cb29c20009e8 | 558 | /do it operation |
acarter2 | 7:cb29c20009e8 | 559 | */ |
acarter2 | 7:cb29c20009e8 | 560 | void sortString(char* inputString) |
acarter2 | 7:cb29c20009e8 | 561 | { |
acarter2 | 7:cb29c20009e8 | 562 | |
acarter2 | 7:cb29c20009e8 | 563 | //if("DELETE" == values[0]){ |
acarter2 | 7:cb29c20009e8 | 564 | |
acarter2 | 7:cb29c20009e8 | 565 | //printCurrentRecord(values[]); |
acarter2 | 7:cb29c20009e8 | 566 | |
acarter2 | 7:cb29c20009e8 | 567 | //} |
acarter2 | 7:cb29c20009e8 | 568 | |
acarter2 | 7:cb29c20009e8 | 569 | //https://developer.mbed.org/teams/Students-Plymouth-University-UK-SoCEM/ |
acarter2 | 7:cb29c20009e8 | 570 | |
acarter2 | 7:cb29c20009e8 | 571 | } |
acarter2 | 7:cb29c20009e8 | 572 | |
acarter2 | 7:cb29c20009e8 | 573 | |
acarter2 | 7:cb29c20009e8 | 574 | |
acarter2 | 7:cb29c20009e8 | 575 | |
acarter2 | 7:cb29c20009e8 | 576 | /* |
acarter2 | 7:cb29c20009e8 | 577 | /this is function that will written to the memory of the from the buffer so that will be fast |
acarter2 | 7:cb29c20009e8 | 578 | / and did not lose anydata |
acarter2 | 7:cb29c20009e8 | 579 | */ |
acarter2 | 7:cb29c20009e8 | 580 | void writtenToMemory(const void* arg ) |
acarter2 | 7:cb29c20009e8 | 581 | { |
acarter2 | 7:cb29c20009e8 | 582 | |
acarter2 | 7:cb29c20009e8 | 583 | } |
acarter2 | 7:cb29c20009e8 | 584 | |
acarter2 | 7:cb29c20009e8 | 585 | |
acarter2 | 7:cb29c20009e8 | 586 | //this is setting all the function for the treads in the program |
acarter2 | 7:cb29c20009e8 | 587 | |
acarter2 | 7:cb29c20009e8 | 588 | |
acarter2 | 7:cb29c20009e8 | 589 | |
acarter2 | 0:4a8b751dbe7c | 590 | |
acarter2 | 7:cb29c20009e8 | 591 | |
acarter2 | 0:4a8b751dbe7c | 592 | /* |
acarter2 | 0:4a8b751dbe7c | 593 | /this is fucntion which the important the updateSample rate |
acarter2 | 0:4a8b751dbe7c | 594 | */ |
acarter2 | 7:cb29c20009e8 | 595 | void updateSample(void const *arg) |
acarter2 | 0:4a8b751dbe7c | 596 | { |
acarter2 | 7:cb29c20009e8 | 597 | bool continueSampling; |
acarter2 | 7:cb29c20009e8 | 598 | |
acarter2 | 7:cb29c20009e8 | 599 | do{ |
acarter2 | 7:cb29c20009e8 | 600 | while(samplePressure.lock()){}; |
acarter2 | 7:cb29c20009e8 | 601 | float currentPressure = samplePressure.getPressure(); |
acarter2 | 7:cb29c20009e8 | 602 | samplePressure.unlock(); |
acarter2 | 7:cb29c20009e8 | 603 | |
acarter2 | 7:cb29c20009e8 | 604 | char samplePress[10]; |
acarter2 | 8:aec689bd13c0 | 605 | printDouble(samplePress, currentPressure, 2); |
acarter2 | 7:cb29c20009e8 | 606 | |
acarter2 | 7:cb29c20009e8 | 607 | |
acarter2 | 7:cb29c20009e8 | 608 | while(sampleHumitaty.lock()){}; |
acarter2 | 7:cb29c20009e8 | 609 | |
acarter2 | 7:cb29c20009e8 | 610 | float current= sampleHumitaty.getHumidity(); |
acarter2 | 7:cb29c20009e8 | 611 | sampleHumitaty.unlock(); |
acarter2 | 7:cb29c20009e8 | 612 | |
acarter2 | 7:cb29c20009e8 | 613 | char sampleHum[10]; |
acarter2 | 7:cb29c20009e8 | 614 | printDouble(sampleHum, current, 2); |
acarter2 | 7:cb29c20009e8 | 615 | |
acarter2 | 7:cb29c20009e8 | 616 | while(sampleTemp.lock()){}; |
acarter2 | 0:4a8b751dbe7c | 617 | |
acarter2 | 7:cb29c20009e8 | 618 | float currentTemp= sampleTemp.getTemp(); |
acarter2 | 7:cb29c20009e8 | 619 | sampleTemp.unlock(); |
acarter2 | 7:cb29c20009e8 | 620 | |
acarter2 | 7:cb29c20009e8 | 621 | char sampleTemp[10]; |
acarter2 | 7:cb29c20009e8 | 622 | printDouble(sampleTemp, currentTemp, 2); |
acarter2 | 7:cb29c20009e8 | 623 | |
acarter2 | 7:cb29c20009e8 | 624 | //time_t currentTime; |
acarter2 | 7:cb29c20009e8 | 625 | //time(¤tTime); |
acarter2 | 7:cb29c20009e8 | 626 | //struct tm tstruct = *localtime(¤tTime); |
acarter2 | 7:cb29c20009e8 | 627 | |
acarter2 | 7:cb29c20009e8 | 628 | |
acarter2 | 7:cb29c20009e8 | 629 | while(takeSample.lock()){}; |
acarter2 | 7:cb29c20009e8 | 630 | |
acarter2 | 7:cb29c20009e8 | 631 | continueSampling = takeSample.getTakeSample(); |
acarter2 | 7:cb29c20009e8 | 632 | takeSample.unlock(); |
acarter2 | 7:cb29c20009e8 | 633 | |
acarter2 | 7:cb29c20009e8 | 634 | int amountOfsec = sampleRate.getSampleRate(); |
acarter2 | 7:cb29c20009e8 | 635 | |
acarter2 | 8:aec689bd13c0 | 636 | sprintf(outputSample, "Pressure : %s, Temp : %s, Hum : %s Time: %d seconds", samplePress, sampleTemp, sampleHum, secondsOfsample); |
acarter2 | 8:aec689bd13c0 | 637 | |
acarter2 | 8:aec689bd13c0 | 638 | Thread sampleDisplayTread(printCurrentRecord); |
acarter2 | 8:aec689bd13c0 | 639 | Thread producerTread(producer); |
acarter2 | 7:cb29c20009e8 | 640 | |
acarter2 | 7:cb29c20009e8 | 641 | secondsOfsample = secondsOfsample + amountOfsec; |
acarter2 | 7:cb29c20009e8 | 642 | |
acarter2 | 7:cb29c20009e8 | 643 | |
acarter2 | 8:aec689bd13c0 | 644 | Thread::wait(amountOfsec*1000); |
acarter2 | 7:cb29c20009e8 | 645 | |
acarter2 | 7:cb29c20009e8 | 646 | |
acarter2 | 7:cb29c20009e8 | 647 | |
acarter2 | 7:cb29c20009e8 | 648 | }while (continueSampling); |
acarter2 | 0:4a8b751dbe7c | 649 | |
acarter2 | 0:4a8b751dbe7c | 650 | |
acarter2 | 0:4a8b751dbe7c | 651 | |
acarter2 | 0:4a8b751dbe7c | 652 | |
acarter2 | 7:cb29c20009e8 | 653 | |
acarter2 | 0:4a8b751dbe7c | 654 | |
acarter2 | 0:4a8b751dbe7c | 655 | } |
acarter2 | 7:cb29c20009e8 | 656 | |
acarter2 | 7:cb29c20009e8 | 657 | |
acarter2 | 0:4a8b751dbe7c | 658 | void splitString(char inputString[]){ |
acarter2 | 0:4a8b751dbe7c | 659 | int sizeOfArray = 1000; |
acarter2 | 0:4a8b751dbe7c | 660 | char opertaion[sizeOfArray]; |
acarter2 | 0:4a8b751dbe7c | 661 | char data[sizeOfArray]; |
acarter2 | 0:4a8b751dbe7c | 662 | |
acarter2 | 0:4a8b751dbe7c | 663 | for (int i = 0; i < 20; i++){ |
acarter2 | 0:4a8b751dbe7c | 664 | for (int j = 0; i < 20; j++){ |
acarter2 | 0:4a8b751dbe7c | 665 | values[i][j] =' '; |
acarter2 | 0:4a8b751dbe7c | 666 | |
acarter2 | 0:4a8b751dbe7c | 667 | } |
acarter2 | 0:4a8b751dbe7c | 668 | } |
acarter2 | 0:4a8b751dbe7c | 669 | |
acarter2 | 0:4a8b751dbe7c | 670 | for (int i = 0; i < sizeOfArray; i++){ |
acarter2 | 0:4a8b751dbe7c | 671 | |
acarter2 | 0:4a8b751dbe7c | 672 | if(inputString[i] == '<'){ |
acarter2 | 0:4a8b751dbe7c | 673 | for(int j=0; j < i; j++){ |
acarter2 | 0:4a8b751dbe7c | 674 | |
acarter2 | 0:4a8b751dbe7c | 675 | opertaion[j] = inputString[j]; |
acarter2 | 0:4a8b751dbe7c | 676 | |
acarter2 | 0:4a8b751dbe7c | 677 | } |
acarter2 | 0:4a8b751dbe7c | 678 | |
acarter2 | 0:4a8b751dbe7c | 679 | int index = 0; |
acarter2 | 0:4a8b751dbe7c | 680 | |
acarter2 | 0:4a8b751dbe7c | 681 | for(int k=i+1; k < sizeOfArray - 1; k++){ |
acarter2 | 0:4a8b751dbe7c | 682 | |
acarter2 | 0:4a8b751dbe7c | 683 | data[index] = (inputString[k]); |
acarter2 | 0:4a8b751dbe7c | 684 | index++; |
acarter2 | 0:4a8b751dbe7c | 685 | |
acarter2 | 0:4a8b751dbe7c | 686 | } |
acarter2 | 0:4a8b751dbe7c | 687 | |
acarter2 | 0:4a8b751dbe7c | 688 | |
acarter2 | 0:4a8b751dbe7c | 689 | |
acarter2 | 0:4a8b751dbe7c | 690 | for(int y = 0; y < 20; y++){ |
acarter2 | 0:4a8b751dbe7c | 691 | values[0][y] = opertaion[y]; |
acarter2 | 0:4a8b751dbe7c | 692 | } |
acarter2 | 0:4a8b751dbe7c | 693 | |
acarter2 | 0:4a8b751dbe7c | 694 | for(int z = 0;z < 20; z++){ |
acarter2 | 0:4a8b751dbe7c | 695 | values[1][z] = data[z]; |
acarter2 | 0:4a8b751dbe7c | 696 | } |
acarter2 | 0:4a8b751dbe7c | 697 | |
acarter2 | 0:4a8b751dbe7c | 698 | break; |
acarter2 | 0:4a8b751dbe7c | 699 | |
acarter2 | 0:4a8b751dbe7c | 700 | } |
acarter2 | 0:4a8b751dbe7c | 701 | } |
acarter2 | 0:4a8b751dbe7c | 702 | } |
acarter2 | 7:cb29c20009e8 | 703 | |
acarter2 | 3:f61ae69ed662 | 704 | |
acarter2 | 7:cb29c20009e8 | 705 | |
acarter2 | 0:4a8b751dbe7c | 706 | /* |
acarter2 | 0:4a8b751dbe7c | 707 | /this is he deleteRecords which will move the buffer regard so that the bufferent will over |
acarter2 | 0:4a8b751dbe7c | 708 | /write to the old data in the fifo buffer |
acarter2 | 0:4a8b751dbe7c | 709 | */ |
acarter2 | 0:4a8b751dbe7c | 710 | void deleteRecord(const void* arg ) |
acarter2 | 0:4a8b751dbe7c | 711 | { |
acarter2 | 0:4a8b751dbe7c | 712 | |
acarter2 | 0:4a8b751dbe7c | 713 | } |
acarter2 | 7:cb29c20009e8 | 714 | |
acarter2 | 7:cb29c20009e8 | 715 | |
acarter2 | 0:4a8b751dbe7c | 716 | /* |
acarter2 | 0:4a8b751dbe7c | 717 | /read Record is the most reason data which which is store in the data |
acarter2 | 0:4a8b751dbe7c | 718 | */ |
acarter2 | 0:4a8b751dbe7c | 719 | void readRecord(const void* arg ) |
acarter2 | 0:4a8b751dbe7c | 720 | { |
acarter2 | 0:4a8b751dbe7c | 721 | |
acarter2 | 0:4a8b751dbe7c | 722 | } |
acarter2 | 7:cb29c20009e8 | 723 | |
acarter2 | 0:4a8b751dbe7c | 724 | |
acarter2 | 7:cb29c20009e8 | 725 | |
acarter2 | 0:4a8b751dbe7c | 726 | void deleteAll(BufferStartPointer* bufferStart, BufferEndPointer* bufferEnd){ |
acarter2 | 0:4a8b751dbe7c | 727 | |
acarter2 | 0:4a8b751dbe7c | 728 | while(bufferStart -> lock()){} |
acarter2 | 0:4a8b751dbe7c | 729 | bufferStart -> setBufferStartPointer(0); |
acarter2 | 0:4a8b751dbe7c | 730 | bufferStart -> unlock(); |
acarter2 | 0:4a8b751dbe7c | 731 | |
acarter2 | 0:4a8b751dbe7c | 732 | while(bufferEnd -> lock()){}; |
acarter2 | 0:4a8b751dbe7c | 733 | bufferEnd -> setBufferEndPointer(0); |
acarter2 | 0:4a8b751dbe7c | 734 | bufferEnd -> unlock(); |
acarter2 | 0:4a8b751dbe7c | 735 | |
acarter2 | 0:4a8b751dbe7c | 736 | |
acarter2 | 0:4a8b751dbe7c | 737 | // need the ram memory |
acarter2 | 0:4a8b751dbe7c | 738 | |
acarter2 | 0:4a8b751dbe7c | 739 | |
acarter2 | 0:4a8b751dbe7c | 740 | |
acarter2 | 0:4a8b751dbe7c | 741 | |
acarter2 | 0:4a8b751dbe7c | 742 | |
acarter2 | 0:4a8b751dbe7c | 743 | } |
acarter2 | 7:cb29c20009e8 | 744 | |
acarter2 | 0:4a8b751dbe7c | 745 | /* |
acarter2 | 0:4a8b751dbe7c | 746 | /this is the fuction which update the the sample rate so the int to |
acarter2 | 0:4a8b751dbe7c | 747 | /update the value whoch might hange |
acarter2 | 0:4a8b751dbe7c | 748 | */ |
acarter2 | 0:4a8b751dbe7c | 749 | void updateSampleRate(double newSampleRate, SampleRate* sampleRate) |
acarter2 | 0:4a8b751dbe7c | 750 | { |
acarter2 | 0:4a8b751dbe7c | 751 | while(!sampleRate -> lock()){}; |
acarter2 | 3:f61ae69ed662 | 752 | char outputString = 'OUT OF RANGE'; |
acarter2 | 3:f61ae69ed662 | 753 | |
acarter2 | 7:cb29c20009e8 | 754 | if(newSampleRate >= 0.1 && newSampleRate <= 60.0){ |
acarter2 | 3:f61ae69ed662 | 755 | sampleRate -> setSampleRate(newSampleRate); |
acarter2 | 3:f61ae69ed662 | 756 | char decimel; |
acarter2 | 3:f61ae69ed662 | 757 | printDouble(&decimel, newSampleRate, 2); |
acarter2 | 3:f61ae69ed662 | 758 | |
acarter2 | 7:cb29c20009e8 | 759 | outputString = 'UPDATE<' + decimel + '>'; |
acarter2 | 3:f61ae69ed662 | 760 | } |
acarter2 | 0:4a8b751dbe7c | 761 | sampleRate -> unlock(); |
acarter2 | 0:4a8b751dbe7c | 762 | |
acarter2 | 0:4a8b751dbe7c | 763 | } |
acarter2 | 7:cb29c20009e8 | 764 | |
acarter2 | 0:4a8b751dbe7c | 765 | /*/ |
acarter2 | 0:4a8b751dbe7c | 766 | /this is update the the log so see if the log if the the sample rate |
acarter2 | 0:4a8b751dbe7c | 767 | ozr not |
acarter2 | 0:4a8b751dbe7c | 768 | */ |
acarter2 | 3:f61ae69ed662 | 769 | void updateLog(char logType, TakeSample *takeSample) |
acarter2 | 0:4a8b751dbe7c | 770 | { |
acarter2 | 3:f61ae69ed662 | 771 | char output = ' '; |
acarter2 | 3:f61ae69ed662 | 772 | |
acarter2 | 3:f61ae69ed662 | 773 | while(takeSample -> lock()){}; |
acarter2 | 0:4a8b751dbe7c | 774 | |
acarter2 | 3:f61ae69ed662 | 775 | |
acarter2 | 3:f61ae69ed662 | 776 | if(logType == 'NO'){ |
acarter2 | 3:f61ae69ed662 | 777 | takeSample -> setTakeSample(false); |
acarter2 | 3:f61ae69ed662 | 778 | output ='Log<ON>'; |
acarter2 | 3:f61ae69ed662 | 779 | |
acarter2 | 0:4a8b751dbe7c | 780 | |
acarter2 | 0:4a8b751dbe7c | 781 | } |
acarter2 | 0:4a8b751dbe7c | 782 | |
acarter2 | 3:f61ae69ed662 | 783 | if(logType == 'YES' ){ |
acarter2 | 3:f61ae69ed662 | 784 | takeSample -> setTakeSample(true); |
acarter2 | 3:f61ae69ed662 | 785 | output ='Log<OFF>'; |
acarter2 | 3:f61ae69ed662 | 786 | |
acarter2 | 3:f61ae69ed662 | 787 | |
acarter2 | 0:4a8b751dbe7c | 788 | } |
acarter2 | 0:4a8b751dbe7c | 789 | takeSample -> unlock(); |
acarter2 | 0:4a8b751dbe7c | 790 | |
acarter2 | 0:4a8b751dbe7c | 791 | //need output tread |
acarter2 | 0:4a8b751dbe7c | 792 | |
acarter2 | 0:4a8b751dbe7c | 793 | } |
acarter2 | 7:cb29c20009e8 | 794 | |
acarter2 | 0:4a8b751dbe7c | 795 | /* |
acarter2 | 0:4a8b751dbe7c | 796 | /tthis is update th date which the system will go from and with change |
acarter2 | 0:4a8b751dbe7c | 797 | / the date so that the records are set with the date. |
acarter2 | 0:4a8b751dbe7c | 798 | */ |
acarter2 | 0:4a8b751dbe7c | 799 | void updateDate(char newDate[]) |
acarter2 | 0:4a8b751dbe7c | 800 | { |
acarter2 | 0:4a8b751dbe7c | 801 | |
acarter2 | 0:4a8b751dbe7c | 802 | } |
acarter2 | 7:cb29c20009e8 | 803 | |
acarter2 | 0:4a8b751dbe7c | 804 | /* |
acarter2 | 0:4a8b751dbe7c | 805 | /tthis is update th time which the system will go from and with change |
acarter2 | 0:4a8b751dbe7c | 806 | / the time so that the records are set with the date. |
acarter2 | 0:4a8b751dbe7c | 807 | */ |
acarter2 | 0:4a8b751dbe7c | 808 | void updateTime(char newTime[]) |
acarter2 | 0:4a8b751dbe7c | 809 | { |
acarter2 | 0:4a8b751dbe7c | 810 | |
acarter2 | 0:4a8b751dbe7c | 811 | } |
acarter2 | 7:cb29c20009e8 | 812 | |
acarter2 | 3:f61ae69ed662 | 813 | void updateDisplayState(SampleDisplay* sampleDisplay, char displayString){ |
acarter2 | 0:4a8b751dbe7c | 814 | |
acarter2 | 3:f61ae69ed662 | 815 | char output = ' '; |
acarter2 | 0:4a8b751dbe7c | 816 | |
acarter2 | 0:4a8b751dbe7c | 817 | while(!sampleDisplay -> lock()){}; |
acarter2 | 3:f61ae69ed662 | 818 | if(displayString == 'NO'){ |
acarter2 | 3:f61ae69ed662 | 819 | sampleDisplay -> setDisplay(false); |
acarter2 | 3:f61ae69ed662 | 820 | output = 'SAMPLING<ON>'; |
acarter2 | 0:4a8b751dbe7c | 821 | |
acarter2 | 0:4a8b751dbe7c | 822 | } |
acarter2 | 0:4a8b751dbe7c | 823 | |
acarter2 | 3:f61ae69ed662 | 824 | if(displayString == 'YES'){ |
acarter2 | 3:f61ae69ed662 | 825 | sampleDisplay -> setDisplay(true); |
acarter2 | 3:f61ae69ed662 | 826 | output = 'SAMPLING<OFF>'; |
acarter2 | 3:f61ae69ed662 | 827 | |
acarter2 | 3:f61ae69ed662 | 828 | |
acarter2 | 0:4a8b751dbe7c | 829 | } |
acarter2 | 0:4a8b751dbe7c | 830 | sampleDisplay -> unlock(); |
acarter2 | 0:4a8b751dbe7c | 831 | |
acarter2 | 0:4a8b751dbe7c | 832 | } |
acarter2 | 7:cb29c20009e8 | 833 | |
acarter2 | 7:cb29c20009e8 | 834 | void readInput() { |
acarter2 | 0:4a8b751dbe7c | 835 | |
acarter2 | 7:cb29c20009e8 | 836 | char input[100]; |
acarter2 | 7:cb29c20009e8 | 837 | int place = 0; |
acarter2 | 7:cb29c20009e8 | 838 | char c; |
acarter2 | 7:cb29c20009e8 | 839 | while ( (c = getchar()) != 0xD ) { |
acarter2 | 7:cb29c20009e8 | 840 | input[place] = c; |
acarter2 | 7:cb29c20009e8 | 841 | place++; |
acarter2 | 7:cb29c20009e8 | 842 | } |
acarter2 | 7:cb29c20009e8 | 843 | input[place]=0; //End of string |
acarter2 | 7:cb29c20009e8 | 844 | |
acarter2 | 7:cb29c20009e8 | 845 | |
acarter2 | 0:4a8b751dbe7c | 846 | |
acarter2 | 7:cb29c20009e8 | 847 | printCurrentString(input, 8); |
acarter2 | 0:4a8b751dbe7c | 848 | |
acarter2 | 0:4a8b751dbe7c | 849 | } |
acarter2 | 7:cb29c20009e8 | 850 | |
acarter2 | 7:cb29c20009e8 | 851 | |
acarter2 | 0:4a8b751dbe7c | 852 | // setting up gobal treads to be uses in the program |
acarter2 | 0:4a8b751dbe7c | 853 | //Thread producter(); |
acarter2 | 0:4a8b751dbe7c | 854 | //Thread customer; |
acarter2 | 0:4a8b751dbe7c | 855 | //Thread tempTread; |
acarter2 | 0:4a8b751dbe7c | 856 | //Thread humTread; |
acarter2 | 0:4a8b751dbe7c | 857 | //Thread pressTread(; |
acarter2 | 0:4a8b751dbe7c | 858 | //Thread displayTread; |
acarter2 | 0:4a8b751dbe7c | 859 | Thread deleteRecordTread(deleteRecord); |
acarter2 | 0:4a8b751dbe7c | 860 | //Thread setDateTread; |
acarter2 | 0:4a8b751dbe7c | 861 | //Thread setTimeTread; |
acarter2 | 0:4a8b751dbe7c | 862 | //Thread dsiplaySelectedRecord; |
acarter2 | 0:4a8b751dbe7c | 863 | //Thread updateTakeDisplay; |
acarter2 | 0:4a8b751dbe7c | 864 | //Thread updateTreadTiming; |
acarter2 | 0:4a8b751dbe7c | 865 | //Thread stringMutipluation; |
acarter2 | 7:cb29c20009e8 | 866 | |
acarter2 | 7:cb29c20009e8 | 867 | |
acarter2 | 0:4a8b751dbe7c | 868 | PwmOut mypwm(PWM_OUT); |
acarter2 | 7:cb29c20009e8 | 869 | |
acarter2 | 7:cb29c20009e8 | 870 | DigitalOut myled(LED1); |
acarter2 | 0:4a8b751dbe7c | 871 | |
acarter2 | 0:4a8b751dbe7c | 872 | |
acarter2 | 0:4a8b751dbe7c | 873 | int main() { |
acarter2 | 0:4a8b751dbe7c | 874 | |
acarter2 | 0:4a8b751dbe7c | 875 | |
acarter2 | 0:4a8b751dbe7c | 876 | |
acarter2 | 7:cb29c20009e8 | 877 | |
acarter2 | 7:cb29c20009e8 | 878 | //Thread* sampleThread = new Thread(doSomething); |
acarter2 | 7:cb29c20009e8 | 879 | Thread sampleTread(updateSample); |
acarter2 | 7:cb29c20009e8 | 880 | |
acarter2 | 7:cb29c20009e8 | 881 | |
acarter2 | 7:cb29c20009e8 | 882 | |
acarter2 | 0:4a8b751dbe7c | 883 | while(1) { |
acarter2 | 0:4a8b751dbe7c | 884 | |
acarter2 | 7:cb29c20009e8 | 885 | |
acarter2 | 7:cb29c20009e8 | 886 | |
acarter2 | 0:4a8b751dbe7c | 887 | readInput(); |
acarter2 | 0:4a8b751dbe7c | 888 | |
acarter2 | 0:4a8b751dbe7c | 889 | |
acarter2 | 0:4a8b751dbe7c | 890 | myled = !myled; |
acarter2 | 0:4a8b751dbe7c | 891 | wait(1); |
acarter2 | 0:4a8b751dbe7c | 892 | } |
acarter2 | 0:4a8b751dbe7c | 893 | } |
acarter2 | 7:cb29c20009e8 | 894 |