/*
  libbd.c - Wrappers to board APIs for Little C interpreter

  Copyright (c) 2008-2010 Micromint USA LLC
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.
  * Neither the name of Micromint USA LLC nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "board.h"
#include "libbd.h"
#include "littlec.h"

#define ID_TO_PORT(x)  (x / 32)
#define ID_TO_PIN(x)   (x % 32)

extern char token[TOK_SIZE]; /* string representation of token */
extern char token_type; /* contains type of token */
extern char tok; /* internal representation of token */

/*  bd_btn() - Get button value */
int
bd_btn(void)
{
  int btn;
  /* Get function arguments and call API */
  eval_exp(&btn);
  return BTN_Get(btn);
}

/*  bd_init() - Initialize handles and ports */
int
bd_init(void)
{
  int fmt;
  /* Get function arguments and call API */
  eval_exp(&fmt);

  boardInit();
  return 0;
}

/*  bd_led() - Set LED value */
int
bd_led(void)
{
  int led, state;
  /* Get function arguments and call API */
  get_first_token();
  eval_exp(&led); /* LED number */
  if ((led < 0) || (led > 1))
    sntx_err(PARAM_ERR);
  get_token(); /* comma */
  if (*token != ',')
    sntx_err(SYNTAX);
  eval_exp(&state);
  get_last_token();
  state ? LED_Set(led) : LED_Clear(led);
  return state;
}

/*  bd_timer() - Returns timer value */
int
bd_timer(void)
{
  static int cnt = 0;
  int fmt;
  /* Get function arguments and call API */
  eval_exp(&fmt);
  return cnt++;
}

#if 0
/*  bd_setin() - Set port as input */
int
bd_setin(void)
{
  int id;
  /* Get function arguments and call API */
  eval_exp(&id);
  return pioSetIn(ID_TO_PORT(id), ID_TO_PIN(id));
}

/*  bd_setout() - Set port as output */
int
bd_setout(void)
{
  int id;
  /* Get function arguments and call API */
  eval_exp(&id);
  return pioSetOut(ID_TO_PORT(id), ID_TO_PIN(id));
}

/*  bd_in() - Get input port value */
int
bd_in(void)
{
  int id;
  /* Get function arguments and call API */
  eval_exp(&id);
  return pioGet(ID_TO_PORT(id), ID_TO_PIN(id));
}

/*  bd_out() - Set output port value */
int
bd_out(void)
{
  int id, state;
  /* Get function arguments and call API */
  get_first_token();
  eval_exp(&id); /* LED number */
  if ((id < 0) || (id > 128))
    sntx_err(PARAM_ERR);
  get_token(); /* comma */
  if (*token != ',')
    sntx_err(SYNTAX);
  eval_exp(&state);
  get_last_token();
  return pioSet(ID_TO_PORT(id), ID_TO_PIN(id), state);
}

/*  bd_adc(ch, r) - Get ADC value on channel ch using range r */
int
call_bd_adc(void)
{
    int ch, r;
    /* Get function arguments and call API */
    get_first_token();
    eval_exp(&ch); /* ADC channel */
    if ((ch < 0) || (ch > 4))
        sntx_err(PARAM_ERR);
    get_token(); /* comma */
    if (*token != ',')
        sntx_err(SYNTAX);
    eval_exp(&r);
    get_last_token();
    return bd_adc(ch, r);
}

/*  bd_lcd(msg) - Send string message to LCD */
int
call_bd_lcd(void)
{
    char *msg;
    int ret;
    /* Get function arguments and call API */
    get_first_token();
    get_token();
    if (token_type != STRING)
        sntx_err(PARAM_ERR);
    msg = token;
    ret = bd_lcd(msg);
    get_last_token();
    return ret;
}

/*  bd_out(port, val) - Send output value to port */
int
call_bd_out(void)
{
    int port, val;
    /* Get function arguments and call API */
    get_first_token();
    eval_exp(&port); /* port number */
    if ((port < 0) || (port > 7))
        sntx_err(PARAM_ERR);
    get_token(); /* comma */
    if (*token != ',')
        sntx_err(SYNTAX);
    eval_exp(&val);
    get_last_token();
    return bd_out(port, val);
}

/*  bd_pwm(cycle) - Send time shared PWM output to port */
int
call_bd_pwm(void)
{
    int cycle;
    /* Get function arguments and call API */
    eval_exp(&cycle);
    return bd_pwm(cycle);
}

/*  bd_pwm2(cycle) - Send fixed PWM output to port */
int
call_bd_pwm2(void)
{
    int cycle;
    /* Get function arguments and call API */
    eval_exp(&cycle);
    return bd_pwm2(cycle);
}

/*  bd_square(val) - Send square wave output to port */
int
bd_square(void)
{
  int val, j;
  /* Get function arguments and call API */
  eval_exp(&val);

  /* Square wave on 8 bits */
  while(1)
    {
      for (j=0; j<val; j++)
        {
          *port[PB] = 0xff;
         }
      for (j=0; j<val; j++)
         {
           *port[PB] = 0x00;
         }
    }

  return 0;
}
#endif
