From 2e778b2df7fa1aedbf275050145aed109424093a Mon Sep 17 00:00:00 2001 From: Caz Yokoyama Date: Sun, 14 Mar 2021 19:09:38 -0700 Subject: [PATCH] drive a piezo speaker which is connected on gpio14 by ThisAircraft.vs. Signed-off-by: Caz Yokoyama --- .../source/SoftRF/src/driver/AudioVario.cpp | 78 +++++++++++++++++++ .../source/SoftRF/src/driver/AudioVario.h | 24 ++++++ .../source/SoftRF/src/driver/Sound.cpp | 11 ++- .../firmware/source/SoftRF/src/driver/Sound.h | 2 + .../source/SoftRF/src/platform/ESP32.h | 2 +- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 software/firmware/source/SoftRF/src/driver/AudioVario.cpp create mode 100644 software/firmware/source/SoftRF/src/driver/AudioVario.h diff --git a/software/firmware/source/SoftRF/src/driver/AudioVario.cpp b/software/firmware/source/SoftRF/src/driver/AudioVario.cpp new file mode 100644 index 000000000..6312a8811 --- /dev/null +++ b/software/firmware/source/SoftRF/src/driver/AudioVario.cpp @@ -0,0 +1,78 @@ +/* + * AudioVario.cpp + * Copyright (C) 2016-2021 Linar Yusupov + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "AudioVario.h" +#include "../system/SoC.h" +#include "EEPROM.h" + +/* + 10 knots = 10 nautical miles/hour = 60761.2 feed / 3600 sec = 16.88 feet/s +*/ +#define MAX_VS 16.88 /* feet/s = 10 knots */ +#define UPDATE_PERIOD 2000 +#define FAST_ONE_CYCLE 75 +#define SLOW_ONE_CYCLE 1000 + +static unsigned long AudioVarioTimeMarker = 0; +static const int freq_array[] = { + 50, 100, 200, 640, 1040, 3000, 3500 +}; +#define CLIMB_START 2 +#define DEAD_BAND 0.2 + +static bool turned_on = false; + +void Audio_Vario(double vs) +{ + unsigned long t = millis(); + unsigned long lapse = t - AudioVarioTimeMarker; + const int max_freq_index = (sizeof(freq_array) / sizeof(freq_array[0])) + - 1 - CLIMB_START; + static int one_cycle = SLOW_ONE_CYCLE, freq_index = 0, + on_period = 1000 / (freq_array[CLIMB_START] / 20); + int hz; + + if (vs > MAX_VS) + vs = MAX_VS; + + if (vs < DEAD_BAND) /* below than dead band */ + hz = 0; + else { + one_cycle = + (SLOW_ONE_CYCLE - FAST_ONE_CYCLE) * ((MAX_VS - vs) / MAX_VS) + + FAST_ONE_CYCLE; + + /* map vs to freq_index */ + freq_index = max_freq_index * vs / MAX_VS + CLIMB_START; + hz = freq_array[freq_index]; + on_period = 1000 / (hz / 20); /* need 20 pulses */ + } + + if (hz > 0 && lapse < on_period) { + if (!turned_on) { + SoC->Sound_tone(hz, settings->volume); + turned_on = true; + } + } else { + SoC->Sound_tone(0, settings->volume); + turned_on = false; + } + + if (lapse > one_cycle) + AudioVarioTimeMarker = t; +} diff --git a/software/firmware/source/SoftRF/src/driver/AudioVario.h b/software/firmware/source/SoftRF/src/driver/AudioVario.h new file mode 100644 index 000000000..3109d7d5e --- /dev/null +++ b/software/firmware/source/SoftRF/src/driver/AudioVario.h @@ -0,0 +1,24 @@ +/* + * AudioVario.h + * Copyright (C) 2016-2021 Linar Yusupov + * + * 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef AUDIOVARIO_H +#define AUDIOVARIO_H + +void Audio_Vario(double vs); + +#endif /* AUDIOVARIO_H */ diff --git a/software/firmware/source/SoftRF/src/driver/Sound.cpp b/software/firmware/source/SoftRF/src/driver/Sound.cpp index aa4db95ab..74517f696 100644 --- a/software/firmware/source/SoftRF/src/driver/Sound.cpp +++ b/software/firmware/source/SoftRF/src/driver/Sound.cpp @@ -51,10 +51,13 @@ bool Sound_Notify(void) void Sound_loop(void) { - if (SoundTimeMarker != 0 && millis() - SoundTimeMarker > ALARM_TONE_MS) { - SoC->Sound_tone(0, settings->volume); - SoundTimeMarker = 0; - } + if (SoundTimeMarker != 0) { /* Sound_Notify */ + if (millis() - SoundTimeMarker > ALARM_TONE_MS) { + SoC->Sound_tone(0, settings->volume); + SoundTimeMarker = 0; + } + } else /* Audio Vario */ + Audio_Vario(ThisAircraft.vs); } void Sound_fini(void) diff --git a/software/firmware/source/SoftRF/src/driver/Sound.h b/software/firmware/source/SoftRF/src/driver/Sound.h index b26602abd..b8e56df25 100644 --- a/software/firmware/source/SoftRF/src/driver/Sound.h +++ b/software/firmware/source/SoftRF/src/driver/Sound.h @@ -19,6 +19,8 @@ #ifndef SOUNDHELPER_H #define SOUNDHELPER_H +#include "AudioVario.h" + #define ALARM_TONE_HZ 1040 #define ALARM_TONE_MS 1000 diff --git a/software/firmware/source/SoftRF/src/platform/ESP32.h b/software/firmware/source/SoftRF/src/platform/ESP32.h index 0cf33b99c..df40e522a 100644 --- a/software/firmware/source/SoftRF/src/platform/ESP32.h +++ b/software/firmware/source/SoftRF/src/platform/ESP32.h @@ -102,7 +102,7 @@ extern Adafruit_NeoPixel strip; SOC_UNUSED_PIN)) #define SOC_GPIO_PIN_BUZZER (hw_info.model != SOFTRF_MODEL_PRIME_MK2 ?\ - 13 : SOC_UNUSED_PIN) + 13 : 14) /* SPI (does match Heltec & TTGO LoRa32 pins mapping) */ #define SOC_GPIO_PIN_MOSI 27