[Taranis] USB deconnection fix
[Taranis / sky9x] Less maths for audio (no more sin)
This commit is contained in:
+38
-10
@@ -39,14 +39,25 @@
|
||||
|
||||
extern OS_MutexID audioMutex;
|
||||
|
||||
const unsigned int toneVolumes[] = {
|
||||
4000,
|
||||
8000,
|
||||
16000,
|
||||
24000,
|
||||
32000
|
||||
const int16_t sineValues[] =
|
||||
{
|
||||
0, 64, 128, 191, 254, 316, 376, 435, 493, 548,
|
||||
601, 652, 700, 746, 789, 828, 864, 897, 926, 952,
|
||||
973, 991, 1005, 1015, 1021, 1024, 1021, 1015, 1005, 991,
|
||||
973, 952, 926, 897, 864, 828, 789, 746, 700, 652,
|
||||
601, 548, 493, 435, 376, 316, 254, 191, 128, 64,
|
||||
0, -64, -128, -191, -254, -316, -376, -435, -493, -548,
|
||||
-601, -652, -700, -746, -789, -828, -864, -897, -926, -952,
|
||||
-973, -991, -1005, -1015, -1021, -1024, -1021, -1015, -1005, -991,
|
||||
-973, -952, -926, -897, -864, -828, -789, -746, -700, -652,
|
||||
-601, -548, -493, -435, -376, -316, -254, -191, -128, -64,
|
||||
};
|
||||
|
||||
#if 1
|
||||
const unsigned int toneVolumes[] = { 2, 4, 8, 12, 16 };
|
||||
#else
|
||||
const unsigned int toneVolumes[] = { 4000, 8000, 16000, 24000, 32000 };
|
||||
#endif
|
||||
|
||||
#if defined(SDCARD)
|
||||
const char * audioFilenames[] = {
|
||||
@@ -423,7 +434,7 @@ int AudioQueue::mixWav(AudioContext &context, AudioBuffer *buffer, int volume, u
|
||||
int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume, unsigned int fade)
|
||||
{
|
||||
AudioFragment & fragment = context.fragment;
|
||||
unsigned int duration = 0;
|
||||
int duration = 0;
|
||||
int result = 0;
|
||||
|
||||
if (fragment.tone.duration > 0) {
|
||||
@@ -432,9 +443,14 @@ int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume,
|
||||
int periods = DIM(context.state.tone.points) / ((AUDIO_SAMPLE_RATE / fragment.tone.freq) + 1);
|
||||
context.state.tone.count = (periods * AUDIO_SAMPLE_RATE) / fragment.tone.freq;
|
||||
if (context.state.tone.idx >= context.state.tone.count) context.state.tone.idx = 0;
|
||||
#if 1
|
||||
for (unsigned int i=0; i<context.state.tone.count; i++)
|
||||
context.state.tone.points[i] = sineValues[((DIM(sineValues)*periods*i)/context.state.tone.count) % DIM(sineValues)] * (toneVolumes[2+volume]);
|
||||
#else
|
||||
double t = (M_PI * 2 * periods) / context.state.tone.count;
|
||||
for (unsigned int i=0; i<context.state.tone.count; i++)
|
||||
context.state.tone.points[i] = sin(t*i) * (toneVolumes[2+volume]);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (fragment.tone.freqIncr)
|
||||
@@ -442,11 +458,23 @@ int AudioQueue::mixBeep(AudioContext &context, AudioBuffer *buffer, int volume,
|
||||
else
|
||||
fragment.tone.freq = 0;
|
||||
|
||||
duration = min<unsigned int>(AUDIO_BUFFER_DURATION, fragment.tone.duration);
|
||||
for (unsigned int i=0; i<duration*(AUDIO_BUFFER_SIZE/AUDIO_BUFFER_DURATION); i++) {
|
||||
duration = AUDIO_BUFFER_DURATION;
|
||||
int points = AUDIO_BUFFER_SIZE;
|
||||
bool end = false;
|
||||
|
||||
if (fragment.tone.duration <= AUDIO_BUFFER_DURATION) {
|
||||
duration = fragment.tone.duration;
|
||||
points = duration*(AUDIO_BUFFER_SIZE/AUDIO_BUFFER_DURATION);
|
||||
end = true;
|
||||
}
|
||||
|
||||
for (int i=0; i<points; i++) {
|
||||
mix(&buffer->data[i], context.state.tone.points[context.state.tone.idx], fade);
|
||||
context.state.tone.idx = context.state.tone.idx + 1;
|
||||
if (context.state.tone.idx >= context.state.tone.count) context.state.tone.idx = 0;
|
||||
if (context.state.tone.idx >= context.state.tone.count) {
|
||||
context.state.tone.idx = 0;
|
||||
if (end && i+DIM(context.state.tone.points) > points) break;
|
||||
}
|
||||
}
|
||||
|
||||
fragment.tone.duration -= duration;
|
||||
|
||||
+1
-1
@@ -104,7 +104,7 @@ class AudioContext {
|
||||
#endif
|
||||
|
||||
struct {
|
||||
uint16_t points[100];
|
||||
int16_t points[100];
|
||||
uint8_t count;
|
||||
uint8_t idx;
|
||||
uint16_t freq;
|
||||
|
||||
+63
-61
@@ -1877,7 +1877,7 @@ uint8_t checkTrim(uint8_t event)
|
||||
setTrimValue(phase, idx, after);
|
||||
#endif
|
||||
|
||||
#if defined (AUDIO)
|
||||
#if defined(AUDIO)
|
||||
// toneFreq higher/lower according to trim position
|
||||
// limit the frequency, range -125 to 125 = toneFreq: 19 to 101
|
||||
if (after > TRIM_MAX)
|
||||
@@ -3501,6 +3501,63 @@ uint8_t calcStickScroll( uint8_t index )
|
||||
}
|
||||
#endif
|
||||
|
||||
void opentxStart()
|
||||
{
|
||||
doSplash();
|
||||
|
||||
#if defined(PCBSKY9X) && defined(SDCARD) && !defined(SIMU)
|
||||
for (int i=0; i<500 && !Card_initialized; i++) {
|
||||
CoTickDelay(1); // 2ms
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CPUARM)
|
||||
eeLoadModel(g_eeGeneral.currModel);
|
||||
#endif
|
||||
|
||||
checkAlarm();
|
||||
checkAll();
|
||||
|
||||
if (g_eeGeneral.chkSum != evalChkSum()) {
|
||||
chainMenu(menuFirstCalib);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(CPUARM) || defined(PCBGRUVIN9X)
|
||||
void opentxClose()
|
||||
{
|
||||
#if defined(FRSKY)
|
||||
FRSKY_End();
|
||||
#endif
|
||||
|
||||
#if defined(SDCARD)
|
||||
closeLogs();
|
||||
sdDone();
|
||||
#endif
|
||||
|
||||
#if defined(HAPTIC)
|
||||
hapticOff();
|
||||
#endif
|
||||
|
||||
saveTimers();
|
||||
|
||||
#if defined(PCBSKY9X)
|
||||
uint32_t mAhUsed = g_eeGeneral.mAhUsed + Current_used * (488 + g_eeGeneral.currentCalib) / 8192 / 36;
|
||||
if (g_eeGeneral.mAhUsed != mAhUsed)
|
||||
g_eeGeneral.mAhUsed = mAhUsed;
|
||||
#endif
|
||||
|
||||
#if defined(CPUARM) && !defined(REVA)
|
||||
if (sessionTimer > 0)
|
||||
g_eeGeneral.globalTimer += sessionTimer;
|
||||
#endif
|
||||
|
||||
g_eeGeneral.unexpectedShutdown = 0;
|
||||
eeDirty(EE_GENERAL);
|
||||
eeCheck(true);
|
||||
}
|
||||
#endif
|
||||
|
||||
void perMain()
|
||||
{
|
||||
#if defined(SIMU)
|
||||
@@ -3642,10 +3699,9 @@ void perMain()
|
||||
#if defined(PCBTARANIS)
|
||||
static bool usbStarted = false;
|
||||
if (!usbStarted && usbPlugged()) {
|
||||
usbStarted = true;
|
||||
sdDone();
|
||||
eeCheck(true);
|
||||
opentxClose();
|
||||
usbStart();
|
||||
usbStarted = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4083,28 +4139,6 @@ void saveTimers()
|
||||
volatile rotenc_t g_rotenc[1] = {0};
|
||||
#endif
|
||||
|
||||
void opentxStart()
|
||||
{
|
||||
doSplash();
|
||||
|
||||
#if defined(PCBSKY9X) && defined(SDCARD) && !defined(SIMU)
|
||||
for (int i=0; i<500 && !Card_initialized; i++) {
|
||||
CoTickDelay(1); // 2ms
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CPUARM)
|
||||
eeLoadModel(g_eeGeneral.currModel);
|
||||
#endif
|
||||
|
||||
checkAlarm();
|
||||
checkAll();
|
||||
|
||||
if (g_eeGeneral.chkSum != evalChkSum()) {
|
||||
chainMenu(menuFirstCalib);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef SIMU
|
||||
|
||||
#if defined(CPUARM)
|
||||
@@ -4310,33 +4344,7 @@ void menusTask(void * pdata)
|
||||
lcd_clear();
|
||||
displayPopup(STR_SHUTDOWN);
|
||||
|
||||
#if defined(FRSKY)
|
||||
FRSKY_End();
|
||||
#endif
|
||||
|
||||
#if defined(SDCARD)
|
||||
closeLogs();
|
||||
#endif
|
||||
|
||||
#if defined(HAPTIC)
|
||||
hapticOff();
|
||||
#endif
|
||||
|
||||
saveTimers();
|
||||
|
||||
#if defined(PCBSKY9X)
|
||||
uint32_t mAhUsed = g_eeGeneral.mAhUsed + Current_used * (488 + g_eeGeneral.currentCalib) / 8192 / 36;
|
||||
if (g_eeGeneral.mAhUsed != mAhUsed)
|
||||
g_eeGeneral.mAhUsed = mAhUsed;
|
||||
#endif
|
||||
|
||||
if (sessionTimer > 0)
|
||||
g_eeGeneral.globalTimer += sessionTimer;
|
||||
|
||||
g_eeGeneral.unexpectedShutdown = 0;
|
||||
|
||||
eeDirty(EE_GENERAL);
|
||||
eeCheck(true);
|
||||
opentxClose();
|
||||
|
||||
lcd_clear();
|
||||
lcdRefresh();
|
||||
@@ -4481,15 +4489,9 @@ int main(void)
|
||||
|
||||
#if defined(PCBGRUVIN9X)
|
||||
// Time to switch off
|
||||
lcd_clear() ;
|
||||
lcd_clear();
|
||||
displayPopup(STR_SHUTDOWN);
|
||||
saveTimers();
|
||||
g_eeGeneral.unexpectedShutdown=0;
|
||||
eeDirty(EE_GENERAL);
|
||||
eeCheck(true);
|
||||
#if defined(SDCARD)
|
||||
closeLogs();
|
||||
#endif
|
||||
opentxClose();
|
||||
lcd_clear() ;
|
||||
lcdRefresh() ;
|
||||
pwrOff(); // Only turn power off if necessary
|
||||
|
||||
@@ -591,6 +591,7 @@ void lcdRefresh()
|
||||
}
|
||||
|
||||
#if defined(PCBTARANIS)
|
||||
void usbStart() { }
|
||||
ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct) { return SUCCESS; }
|
||||
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct) { return SUCCESS; }
|
||||
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct) { }
|
||||
|
||||
@@ -384,7 +384,6 @@ inline void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) {
|
||||
#define GPIO_ResetBits(GPIOx, pin) GPIOx->BSRRL &= ~pin
|
||||
#define GPIO_ReadInputDataBit(GPIOx, pin) (GPIOx->BSRRL & pin)
|
||||
#define RCC_AHB1PeriphClockCmd(...)
|
||||
#define GPIO_ReadInputDataBit(...) true
|
||||
#endif
|
||||
|
||||
#define configure_pins(...)
|
||||
|
||||
@@ -181,4 +181,7 @@ void pwrOff();
|
||||
|
||||
#define EEPROM_MASSSTORAGE() (0)
|
||||
|
||||
// Haptic driver
|
||||
#define hapticOff() // TODO hapticOn() cleaner ...
|
||||
|
||||
#endif
|
||||
|
||||
@@ -175,22 +175,26 @@ void disable_ssc();
|
||||
#define disable_dsm2(port) disable_ssc()
|
||||
|
||||
// SD driver
|
||||
#if !defined(SIMU)
|
||||
#define sdPoll10ms()
|
||||
void sdMountPoll();
|
||||
extern "C" {
|
||||
void init_SDcard();
|
||||
void sdInit();
|
||||
uint32_t sd_card_ready();
|
||||
uint32_t sdMounted();
|
||||
}
|
||||
#if defined(SIMU)
|
||||
#define sdInit()
|
||||
#define sdDone()
|
||||
#else
|
||||
#define sdPoll10ms()
|
||||
void sdMountPoll();
|
||||
extern "C" {
|
||||
void init_SDcard();
|
||||
void sdInit();
|
||||
void sdDone();
|
||||
uint32_t sd_card_ready();
|
||||
uint32_t sdMounted();
|
||||
}
|
||||
#endif
|
||||
|
||||
// WDT driver
|
||||
#if !defined(SIMU)
|
||||
#define wdt_disable()
|
||||
#define wdt_enable(x) WDT->WDT_MR = 0x3FFF207F
|
||||
#define wdt_reset() WDT->WDT_CR = 0xA5000001
|
||||
#define wdt_disable()
|
||||
#define wdt_enable(x) WDT->WDT_MR = 0x3FFF207F
|
||||
#define wdt_reset() WDT->WDT_CR = 0xA5000001
|
||||
#endif
|
||||
|
||||
// Backlight driver
|
||||
|
||||
@@ -1089,6 +1089,14 @@ void sdInit()
|
||||
Card_initialized = 1;
|
||||
}
|
||||
|
||||
void sdDone()
|
||||
{
|
||||
if (sdMounted()) {
|
||||
audioQueue.stopSD();
|
||||
f_mount(0, 0); // unmount SD
|
||||
}
|
||||
}
|
||||
|
||||
// Checks for card ready for read/write
|
||||
// returns 1 for YES, 0 for NO
|
||||
uint32_t sd_card_ready( void )
|
||||
|
||||
@@ -93,6 +93,7 @@ void delay_01us(uint16_t nb);
|
||||
// SD driver
|
||||
#if defined(SIMU)
|
||||
#define sdInit()
|
||||
#define sdDone()
|
||||
#else
|
||||
#define SD_IS_HC() (1)
|
||||
#define SD_GET_SIZE_MB() (0)
|
||||
|
||||
@@ -980,7 +980,6 @@ void sdDone()
|
||||
{
|
||||
if (sdMounted()) {
|
||||
audioQueue.stopSD();
|
||||
closeLogs();
|
||||
#if defined(DEBUG)
|
||||
f_close(&g_telemetryFile);
|
||||
#endif
|
||||
|
||||
@@ -119,12 +119,6 @@ void USBD_USR_DeviceConnected (void)
|
||||
*/
|
||||
void USBD_USR_DeviceDisconnected (void)
|
||||
{
|
||||
// TODO: Separate the shutdown operations in main() into a shutdown() function in opentx.cpp,
|
||||
// and call that before the NVIC_SystemReset()?
|
||||
g_eeGeneral.unexpectedShutdown = 0;
|
||||
eeDirty(EE_GENERAL);
|
||||
eeCheck(true);
|
||||
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user