// Phil_G on most forums, philg@talk21.com  http://www.singlechannel.co.uk
// Simple encoder for converted period GG sets using on-board GG Re-coder & Tobe Rand clone,
// Propo rudder, propo elevator  & progressive throttle
// Separate trimpots on A6 & A7 for rudder & elevator travel adjustment

#define throttle_increment 5

static int ppm = 0;                 // PPM output pin for DIY-more board
static int sw_throttleUp = 9;       // Throttle Up toggle
static int sw_throttleDown = 10;    // Throttle Down toggle
static int sw_btn = 11;             // calibrate

int ppmPulse = 300, neutralPulse = 1200, raw, ch, calibrated = 1, startup = 1, stickcalHi[] = {0, 0, 0, 0}, stickcalLo[] = {1023, 1023, 1023, 1023};
float r_rate = 0, e_rate = 0, chtemp, channel[] = {0, 0, 0, 0, 0, 0, 8000}; // last is sync
float throttle = -500;
byte reverse[] = {0, 0, 0, 0, 0, 0};

#include <EEPROM.h>

void setup()  {
  noInterrupts();
  pinMode(ppm, OUTPUT);
  pinMode(sw_throttleUp, INPUT_PULLUP);
  pinMode(sw_throttleDown, INPUT_PULLUP);
  pinMode(sw_btn, INPUT_PULLUP);
}

void loop() {
  while (startup == 1 && digitalRead(sw_btn) == 0) {
    // calibrate sticks
    calibrated = 0;
    for (int stick = 0; stick < 2; ++stick) {
      raw = analogRead(stick);
      if (raw > stickcalHi[stick]) stickcalHi[stick] = raw;
      if (raw < stickcalLo[stick]) stickcalLo[stick] = raw;
    }
  }
  if (startup == 1 && calibrated == 0) {
    for (ch = 0; ch < 2; ch++) {
      EEPROMWriteInt(ch * 4, stickcalLo[ch]);
      EEPROMWriteInt(ch * 4 + 2, stickcalHi[ch]);
    }
    calibrated = 1;
  }
  if (startup == 1) {
    for (ch = 0; ch < 2; ch++) {
      stickcalLo[ch] = EEPROMReadInt(ch * 4);
      stickcalHi[ch] = EEPROMReadInt(ch * 4 + 2);
      reverse[ch] = EEPROM.read(ch + 16) & 1;
    }
    reverse[3] = 0; // no throttle reverse for safety
  }

  for (ch = 0; ch < 2; ch++) {
    channel[ch] = map(analogRead(ch), stickcalLo[ch], stickcalHi[ch], -500, 500);
    channel[ch] = constrain(channel[ch], -600, 600);
  }

  // Progressive throttle
  if (digitalRead(sw_throttleUp) == 0 && throttle < 500) throttle += throttle_increment;
  if (digitalRead(sw_throttleDown) == 0 && throttle > -500) throttle -= throttle_increment;
  if (startup == 1) throttle = -500;
  channel[3] = throttle;

  // check for reversing, stick over on power-up
  if (startup == 1) {
    for (ch = 0; ch < 2; ch++) {
      if (channel[ch] > 420 || channel[ch] < -420) {
        reverse[ch] ^= B00000001;
        EEPROM.write(16 + ch, reverse[ch]);
      }
    }
  }

  // end of startup routines
  startup = 0;

  // read rates pots
  r_rate = map(analogRead(A6), 0, 1023, 100, 10);
  r_rate /= 100;
  channel[0] *= r_rate;

  e_rate = map(analogRead(A7), 0, 1023, 100, 10);
  e_rate /= 100;
  channel[1] *= e_rate;

  // format the frame, still AERTxx at this point
  for (ch = 0; ch < 6; ch++) {
    channel[ch] += neutralPulse;
    if (reverse[ch] == 1) channel[ch] = 2400 - channel[ch];
  }

  channel[6] = 10000;
  channel[5] = 1200;
  channel[4] = 1200;
  //channel[3] is throttle
  channel[2] = 1200;
  //channel[1] is elevator
  //channel[0] is rudder

  chtemp = channel[3]; channel[3] = channel[2]; channel[2] = chtemp; //                                           change channel order to Futaba AETR
  //chtemp=channel[3]; channel[3]=channel[2]; channel[2]=channel[1]; channel[1]=channel[0]; channel[0]=chtemp; // change channel order to JR TAER

  //send ppm frame, last channel holds sync value
  for (int ch = 0; ch < 7; ch++) {
    digitalWrite(ppm, HIGH);
    delayMicroseconds(ppmPulse);
    digitalWrite(ppm, LOW);
    delayMicroseconds(channel[ch]);
  }
}

// This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, int p_value)
{
  byte lowByte = p_value % 256;
  byte highByte = p_value / 256;
  EEPROM.write(p_address, lowByte);
  EEPROM.write(p_address + 1, highByte);
}

//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
{
  byte lowByte = EEPROM.read(p_address);
  byte highByte = EEPROM.read(p_address + 1);
  return lowByte + highByte * 256;
}








