//
// Copyright (c) 1999-2004 Darius Rad
//
// This file is part of the OpenOtto project.
//
// OpenOtto 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 2 of the License, or
// (at your option) any later version.
//

/// \file
/// \brief Software LLC for VPW bus


#include <assert.h>
#include <stdlib.h>

#include <otto/packet.h>
#include "llc.h"


static const char _ID_[] __attribute__ ((unused)) =
"$Id: llc_vpw.c,v 1.6 2004/07/07 16:14:01 alpha Exp $";


/// Bit timing for VPW bus
static const struct otto_llc_timing vpw_timing[] =
{
  //{ min, nom, max },
  { 34, 64, 96 },  ///< Tv1: short pulse
  { 96, 128, 163 },  ///< Tv2: long pulse
  { 163, 200, 239 },  ///< Tv3: SOF/EOD time
  { 239, 280, -1 },  ///< Tv4: EOF time
  { 239, 300, 1000000 },  ///< Tv5: BRK time
  { 280, 300, -1 },  ///< Tv6: IFS time
};


/// \brief Decode pulse into symbol
///
/// \return decoded symbol
static enum otto_llc_symbol
vpw_decode_symbol(
  const struct otto_llc_pulse bit  ///< Bit to decode
)
{
  enum otto_llc_symbol symbol;


  if( (bit.length > vpw_timing[5].min) && !bit.polarity )
  {
    symbol = OTTO_LLC_SYM_IFS;
  }
  else if( (bit.length > vpw_timing[4].min) && bit.polarity )
  {
    symbol = OTTO_LLC_SYM_BRK;
  }
  else if( (bit.length > vpw_timing[3].min) && !bit.polarity )
  {
    symbol = OTTO_LLC_SYM_EOF;
  }
  else if( (bit.length > vpw_timing[2].min) && (bit.length < vpw_timing[2].max) )
  {
    symbol = bit.polarity ? OTTO_LLC_SYM_SOF : OTTO_LLC_SYM_EOD;
  }
  else if( (bit.length > vpw_timing[1].min) && (bit.length < vpw_timing[1].max) )
  {
    symbol = bit.polarity ? OTTO_LLC_SYM_ZERO : OTTO_LLC_SYM_ONE;
  }
  else if( (bit.length > vpw_timing[0].min) && (bit.length < vpw_timing[0].max) )
  {
    symbol = bit.polarity ? OTTO_LLC_SYM_ONE : OTTO_LLC_SYM_ZERO;
  }
  else
  {
    symbol = OTTO_LLC_SYM_UNKNOWN;
  }

  return(symbol);
}



/// \brief Encodes symbol into data type for driver
static int
vpw_encode_symbol(
  struct otto_llc_pulse *bit,  ///< Encoded bit
  const enum otto_llc_symbol sym,  ///< Symbol to encode
  const struct otto_llc_pulse bit_prev,  ///< Previous encoded bit
  const enum otto_llc_symbol sym_prev  ///< Previous symbol
)
{
  assert( bit != NULL );

  bit->polarity = !bit_prev.polarity;

  switch( sym )
  {
    case OTTO_LLC_SYM_ZERO:
      bit->length = bit->polarity ? vpw_timing[1].nom : vpw_timing[0].nom;
      break;

    case OTTO_LLC_SYM_ONE:
      bit->length = bit->polarity ? vpw_timing[0].nom : vpw_timing[1].nom;
      break;

    case OTTO_LLC_SYM_SOF:
      assert( sym_prev == OTTO_LLC_SYM_IFS );
      assert( bit->polarity = 1 );
      bit->length = vpw_timing[2].nom;
      break;

    case OTTO_LLC_SYM_EOF:
      assert( bit->polarity = 0 );
      bit->length = vpw_timing[3].nom;
      break;

    case OTTO_LLC_SYM_EOD:
      assert( bit->polarity = 0 );
      bit->length = vpw_timing[2].nom;
      break;

    case OTTO_LLC_SYM_BRK:
      assert( bit->polarity = 1 );
      bit->length = vpw_timing[4].nom;
      break;

    case OTTO_LLC_SYM_IFS:
      assert( bit->polarity = 0 );
      bit->length = vpw_timing[4].nom;
      break;

    case OTTO_LLC_SYM_NB0:
      assert( bit->polarity = 1 );
      bit->length = vpw_timing[1].nom;
      break;

    case OTTO_LLC_SYM_NB1:
      assert( bit->polarity = 1 );
      bit->length = vpw_timing[0].nom;
      break;

    case OTTO_LLC_SYM_UNKNOWN:
      // nothing
      break;

    default:
      // nothing
      break;
  }

  // all symbols are single pulse
  return(0);
}


static struct otto_llc_format vpw_format =
{
  .name = "vpw",
  .desc = "Variable pulse width modulation",
  .decode_symbol = vpw_decode_symbol,
  .encode_symbol = vpw_encode_symbol,
};
