|
| 1 | +#include "deco/LastDive.h" |
| 2 | + |
| 3 | +void LastDive::_debugLastDiveData(LastDiveData* lastDiveData) |
| 4 | +{ |
| 5 | + Serial.print("lDate: "); |
| 6 | + Serial.println(lastDiveData->diveDateTimestamp); |
| 7 | + Serial.print("lMaxDepth: "); |
| 8 | + Serial.println(lastDiveData->maxDepthInMeters); |
| 9 | + Serial.print("lDur: "); |
| 10 | + Serial.println(lastDiveData->durationInSeconds); |
| 11 | + Serial.print("lNoFly: "); |
| 12 | + Serial.println(lastDiveData->noFlyTimeInMinutes); |
| 13 | + Serial.print("lDeco: "); |
| 14 | + Serial.println(lastDiveData->wasDecoDive); |
| 15 | + |
| 16 | + for (int i=0; i<COMPARTMENT_COUNT; i++) { |
| 17 | + Serial.print("lComp"); |
| 18 | + Serial.print(i); |
| 19 | + Serial.print(" : "); |
| 20 | + Serial.println(lastDiveData->compartmentPartialPressures[i]); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +LastDiveData* LastDive::loadLastDiveData() |
| 25 | +{ |
| 26 | + LastDiveData* lastDiveData = new LastDiveData; |
| 27 | + |
| 28 | + Preferences prefs; |
| 29 | + prefs.begin("DiveIno", true); // read-only |
| 30 | + |
| 31 | + lastDiveData->diveDateTimestamp = prefs.getLong("lDate", ez.clock.tz.now()); |
| 32 | + lastDiveData->maxDepthInMeters = prefs.getFloat("lMaxDepth", 0.0); |
| 33 | + lastDiveData->durationInSeconds = prefs.getInt("lDur", 0); |
| 34 | + lastDiveData->noFlyTimeInMinutes = prefs.getLong("lNoFly" , 0); |
| 35 | + lastDiveData->wasDecoDive = prefs.getBool("lDeco", false); |
| 36 | + |
| 37 | + for (int i=0; i<COMPARTMENT_COUNT; i++) { |
| 38 | + lastDiveData->compartmentPartialPressures[i] = prefs.getFloat("lComp" + i, 0.0); |
| 39 | + } |
| 40 | + |
| 41 | + prefs.end(); |
| 42 | + |
| 43 | + Serial.println("LOAD Last Dive Data"); |
| 44 | + this->_debugLastDiveData(lastDiveData); |
| 45 | + |
| 46 | + return lastDiveData; |
| 47 | +} |
| 48 | + |
| 49 | +void LastDive::storeLastDiveData(LastDiveData* lastDiveData) |
| 50 | +{ |
| 51 | + Preferences prefs; |
| 52 | + prefs.begin("DiveIno"); |
| 53 | + |
| 54 | + prefs.putLong("lDate", lastDiveData->diveDateTimestamp); |
| 55 | + prefs.putFloat("lMaxDepth", lastDiveData->maxDepthInMeters); |
| 56 | + prefs.putInt("lDur", lastDiveData->durationInSeconds); |
| 57 | + prefs.putLong("lNoFly" , lastDiveData->noFlyTimeInMinutes); |
| 58 | + prefs.putBool("lDeco", lastDiveData->wasDecoDive); |
| 59 | + |
| 60 | + for (int i=0; i<COMPARTMENT_COUNT; i++) { |
| 61 | + prefs.putFloat("lComp" + i, lastDiveData->compartmentPartialPressures[i]); |
| 62 | + } |
| 63 | + |
| 64 | + prefs.end(); |
| 65 | + |
| 66 | + Serial.println("STORE Last Dive Data"); |
| 67 | + this->_debugLastDiveData(lastDiveData); |
| 68 | +} |
| 69 | + |
| 70 | +bool LastDive::clearLastDiveData() |
| 71 | +{ |
| 72 | + Preferences prefs; |
| 73 | + prefs.begin("DiveIno"); |
| 74 | + |
| 75 | + prefs.remove("lDate"); |
| 76 | + prefs.remove("lMaxDepth"); |
| 77 | + prefs.remove("lDur"); |
| 78 | + prefs.remove("lNoFly"); |
| 79 | + prefs.remove("lDeco"); |
| 80 | + |
| 81 | + for (int i=0; i<COMPARTMENT_COUNT; i++) { |
| 82 | + prefs.remove("lComp" + i); |
| 83 | + } |
| 84 | + |
| 85 | + prefs.end(); |
| 86 | + |
| 87 | + Serial.println("CLEAR Last Dive Data"); |
| 88 | + |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +long LastDive::getNoFlyTimeInMinutes() |
| 93 | +{ |
| 94 | + Preferences prefs; |
| 95 | + prefs.begin("DiveIno", true); // read-only |
| 96 | + long noFlyTime = prefs.getLong("lNoFly" , 0L); |
| 97 | + prefs.end(); |
| 98 | + |
| 99 | + return noFlyTime; |
| 100 | +} |
| 101 | + |
| 102 | +long LastDive::getCurrentSurfaceIntervalInMinutes() |
| 103 | +{ |
| 104 | + long nowTimestamp = ez.clock.tz.now(); |
| 105 | + |
| 106 | + Preferences prefs; |
| 107 | + prefs.begin("DiveIno", true); // read-only |
| 108 | + long diveDateTimestamp = prefs.getLong("lDate", nowTimestamp); |
| 109 | + prefs.end(); |
| 110 | + |
| 111 | + //Calculate surface interval |
| 112 | + return (nowTimestamp - diveDateTimestamp) / 60; |
| 113 | +} |
| 114 | + |
| 115 | +DiveResult* LastDive::spendTimeOnSurface(int surfaceIntervalInMinutes, Buhlmann* buhlmann) |
| 116 | +{ |
| 117 | + DiveResult* diveResult = new DiveResult; |
| 118 | + diveResult = buhlmann->initializeCompartments(); |
| 119 | + |
| 120 | + //Retrieve last dive data |
| 121 | + LastDiveData* lastDiveData = this->loadLastDiveData(); |
| 122 | + |
| 123 | + long nowTimestamp = ez.clock.tz.now(); |
| 124 | + |
| 125 | + //Last dive happened within 48 hours and there is an active no fly time |
| 126 | + if (lastDiveData != NULL && (lastDiveData->diveDateTimestamp + 172800) > nowTimestamp && lastDiveData->noFlyTimeInMinutes > 0) { |
| 127 | + |
| 128 | + Serial.print(F("BEFORE dive No Fly time (min): ")); |
| 129 | + Serial.println(lastDiveData->noFlyTimeInMinutes); |
| 130 | + |
| 131 | + //Copy Last Dive Data |
| 132 | + diveResult->noFlyTimeInMinutes = lastDiveData->noFlyTimeInMinutes; |
| 133 | + diveResult->previousDiveDateTimestamp = lastDiveData->diveDateTimestamp; |
| 134 | + diveResult->wasDecoDive = lastDiveData->wasDecoDive; |
| 135 | + for (byte i=0; i <COMPARTMENT_COUNT; i++) { |
| 136 | + diveResult->compartmentPartialPressures[i] = lastDiveData->compartmentPartialPressures[i]; |
| 137 | + |
| 138 | + Serial.print(F("BEFORE: Dive compartment ")); |
| 139 | + Serial.print(i); |
| 140 | + Serial.print(F(": ")); |
| 141 | + Serial.print(diveResult->compartmentPartialPressures[i], 0); |
| 142 | + Serial.println(F(" ppN2")); |
| 143 | + } |
| 144 | + |
| 145 | + //Calculate surface time in minutes |
| 146 | + int surfaceTime = (nowTimestamp - lastDiveData->diveDateTimestamp) / 60; |
| 147 | + |
| 148 | + Serial.print(F("Surface time (min): ")); |
| 149 | + Serial.println(surfaceTime); |
| 150 | + |
| 151 | + //Spend the time on the surface |
| 152 | + diveResult = buhlmann->surfaceInterval(surfaceTime, diveResult); |
| 153 | + |
| 154 | + for (byte i=0; i <COMPARTMENT_COUNT; i++) { |
| 155 | + Serial.print(F("AFTER: Dive compartment ")); |
| 156 | + Serial.print(i); |
| 157 | + Serial.print(F(": ")); |
| 158 | + Serial.print(diveResult->compartmentPartialPressures[i], 0); |
| 159 | + Serial.println(F(" ppN2")); |
| 160 | + } |
| 161 | + |
| 162 | + Serial.print(F("AFTER dive No Fly time (min): ")); |
| 163 | + Serial.println(diveResult->noFlyTimeInMinutes); |
| 164 | + } |
| 165 | + return diveResult; |
| 166 | +} |
0 commit comments