misc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <string.h>
  17. #include "misc.h"
  18. #include "settings.h"
  19. const uint8_t fm_radio_countdown_500ms = 2000 / 500; // 2 seconds
  20. const uint16_t fm_play_countdown_scan_10ms = 100 / 10; // 100ms
  21. const uint16_t fm_play_countdown_noscan_10ms = 1200 / 10; // 1.2 seconds
  22. const uint16_t fm_restore_countdown_10ms = 5000 / 10; // 5 seconds
  23. const uint8_t vfo_state_resume_countdown_500ms = 2500 / 500; // 2.5 seconds
  24. const uint8_t menu_timeout_500ms = 20000 / 500; // 20 seconds
  25. const uint16_t menu_timeout_long_500ms = 120000 / 500; // 2 minutes
  26. const uint8_t DTMF_RX_live_timeout_500ms = 6000 / 500; // 6 seconds live decoder on screen
  27. #ifdef ENABLE_DTMF_CALLING
  28. const uint8_t DTMF_RX_timeout_500ms = 10000 / 500; // 10 seconds till we wipe the DTMF receiver
  29. const uint8_t DTMF_decode_ring_countdown_500ms = 15000 / 500; // 15 seconds .. time we sound the ringing for
  30. const uint8_t DTMF_txstop_countdown_500ms = 3000 / 500; // 6 seconds
  31. #endif
  32. const uint8_t key_input_timeout_500ms = 8000 / 500; // 8 seconds
  33. const uint16_t key_repeat_delay_10ms = 400 / 10; // 400ms
  34. const uint16_t key_repeat_10ms = 80 / 10; // 80ms .. MUST be less than 'key_repeat_delay'
  35. const uint16_t key_debounce_10ms = 20 / 10; // 20ms
  36. const uint8_t scan_delay_10ms = 210 / 10; // 210ms
  37. const uint16_t dual_watch_count_after_tx_10ms = 3600 / 10; // 3.6 sec after TX ends
  38. const uint16_t dual_watch_count_after_rx_10ms = 1000 / 10; // 1 sec after RX ends ?
  39. const uint16_t dual_watch_count_after_1_10ms = 5000 / 10; // 5 sec
  40. const uint16_t dual_watch_count_after_2_10ms = 3600 / 10; // 3.6 sec
  41. const uint16_t dual_watch_count_noaa_10ms = 70 / 10; // 70ms
  42. #ifdef ENABLE_VOX
  43. const uint16_t dual_watch_count_after_vox_10ms = 200 / 10; // 200ms
  44. #endif
  45. const uint16_t dual_watch_count_toggle_10ms = 100 / 10; // 100ms between VFO toggles
  46. const uint16_t scan_pause_delay_in_1_10ms = 5000 / 10; // 5 seconds
  47. const uint16_t scan_pause_delay_in_2_10ms = 500 / 10; // 500ms
  48. const uint16_t scan_pause_delay_in_3_10ms = 200 / 10; // 200ms
  49. const uint16_t scan_pause_delay_in_4_10ms = 300 / 10; // 300ms
  50. const uint16_t scan_pause_delay_in_5_10ms = 1000 / 10; // 1 sec
  51. const uint16_t scan_pause_delay_in_6_10ms = 100 / 10; // 100ms
  52. const uint16_t scan_pause_delay_in_7_10ms = 3600 / 10; // 3.6 seconds
  53. const uint16_t battery_save_count_10ms = 10000 / 10; // 10 seconds
  54. const uint16_t power_save1_10ms = 100 / 10; // 100ms
  55. const uint16_t power_save2_10ms = 200 / 10; // 200ms
  56. #ifdef ENABLE_VOX
  57. const uint16_t vox_stop_count_down_10ms = 1000 / 10; // 1 second
  58. #endif
  59. const uint16_t NOAA_countdown_10ms = 5000 / 10; // 5 seconds
  60. const uint16_t NOAA_countdown_2_10ms = 500 / 10; // 500ms
  61. const uint16_t NOAA_countdown_3_10ms = 200 / 10; // 200ms
  62. const uint32_t gDefaultAesKey[4] = {0x4AA5CC60, 0x0312CC5F, 0xFFD2DABB, 0x6BBA7F92};
  63. const uint8_t gMicGain_dB2[5] = {3, 8, 16, 24, 31};
  64. bool gSetting_350TX;
  65. #ifdef ENABLE_DTMF_CALLING
  66. bool gSetting_KILLED;
  67. #endif
  68. bool gSetting_200TX;
  69. bool gSetting_500TX;
  70. bool gSetting_350EN;
  71. uint8_t gSetting_F_LOCK;
  72. bool gSetting_ScrambleEnable;
  73. enum BacklightOnRxTx_t gSetting_backlight_on_tx_rx;
  74. #ifdef ENABLE_AUDIO_BAR
  75. bool gSetting_mic_bar;
  76. #endif
  77. bool gSetting_live_DTMF_decoder;
  78. uint8_t gSetting_battery_text;
  79. bool gMonitor = false; // true opens the squelch
  80. uint32_t gCustomAesKey[4];
  81. bool bHasCustomAesKey;
  82. uint32_t gChallenge[4];
  83. uint8_t gTryCount;
  84. uint16_t gEEPROM_RSSI_CALIB[7][4];
  85. uint16_t gEEPROM_1F8A;
  86. uint16_t gEEPROM_1F8C;
  87. ChannelAttributes_t gMR_ChannelAttributes[FREQ_CHANNEL_LAST + 1];
  88. volatile uint16_t gBatterySaveCountdown_10ms = battery_save_count_10ms;
  89. volatile bool gPowerSaveCountdownExpired;
  90. volatile bool gSchedulePowerSave;
  91. volatile bool gScheduleDualWatch = true;
  92. volatile uint16_t gDualWatchCountdown_10ms;
  93. bool gDualWatchActive = false;
  94. volatile uint8_t gSerialConfigCountDown_500ms;
  95. volatile bool gNextTimeslice_500ms;
  96. volatile uint16_t gTxTimerCountdown_500ms;
  97. volatile bool gTxTimeoutReached;
  98. volatile uint16_t gTailToneEliminationCountdown_10ms;
  99. volatile uint8_t gVFOStateResumeCountdown_500ms;
  100. #ifdef ENABLE_NOAA
  101. volatile uint16_t gNOAA_Countdown_10ms;
  102. #endif
  103. bool gEnableSpeaker;
  104. uint8_t gKeyInputCountdown = 0;
  105. uint8_t gKeyLockCountdown;
  106. uint8_t gRTTECountdown_10ms;
  107. bool bIsInLockScreen;
  108. uint8_t gUpdateStatus;
  109. uint8_t gFoundCTCSS;
  110. uint8_t gFoundCDCSS;
  111. bool gEndOfRxDetectedMaybe;
  112. int16_t gVFO_RSSI[2];
  113. uint8_t gVFO_RSSI_bar_level[2];
  114. uint8_t gReducedService;
  115. uint8_t gBatteryVoltageIndex;
  116. bool gCssBackgroundScan;
  117. volatile bool gScheduleScanListen = true;
  118. volatile uint16_t gScanPauseDelayIn_10ms;
  119. #if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
  120. AlarmState_t gAlarmState;
  121. #endif
  122. uint16_t gMenuCountdown;
  123. bool gPttWasReleased;
  124. bool gPttWasPressed;
  125. uint8_t gKeypadLocked;
  126. bool gFlagReconfigureVfos;
  127. uint8_t gVfoConfigureMode;
  128. bool gFlagResetVfos;
  129. bool gRequestSaveVFO;
  130. uint8_t gRequestSaveChannel;
  131. bool gRequestSaveSettings;
  132. #ifdef ENABLE_FMRADIO
  133. bool gRequestSaveFM;
  134. #endif
  135. bool gFlagPrepareTX;
  136. bool gFlagAcceptSetting;
  137. bool gFlagRefreshSetting;
  138. #ifdef ENABLE_FMRADIO
  139. bool gFlagSaveFM;
  140. #endif
  141. bool g_CDCSS_Lost;
  142. uint8_t gCDCSSCodeType;
  143. bool g_CTCSS_Lost;
  144. bool g_CxCSS_TAIL_Found;
  145. #ifdef ENABLE_VOX
  146. bool g_VOX_Lost;
  147. bool gVOX_NoiseDetected;
  148. uint16_t gVoxResumeCountdown;
  149. uint16_t gVoxPauseCountdown;
  150. #endif
  151. bool g_SquelchLost;
  152. volatile uint16_t gFlashLightBlinkCounter;
  153. bool gFlagEndTransmission;
  154. uint8_t gNextMrChannel;
  155. ReceptionMode_t gRxReceptionMode;
  156. bool gRxVfoIsActive;
  157. #ifdef ENABLE_ALARM
  158. uint8_t gAlarmToneCounter;
  159. uint16_t gAlarmRunningCounter;
  160. #endif
  161. bool gKeyBeingHeld;
  162. bool gPttIsPressed;
  163. uint8_t gPttDebounceCounter;
  164. uint8_t gMenuListCount;
  165. uint8_t gBackup_CROSS_BAND_RX_TX;
  166. uint8_t gScanDelay_10ms;
  167. uint8_t gFSKWriteIndex;
  168. #ifdef ENABLE_NOAA
  169. bool gIsNoaaMode;
  170. uint8_t gNoaaChannel;
  171. #endif
  172. bool gUpdateDisplay;
  173. bool gF_LOCK = false;
  174. uint8_t gShowChPrefix;
  175. volatile bool gNextTimeslice;
  176. volatile uint8_t gFoundCDCSSCountdown_10ms;
  177. volatile uint8_t gFoundCTCSSCountdown_10ms;
  178. #ifdef ENABLE_VOX
  179. volatile uint16_t gVoxStopCountdown_10ms;
  180. #endif
  181. volatile bool gNextTimeslice40ms;
  182. #ifdef ENABLE_NOAA
  183. volatile uint16_t gNOAACountdown_10ms = 0;
  184. volatile bool gScheduleNOAA = true;
  185. #endif
  186. volatile bool gFlagTailToneEliminationComplete;
  187. #ifdef ENABLE_FMRADIO
  188. volatile bool gScheduleFM;
  189. #endif
  190. volatile uint8_t boot_counter_10ms;
  191. uint8_t gIsLocked = 0xFF;
  192. inline void FUNCTION_NOP() { ; }
  193. int32_t NUMBER_AddWithWraparound(int32_t Base, int32_t Add, int32_t LowerLimit, int32_t UpperLimit)
  194. {
  195. Base += Add;
  196. if (Base == 0x7fffffff || Base < LowerLimit)
  197. return UpperLimit;
  198. if (Base > UpperLimit)
  199. return LowerLimit;
  200. return Base;
  201. }
  202. unsigned long StrToUL(const char * str)
  203. {
  204. unsigned long ul = 0;
  205. for(uint8_t i = 0; i < strlen(str); i++){
  206. char c = str[i];
  207. if(c < '0' || c > '9')
  208. break;
  209. ul = ul * 10 + (uint8_t)(c-'0');
  210. }
  211. return ul;
  212. }
  213. sLevelAttributes GetSLevelAttributes(const int16_t rssi, const uint32_t frequency)
  214. {
  215. sLevelAttributes att;
  216. // S0 .. base level
  217. int16_t s0_dBm = -130;
  218. // all S1 on max gain, no antenna
  219. const int8_t dBmCorrTable[7] = {
  220. -5, // band 1
  221. -38, // band 2
  222. -37, // band 3
  223. -20, // band 4
  224. -23, // band 5
  225. -23, // band 6
  226. -16 // band 7
  227. };
  228. // use UHF/VHF S-table for bands above HF
  229. if(frequency > HF_FREQUENCY)
  230. s0_dBm-=20;
  231. att.dBmRssi = Rssi2DBm(rssi)+dBmCorrTable[FREQUENCY_GetBand(frequency)];
  232. att.sLevel = MIN(MAX((att.dBmRssi - s0_dBm) / 6, 0), 9);
  233. att.over = MIN(MAX(att.dBmRssi - (s0_dBm + 9*6), 0), 99);
  234. return att;
  235. }
  236. int Rssi2DBm(const uint16_t rssi)
  237. {
  238. return (rssi >> 1) - 160;
  239. }