14 #include "../includes/mass_flow_meter.h"
18 #include <HardwareSerial.h>
19 #include <IWatchdog.h>
20 #include <OneButton.h>
25 #include "../includes/buzzer_control.h"
26 #include "../includes/config.h"
27 #include "../includes/parameters.h"
28 #include "../includes/screen.h"
35 #ifdef MASS_FLOW_METER_ENABLED
39 #define MASS_FLOW_TIMER_FREQ 10000
42 #define MASS_FLOW_PERIOD 100
66 #define MFM_MEAN_SAMPLES 40
80 #define MFM_WAIT_RESET_PERIODS 13
81 #define MFM_WAIT_WARMUP_PERIODS 8
82 #define MFM_WAIT_SOFTRESET_PERIODS 3
83 #define MFM_WAIT_READSERIALR1_PERIODS 1
100 #if (STM32_CORE_VERSION < 0x01090000)
107 #if MODE == MODE_MFM_TESTS
114 #if MASS_FLOW_METER_SENSOR == MFM_SFM3019
121 if (readCountbis != 3u) {
129 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
142 if (readCount != 2u) {
166 #if MASS_FLOW_METER_SENSOR == MFM_SFM3019 || MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
191 #if MASS_FLOW_METER_SENSOR_EXPI == MFM_SFM_3300D
206 if (readCountExpi != 2u) {
253 #if MASS_FLOW_METER_SENSOR == MFM_SFM3019
265 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
270 uint8_t status = Wire.endTransmission();
291 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
299 #if MASS_FLOW_METER_SENSOR_EXPI == MFM_SFM_3300D
315 #if MODE == MODE_MFM_TESTS
324 uint32_t errorCount = 0;
351 #if MASS_FLOW_METER_SENSOR == MFM_SFM3019
353 Wire.beginTransmission(0x00);
355 Wire.endTransmission();
406 errorCount = Wire.endTransmission();
410 if (errorCount == 0u) {
417 uint32_t sn_inspi = 0;
431 sn_inspi |= Wire.read();
433 sn_inspi |= Wire.read();
436 sn_inspi |= Wire.read();
438 sn_inspi |= Wire.read();
449 errorCount += Wire.endTransmission();
456 if (errorCount != 0u) {
462 #if MASS_FLOW_METER_SENSOR_EXPI == MFM_SFM_3300D
467 errorCount = Wire.endTransmission();
473 errorCount += Wire.endTransmission();
476 if (errorCount == 0u) {
477 u_int32_t sn_expi = 0;
478 sn_expi = Wire.read();
480 sn_expi |= Wire.read();
483 sn_expi |= Wire.read();
485 sn_expi |= Wire.read();
493 errorCount += Wire.endTransmission();
497 if (errorCount != 0u) {
503 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
516 uint8_t txOk = Wire.endTransmission();
534 if ((txOk != 0u) || (rxcount != 4u)) {
542 #if MODE == MODE_MFM_TESTS
543 Serial.println(
"Read 1");
545 Serial.println(
"fault condition:");
582 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
584 #elif MASS_FLOW_METER_SENSOR == MFM_SFM3019
590 #if MASS_FLOW_METER_SENSOR_EXPI == MFM_SFM_3300D
615 int32_t zeroFlow = 0;
621 if ((zeroFlow < 10000) && (zeroFlow > -10000)) {
640 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF || MASS_FLOW_METER_SENSOR == MFM_SFM3019
647 if (reset_after_read) {
657 #if MASS_FLOW_METER_SENSOR_EXPI == MFM_SFM_3300D
662 if (reset_after_read) {
669 #if MODE == MODE_MFM_TESTS
671 void onStartClick() {
682 Serial.begin(115200);
683 Serial.println(
"init mass flow meter");
693 screen.print(
"debug prog");
695 screen.print(
"mass flow sensor");
697 screen.print(ok ?
"sensor OK" :
"sensor not OK");
701 #if MASS_FLOW_METER_SENSOR == MFM_HONEYWELL_HAF
703 #elif MASS_FLOW_METER_SENSOR == MFM_SFM3019
706 Serial.println(buffer);
708 #ifdef MASS_FLOW_METER_SENSOR_EXPI
710 Serial.println(buffer);
713 Serial.print(
"calibration status=");
714 Serial.println(calib);
715 Serial.print(
"offset calibration=");
718 btn_alarmoff.attachClick(onStartClick);
719 btn_alarmoff.setDebounceTicks(0);
720 btn_pause.attachClick(onPauseClick);
721 btn_pause.setDebounceTicks(0);
723 Serial.println(
"init done");
731 if (loopcounter == 50) {
739 screen.print(
"mass flow sensor");
743 screen.print(
"sensor not OK");
745 screen.print(
"sensor OK");
749 (void)snprintf(buffer,
sizeof(buffer),
"->vol=%dmL %dmLpm ", volume,
MFM_read_airflow());
753 (void)snprintf(buffer,
sizeof(buffer),
"<-vol=%dmL %dmLpm ", volumeExpi,
#define MFM_RANGE
Defines the range of the Mass Flow Meter in SLM (standard liter per minute)
void MFM_reset(void)
Reset the volume counter.
volatile int32_t mfmExpiratorySensorDetected
uint32_t mfmSfm3019SerialNumber
int32_t MFM_getOffset(void)
Get massflow meter offset.
volatile int32_t mfmInspiratoryAirVolumeSumMilliliters
volatile int32_t mfmInspiratoryInstantAirFlow
int32_t mfmInspiratoryLastValue
volatile int32_t mfmExpiratoryAirVolumeSumMilliliters
#define MFM_WAIT_SOFTRESET_PERIODS
volatile int32_t mfmInspiratoryInstantAirFlowLastValues[MFM_MEAN_SAMPLES]
volatile int32_t mfmInspiratoryCalibrationOffset
volatile int32_t mfmExpiratoryInstantAirFlow
HardwareTimer * massFlowTimer
volatile int32_t mfmExpiratoryCalibrationOffset
uint32_t MFM_expi_read_serial_number(void)
return the serial number of the expiratory flow meter
volatile int32_t mfmInspiratoryLastValueFixedFloat
volatile int32_t mfmExpiratoryLastValueFixedFloat
int8_t MFM_calibrateZero(void)
If the massflow meter needs to be calibrated, this function will be usefull.
int32_t MFM_read_airflow(void)
Read instant air flow.
int32_t mfmResetStateMachine
uint32_t MFM_read_serial_number(void)
Get the serial number of the inspiratory flow meter.
#define MASS_FLOW_TIMER_FREQ
volatile int16_t mfmInspiratoryInstantAirFlowLastValuesIndex
int32_t MFM_expi_read_airflow(void)
Read instant air flow.
volatile uint16_t MFM_force_release_I2C
void MFM_expi_reset(void)
int32_t MFM_read_milliliters(bool reset_after_read)
Get the number of milliliters since last reset.
volatile bool mfmFaultCondition
void MFM_Timer_Callback(HardwareTimer *)
#define MFM_WAIT_RESET_PERIODS
int32_t MFM_expi_read_milliliters(bool reset_after_read)
Get the number of milliliters since last reset for expiratory sensor.
volatile int32_t mfmInspiratorySensorDetected
#define MFM_WAIT_WARMUP_PERIODS
uint32_t mfmSfm3300SerialNumberExpi
uint16_t mfmExpiSFM3300FailCounter
volatile bool mfmInspiratoryInstantAirFlowRecord
uint32_t mfmHoneywellHafSerialNumber
#define MFM_WAIT_READSERIALR1_PERIODS
bool MFM_init(void)
Initialize Mass Flow Meter.
#define MFM_CALIBRATION_IMPOSSIBLE
#define MFM_SFM_3300D_I2C_ADDRESS
#define MFM_SFM3019_I2C_ADDRESS
#define MFM_CALIBRATION_OUT_OF_RANGE
#define MFM_CALIBRATION_OK
#define MFM_HONEYWELL_HAF_I2C_ADDRESS
#define MFM_FORCE_RELEASE_I2C_TRUE
#define MFM_FORCE_RELEASE_I2C_FALSE
#define MASS_FLOW_CHANNEL
#define MFM_POWER_CONTROL
#define MASS_FLOW_ERROR_VALUE
#define PIN_BTN_ALARM_OFF
void resetScreen()
Erase everything that is on the screen.
void startScreen()
Start the screen.
LiquidCrystal screen
Instance of the screen controller.