// =====================================================================================================================================================================================================
// (c) 2021 Lynn Hansen, KU7Q															                                                                                                               |
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this  file, You can obtain one at https://mozilla.org/MPL/2.0/.  |
// =====================================================================================================================================================================================================



Firmware Design Notes for CTR2

Use this file to capture design considerations and oddities in CTR2

FFT TRANSPARENT MODE
1. If you leave the Home page for another page (like Keypad) that doesn't require periodic updates from the proc the HMI will lock up if it the FFT graph was left in Transparent mode when we left the Home page.
   a. Sending null pacjets the size of the fft graph when off the home page to (hopefully) eliminate this problem

================================================================================================================================================================================
SGTL5000_1 NOISE ON BOTH LINE-IN INPUTS

SYMPTOMS:
1. There is low level 'puttering' noise on the Headphone Output (and LineOut). This is caused (I think) by rounding errors in the 16-bit ineger ADC.
   a. Reduced the sgtl5000_1.dacVolume() from it's default of 1.0 to .9. This adds a small digital attenuation to the ADC input an reduces the low level noise.
2. Used right Line-in channel for mic input up to HMI v3.1 hardware - found that it is too noisy even with mic-bias turned off
   a. Redesigned input circuit to use MIC input on audio board - strap on board will just bypass a 2.2 uF cap to allow dc bias to electec mics
   b. Switch sgtl5000_1 input source between Line In and Mic when switching modes.
RESOLUTION:
1. Found that it's the ESP8266 putting noise out on the 3.3VA and 3.3V lines (in 5V line to a lesser extent). This gets into the audio board. Remote mounting it didn't help
2. Found that once the ESP8266 is initialized the noise drops considerably. Added code to Setup() to initialze it.

================================================================================================================================================================================

RX NOISE WHEN SENDING LONG CW BUFFERS
4/19/21
1. Found that requesting long tx buffer strings from HMI caused noise in Rx path when starting CW buffer
   a. Changed method of loading buffer to use gAction to index the selected buffer for tx

================================================================================================================================================================================

AUDIO LOCKUP PROBLEMS
1. Found waiting for tranparent reply when drawing FFT in UpdateFFT can cause audio to hang
2. 12/29/20: Experiencing intermittent lockups while keying - couldn't narrow it down to a specific routine. Seems like just adding additional code to the ISR routine changes it???
3. 1/11/21: Still having intermittant audio lockups - every 10 sec for about 1.5 seconds - just adding code anywhere seems to cause it
   a. Maybe related to SerialOut calls during ISR
4. 2/16/21: Adjusted ISR inteval from 1 mSec to 2 mSec - this seems to have fixed the audio lockup issues - finger crossed
   a. Didn't work
5. Found that when a audio crash occurs if I just add or delete ONE LINE OF CODE at start of Setup(), the problems goes away
   a. There is a sacrificial call to SerialOut() at the first line in setup() - add or delete it if the audio locks up
6. Determined that calling delay() during an ISR may be what's causing this audio lockup issue 
   a. Added myDelay() - it waits for ISRActive to go false before calling delay() - not sure it's working - still seeing audio lockups if I add debug print or take it out :(
   b. 5/24/21: Removed myDelay - use millis() loops for delay when needed
7. 5/24/21: Reduced AudioMem() from 200 to 50 - using AudioMemoryUsageMax() shows a max of 22 audio memory units used by program
8. 6/16/21: Moved audio adapter initialization code to top of Setup() and AudioMemory() to end of Setup() - suspected noise issue is due to location of AudioMemory() allocation???
9. 7/13/21: Updated TeensyDuino to v1.54. I haven't had an audio lockup since this update so I susspect (hope) there was a bug in the audio libraries that got fixed. Fingers crossed!

================================================================================================================================================================================

ANNOUNCE (say values)
1. gVocal is enabled by touching Announce button on Config page
2. hAnnounce will hold the specific things to announce based on bit values

================================================================================================================================================================================

FLEX RADIO
1. Flex Radio RF Power and Tune Power 
   a. The Flex radio API only updates the rfpower= and tunepower= values when it transmits
   b. This means that if you want to change them from the Flex Properties page, you must enter the new values then transmit (or start the ATU)
   c. When you start the ATU the Pwr Out and Tune Pwr values will change to 10 because that's the default tuner power. Once tuning has completed they will return to the correct values
   d. This bug may cause confusion especially if the user changes the power levels and doesn't transmit, then changes the sliders on SmartSDR
   ==> According to Eric at Flex, this is proper operation because the HMI is not bound to any GUI and is not a GUI itself so the radio doesn't know how to set the power levels until the HMI sends the Tx command
   e. Added note to FLex page to adjust power during Tx. This solves the above problem

2. Flex bugs
   a. THERE IS A PROBLEM IN SMARTSDR 2.6.2 THAT IF YOU CONNECT THE HMI TO THE FLEX THEN START UP SMARTSDR, THE RADIO WILL LOCK UP AND NEED TO BE POWER CYCLED TO REMOVE THE LATCHED ON IP CONNECTION - IT DOESN'T KNOW HOW TO DISCONNECT!
   b. I'm seeing SmartSDR lock up occasionally when connected to the HMI - random?

================================================================================================================================================================================

SCAN MDOES
1. When scanning Memory channels it takes 5 seconds for each channel because it takes 4 seconds to update the display for the new channel
   a) Blank channels take 5 seconds to cycle through too - it's the users responsibility to make sure their memory frequencies are contiguous

================================================================================================================================================================================

ANTENNA SELECTION
1. gAnt contains the address of the antenna for the selected band (as specified by gAntBand)
2. If gAntBand = 0xff then use the same gAnt for all bands
3. If gAntBand < bandMemMax then gAnt is stored in the bandMemAnt[] array - pick the correct antenna in CalcBand()
4. When addressing Ant Sw, leave ANT_SW_STROBE high after last write. It's tied to Inhibit. If you leave it low it will keep the last relay selected pulled in.

================================================================================================================================================================================

YAESU FTdx101 CAT ISSUES
1. The mode command (MDxx;) is not properly mapped in the FTdx101 (and probably other FTdx models)
   a. Instead of being MDxx; it is MCxx;
   b. Other CAT commands don't seem to work either
2. The Info command (IF;) returns back with an extra 0 (0x30) and 12 extra spaces (0x20) before the terminator (;, 0x3b)
   a. Setting the radioRxBytes in GetFreqMode() to 41 for now, it should be 28 according to the CAT spec

================================================================================================================================================================================

GENERAL CAT ISSUES
1. Found FT817 generates random single chrs on CAT while manually tuning radio
   a. Control value of radioMsgTimeoutDefault depending on radio protocol used - use 50 mS for Yaesu because of the short message length - use 150-250 for others base on msg length

================================================================================================================================================================================

GENERAL AUDIO ISSUES
1. Found that CAT data is noising Tx audio - causing pulsing white noise on tx cxr
   a. Turned off CAT polling for freq/mode changes during Tx

================================================================================================================================================================================

USING THE NEXTION SIMULATOR
1. Connect a 3.3v USB/TTL FTDI dongle to Gnd/TXD/RXD on controller's LOCAL DISPLAY - TXD to TXD and RXD to RXD
2. Power up controller - it will wait for display to come online
3. Start Nextion and load firmware for display
4. Click [Debug] in Nextion program
5. In simulator, click [Use MCU Inpt] radio button
   a. Set Com port to port of FTDI dongle
   b. Set Baud to 115000
   c. Click [Start]


================================================================================================================================================================================
