diff --git a/include/input_mapper.h b/include/input_mapper.h new file mode 100644 index 0000000..7211713 --- /dev/null +++ b/include/input_mapper.h @@ -0,0 +1,27 @@ +#ifndef INPUT_MAPPER_H +#define INPUT_MAPPER_H + +#include "stdint.h" + +namespace InputMapper +{ + void begin(); + + void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y); + + void mapTriggers(uint32_t values[2]); + + // 0, 1 - triggers dunno if triggers will be dual stage + // 4, 5 - start, select = xinput + // 6, 7 - trackpads = xinput L3 & R3 + // 0, 1 << 8 - bumpers = xinput + // 2 << 8 - home = xinput + // 4 - 7 << 8 - grips = xinput ABXY + void mapButtons(uint16_t buttons); + + // void mapGyro(); + + void sendReport(); +} + +#endif \ No newline at end of file diff --git a/lib/usb_device/include/usb_device.h b/lib/usb_device/include/usb_device.h index ce231e3..5c50cc6 100644 --- a/lib/usb_device/include/usb_device.h +++ b/lib/usb_device/include/usb_device.h @@ -15,7 +15,8 @@ class USB_Device uint8_t msg_type:8; // byte 0 uint8_t msg_len:8; // byte 1 - uint16_t buttons:16; // bytes 2 and 3 + uint8_t dpad:4; // byte 2 first nibble + uint16_t buttons:12; // byte 2 second nibble and byte 3 uint8_t trigger_left:8; // byte 4 uint8_t trigger_right:8; // byte 5 int16_t lx:16; // byte 6 @@ -38,7 +39,7 @@ class USB_Device void begin(); void end(); - void buttons(uint32_t buttons); + void buttons(uint16_t buttons); void button(uint8_t button, bool val); void dpad(uint8_t dir); diff --git a/lib/usb_device/src/usb_device.cpp b/lib/usb_device/src/usb_device.cpp index d7148b4..3ccec42 100644 --- a/lib/usb_device/src/usb_device.cpp +++ b/lib/usb_device/src/usb_device.cpp @@ -26,7 +26,7 @@ void USB_Device::end() HID_Custom_DeInit(); } -void USB_Device::buttons(uint32_t buttons) +void USB_Device::buttons(uint16_t buttons) { xinput_report.buttons = buttons; } @@ -45,7 +45,7 @@ void USB_Device::button(uint8_t button, bool val) void USB_Device::dpad(uint8_t dir) { - xinput_report.buttons = dir & 0b1111; + xinput_report.dpad = dir & 0b1111; } void USB_Device::joystick_left(int16_t x, int16_t y) diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp new file mode 100644 index 0000000..1e268dd --- /dev/null +++ b/src/input_mapper.cpp @@ -0,0 +1,103 @@ +#include "input_mapper.h" + +#include "usb_device.h" +#include "touch_controls_all.h" + +namespace InputMapper +{ + USB_Device device; + + TouchJoystick tjoystick_right; + TouchJoystick tjoystick_left; + TouchDpad tdpad_right; + TouchDpad tdpad_left; + + TouchControl* tcontrols[] = + { + &tjoystick_right, + &tdpad_right, + }; + + uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*); + + + void begin() + { + float ppmX = 1872 / 62.5; + float ppmY = 3276 / 103.9; + int32_t pos_x = 31.25 * ppmX; + int32_t pos_y = (103.9 - 31.25) * ppmY; + int32_t pos_r = 70 * ppmX / 2; + int32_t dead_zone_inner = 3 * ppmX; + int32_t dead_zone_outer = 13 * ppmX; + + tjoystick_right.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); + tjoystick_right.setDeadZoneInner(dead_zone_inner); + tjoystick_right.setDeadZoneOuter(dead_zone_outer); + tjoystick_right.setInvertX(); + //tjoystick_right.setInvertY(); + + pos_x = (62.5 - 20.636) * ppmX; + pos_y = 20.636 * ppmY; + pos_r = 45 * ppmX / 2; + tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR8); + tdpad_right.setDeadZoneInner(dead_zone_inner); + tdpad_right.setInvertX(); + tdpad_right.setInvertY(); + + //tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); + //tjoystick_left.setDeadZoneInner(dead_zone_inner); + //tjoystick_left.setDeadZoneOuter(dead_zone_outer); + + device.begin(); + } + + void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y) + { + for (uint8_t c = 0; c < num_controls; ++c) + { + int8_t res = tcontrols[c]->touch(fid, x, y); + if (res < 0) + { + break; + } + + switch(tcontrols[c]->getControlType()) + { + case TouchControl::CT_NONE: + break; + + case TouchControl::CT_JOYSTICK: + //Serial.printf("%i, %i\n", ((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); + //device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); + device.joystick_right(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); + break; + + case TouchControl::CT_DPAD: + device.dpad(((TouchDpad*)tcontrols[c])->getButton()); + break; + } + + if (res > 0) + { + break; + } + } + } + + void mapTriggers(uint32_t values[2]) + { + //device.trigger_left(values[0]); + device.trigger_right(values[1]); + } + + void mapButtons(uint16_t buttons) + { + device.buttons(buttons >> 4); + } + + void sendReport() + { + device.sendReport(); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ea40cb4..9b92406 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,38 +1,19 @@ #include -//#include "usbd_hid_custom_if.h" - -#include "usb_device.h" -USB_Device device; - #include "trackpad.h" -TrackPad trackpad_right(0); -TrackPad trackpad_left(1); +#include "input_mapper.h" -void int_touchpad_right(){trackpad_right.int_on_clock();} -void int_touchpad_left(){trackpad_left.int_on_clock();} +const uint8_t pin_trigger[2] = {0, PA3}; -const uint8_t TRIGGER_RIGHT_PIN = PA3; -const uint8_t TRACKPAD_CLICK_RIGHT_PIN = PB4; -const uint8_t DATA_PIN_right = PB9; -const uint8_t CLOCK_PIN_right = PB8; -const uint8_t DATA_PIN_left = PB7; -const uint8_t CLOCK_PIN_left = PB6; +const uint8_t pin_trackpad_click[2] = {0, PB4}; -#include "touch_controls_all.h" -TouchJoystick tjoystick_right; -TouchJoystick tjoystick_left; -TouchDpad tdpad_right; -TouchDpad tdpad_left; +const uint8_t pin_trackpad_data[2] = {PB7, PB9}; +const uint8_t pin_trackpad_clock[2] = {PB6, PB8}; +TrackPad trackpad[2]; // 0 - left, 1 - right -TouchControl* tcontrols[] = -{ - &tjoystick_right, - &tdpad_right, -}; - -uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*); +void int_touchpad_0(){trackpad[0].int_on_clock();} +void int_touchpad_1(){trackpad[1].int_on_clock();} void setup() { @@ -42,43 +23,19 @@ void setup() Serial.begin(256000); - pinMode(TRIGGER_RIGHT_PIN, INPUT_ANALOG); - pinMode(TRACKPAD_CLICK_RIGHT_PIN, INPUT_PULLDOWN); + // Init Hardware + pinMode(pin_trigger[1], INPUT_ANALOG); + pinMode(pin_trackpad_click[1], INPUT_PULLDOWN); - attachInterrupt(CLOCK_PIN_right, int_touchpad_right, FALLING); - trackpad_right.initialize(CLOCK_PIN_right, DATA_PIN_right); + //attachInterrupt(pin_trackpad_clock[0], int_touchpad_0, FALLING); + //trackpad[0].initialize(pin_trackpad_clock[0], pin_trackpad_data[0]); + + attachInterrupt(pin_trackpad_clock[1], int_touchpad_1, FALLING); + trackpad[1].initialize(pin_trackpad_clock[1], pin_trackpad_data[1]); + - //attachInterrupt(CLOCK_PIN_left, int_touchpad_left, FALLING); - //trackpad_left.initialize(CLOCK_PIN_left, DATA_PIN_left); - - float ppmX = trackpad_right.getMaxY() / 62.5; - float ppmY = trackpad_right.getMaxX() / 103.9; - int32_t pos_x = 31.25 * ppmX; - int32_t pos_y = (103.9 - 31.25) * ppmY; - int32_t pos_r = 70 * ppmX / 2; - int32_t dead_zone_inner = 3 * ppmX; - int32_t dead_zone_outer = 13 * ppmX; - - tjoystick_right.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); - tjoystick_right.setDeadZoneInner(dead_zone_inner); - tjoystick_right.setDeadZoneOuter(dead_zone_outer); - tjoystick_right.setInvertX(); - //tjoystick_right.setInvertY(); - - pos_x = (62.5 - 20.636) * ppmX; - pos_y = 20.636 * ppmY; - pos_r = 45 * ppmX / 2; - tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR8); - tdpad_right.setDeadZoneInner(dead_zone_inner); - tdpad_right.setInvertX(); - tdpad_right.setInvertY(); - - //tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r); - //tjoystick_left.setDeadZoneInner(dead_zone_inner); - //tjoystick_left.setDeadZoneOuter(dead_zone_outer); - - device.begin(); + InputMapper::begin(); // Turn off LED digitalWrite(PC13, HIGH); @@ -87,13 +44,9 @@ void setup() uint8_t tevent_size; TouchEvent tevent[5]; -uint32_t buttons = 1; - void loop() { - int8_t ret = trackpad_right.poll(tevent, tevent_size); - - if (ret > 0) + if (trackpad[1].poll(tevent, tevent_size) > 0) { for (uint8_t i = 0; i < tevent_size; ++i) { @@ -117,55 +70,15 @@ void loop() break; } - //Serial.printf("%u\n", tevent[i].type); - - for (uint8_t c = 0; c < num_controls; ++c) - { - int8_t res = tcontrols[c]->touch(tevent[i].finger_id, y, x); - if (res < 0) - { - Serial.printf("Impossible Error\n"); - break; - } - - switch(tcontrols[c]->getControlType()) - { - case TouchControl::CT_NONE: - Serial.printf("Control type not set\n"); - break; - - case TouchControl::CT_JOYSTICK: - //Serial.printf("%i, %i\n", ((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); - device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); - device.joystick_right(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); - break; - - case TouchControl::CT_DPAD: - device.dpad(((TouchDpad*)tcontrols[c])->getButton()); - break; - } - - if (res > 0) - { - break; - } - } + InputMapper::mapTrackpad(1, tevent[i].finger_id, y, x); // trackpad is rotated 90 deg so x and y are flipped } } - uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN); - uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN); - - //device.joystick_right(30000, 30000); - - device.trigger_right(right_trigger); - device.button(7, right_tp_click); // R3 - - //buttons <<= 1; - //if (buttons >= 2048) buttons = 1u; + uint32_t triggers[] = {0, analogRead(pin_trigger[1])}; + InputMapper::mapTriggers(triggers); - //device.buttons(buttons); - //delay(500); + uint16_t right_tp_click = digitalRead(pin_trackpad_click[1]); + InputMapper::mapButtons(right_tp_click << 7); // R3 - device.sendReport(); + InputMapper::sendReport(); }