Skip to content

Commit

Permalink
drive a piezo speaker which is connected on
Browse files Browse the repository at this point in the history
gpio14 by ThisAircraft.vs.

Signed-off-by: Caz Yokoyama <[email protected]>
  • Loading branch information
CazYokoyama committed Mar 20, 2021
1 parent b14a1c4 commit 2e778b2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
78 changes: 78 additions & 0 deletions software/firmware/source/SoftRF/src/driver/AudioVario.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#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;
}
24 changes: 24 additions & 0 deletions software/firmware/source/SoftRF/src/driver/AudioVario.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#ifndef AUDIOVARIO_H
#define AUDIOVARIO_H

void Audio_Vario(double vs);

#endif /* AUDIOVARIO_H */
11 changes: 7 additions & 4 deletions software/firmware/source/SoftRF/src/driver/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions software/firmware/source/SoftRF/src/driver/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef SOUNDHELPER_H
#define SOUNDHELPER_H

#include "AudioVario.h"

#define ALARM_TONE_HZ 1040
#define ALARM_TONE_MS 1000

Expand Down
2 changes: 1 addition & 1 deletion software/firmware/source/SoftRF/src/platform/ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e778b2

Please sign in to comment.