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