/* * Copyright (C) EdgeTX * * Based on code named * opentx - https://github.com/opentx/opentx * th9x - http://code.google.com/p/th9x * er9x - http://code.google.com/p/er9x * gruvin9x - http://code.google.com/p/gruvin9x * * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * 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. */ #include "edgetx.h" #include "switches.h" #include "tasks/mixer_task.h" #include "hal/audio_driver.h" #include "os/time.h" #if defined(VIDEO_SWITCH) #include "videoswitch_driver.h" #if defined(SIMU) void switchToRadio() {}; void switchToVideo() {}; #endif #endif CustomFunctionsContext modelFunctionsContext = { 0 }; CustomFunctionsContext globalFunctionsContext = { 0 }; #if defined(DEBUG) /* * This is a test function for debugging purpose, you may insert there your code and compile with the option DEBUG=YES */ void testFunc() { #ifdef SIMU printf("testFunc\n"); fflush(stdout); #endif // for testing the WD reset uncomment the following line // while (1); } #endif PLAY_FUNCTION(playValue, mixsrc_t idx) { if (IS_FAI_FORBIDDEN(idx)) return; if (idx == MIXSRC_NONE) return; getvalue_t val = getValue(idx); idx = abs(idx); // Don't need negative form any longer if (idx >= MIXSRC_FIRST_TELEM) { TelemetrySensor & telemetrySensor = g_model.telemetrySensors[(idx-MIXSRC_FIRST_TELEM) / 3]; uint8_t attr = 0; // Preserve the sign int sign = (val >= 0) ? 1 : -1; val = abs(val); if (telemetrySensor.prec > 0) { if (telemetrySensor.prec == 2) { if (val >= 5000) { val = divRoundClosest(val, 100); } else { val = divRoundClosest(val, 10); attr = PREC1; } } else { if (val >= 500) { val = divRoundClosest(val, 10); } else { attr = PREC1; } } } val *= sign; // Reapply sign if needed PLAY_NUMBER(val, telemetrySensor.unit == UNIT_CELLS ? UNIT_VOLTS : telemetrySensor.unit, attr); } else if (idx >= MIXSRC_FIRST_TIMER && idx <= MIXSRC_LAST_TIMER) { int flag = 0; if (abs(val) > LONG_TIMER_DURATION) { flag = PLAY_LONG_TIMER; } PLAY_DURATION(val, flag); } else if (idx == MIXSRC_TX_TIME) { PLAY_DURATION(val * 60, PLAY_TIME); } else if (idx == MIXSRC_TX_VOLTAGE) { PLAY_NUMBER(val, UNIT_VOLTS, PREC1); #if defined(LUMINOSITY_SENSOR) } else if (idx == MIXSRC_LIGHT) { PLAY_NUMBER(val, UNIT_RAW, 0); #endif } else { if (idx <= MIXSRC_LAST_CH) { val = calcRESXto100(val); } PLAY_NUMBER(val, 0, 0); } } void playCustomFunctionFile(const CustomFunctionData * sd, uint8_t id) { if (sd->play.name[0] != '\0') { char filename[sizeof(SOUNDS_PATH) + LEN_FUNCTION_NAME + sizeof(SOUNDS_EXT)] = SOUNDS_PATH "/"; strncpy(filename + SOUNDS_PATH_LNG_OFS, currentLanguagePack->id, 2); strncpy(filename + sizeof(SOUNDS_PATH), sd->play.name, LEN_FUNCTION_NAME); filename[sizeof(SOUNDS_PATH) + LEN_FUNCTION_NAME] = '\0'; strcat(filename + sizeof(SOUNDS_PATH), SOUNDS_EXT); PLAY_FILE(filename, sd->func == FUNC_BACKGND_MUSIC ? PLAY_BACKGROUND : 0, id); } } bool isRepeatDelayElapsed(const CustomFunctionData * functions, CustomFunctionsContext & functionsContext, uint8_t index) { const CustomFunctionData * cfn = &functions[index]; tmr10ms_t tmr10ms = get_tmr10ms(); int8_t repeatParam = CFN_PLAY_REPEAT(cfn); // hold prompts during startup: silence window is the floor, extended while permanent Lua scripts load bool startupBusy = !IS_SILENCE_PERIOD_ELAPSED(); #if defined(LUA) if (luaState >= INTERPRETER_RELOAD_PERMANENT_SCRIPTS && luaState < INTERPRETER_RUNNING) startupBusy = true; #endif if (startupBusy) { if (repeatParam == CFN_PLAY_REPEAT_NOSTART) { functionsContext.lastFunctionTime[index] = tmr10ms; // '!1x': suppress at startup } else { return false; // defer until startup settles, don't stamp } } if (!functionsContext.lastFunctionTime[index] || (repeatParam && repeatParam!=CFN_PLAY_REPEAT_NOSTART && (signed)(tmr10ms-functionsContext.lastFunctionTime[index])>=100*repeatParam)) { functionsContext.lastFunctionTime[index] = tmr10ms; return true; } else { return false; } } static bool isUIFunction(uint16_t f, int16_t p) { return f == FUNC_LOGS || f == FUNC_SET_SCREEN || f == FUNC_SCREENSHOT || f == FUNC_BACKLIGHT #if defined(AUDIO) || f == FUNC_VOLUME #endif #if defined(HARDWARE_TOUCH) || f == FUNC_DISABLE_TOUCH #endif #if defined(KEYS_LOCK_KEY1) && defined(KEYS_LOCK_KEY2) || f == FUNC_DISABLE_KEYS #endif || (f == FUNC_RESET && p == FUNC_RESET_FLIGHT); } void evalFunctions(CustomFunctionData * functions, CustomFunctionsContext & functionsContext) { MASK_FUNC_TYPE newActiveFunctions = 0; MASK_CFN_TYPE newActiveSwitches = 0; #if defined(FUNCTION_SWITCHES) g_model.cfsResetSFState(); #endif uint8_t playFirstIndex = (functions == g_model.customFn ? 1 : 1+MAX_SPECIAL_FUNCTIONS); #define PLAY_INDEX (i+playFirstIndex) #if defined(OVERRIDE_CHANNEL_FUNCTION) for (uint8_t i=0; i= FUNC_RESET_PARAM_FIRST_TELEM) { uint8_t item = CFN_PARAM(cfn) - FUNC_RESET_PARAM_FIRST_TELEM; if (item < MAX_TELEMETRY_SENSORS) { telemetryItems[item].clear(); } } break; case FUNC_SET_TIMER: timerSet(CFN_TIMER_INDEX(cfn), CFN_PARAM(cfn)); break; case FUNC_SET_FAILSAFE: setCustomFailsafe(CFN_PARAM(cfn)); break; #if defined(DANGEROUS_MODULE_FUNCTIONS) case FUNC_RANGECHECK: case FUNC_BIND: { unsigned int moduleIndex = CFN_PARAM(cfn); if (moduleIndex < NUM_MODULES) { moduleState[moduleIndex].mode = 1 + CFN_FUNC(cfn) - FUNC_RANGECHECK; } break; } #endif #if defined(GVARS) case FUNC_ADJUST_GVAR: if (CFN_GVAR_MODE(cfn) == FUNC_ADJUST_GVAR_CONSTANT) { SET_GVAR(CFN_GVAR_INDEX(cfn), CFN_PARAM(cfn), mixerCurrentFlightMode); } else if (CFN_GVAR_MODE(cfn) == FUNC_ADJUST_GVAR_GVAR) { SET_GVAR(CFN_GVAR_INDEX(cfn), GVAR_VALUE(CFN_PARAM(cfn), getGVarFlightMode(mixerCurrentFlightMode, CFN_PARAM(cfn))), mixerCurrentFlightMode); } else if (CFN_GVAR_MODE(cfn) == FUNC_ADJUST_GVAR_INCDEC) { if (!functionsContext.isFunctionSwitchActive(i)) { SET_GVAR(CFN_GVAR_INDEX(cfn), limit(MODEL_GVAR_MIN(CFN_GVAR_INDEX(cfn)), GVAR_VALUE(CFN_GVAR_INDEX(cfn), getGVarFlightMode( mixerCurrentFlightMode, CFN_GVAR_INDEX(cfn))) + CFN_PARAM(cfn), MODEL_GVAR_MAX(CFN_GVAR_INDEX(cfn))), mixerCurrentFlightMode); } } else if (CFN_PARAM(cfn) >= MIXSRC_FIRST_TRIM && CFN_PARAM(cfn) <= MIXSRC_LAST_TRIM) { trimGvar[CFN_PARAM(cfn) - MIXSRC_FIRST_TRIM] = CFN_GVAR_INDEX(cfn); } else { if (CFN_GVAR_MODE(cfn) == FUNC_ADJUST_GVAR_SOURCE) SET_GVAR(CFN_GVAR_INDEX(cfn), limit(MODEL_GVAR_MIN(CFN_GVAR_INDEX(cfn)), calcRESXto100(getValue(CFN_PARAM(cfn))), MODEL_GVAR_MAX(CFN_GVAR_INDEX(cfn))), mixerCurrentFlightMode); else SET_GVAR(CFN_GVAR_INDEX(cfn), limit(MODEL_GVAR_MIN(CFN_GVAR_INDEX(cfn)), getValue(CFN_PARAM(cfn)), MODEL_GVAR_MAX(CFN_GVAR_INDEX(cfn))), mixerCurrentFlightMode); } break; #endif case FUNC_PLAY_SOUND: case FUNC_PLAY_TRACK: case FUNC_PLAY_VALUE: #if defined(HAPTIC) case FUNC_HAPTIC: #endif { if (isRepeatDelayElapsed(functions, functionsContext, i)) { if (!IS_PLAYING(PLAY_INDEX)) { if (CFN_FUNC(cfn) == FUNC_PLAY_SOUND) { AUDIO_PLAY(AU_SPECIAL_SOUND_FIRST + CFN_PARAM(cfn)); } else if (CFN_FUNC(cfn) == FUNC_PLAY_VALUE) { PLAY_VALUE(CFN_PARAM(cfn), PLAY_INDEX); } #if defined(HAPTIC) else if (CFN_FUNC(cfn) == FUNC_HAPTIC) { haptic.event(AU_SPECIAL_SOUND_LAST + CFN_PARAM(cfn)); } #endif else { playCustomFunctionFile(cfn, PLAY_INDEX); } } } break; } case FUNC_BACKGND_MUSIC: if (!(newActiveFunctions & (1 << FUNCTION_BACKGND_MUSIC))) { newActiveFunctions |= (1 << FUNCTION_BACKGND_MUSIC); if (!IS_PLAYING(PLAY_INDEX)) { playCustomFunctionFile(cfn, PLAY_INDEX); } } break; case FUNC_BACKGND_MUSIC_PAUSE: newActiveFunctions |= (1 << FUNCTION_BACKGND_MUSIC_PAUSE); break; #if defined(VARIO) case FUNC_VARIO: newActiveFunctions |= (1u << FUNCTION_VARIO); break; #endif #if defined(FUNCTION_SWITCHES) case FUNC_PUSH_CUST_SWITCH: if (CFN_PARAM(cfn)) { // Duration is set if (! CFN_VAL2(cfn) ) { // Duration not started yet CFN_VAL2(cfn) = time_get_ms() + CFN_PARAM(cfn) * 100; g_model.cfsSetSFState(CFN_CS_INDEX(cfn), 1); } else if (time_get_ms() < (uint32_t)CFN_VAL2(cfn) ) { // Still within push duration g_model.cfsSetSFState(CFN_CS_INDEX(cfn), 1); } } else { // No duration set g_model.cfsSetSFState(CFN_CS_INDEX(cfn), 1); } break; #endif #if defined(PXX2) case FUNC_RACING_MODE: if (isRacingModeEnabled()) { newActiveFunctions |= (1u << FUNCTION_RACING_MODE); } break; #endif #if defined(AUDIO_MUTE_GPIO) case FUNC_DISABLE_AUDIO_AMP: newActiveFunctions |= (1u << FUNCTION_DISABLE_AUDIO_AMP); break; #endif #if defined(VIDEO_SWITCH) case FUNC_LCD_TO_VIDEO: switchToVideo(); videoEnabled = true; break; #endif #if defined(DEBUG) case FUNC_TEST: testFunc(); break; #endif } newActiveSwitches |= ((MASK_CFN_TYPE)1 << i); } else { #if defined(FUNCTION_SWITCHES) if (CFN_FUNC(cfn) == FUNC_PUSH_CUST_SWITCH) { // Handling duration after function is active if (time_get_ms() < (uint32_t)CFN_VAL2(cfn)) { g_model.cfsSetSFState(CFN_CS_INDEX(cfn), 1); } else { CFN_VAL2(cfn) = 0; } } #endif functionsContext.lastFunctionTime[i] = 0; #if defined(DANGEROUS_MODULE_FUNCTIONS) if (functionsContext.isFunctionSwitchActive(i)) { switch (CFN_FUNC(cfn)) { case FUNC_RANGECHECK: case FUNC_BIND: { unsigned int moduleIndex = CFN_PARAM(cfn); if (moduleIndex < NUM_MODULES) { moduleState[moduleIndex].mode = 0; } break; } } } #endif } } } #if defined(VIDEO_SWITCH) if (!videoEnabled) switchToRadio(); #endif functionsContext.activeSwitches = newActiveSwitches; functionsContext.activeFunctions = newActiveFunctions; } void evalUIFunctions(CustomFunctionData * functions, CustomFunctionsContext & functionsContext) { MASK_FUNC_TYPE newActiveFunctions = 0; MASK_CFN_TYPE newActiveSwitches = 0; for (uint8_t i=0; i(0, g_eeGeneral.speakerVolume + VOLUME_LEVEL_DEF, VOLUME_LEVEL_MAX); } } #endif } const char* funcGetLabel(uint8_t func) { switch(func) { case FUNC_OVERRIDE_CHANNEL: return STR_SF_SAFETY; case FUNC_TRAINER: return STR_SF_TRAINER; case FUNC_INSTANT_TRIM: return STR_SF_INST_TRIM; case FUNC_RESET: return STR_SF_RESET; case FUNC_SET_TIMER: return STR_SF_SET_TIMER; #if defined(GVARS) case FUNC_ADJUST_GVAR: return STR_ADJUST_GVAR; #endif case FUNC_VOLUME: return STR_SF_VOLUME; case FUNC_SET_FAILSAFE: return STR_SF_FAILSAFE; case FUNC_RANGECHECK: return STR_SF_RANGE_CHECK; case FUNC_BIND: return STR_SF_MOD_BIND; #if defined(AUDIO) case FUNC_PLAY_SOUND: return STR_SOUND; #endif case FUNC_PLAY_TRACK: return STR_PLAY_TRACK; case FUNC_PLAY_VALUE: return STR_PLAY_VALUE; #if defined(LUA) case FUNC_PLAY_SCRIPT: return STR_SF_PLAY_SCRIPT; #endif case FUNC_BACKGND_MUSIC: return STR_SF_BG_MUSIC; case FUNC_BACKGND_MUSIC_PAUSE: return STR_SF_BG_MUSIC_PAUSE; #if defined(VARIO) case FUNC_VARIO: return STR_SF_VARIO; #endif #if defined(HAPTIC) case FUNC_HAPTIC: return STR_SF_HAPTIC; #endif case FUNC_LOGS: return STR_SF_LOGS; case FUNC_BACKLIGHT: #if OLED_SCREEN return STR_BRIGHTNESS; #else return STR_SF_BACKLIGHT; #endif case FUNC_SCREENSHOT: return STR_SF_SCREENSHOT; case FUNC_RACING_MODE: return STR_SF_RACING_MODE; #if defined(COLORLCD) case FUNC_DISABLE_TOUCH: return STR_SF_DISABLE_TOUCH; #endif #if defined(KEYS_LOCK_KEY1) && defined(KEYS_LOCK_KEY2) case FUNC_DISABLE_KEYS: return STR_SF_DISABLE_KEYS; #endif case FUNC_SET_SCREEN: return STR_SF_SET_SCREEN; #if defined(AUDIO_MUTE_GPIO) case FUNC_DISABLE_AUDIO_AMP: return STR_SF_DISABLE_AUDIO_AMP; #endif case FUNC_RGB_LED: return STR_SF_RGBLEDS; #if defined(VIDEO_SWITCH) case FUNC_LCD_TO_VIDEO: return STR_SF_LCD_TO_VIDEO; #endif #if defined(FUNCTION_SWITCHES) case FUNC_PUSH_CUST_SWITCH: return STR_SF_PUSH_CUST_SWITCH; #endif #if defined(DEBUG) case FUNC_TEST: return STR_SF_TEST; #endif default: return STR_EMPTY; } }