//
// 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 PWM bus


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

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


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


/// Bit timing for PWM bus
static const struct otto_llc_timing pwm_timing[] =
{
  //{ min, nom, max },
  { 6, 8, 11 },  ///< Tp1: active phase "1"
  { 14, 16, 19 },  ///< Tp2: active phase "0"
  { 22, 24, 27 },  ///< Tp3: bit time
  { 46, 48, 63 },  ///< Tp4: SOF/EOD time
  { 70, 72, -1 },  ///< Tp5: EOF time
  { -1, 96, -1 },  ///< Tp6: IFS time
  { 30, 32, 35 },  ///< Tp7: active SOF
  { 38, 40, 43 },  ///< Tp8: active BRK
  { -1, 120, -1 },  ///< Tp9: BRK to IFS time
  { 45, 48, 52 },  ///< Tp10: SOF to data rising edge
  { 4, -1, -1 },  ///< Tp11: passive to next rising edge
};


/// \todo
static enum otto_llc_symbol
pwm_decode_symbol(
  const struct otto_llc_pulse bit
)
{
  enum otto_llc_symbol symbol;


  symbol = OTTO_LLC_SYM_UNKNOWN;

  return(symbol);
}


/// \todo
/// \return number of pulses remaining for this symbol
static int
pwm_encode_symbol(
  struct otto_llc_pulse *bit,
  const enum otto_llc_symbol sym,
  const struct otto_llc_pulse bit_prev,
  const enum otto_llc_symbol sym_prev
)
{
  assert( bit != NULL );

  int ret;

  bit->polarity = !bit_prev.polarity;
  ret = 0;

  return(ret);
}


static struct otto_llc_format pwm_format =
{
  .name = "pwm",
  .desc = "Pulse width modulation",
  .decode_symbol = pwm_decode_symbol,
  .encode_symbol = pwm_encode_symbol,
};
