/************************************************************************* * Copyright (C) 2019 by Justin Byers * * This file is part of clubdance_v2. * * clubdance_v2 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 3 of the License, or * (at your option) any later version. * * clubdance_v2 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 clubdance_v2. If not, see . *************************************************************************/ /** * @file dancepad.c * @author Justin Byers * @date 6 Aug 2019 * @brief Processes pad inputs and communicates with USB host. */ #include "dancepad.h" #include "usb.h" #include "usb_device_hid.h" #include "padhal.h" typedef union _INTPUT_CONTROLS_TYPEDEF { struct { struct { uint8_t square:1; uint8_t x:1; uint8_t o:1; uint8_t triangle:1; uint8_t L1:1; uint8_t R1:1; uint8_t L2:1; uint8_t R2:1;// uint8_t select:1; uint8_t start:1; uint8_t left_stick:1; uint8_t right_stick:1; uint8_t home:1; uint8_t :3; //filler } buttons; struct { uint8_t hat_switch:4; uint8_t :4;//filler } hat_switch; struct { uint8_t X; uint8_t Y; uint8_t Z; uint8_t Rz; } analog_stick; } members; uint8_t val[7]; } INPUT_CONTROLS; // USB data must exist within the USB RAM memory space INPUT_CONTROLS joystick_input __at(0x500); // handle to the last data transmission - allows us to check if it completed USB_VOLATILE USB_HANDLE lastTransmission = 0; void DANCEPAD_Initialize() { lastTransmission = 0; //enable the HID endpoint USBEnableEndpoint(JOYSTICK_EP, USB_IN_ENABLED | USB_HANDSHAKE_ENABLED | USB_DISALLOW_SETUP); } void DANCEPAD_Tasks() { // do not start another transmission if the previous one isn't completed if(HIDTxHandleBusy(lastTransmission)) return; /* * populate & send a HID report to the host */ //Buttons joystick_input.val[0] = PADHAL_GetButtons(); joystick_input.val[1] = 0x00; //Hat switch joystick_input.val[2] = 0x08; //Analog sticks joystick_input.val[3] = 0x80; joystick_input.val[4] = 0x80; joystick_input.val[5] = 0x80; joystick_input.val[6] = 0x80; //Send the 8 byte packet over USB to the host. lastTransmission = HIDTxPacket(JOYSTICK_EP, (uint8_t*)&joystick_input, sizeof(joystick_input)); }