/*********************************************************************************
 *
 *  lora_aprs_node_pico is a LoRa APRS KISS modem with additional PE1RXF telemetry
 *  capabilities. It runs on a Raspberry Pi Pico.
 *
 * (C)2023 M.T. Konstapel https://meezenest.nl/mees
 *
 * This file is part of lora_aprs_node_pico.
 *
 *    lora_aprs_node_pico is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    lora_aprs_node_pico 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.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with lora_aprs_node_pico. If not, see <https://www.gnu.org/licenses/>.
 *
 **********************************************************************************/
#ifndef CONFIG_H
  #define CONFIG_H

  #define MCU_VARIANT RPI_PICO

  #define MTU  255
  #define CMD_L       4
  int     lastRssi = -292;
  uint8_t lastRssiRaw = 0x00;
  size_t  readLength = 0;

  #if MCU_VARIANT == RPI_PICO
    const int pinNSS = 10;
    const int pinNRST = 9;
    const int pinDIO0 = 2;
    // const int pinLedRx = 5;
    // const int pinLedTx = 4;

  #endif

  #define OFF 0
  #define ON 1
    
  #define CR		13
  
  const long serialBaudRate   = 38400;
  const int  rssiOffset      = 292;

  const int  loraRxTurnaround = 50;

  uint8_t txBuffer[MTU];
  uint8_t rxBuffer[MTU];

  uint32_t statRx = 0;
  uint32_t statTx = 0;

  bool outboundReady = false;

  bool statSignalDetected = false;
  bool statSignalSynced   = false;
  bool statRxOngoing      = false;
  bool dcd                = false;
  bool dcdLed             = false;
  bool dcdWaiting         = false;
  uint16_t dcdCount       = 0;
  uint16_t dcdThreshold   = 15;

  uint32_t statusIntervalms = 3;
  uint32_t lastStatusUpdate = 0;

  // Status flags
  const uint8_t SIG_DETECT = 0x01;
  const uint8_t SIG_SYNCED = 0x02;
  const uint8_t RX_ONGOING = 0x04;
  
  // The size of this struc should be a exactly 256 bytes (or a multiple), which is the FLASH_PAGE_SIZE of the RPi pico's flash memory
  // Struct can not be bigger than 4kbyte
  struct aprssettings {
      uint8_t ValidFlashData = 0x5A;  // Indicates flash contains valid data - 1 bytes
      uint8_t MyCall[10] = { 'P','E','1','R','X','F','-','6', 0, 0} ;      // 10 bytes
      uint8_t ServerCall[10] = { 'P','E','1','R','X','F','-','3', 0, 0} ;  // 10 bytes
      uint8_t Destination[10] = { 'A','P','Z','M','D','M', 0, 0, 0 ,0} ;   // 10 bytes
      uint8_t Path1[10] = { 0,'I','D','E','1','-', '1', 0, 0, 0} ;         // 10 bytes
      uint8_t Path2[10] = { 0,'I','D','E','2','-', '2', 0, 0 ,0} ;         // 10 bytes
            
      uint8_t FirmwareVersion[20] = { 'V','2',',','C','o','n','t','r', 'o','l','l','e','r',' ','0','1', 0, 0, 0, 0} ;
                                                                        // 20 bytes
                                                                       
      // Default LoRa settings
      uint16_t  loraSpreadingFactor = 12;                               // 2 bytes
      uint16_t  loraPreamble        = 8;                                // 2 bytes
      uint16_t  loraCodingRate      = 5;                                // 2 bytes
      uint16_t  loraTxPower         = 17;                               // 2 bytes
      uint16_t  loraPaSelect        = 1;                                // 2 bytes
      uint32_t  loraBandwidth       = 125E3;                            // 4 bytes
      uint32_t  loraFrequency       = 433775000;                        // 4 bytes

      // Telemetry settings
      /* Time between telemetry transmissions
       * 10 min =  6e+8 us
       * 20 min = 12e+8 us
       * 30 min = 18e+8 us
       * 40 min = 24e+8 us
       * 50 min = 30e+8 us
       * 60 min = 36e+8 us
      */
      uint64_t  TelemetryPeriod     = 0xFFFFFFFFFFFFFFFF;                             // 8 bytes, default all ones means telemetry disabled
      
                                                                 // Total 97 bytes
      
      uint8_t   FillerData[146];// was 155
  } AprsSettings;
  
  struct status {
      bool PowerSupply24V;
      bool PowerSupply12V;
      bool PowerSupply5V;
      bool ControlRelay;
      
      uint8_t StatusString[6] = { '0','0','0','0','0',0};
      
      uint8_t DescriptionString[20] ={ 'N','C',',','C','n','t','r',',','5','V',',','1','2','V',',','2','4','V',0,0};

      // Payload for APRS mesage can be up to 67 bytes long. Plus one for the NULL terminator used by C.
      uint8_t TelemetryString[68];

      uint8_t TelemetryDescriptionString[9] = { 'T','e','m','p',',','H','u','m',0};
      uint8_t TelemetryUnitString[8] = { 'd','e','g',' ','C',',','%',0};
      
      uint8_t KissMode = OFF;
  } Status;
  


#endif
