0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-04-27 22:01:26 +03:00

377 lines
12 KiB
C
Raw Permalink Normal View History

2023-09-09 08:03:56 +01:00
/* Copyright 2023 Dual Tachyon
* https://github.com/DualTachyon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "misc.h"
#include "settings.h"
2023-09-09 08:03:56 +01:00
const uint8_t obfuscate_array[16] = {
0x16, 0x6C, 0x14, 0xE6, 0x2E, 0x91, 0x0D, 0x40, 0x21, 0x35, 0xD5, 0x40, 0x13, 0x03, 0xE9, 0x80
};
2023-12-19 10:52:24 +00:00
const uint16_t tx_timeout_secs[16] =
{
(60 * 0) + 0,
(60 * 0) + 30,
(60 * 1) + 0,
(60 * 1) + 30,
(60 * 2) + 0,
(60 * 2) + 30,
(60 * 3) + 0,
(60 * 3) + 30,
(60 * 4) + 0,
(60 * 4) + 30,
(60 * 5) + 0,
(60 * 6) + 0,
(60 * 7) + 0,
(60 * 8) + 0,
(60 * 10) + 0,
(60 * 15) + 0
};
2023-10-08 20:23:37 +01:00
// ***********************************************
const uint8_t fm_resume_500ms = 2500 / 500; // 2.5 seconds
const uint8_t fm_radio_500ms = 2000 / 500; // 2 seconds
const uint16_t fm_play_scan_10ms = 60 / 10; // 60ms
const uint16_t fm_play_noscan_10ms = 1200 / 10; // 1.2 seconds
2023-10-08 20:23:37 +01:00
const uint8_t menu_timeout_500ms = 30000 / 500; // 30 seconds
const uint16_t menu_timeout_long_500ms = 120000 / 500; // 2 minutes
2023-11-10 16:45:07 +00:00
const uint16_t backlight_tx_rx_time_secs = 10; // 10 seconds
2023-10-08 20:23:37 +01:00
#ifdef ENABLE_DTMF_LIVE_DECODER
const uint8_t dtmf_rx_live_timeout_500ms = 6000 / 500; // 6 seconds live decoder on screen
#endif
2023-10-16 18:19:28 +01:00
const uint8_t dtmf_rx_timeout_500ms = 10000 / 500; // 10 seconds till we wipe the DTMF receiver
const uint8_t dtmf_decode_ring_500ms = 15000 / 500; // 15 seconds .. time we sound the ringing for
const uint8_t dtmf_txstop_500ms = 3000 / 500; // 6 seconds
2023-10-29 22:33:38 +00:00
const uint8_t serial_config_tick_500ms = 3000 / 500; // 3 seconds
2023-12-01 12:26:57 +00:00
const uint8_t key_input_timeout_500ms = 8000 / 500; // 8 seconds
#ifdef ENABLE_KEYLOCK
2023-10-23 14:02:54 +01:00
const uint8_t key_lock_timeout_500ms = 30000 / 500; // 30 seconds
#endif
2023-10-08 20:23:37 +01:00
2023-10-16 18:19:28 +01:00
const uint8_t key_debounce_10ms = 30 / 10; // 30ms
const uint8_t key_side_long_press_10ms = 1000 / 10; // 1 second
2023-11-22 11:21:53 +00:00
const uint8_t key_long_press_10ms = 320 / 10; // 320ms
2023-11-29 21:48:02 +00:00
const uint8_t key_repeat_initial_10ms = 200 / 10; // 200ms
2023-11-22 09:38:41 +00:00
const uint8_t key_repeat_fastest_10ms = 10 / 10; // 10ms
2023-11-22 11:21:53 +00:00
const uint16_t key_repeat_speedup_10ms = 2500 / 10; // speed-up key repeat once every 2.5 seconds
2023-10-08 20:23:37 +01:00
2023-10-30 11:23:56 +00:00
const uint16_t search_freq_css_10ms = 10000 / 10; // 10 seconds
const uint16_t search_10ms = 210 / 10; // 210ms .. don't reduce this
2023-10-16 18:19:28 +01:00
#ifdef ENABLE_VOX
const uint16_t dual_watch_delay_after_vox_10ms = 200 / 10; // 200ms
#endif
const uint16_t dual_watch_delay_after_tx_10ms = 7000 / 10; // 7 sec after TX ends
const uint16_t dual_watch_delay_noaa_10ms = 70 / 10; // 70ms
const uint16_t dual_watch_delay_toggle_10ms = 100 / 10; // 100ms between VFO toggles
const uint16_t scan_pause_code_10ms = 1000 / 10; // 1 sec
const uint16_t scan_pause_css_10ms = 500 / 10; // 500ms
const uint16_t scan_pause_ctcss_10ms = 200 / 10; // 200ms
const uint16_t scan_pause_cdcss_10ms = 300 / 10; // 300ms
const uint16_t scan_pause_freq_10ms = 100 / 10; // 100ms
const uint16_t scan_pause_chan_10ms = 200 / 10; // 200ms
2023-11-10 07:50:24 +00:00
const uint16_t power_save_pause_10ms = 10000 / 10; // 10 seconds
2023-10-16 18:19:28 +01:00
const uint16_t power_save1_10ms = 100 / 10; // 100ms
const uint16_t power_save2_10ms = 200 / 10; // 200ms
2023-09-21 23:06:47 +01:00
#ifdef ENABLE_VOX
const uint16_t vox_stop_10ms = 1000 / 10; // 1 second
#endif
2023-10-29 22:33:38 +00:00
const uint16_t noaa_tick_10ms = 5000 / 10; // 5 seconds
const uint16_t noaa_tick_2_10ms = 500 / 10; // 500ms
const uint16_t noaa_tick_3_10ms = 200 / 10; // 200ms
2023-09-25 13:27:52 +01:00
2023-10-08 20:23:37 +01:00
// ***********************************************
2023-11-02 10:00:51 +00:00
const uint32_t g_default_aes_key[4] = {0x4AA5CC60, 0x0312CC5F, 0xFFD2DABB, 0x6BBA7F92};
2023-11-02 10:00:51 +00:00
const uint8_t g_mic_gain_dB_2[5] = {3, 8, 16, 24, 31};
2023-11-10 07:31:50 +00:00
uint8_t g_mic_sensitivity_tuning;
2023-10-29 22:33:38 +00:00
bool g_monitor_enabled;
2023-11-02 10:00:51 +00:00
bool g_has_aes_key;
2023-10-08 20:23:37 +01:00
uint32_t g_challenge[4];
2023-09-09 08:03:56 +01:00
2023-11-10 07:50:24 +00:00
volatile uint16_t g_power_save_pause_tick_10ms = power_save_pause_10ms;
volatile bool g_power_save_pause_done;
volatile bool g_power_save_expired;
2023-10-29 22:33:38 +00:00
volatile uint16_t g_dual_watch_tick_10ms;
volatile bool g_dual_watch_delay_down_expired = true;
#if defined(ENABLE_UART)
volatile uint8_t g_serial_config_tick_500ms;
#endif
2023-10-08 20:23:37 +01:00
volatile bool g_next_time_slice_500ms;
2023-09-28 17:39:45 +01:00
volatile uint16_t g_tx_timer_tick_500ms;
2023-10-08 20:23:37 +01:00
volatile bool g_tx_timeout_reached;
2023-09-28 17:39:45 +01:00
volatile uint16_t g_tail_tone_elimination_tick_10ms;
2023-09-28 17:39:45 +01:00
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_NOAA
volatile uint16_t g_noaa_tick_10ms;
2023-09-09 08:03:56 +01:00
#endif
2023-09-13 18:45:52 +01:00
2023-10-31 10:07:29 +00:00
uint8_t g_update_screen_tick_500ms;
2023-10-29 22:33:38 +00:00
uint8_t g_key_input_count_down;
#ifdef ENABLE_KEYLOCK
2023-10-29 22:33:38 +00:00
uint8_t g_key_lock_tick_500ms;
#endif
2023-10-08 20:23:37 +01:00
uint8_t g_rtte_count_down;
2023-11-10 07:31:50 +00:00
2023-10-08 20:23:37 +01:00
uint8_t g_update_status;
2023-11-10 07:31:50 +00:00
bool g_update_display;
bool g_update_rssi;
bool g_update_menu;
bool g_password_locked;
2023-10-14 09:43:53 +01:00
uint8_t g_found_ctcss;
uint8_t g_found_cdcss;
2023-11-10 07:31:50 +00:00
2023-10-08 20:23:37 +01:00
bool g_end_of_rx_detected_maybe;
int16_t g_vfo_rssi[2];
uint8_t g_vfo_rssi_bar_level[2];
uint8_t g_reduced_service;
uint8_t g_battery_voltage_index;
2023-12-08 17:43:01 +00:00
#if defined(ENABLE_ALARM) || defined(ENABLE_TX_TONE_HZ)
2023-10-08 20:23:37 +01:00
alarm_state_t g_alarm_state;
2023-09-09 09:01:52 +01:00
#endif
2023-10-29 22:33:38 +00:00
uint16_t g_menu_tick_10ms;
2023-10-08 20:23:37 +01:00
bool g_flag_reconfigure_vfos;
uint8_t g_vfo_configure_mode;
bool g_flag_reset_vfos;
bool g_request_save_vfo;
uint8_t g_request_save_channel;
bool g_request_save_settings;
#ifdef ENABLE_FMRADIO
2023-10-08 20:23:37 +01:00
bool g_request_save_fm;
2023-11-10 07:31:50 +00:00
bool g_flag_save_fm;
2023-09-14 09:56:30 +01:00
#endif
2023-11-10 07:31:50 +00:00
2023-10-08 20:23:37 +01:00
bool g_flag_prepare_tx;
2023-10-14 09:43:53 +01:00
bool g_flag_accept_setting;
2023-10-14 09:43:53 +01:00
bool g_flag_save_vfo;
bool g_flag_save_settings;
bool g_flag_save_channel;
2023-11-10 07:31:50 +00:00
css_scan_mode_t g_css_scan_mode;
bool g_cdcss_lost;
uint8_t g_cdcss_code_type;
bool g_ctcss_lost;
bool g_cxcss_tail_found;
uint8_t g_ctcss_tail_phase_shift_rx;
#ifdef ENABLE_VOX
2023-10-08 20:23:37 +01:00
bool g_vox_lost;
2023-11-02 15:22:32 +00:00
bool g_vox_audio_detected;
2023-10-29 22:33:38 +00:00
uint16_t g_vox_resume_tick_10ms;
uint16_t g_vox_pause_tick_10ms;
2023-11-10 07:31:50 +00:00
volatile uint16_t g_vox_stop_tick_10ms;
#endif
2023-10-29 22:33:38 +00:00
2023-10-23 14:02:54 +01:00
bool g_squelch_open;
2023-11-10 07:31:50 +00:00
reception_mode_t g_rx_reception_mode;
2023-10-14 09:43:53 +01:00
2023-10-08 20:23:37 +01:00
uint8_t g_flash_light_state;
2023-10-28 22:02:07 +01:00
uint16_t g_flash_light_blink_tick_10ms;
2023-10-14 09:43:53 +01:00
2023-10-08 20:23:37 +01:00
bool g_flag_end_tx;
2023-11-10 07:31:50 +00:00
uint16_t g_low_battery_tick_10ms;
2023-10-08 20:23:37 +01:00
2023-11-08 11:27:26 +00:00
uint32_t g_scan_initial_lower;
uint32_t g_scan_initial_upper;
uint32_t g_scan_initial_step_size;
uint8_t g_scan_next_channel;
scan_next_chan_t g_scan_current_scan_list;
uint8_t g_scan_restore_channel;
uint32_t g_scan_restore_frequency;
bool g_scan_pause_time_mode; // set if we stopped in SCAN_RESUME_TIME mode
2023-11-07 08:48:32 +00:00
volatile uint16_t g_scan_tick_10ms;
2023-10-14 09:43:53 +01:00
scan_state_dir_t g_scan_state_dir;
2023-10-08 20:23:37 +01:00
2023-11-02 10:00:51 +00:00
uint8_t g_rx_vfo_num;
2023-10-08 20:23:37 +01:00
bool g_rx_vfo_is_active;
2023-11-02 10:00:51 +00:00
2023-12-08 17:43:01 +00:00
uint16_t g_alarm_tone_counter_10ms;
uint16_t g_alarm_running_counter_10ms;
2023-11-10 07:31:50 +00:00
2023-10-08 20:23:37 +01:00
uint8_t g_menu_list_count;
2023-11-02 10:00:51 +00:00
uint8_t g_backup_cross_vfo;
2023-09-09 08:03:56 +01:00
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_NOAA
2023-11-02 10:00:51 +00:00
bool g_noaa_mode;
2023-10-08 20:23:37 +01:00
uint8_t g_noaa_channel;
2023-11-10 07:31:50 +00:00
volatile uint16_t g_noaa_tick_10ms;
volatile bool g_schedule_noaa = true;
2023-09-09 08:03:56 +01:00
#endif
2023-11-10 07:31:50 +00:00
bool g_unhide_hidden;
2023-10-08 20:23:37 +01:00
volatile bool g_next_time_slice;
2023-11-10 07:31:50 +00:00
volatile bool g_next_time_slice_40ms;
volatile uint8_t g_found_cdcss_tick_10ms;
volatile uint8_t g_found_ctcss_tick_10ms;
2023-11-10 07:31:50 +00:00
2023-10-08 20:23:37 +01:00
volatile bool g_flag_tail_tone_elimination_complete;
2023-10-29 22:33:38 +00:00
volatile uint16_t g_boot_tick_10ms = 4000 / 10; // 4 seconds
2023-09-09 08:03:56 +01:00
2023-11-10 07:31:50 +00:00
int16_t g_current_rssi[2];
uint16_t g_current_glitch[2];
uint16_t g_current_noise[2];
2023-09-09 08:03:56 +01:00
2023-11-18 18:44:10 +00:00
// original QS front end register settings
// 0x03BE 00000 011 101 11 110
const uint8_t g_orig_lnas = 3; // 0dB
const uint8_t g_orig_lna = 5; // -4dB
const uint8_t g_orig_mixer = 3; // 0dB
const uint8_t g_orig_pga = 6; // -3dB
2023-11-10 07:31:50 +00:00
// ***************************
2023-11-02 10:00:51 +00:00
2023-10-08 20:23:37 +01:00
unsigned int get_RX_VFO(void)
{
2023-11-02 10:00:51 +00:00
unsigned int rx_vfo = g_eeprom.config.setting.tx_vfo_num;
if (g_eeprom.config.setting.cross_vfo == CROSS_BAND_CHAN_B)
rx_vfo = 0;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.cross_vfo == CROSS_BAND_CHAN_A)
rx_vfo = 1;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch == DUAL_WATCH_CHAN_B)
rx_vfo = 1;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch == DUAL_WATCH_CHAN_A)
rx_vfo = 0;
return rx_vfo;
}
2023-10-08 20:23:37 +01:00
unsigned int get_TX_VFO(void)
{
2023-11-02 10:00:51 +00:00
unsigned int tx_vfo = g_eeprom.config.setting.tx_vfo_num;
if (g_eeprom.config.setting.cross_vfo == CROSS_BAND_CHAN_B)
tx_vfo = 1;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.cross_vfo == CROSS_BAND_CHAN_A)
tx_vfo = 0;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch == DUAL_WATCH_CHAN_B)
tx_vfo = 1;
else
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch == DUAL_WATCH_CHAN_A)
tx_vfo = 0;
return tx_vfo;
}
2023-09-09 08:03:56 +01:00
void NUMBER_Get(char *pDigits, uint32_t *pInteger)
{
unsigned int i;
2023-10-20 18:00:36 +01:00
uint32_t mul = 10000000;
uint32_t val = 0;
2023-09-09 08:03:56 +01:00
for (i = 0; i < 8; i++)
{
if (pDigits[i] > 9)
break;
2023-10-20 18:00:36 +01:00
val += pDigits[i] * mul;
mul /= 10u;
2023-09-09 08:03:56 +01:00
}
2023-10-20 18:00:36 +01:00
*pInteger = val;
2023-09-09 08:03:56 +01:00
}
void NUMBER_ToDigits(uint32_t Value, char *pDigits)
{
unsigned int i;
2023-09-09 08:03:56 +01:00
for (i = 0; i < 8; i++)
{
const uint32_t Result = Value / 10U;
pDigits[7 - i] = Value - (Result * 10U);
Value = Result;
}
pDigits[8] = 0;
2023-09-09 08:03:56 +01:00
}
int32_t NUMBER_AddWithWraparound(int32_t Base, int32_t Add, int32_t LowerLimit, int32_t UpperLimit)
2023-09-09 08:03:56 +01:00
{
Base += Add;
if (Base == 0x7fffffff || Base < LowerLimit)
2023-09-09 08:03:56 +01:00
return UpperLimit;
2023-09-09 08:03:56 +01:00
if (Base > UpperLimit)
return LowerLimit;
2023-09-09 08:03:56 +01:00
return Base;
}
void NUMBER_trim_trailing_zeros(char *str)
{
if (str != NULL)
{
2023-10-24 04:43:26 +01:00
bool found_dp = false;
int i = 0;
while (i < 16 && str[i] != 0)
{
if (str[i] == '.')
found_dp = true;
i++;
}
if (found_dp)
{
i--;
while (i > 0 && (str[i] == '0' || str[i] == ' ') && str[i - 1] != '.')
str[i--] = 0;
}
}
}
2023-11-21 19:05:43 +00:00
// linear search, ascending, using addition
uint16_t NUMBER_isqrt(const uint32_t y)
{
uint16_t L = 0;
uint32_t a = 1;
uint32_t d = 3;
while (a <= y)
{
a += d; // (a + 1) ^ 2
d += 2;
L += 1;
}
return L;
}