Skip to content

Commit

Permalink
Fix various bugs found by Sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
dsferruzza authored and Keruspe committed May 2, 2020
1 parent 0959191 commit e167f33
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
11 changes: 8 additions & 3 deletions srcs/integration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,13 @@ void loop() {
waitForInMs(10000);

char msg[SCREEN_LINE_LENGTH + 1];
(void)snprintf(msg, SCREEN_LINE_LENGTH + 1, "Moyenne : %3d mmH2O",
pressureOffsetSum / pressureOffsetCount);
int32_t mean;
if (pressureOffsetCount != 0) {
mean = pressureOffsetSum / pressureOffsetCount;
} else {
mean = 0;
}
(void)snprintf(msg, SCREEN_LINE_LENGTH + 1, "Moyenne : %3dl mmH2O", mean);
displayLine(msg, 1);
(void)snprintf(msg, SCREEN_LINE_LENGTH + 1, "Fuite : %3d mmH2O",
maxOffsetValue - minOffsetValue);
Expand Down Expand Up @@ -433,7 +438,7 @@ void loop() {
updateBatterySample();
if ((millis() - last_time) >= 200) {
char msg[SCREEN_LINE_LENGTH + 1];
(void)snprintf(msg, SCREEN_LINE_LENGTH + 1, "Batterie : %2u.%1u V", getBatteryLevel(),
(void)snprintf(msg, SCREEN_LINE_LENGTH + 1, "Batterie : %2ul.%1ul V", getBatteryLevel(),
(getBatteryLevelX10() - (10u * getBatteryLevel())));
displayLine(msg, 3);
last_time = millis();
Expand Down
41 changes: 39 additions & 2 deletions srcs/respirator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Blower blower;
int16_t pressureOffset;
int32_t pressureOffsetSum;
uint32_t pressureOffsetCount;
int16_t minOffsetValue = 0;
int16_t maxOffsetValue = 0;

#if HARDWARE_VERSION == 2
HardwareSerial Serial6(PIN_TELEMETRY_SERIAL_RX, PIN_TELEMETRY_SERIAL_TX);
Expand All @@ -64,10 +66,18 @@ HardwareSerial Serial6(PIN_TELEMETRY_SERIAL_RX, PIN_TELEMETRY_SERIAL_TX);
*/
void waitForInMs(uint16_t ms) {
uint16_t start = millis();
minOffsetValue = readPressureSensor(0, 0);
maxOffsetValue = readPressureSensor(0, 0);
pressureOffsetSum = 0;
pressureOffsetCount = 0;

while ((millis() - start) < ms) {
// Measure 1 pressure per ms we wait
if ((millis() - start) > pressureOffsetCount) {
pressureOffsetSum += readPressureSensor(0, 0);
int16_t pressureValue = readPressureSensor(0, 0);
pressureOffsetSum += pressureValue;
minOffsetValue = min(pressureValue, minOffsetValue);
maxOffsetValue = max(pressureValue, maxOffsetValue);
pressureOffsetCount++;
}
continue;
Expand Down Expand Up @@ -180,8 +190,13 @@ void setup(void) {
screen.setCursor(0, 3);
screen.print("unplugged");
waitForInMs(3000);

resetScreen();
pressureOffset = pressureOffsetSum / static_cast<int32_t>(pressureOffsetCount);
if (pressureOffsetCount != 0u) {
pressureOffset = pressureOffsetSum / static_cast<int32_t>(pressureOffsetCount);
} else {
pressureOffset = 0;
}
DBG_DO({
Serial.print("pressure offset = ");
Serial.print(pressureOffsetSum);
Expand All @@ -191,6 +206,28 @@ void setup(void) {
Serial.print(pressureOffset);
Serial.println();
})

// Happens when patient is plugged at starting
if ((maxOffsetValue - minOffsetValue) >= 10) {
resetScreen();
screen.setCursor(0, 0);
char line1[SCREEN_LINE_LENGTH + 1];
(void)snprintf(line1, SCREEN_LINE_LENGTH + 1, "P offset is unstable");
screen.print(line1);
screen.setCursor(0, 1);
char line2[SCREEN_LINE_LENGTH + 1];
(void)snprintf(line2, SCREEN_LINE_LENGTH + 1, "Max-Min: %3d mmH2O",
maxOffsetValue - minOffsetValue);
screen.print(line2);
screen.setCursor(0, 2);
screen.print("Unplug patient and");
screen.setCursor(0, 3);
screen.print("reboot");
Buzzer_High_Prio_Start();
while (true) {
}
}

if (pressureOffset >= MAX_PRESSURE_OFFSET) {
resetScreen();
screen.setCursor(0, 0);
Expand Down

0 comments on commit e167f33

Please sign in to comment.