3rd Repo, trying to figure this out.
Fork of SOFT253_Template_Weather_OS_54 by
CircularArray/CircularArray.cpp@80:959151952153, 2017-05-10 (annotated)
- Committer:
- aburch1
- Date:
- Wed May 10 10:02:22 2017 +0000
- Revision:
- 80:959151952153
- Parent:
- 79:4e6b53eb678b
- Child:
- 81:996c0a3319b4
Fixed application crashing when sending 2 messages through to the logger at the same time. Other messages need to be changed to use the logger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Netaphous | 64:8ada3e0b2048 | 1 | #include "CircularArray.h" |
Netaphous | 64:8ada3e0b2048 | 2 | #include <stdio.h> |
Netaphous | 64:8ada3e0b2048 | 3 | #include <ctype.h> |
Netaphous | 64:8ada3e0b2048 | 4 | /* |
Netaphous | 64:8ada3e0b2048 | 5 | Constructor |
Netaphous | 64:8ada3e0b2048 | 6 | */ |
Netaphous | 64:8ada3e0b2048 | 7 | CircularArray::CircularArray(int limit) |
Netaphous | 64:8ada3e0b2048 | 8 | { |
Netaphous | 64:8ada3e0b2048 | 9 | maxSize = limit; |
Netaphous | 64:8ada3e0b2048 | 10 | array = new Measure[maxSize]; |
Netaphous | 64:8ada3e0b2048 | 11 | firstValue = 0; |
Netaphous | 64:8ada3e0b2048 | 12 | currentSize = 0; |
Netaphous | 64:8ada3e0b2048 | 13 | } |
Netaphous | 64:8ada3e0b2048 | 14 | void CircularArray::pushValue(Measure _measure) |
Netaphous | 64:8ada3e0b2048 | 15 | { |
Netaphous | 64:8ada3e0b2048 | 16 | if(currentSize >= maxSize) |
Netaphous | 64:8ada3e0b2048 | 17 | { |
Netaphous | 64:8ada3e0b2048 | 18 | firstValue++; |
Netaphous | 64:8ada3e0b2048 | 19 | currentSize--; |
Netaphous | 64:8ada3e0b2048 | 20 | } |
aburch1 | 72:ef4a4e3089c1 | 21 | int next = nextSpace(); |
aburch1 | 72:ef4a4e3089c1 | 22 | array[next] = _measure; |
Netaphous | 64:8ada3e0b2048 | 23 | currentSize++; |
Netaphous | 64:8ada3e0b2048 | 24 | } |
aburch1 | 80:959151952153 | 25 | |
aburch1 | 80:959151952153 | 26 | Measure CircularArray::pullValue() |
aburch1 | 80:959151952153 | 27 | { |
aburch1 | 80:959151952153 | 28 | return array[firstValue]; |
aburch1 | 80:959151952153 | 29 | } |
aburch1 | 80:959151952153 | 30 | |
Netaphous | 65:3723d2729b68 | 31 | void CircularArray::readX(int x) |
Netaphous | 64:8ada3e0b2048 | 32 | { |
Netaphous | 64:8ada3e0b2048 | 33 | if(x > currentSize) |
Netaphous | 64:8ada3e0b2048 | 34 | { |
Netaphous | 64:8ada3e0b2048 | 35 | x = currentSize; |
Netaphous | 64:8ada3e0b2048 | 36 | printf("Changed print amount to currentSize"); |
Netaphous | 64:8ada3e0b2048 | 37 | } |
Netaphous | 64:8ada3e0b2048 | 38 | int currentElement = nextSpace() - x; |
Netaphous | 64:8ada3e0b2048 | 39 | if(currentElement < 0) |
Netaphous | 64:8ada3e0b2048 | 40 | { |
Netaphous | 64:8ada3e0b2048 | 41 | currentElement += maxSize; |
Netaphous | 64:8ada3e0b2048 | 42 | } |
aburch1 | 79:4e6b53eb678b | 43 | |
aburch1 | 79:4e6b53eb678b | 44 | char* ptr; |
Netaphous | 64:8ada3e0b2048 | 45 | for(int i = 0; i < x; i++) |
Netaphous | 64:8ada3e0b2048 | 46 | { |
aburch1 | 79:4e6b53eb678b | 47 | ptr = array[currentElement].date.ToString(); |
aburch1 | 74:749727490f44 | 48 | printf("\n\r%i. %s T: %f | H: %f | P: %f |",i + 1,ptr , array[currentElement].temperature, array[currentElement].humidity, array[currentElement].pressure); |
Netaphous | 64:8ada3e0b2048 | 49 | currentElement++; |
Netaphous | 64:8ada3e0b2048 | 50 | currentElement = currentElement % maxSize; |
Netaphous | 64:8ada3e0b2048 | 51 | } |
Netaphous | 64:8ada3e0b2048 | 52 | } |
Netaphous | 65:3723d2729b68 | 53 | void CircularArray::readAll() |
Netaphous | 64:8ada3e0b2048 | 54 | { |
Netaphous | 65:3723d2729b68 | 55 | readX(currentSize); |
Netaphous | 64:8ada3e0b2048 | 56 | } |
Netaphous | 64:8ada3e0b2048 | 57 | void CircularArray::deleteX(int x) |
Netaphous | 64:8ada3e0b2048 | 58 | { |
Netaphous | 64:8ada3e0b2048 | 59 | if(x >= maxSize) |
Netaphous | 64:8ada3e0b2048 | 60 | { |
Netaphous | 64:8ada3e0b2048 | 61 | deleteAll(); |
Netaphous | 64:8ada3e0b2048 | 62 | } |
Netaphous | 64:8ada3e0b2048 | 63 | else |
Netaphous | 64:8ada3e0b2048 | 64 | { |
Netaphous | 64:8ada3e0b2048 | 65 | firstValue += x; |
Netaphous | 64:8ada3e0b2048 | 66 | currentSize -= x; |
Netaphous | 64:8ada3e0b2048 | 67 | firstValue = firstValue % maxSize; |
Netaphous | 64:8ada3e0b2048 | 68 | } |
Netaphous | 64:8ada3e0b2048 | 69 | } |
Netaphous | 64:8ada3e0b2048 | 70 | void CircularArray::deleteAll() |
Netaphous | 64:8ada3e0b2048 | 71 | { |
Netaphous | 64:8ada3e0b2048 | 72 | currentSize = 0; |
Netaphous | 64:8ada3e0b2048 | 73 | } |
Netaphous | 64:8ada3e0b2048 | 74 | int CircularArray::getSize() |
Netaphous | 64:8ada3e0b2048 | 75 | { |
Netaphous | 64:8ada3e0b2048 | 76 | return currentSize; |
Netaphous | 64:8ada3e0b2048 | 77 | } |
Netaphous | 64:8ada3e0b2048 | 78 | int CircularArray::nextSpace() |
Netaphous | 64:8ada3e0b2048 | 79 | { |
Netaphous | 64:8ada3e0b2048 | 80 | int nextSpace = firstValue + currentSize; |
Netaphous | 64:8ada3e0b2048 | 81 | nextSpace = nextSpace % maxSize; |
Netaphous | 64:8ada3e0b2048 | 82 | return nextSpace; |
Netaphous | 64:8ada3e0b2048 | 83 | } |