Flying Silicon Sensor
fsSensor.cpp File Reference

Sketch main module. More...

#include <Arduino.h>
#include <Wire.h>
#include <sdpsensor.h>
#include <SensirionI2CSht4x.h>
#include <Adafruit_DPS310.h>
#include "pindefs.h"
#include "sounds.h"
#include "debug.h"
#include "ptvBleInterface.h"
#include "deepSleep.h"
#include "pwrSupply.h"

Go to the source code of this file.

Macros

#define DEVICE_MANUFACTURER   "Flying Silicon"
 Manufacturer meta data.
 
#define DEVICE_NAME   "FS-SP4"
 Device name meta data (also sent in BLE advertising, truncated to 31)
 
#define DEVICE_SERIAL   "23-001"
 Serial number meta data.
 
#define ACTIVE_WITH_USB   0
 Run or stay passive (and charge) when USB is connected.
 
#define FLASH_ALWAYS   1
 Flash blue LED (when BLE connected) also when running on battery.
 
#define SEND_LONG   0
 Use the long PTVSOAR message format also for data.
 
#define DEEP_SLEEP   1
 Enter deep sleep when battery drained (else suspend loop)
 
#define AUTO_OFF_MS   3600000
 Delay before auto power off (without BLE connection)
 
#define META_REPEAT   16
 Number of meta headers to send on (re)connect.
 
#define PATCH_LEDR   digitalWrite(LEDR, HIGH)
 Patch the red LED which is going on when BLE device is bonded.
 
#define IntervalMSG   125
 Message send interval ms (critical)
 
#define IntervalSDP   IntervalMSG
 SDP read interval ms.
 
#define IntervalDPS   IntervalMSG
 DPS read interval ms.
 
#define IntervalSHT   1000
 SHT read interval ms.
 
#define IntervalPWR   1000
 Power state update interval ms.
 
#define IntervalPOL   IntervalMSG
 Time ms before BLE receive repeats.
 
#define LoopPassiveMs   125
 Loop interval ms when disabled.
 
#define LoopIdleMs   125
 Loop interval when not connected.
 
#define NopDelayMs   10
 Delay ms when no message needs to be sent.
 
#define IdleIntervalLED   1250
 LED flashing interval when BLE is active but disconnected.
 
#define IdleLightLED   625
 Time LED is on.
 
#define IntervalLED   2500
 LED flashing interval when BLE is active and connected.
 
#define LightLED   125
 Time LED is on.
 

Functions

static void resetStatus (void)
 
static void resetTimers (void)
 
static void clearLEDs (void)
 
static void readSDP (void)
 
static void readDPS (void)
 
static void readSHT (void)
 
static void flashBlueLED (int active, unsigned int intervalMs, unsigned int lightMs)
 
static void managePower ()
 
static void processCmd (const String &cmd)
 
static void enable (void)
 
static void disable (void)
 
static void suspend (void)
 
static void loopIdle (void)
 
static void loopConnected (void)
 
static void loopPassive (void)
 
void appLoop (void)
 
static void configure (void)
 
void appSetup (void)
 

Variables

static const int Melody []
 Startup melody.
 
static PtvBleInterface Remote
 Singleton connection to be used (all BLE stuff is singleton)
 
static SDP3XSensor sdp
 Sensirion SDP31 for Pitot.
 
static SensirionI2CSht4x sht
 Sensirion SHT for temperature / humidity.
 
static Adafruit_DPS310 dps
 Infineon DPS310 for barometric pressure.
 
static PtvBleInterface::SensorData sensorData
 Sensor data to work with.
 
static int isBlueLedOn = 0
 LED blinking state 0/1.
 
static int isEnabled = 0
 Operating mode 0/1.
 
static int isConnected = 0
 Connection state 0/1.
 
static unsigned int sentMeta = 0
 Meta data repetition counter.
 
static unsigned long timeMs = 0
 Current loop time ms.
 
static unsigned long lastActiveMs
 
static unsigned long lastSHTMs
 Last read SHT ambient temperature/humidity.
 
static unsigned long lastSDPMs
 Last read SDP differential pressure.
 
static unsigned long lastDPSMs
 Last read DPS atmospheric pressure.
 
static unsigned long lastMSGMs
 Last time message sent.
 
static unsigned long lastPOLMs
 Last BLE receive poll.
 
static unsigned long lastLEDMs
 Last LED state change.
 
static unsigned long lastPWRMs
 Last read of battery/voltage status.
 

Detailed Description

Sketch main module.

Arduino code for Flying Silicon Airspeed/Vario Probe and TotalVario Android app.

For more information refer to: https://flyingsilicon.blogspot.com/
https://totalvario.blogspot.com/

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License http://www.gnu.org/licenses/ for more details.

Definition in file fsSensor.cpp.

Macro Definition Documentation

◆ ACTIVE_WITH_USB

#define ACTIVE_WITH_USB   0

Run or stay passive (and charge) when USB is connected.

Definition at line 55 of file fsSensor.cpp.

◆ AUTO_OFF_MS

#define AUTO_OFF_MS   3600000

Delay before auto power off (without BLE connection)

Definition at line 70 of file fsSensor.cpp.

◆ DEEP_SLEEP

#define DEEP_SLEEP   1

Enter deep sleep when battery drained (else suspend loop)

Definition at line 64 of file fsSensor.cpp.

◆ DEVICE_MANUFACTURER

#define DEVICE_MANUFACTURER   "Flying Silicon"

Manufacturer meta data.

Definition at line 48 of file fsSensor.cpp.

◆ DEVICE_NAME

#define DEVICE_NAME   "FS-SP4"

Device name meta data (also sent in BLE advertising, truncated to 31)

Definition at line 50 of file fsSensor.cpp.

◆ DEVICE_SERIAL

#define DEVICE_SERIAL   "23-001"

Serial number meta data.

Definition at line 52 of file fsSensor.cpp.

◆ FLASH_ALWAYS

#define FLASH_ALWAYS   1

Flash blue LED (when BLE connected) also when running on battery.

Definition at line 58 of file fsSensor.cpp.

◆ IdleIntervalLED

#define IdleIntervalLED   1250

LED flashing interval when BLE is active but disconnected.

Definition at line 103 of file fsSensor.cpp.

◆ IdleLightLED

#define IdleLightLED   625

Time LED is on.

Definition at line 105 of file fsSensor.cpp.

◆ IntervalDPS

#define IntervalDPS   IntervalMSG

DPS read interval ms.

Definition at line 87 of file fsSensor.cpp.

◆ IntervalLED

#define IntervalLED   2500

LED flashing interval when BLE is active and connected.

Definition at line 108 of file fsSensor.cpp.

◆ IntervalMSG

#define IntervalMSG   125

Message send interval ms (critical)

Definition at line 83 of file fsSensor.cpp.

◆ IntervalPOL

#define IntervalPOL   IntervalMSG

Time ms before BLE receive repeats.

Definition at line 93 of file fsSensor.cpp.

◆ IntervalPWR

#define IntervalPWR   1000

Power state update interval ms.

Definition at line 91 of file fsSensor.cpp.

◆ IntervalSDP

#define IntervalSDP   IntervalMSG

SDP read interval ms.

Definition at line 85 of file fsSensor.cpp.

◆ IntervalSHT

#define IntervalSHT   1000

SHT read interval ms.

Definition at line 89 of file fsSensor.cpp.

◆ LightLED

#define LightLED   125

Time LED is on.

Definition at line 110 of file fsSensor.cpp.

◆ LoopIdleMs

#define LoopIdleMs   125

Loop interval when not connected.

Definition at line 98 of file fsSensor.cpp.

◆ LoopPassiveMs

#define LoopPassiveMs   125

Loop interval ms when disabled.

Definition at line 96 of file fsSensor.cpp.

◆ META_REPEAT

#define META_REPEAT   16

Number of meta headers to send on (re)connect.

Definition at line 73 of file fsSensor.cpp.

◆ NopDelayMs

#define NopDelayMs   10

Delay ms when no message needs to be sent.

Definition at line 100 of file fsSensor.cpp.

◆ PATCH_LEDR

#define PATCH_LEDR   digitalWrite(LEDR, HIGH)

Patch the red LED which is going on when BLE device is bonded.

Definition at line 76 of file fsSensor.cpp.

◆ SEND_LONG

#define SEND_LONG   0

Use the long PTVSOAR message format also for data.

Definition at line 61 of file fsSensor.cpp.

Function Documentation

◆ appLoop()

◆ appSetup()

void appSetup ( void  )

◆ clearLEDs()

static void clearLEDs ( void  )
static

Switch RGB LEDs off

Definition at line 221 of file fsSensor.cpp.

References LEDB, LEDG, and LEDR.

Referenced by configure(), and suspend().

◆ configure()

static void configure ( void  )
static

Configure pins etc

Definition at line 577 of file fsSensor.cpp.

References ANALOG_REFV, clearLEDs(), PIN_BATTV, PIN_BATTV_ENABLE, PIN_BUZZER, PIN_CHG, PIN_HICHG, and PIN_USB.

Referenced by appSetup().

◆ disable()

static void disable ( void  )
static

Called when device becomes deactivated

Definition at line 382 of file fsSensor.cpp.

References isEnabled, LOGL, Sounds::playSwitchOff(), Remote, and BleInterface::shutdown().

Referenced by appLoop(), and appSetup().

◆ enable()

◆ flashBlueLED()

static void flashBlueLED ( int  active,
unsigned int  intervalMs,
unsigned int  lightMs 
)
static

Flash the blue LED with the specified timing

Parameters
activeenable (1) or disable (0) blinking
intervalMsduration of a blink period in ms
lightMstime in ms that the LED is on

Definition at line 300 of file fsSensor.cpp.

References isBlueLedOn, lastLEDMs, LEDB, and timeMs.

Referenced by loopConnected(), and loopIdle().

◆ loopConnected()

◆ loopIdle()

static void loopIdle ( void  )
static

Periodically called when device is active and no BLE central is connected

Definition at line 423 of file fsSensor.cpp.

References flashBlueLED(), IdleIntervalLED, IdleLightLED, LoopIdleMs, and PATCH_LEDR.

Referenced by appLoop().

◆ loopPassive()

static void loopPassive ( void  )
static

Periodically called when device is inactive

Definition at line 488 of file fsSensor.cpp.

References PtvBleInterface::SensorData::isCharging, LEDB, LoopPassiveMs, PATCH_LEDR, and sensorData.

Referenced by appLoop().

◆ managePower()

◆ processCmd()

static void processCmd ( const String &  cmd)
static

Process a control command received from remote

Parameters
cmdString holding the command

Definition at line 347 of file fsSensor.cpp.

References LOGL.

Referenced by loopConnected().

◆ readDPS()

static void readDPS ( void  )
static

Periodically read atmospheric pressure from DPS sensor

Definition at line 257 of file fsSensor.cpp.

References PtvBleInterface::SensorData::absHpa, dps, LOG, LOGL, and sensorData.

Referenced by loopConnected().

◆ readSDP()

static void readSDP ( void  )
static

Periodically read differential pressure from SDP31 sensor

Definition at line 236 of file fsSensor.cpp.

References PtvBleInterface::SensorData::absHpa, PtvBleInterface::SensorData::diffPa, LOG, LOGL, sdp, and sensorData.

Referenced by loopConnected().

◆ readSHT()

static void readSHT ( void  )
static

Periodically read temperature and humidity from SHT sensor

Definition at line 279 of file fsSensor.cpp.

References PtvBleInterface::SensorData::humPct, LOG, LOGL, sensorData, sht, and PtvBleInterface::SensorData::tempC.

Referenced by loopConnected().

◆ resetStatus()

◆ resetTimers()

static void resetTimers ( void  )
static

Initialise the function timers

Definition at line 200 of file fsSensor.cpp.

References IntervalDPS, IntervalMSG, IntervalPOL, IntervalPWR, IntervalSDP, IntervalSHT, lastActiveMs, lastDPSMs, lastLEDMs, lastMSGMs, lastPOLMs, lastPWRMs, lastSDPMs, lastSHTMs, and timeMs.

Referenced by appSetup(), and enable().

◆ suspend()

static void suspend ( void  )
static

Shut down BLE and suspend loop until next restart

  • device can still be charged but will not run until a power cycle occurs Calling suspend() never returns!

Definition at line 395 of file fsSensor.cpp.

References clearLEDs(), enterDeepSleep(), LOGL, Sounds::playSwitchOff(), Remote, setLowPowerMode(), and BleInterface::shutdown().

Referenced by appLoop(), and appSetup().

Variable Documentation

◆ dps

Adafruit_DPS310 dps
static

Infineon DPS310 for barometric pressure.

Definition at line 139 of file fsSensor.cpp.

Referenced by appSetup(), and readDPS().

◆ isBlueLedOn

int isBlueLedOn = 0
static

LED blinking state 0/1.

Definition at line 150 of file fsSensor.cpp.

Referenced by flashBlueLED(), and resetStatus().

◆ isConnected

int isConnected = 0
static

Connection state 0/1.

Definition at line 154 of file fsSensor.cpp.

Referenced by appLoop(), and resetStatus().

◆ isEnabled

int isEnabled = 0
static

Operating mode 0/1.

Definition at line 152 of file fsSensor.cpp.

Referenced by appLoop(), disable(), enable(), and resetStatus().

◆ lastActiveMs

unsigned long lastActiveMs
static

Definition at line 163 of file fsSensor.cpp.

Referenced by appLoop(), and resetTimers().

◆ lastDPSMs

unsigned long lastDPSMs
static

Last read DPS atmospheric pressure.

Definition at line 167 of file fsSensor.cpp.

Referenced by loopConnected(), and resetTimers().

◆ lastLEDMs

unsigned long lastLEDMs
static

Last LED state change.

Definition at line 170 of file fsSensor.cpp.

Referenced by flashBlueLED(), and resetTimers().

◆ lastMSGMs

unsigned long lastMSGMs
static

Last time message sent.

Definition at line 168 of file fsSensor.cpp.

Referenced by loopConnected(), and resetTimers().

◆ lastPOLMs

unsigned long lastPOLMs
static

Last BLE receive poll.

Definition at line 169 of file fsSensor.cpp.

Referenced by loopConnected(), and resetTimers().

◆ lastPWRMs

unsigned long lastPWRMs
static

Last read of battery/voltage status.

Definition at line 171 of file fsSensor.cpp.

Referenced by appLoop(), and resetTimers().

◆ lastSDPMs

unsigned long lastSDPMs
static

Last read SDP differential pressure.

Definition at line 166 of file fsSensor.cpp.

Referenced by loopConnected(), and resetTimers().

◆ lastSHTMs

unsigned long lastSHTMs
static

Last read SHT ambient temperature/humidity.

Definition at line 165 of file fsSensor.cpp.

Referenced by loopConnected(), and resetTimers().

◆ Melody

const int Melody[]
static
Initial value:
= {
NOTE_C3, 16,
NOTE_G3, 16,
NOTE_E3, 16,
NOTE_C3, 32,
NOTE_G3, -16,
NOTE_E3, 8,
}
#define NOTE_E3
Definition sounds.h:61
#define NOTE_G3
Definition sounds.h:58
#define MELODY_END
Definition sounds.h:114
#define NOTE_C3
Definition sounds.h:65

Startup melody.

Definition at line 114 of file fsSensor.cpp.

Referenced by enable().

◆ Remote

PtvBleInterface Remote
static

Singleton connection to be used (all BLE stuff is singleton)

Definition at line 130 of file fsSensor.cpp.

Referenced by appLoop(), appSetup(), disable(), enable(), loopConnected(), and suspend().

◆ sdp

SDP3XSensor sdp
static

Sensirion SDP31 for Pitot.

Definition at line 133 of file fsSensor.cpp.

Referenced by appSetup(), and readSDP().

◆ sensorData

PtvBleInterface::SensorData sensorData
static

Sensor data to work with.

Definition at line 142 of file fsSensor.cpp.

Referenced by enable(), loopConnected(), loopPassive(), managePower(), readDPS(), readSDP(), readSHT(), and resetStatus().

◆ sentMeta

unsigned int sentMeta = 0
static

Meta data repetition counter.

Definition at line 157 of file fsSensor.cpp.

Referenced by appLoop(), loopConnected(), and resetStatus().

◆ sht

SensirionI2CSht4x sht
static

Sensirion SHT for temperature / humidity.

Definition at line 136 of file fsSensor.cpp.

Referenced by appSetup(), and readSHT().

◆ timeMs

unsigned long timeMs = 0
static

Current loop time ms.

Last time when connected via BLE

Definition at line 160 of file fsSensor.cpp.

Referenced by appLoop(), flashBlueLED(), loopConnected(), and resetTimers().