// ROVotron transmitter main program header file
// (C) 2010 David Forbes

#ifndef _RTX_H_
#define _RTX_H_

//------------ Macros ------------------------------- //

// this is a macro shortcut for reversing the bit order 
#define bitrev(c)	c = (c & 0x0F) << 4 | (c & 0xF0) >> 4; \
					c = (c & 0x33) << 2 | (c & 0xCC) >> 2; \
					c = (c & 0x55) << 1 | (c & 0xAA) >> 1;

#define Delay(x) DELAY = x; while(--DELAY){ Nop(); Nop(); }

/***** Array sizes *****/
#define NANALOG 11		// number of analog control pairs on PS2 plus zero
#define NBUTTONS 21		// number of buttons on PS2 controller plus box plus zero

#define MOM 1			// mode for momentary switch control 
#define ONOFF 2			// mode for on-button/off-button switch control 

/***** RTXA Ports *****/
#define pin_BoxBtn1			PORTAbits.RA0		// in	Box button 1
#define pin_BoxBtn2			PORTAbits.RA1		// in	Box button 2
#define pin_BoxBtn3			PORTAbits.RA2		// in	Box button 3
#define pin_BoxBtn4			PORTAbits.RA3		// in	Box button 4
#define pin_BoxBtn5			PORTAbits.RA4		// in	Box button 5
#define pin_BoxBtn6			PORTAbits.RA5		// in	Box button 6
#define pin_BoxLED1			LATBbits.LATB0		// out	Box LED 1
#define pin_BoxLED2			LATBbits.LATB1		// out	Box LED 2
#define pin_BoxLED3			LATBbits.LATB2		// out	Box LED 3
#define pin_BoxLED4			LATBbits.LATB3		// out	Box LED 4
#define pin_Tether_TXE		LATBbits.LATB5		// out	tx enable
#define pin_PS2_ACK			PORTCbits.RC0		// in	acknowledge (not needed?)
#define pin_PS2_ATT_A		LATCbits.LATC1		// out	Controller A attention
#define pin_PS2_ATT_B		LATCbits.LATC2		// out	Controller B attention
#define pin_PS2_SCK			LATCbits.LATC3		// out	SPI clk
#define pin_PS2_SDI			PORTCbits.RC4		// in	SPI d in
#define pin_PS2_SDO			LATCbits.LATC5		// out	SPI d out
#define pin_Tether_TX		LATCbits.LATC6		// out	serial out
#define pin_Tether_RX		PORTCbits.RC7		// in	serial in
#define pin_LCD_RS			LATEbits.LATE0		// out	LCD reg sel
#define pin_LCD_WR			LATEbits.LATE1		// out	LCD Write low
#define pin_LCD_E			LATEbits.LATE2		// out	LCD Enable stb
							
/***** Buttons *****/
// these macros can be used as meaningful shortcuts into
// the response bytes in a PS2's return packet.
#define PS2_Select			PS2_byte1.bit7
#define PS2_L3				PS2_byte1.bit6  //  joystick buttons 
#define PS2_R3				PS2_byte1.bit5  //     "        "
#define PS2_Start			PS2_byte1.bit4
#define PS2_Up				PS2_byte1.bit3
#define PS2_Right			PS2_byte1.bit2
#define PS2_Down			PS2_byte1.bit1
#define PS2_Left			PS2_byte1.bit0
#define PS2_L2				PS2_byte2.bit7
#define PS2_R2				PS2_byte2.bit6
#define PS2_L1				PS2_byte2.bit5
#define PS2_R1				PS2_byte2.bit4
#define PS2_Triangle		PS2_byte2.bit3
#define PS2_Circle			PS2_byte2.bit2
#define PS2_Cross			PS2_byte2.bit1
#define PS2_Square			PS2_byte2.bit0

/****Analog controls*****/
#define PS2_RightStickX		response[6]
#define PS2_RightStickY		response[7]
#define PS2_LeftStickX		response[8]
#define PS2_LeftStickY		response[9]
#define PS2_RightPress		response[10]
#define PS2_LeftPress		response[11]
#define PS2_UpPress			response[12]
#define PS2_DownPress		response[13]
#define PS2_TrianglePress	response[14]
#define PS2_CirclePress		response[15]
#define PS2_CrossPress		response[16]
#define PS2_SquarePress		response[17]
#define PS2_L1Press			response[18]
#define PS2_R1Press			response[19]
#define PS2_L2Press			response[20]
#define PS2_R2Press			response[21]

/***** Types *****/
// the union lets us access the bytes coming back from a PS2
// either bit by bit or by a whole byte.  same memory location,
// just two different names.
typedef union {
  struct {
    unsigned bit7:1;
    unsigned bit6:1;
    unsigned bit5:1;
    unsigned bit4:1;
    unsigned bit3:1;
    unsigned bit2:1;
    unsigned bit1:1;
    unsigned bit0:1;
  };
  struct {
    unsigned char byte;
  };
} uint8; 

// ------------------ defines ----------------------- //


// name the buttons for the control code
enum {L_UP_B = 1, L_DOWN_B, L_LEFT_B, L_RIGHT_B,
	TRIANGLE_B, CIRCLE_B, CROSS_B, SQUARE_B,
	L_TRIG_1_B, L_TRIG_2_B, R_TRIG_1_B, R_TRIG_2_B, 
	L_JOY_B, R_JOY_B};

//----------------- Prototypes ----------------------- //

void write_eeprom (unsigned char addr, unsigned char data_byte);
unsigned char read_eeprom (unsigned char addr);
void write_conf_to_eeprom(void);
char read_conf_from_eeprom(void);
void translate_volts_to_telems(void);
void display_telem(char chan);
void display_screen(void);
void display_motor_screen(void);
void display_servo_screen(void);
void display_switch_screen(void);
void display_telem_screen(void);
void display_field_open(char disp_field);
void display_field_close(char disp_field);
void move_field(char button);
void modify_index_field(char button);
void modify_char_field(void);
void modify_int_field(void);
char get_button_push(void);
void delay_2sec(void);
void config_loop(void);
void Initial(void);
void SetLineXLCD(char line);
char atoxdigit(char chr);
int atox(char *str, char n);
void translate_response_to_controls(void);
void translate_controls_to_commands(void);
void build_command_msg(char *msg);
void initialize_controller(char chan);
void ps2_txrx(unsigned char data_out,unsigned char *reg_in);
void rw_ps2_packet(char chan, rom char *command); 
void display_controls(char row);
void display_commands(unsigned char *mots, unsigned char *sws, unsigned char *sers);
void display_all_telems(void);
				
#endif
