
#ifndef JOYSWIS_H_INCLUDED
#define JOYSWIS_H_INCLUDED

#include "Joystick.h"
#include "usbjoystick.h"   // joydata_struct


// Public numbers and structures live in Joystick.h (exported). CMHG
// only builds the string table OS_SWINumberFromString/ToString use; it
// generates no C constants for the SWIs, so module_swi() switches on those
// numbers. Their order must match cmhg.USBJoystickHdr's swi-decoding-table.
#define USBJoystickSWI_Base  0x43F40
#define USBJoystickSWI_Name  Joystick

#define MODULE_SWI(name) (name - USBJoystickSWI_Base)

// Joystick_Read result ranges. The 8-bit read returns Y in R0 bits 0-7, X in
// bits 8-15 and the buttons in bits 16-31; the 16-bit read returns Y in R0
// bits 0-15, X in bits 16-31, and the buttons in R1.
#define Joystick_Read_Buttons           32

#define Joystick_Read8_Count            255   // for deriving the slope from a device's range
#define Joystick_Read8_Min              -127
#define Joystick_Read8_Max               127
#define Joystick_Read8_Rest                0
#define Joystick_Read8_Hat                64    // value of a HAT/D-pad at an extreme

#define Joystick_Read16_Count         65536
#define Joystick_Read16_Min                0
#define Joystick_Read16_Max            65535
#define Joystick_Read16_Mid            32768    // single-axis centre
#define Joystick_Read16_HatMin         12288
#define Joystick_Read16_HatMid         32768
#define Joystick_Read16_HatMax         53247
#define Joystick_Read16_AxesRest       ((int)0x80008000)    // R0 with both axes centred
#define Joystick_Read16_ButtonsRest        0


_kernel_oserror* swi_joystick_read(_kernel_swi_regs *r);
_kernel_oserror* joystick_read_8bit(_kernel_swi_regs *r, uint32_t i);
_kernel_oserror* joystick_read_16bit(_kernel_swi_regs *r, uint32_t i);

int32_t scaled_value_8(struct axisdata_struct *a);
int32_t scaled_value_16(struct axisdata_struct *a);
int32_t scaled_value_pc(struct axisdata_struct *a);


_kernel_oserror* swi_joystick_status(_kernel_swi_regs *r);
uint32_t encode_stick_serial_port(int32_t i);


_kernel_oserror* swi_joystick_enumerate(_kernel_swi_regs *r);
_kernel_oserror* swi_joystick_device_info(_kernel_swi_regs *r);
_kernel_oserror* swi_joystick_axis_info(_kernel_swi_regs *r);
_kernel_oserror* swi_joystick_axis_values(_kernel_swi_regs *r);

void joyapi_build_guid(uint32_t slot, uint8_t guid[16]);
uint32_t joyapi_capabilities(uint32_t slot);

#endif
