/*
 * $Id: psx.h 218 2005-10-04 00:55:09Z anders $
 * 
 * Copyright (C) 2005 Anders Dubgaard <anders@dubgaard.net>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _PSX_H
#define _PSX_H 1

#include <avr/signal.h>
#include "common.h"
#include "timer.h"

/* Pin definitions */
#define DATA PC0  /* data,        In,  uC <-- joy */
#define CMD  PC1  /* command,     Out, uC --> joy */
#define ATT  PC2  /* attention,   Out, uC --> joy */
#define CLK  PC3  /* clock,       Out, uC --> joy */

/* Macros to set pins high or low. */
#define clk_hi() (setbit(PORTC, CLK))
#define clk_lo() (clrbit(PORTC, CLK))
#define att_hi() (setbit(PORTC, ATT))
#define att_lo() (clrbit(PORTC, ATT))
#define cmd_hi() (setbit(PORTC, CMD))
#define cmd_lo() (clrbit(PORTC, CMD))

/* PSX Controller IDs
 * DIGIPAD and ANALOG_RED are the most common types around. */
#define DIGIPAD      0x41
#define ANALOG_RED   0x73
#define NEGCON       0x23
#define ANALOG_GREEN 0x53
#define PSX_MOUSE    0x12

/* The protocol consists of 5 to 9 bytes. The 1st and 3rd byte never changes;
 * Byte 1 = 0xff and byte 3 = 0x54 ( = "here comes the data")
 * - so they go to /dev/null ;-) */
typedef struct {
	uint8_t type;      /* 2nd */
	uint8_t buttons;   /* 4th */
	uint8_t buttons2;  /*  |  */
	uint8_t r_horz;    /*  |  */
	uint8_t r_vert;    /*  |  */
	uint8_t l_horz;    /*  V  */
	uint8_t l_vert;    /* 9th */
} psx_data;

void psx_init(void);
void start_psx_timer(void);
int check_psx_type(int);
volatile psx_data *get_psx_data(void);
void disable_psx_int(void);
void enable_psx_int(void);
uint8_t psx_pwm_calc(uint8_t axis_pos);

volatile psx_data joydata;
volatile unsigned char *psx_arr;    
volatile int psx_arr_len;

#endif  // _PSX_H
