From d8f1771713616f537380e468f20e7b720e342bda Mon Sep 17 00:00:00 2001 From: NepEgor Date: Tue, 12 Apr 2022 00:59:33 +0600 Subject: [PATCH] mapped all the buttons; todo fix multiple buttons mapped to one --- .vscode/extensions.json | 17 +++-- include/input_mapper.h | 29 +++++--- lib/usb_device/include/usb_device.h | 30 ++++++-- lib/usb_device/src/usb_device.cpp | 22 +----- platformio.ini | 7 +- src/input_mapper.cpp | 108 +++++++++++++++++++++------- src/main.cpp | 54 +++++++++----- 7 files changed, 184 insertions(+), 83 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/input_mapper.h b/include/input_mapper.h index 7211713..50599e7 100644 --- a/include/input_mapper.h +++ b/include/input_mapper.h @@ -9,15 +9,26 @@ namespace InputMapper 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 mapTriggers(uint32_t value[2]); + + enum HardwareButtons: uint8_t + { + START, + SELECT, + TRACKPAD_LEFT, + TRACKPAD_RIGHT, + BUMPER_LEFT, + BUMPER_RIGHT, + HOME, + GRIP_A, + GRIP_B, + GRIP_X, + GRIP_Y, + }; + + extern uint16_t button_map[]; + + void mapButton(HardwareButtons button, bool value); // void mapGyro(); diff --git a/lib/usb_device/include/usb_device.h b/lib/usb_device/include/usb_device.h index 5d70efe..81e0d26 100644 --- a/lib/usb_device/include/usb_device.h +++ b/lib/usb_device/include/usb_device.h @@ -15,8 +15,7 @@ class USB_Device uint8_t msg_type:8; // byte 0 uint8_t msg_len:8; // byte 1 - uint8_t dpad:4; // byte 2 first nibble - uint16_t buttons:12; // byte 2 second nibble and byte 3 + uint16_t buttons:16; // byte 2 and byte 3 uint8_t trigger_left:8; // byte 4 uint8_t trigger_right:8; // byte 5 int16_t lx:16; // byte 6 @@ -34,14 +33,35 @@ class USB_Device static const int16_t usb_joystick_y = 0; static const int16_t usb_joystick_r = 0x7FFF; + enum XinputButtons: uint16_t + { + DPAD_UP = 0x0001, + DPAD_DOWN = 0x0002, + DPAD_LEFT = 0x0004, + DPAD_RIGHT = 0x0008, + + START = 0x0010, + SELECT = 0x0020, + JOYSTICK_LEFT = 0x0040, + JOYSTICK_RIGHT = 0x0080, + + BUMPER_LEFT = 0x0100, + BUMPER_RIGHT = 0x0200, + HOME = 0x0400, + EMPTY = 0x0800, + + FACE_A = 0x1000, + FACE_B = 0x2000, + FACE_X = 0x4000, + FACE_Y = 0x8000, + }; + USB_Device() {} void begin(); void end(); - void buttons(uint16_t buttons); - void button(uint8_t button, bool val); - void dpad(uint8_t dir); + void button(uint16_t button, uint16_t value); void joystick(uint8_t id, int16_t x, int16_t y); diff --git a/lib/usb_device/src/usb_device.cpp b/lib/usb_device/src/usb_device.cpp index 7419089..fc64b3d 100644 --- a/lib/usb_device/src/usb_device.cpp +++ b/lib/usb_device/src/usb_device.cpp @@ -26,26 +26,10 @@ void USB_Device::end() HID_Custom_DeInit(); } -void USB_Device::buttons(uint16_t buttons) +void USB_Device::button(uint16_t button, uint16_t value) { - xinput_report.buttons = buttons; -} - -void USB_Device::button(uint8_t button, bool val) -{ - if (val) - { - xinput_report.buttons |= (1u << button); - } - else - { - xinput_report.buttons &= ~(1u << button); - } -} - -void USB_Device::dpad(uint8_t dir) -{ - xinput_report.dpad = dir & 0b1111; + xinput_report.buttons = ((xinput_report.buttons | button) & value) | + ((xinput_report.buttons & ~button) & ~value); } void USB_Device::joystick(uint8_t id, int16_t x, int16_t y) diff --git a/platformio.ini b/platformio.ini index 0afd1e4..1c7d8e9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,12 +8,15 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[env] +lib_extra_dirs = F:\stm32\libraries + [env:blackpill_f411ce] platform = ststm32 board = blackpill_f411ce upload_protocol = stlink -monitor_port = COM[4] -monitor_speed = 256000 +monitor_port = COM[3] +monitor_speed = 115200 framework = arduino build_flags = -D PIO_FRAMEWORK_ARDUINO_ENABLE_HID diff --git a/src/input_mapper.cpp b/src/input_mapper.cpp index bc76489..f7795b4 100644 --- a/src/input_mapper.cpp +++ b/src/input_mapper.cpp @@ -54,28 +54,72 @@ namespace InputMapper tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4); tdpad_right.setDeadZoneInner(dead_zone_inner); + pos_x = (62.5 - 20.636) * ppmX; + pos_y = 20.636 * ppmY; + + tdpad_left.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4); + tdpad_left.setDeadZoneInner(dead_zone_inner); + device.begin(); } + uint16_t dpad_map[][4] = + { + { + USB_Device::DPAD_UP, + USB_Device::DPAD_DOWN, + USB_Device::DPAD_LEFT, + USB_Device::DPAD_RIGHT, + }, + { + USB_Device::FACE_Y, + USB_Device::FACE_A, + USB_Device::FACE_X, + USB_Device::FACE_B, + }, + }; + + uint16_t mapDpad(uint8_t dpad, uint8_t direction) + { + uint16_t button = 0; + + for (uint8_t i = 0; i < 4; ++i) + { + if (direction & (1 << i)) + { + button |= dpad_map[dpad][i]; + } + } + + return button; + } + 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[id][c]->touch(fid, x, y); - + int res; + switch(tcontrols[id][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()); + res = tcontrols[id][c]->touch(fid, x, y); device.joystick(id, ((TouchJoystick*)tcontrols[id][c])->getX(), ((TouchJoystick*)tcontrols[id][c])->getY()); break; case TouchControl::CT_DPAD: - device.dpad(((TouchDpad*)tcontrols[id][c])->getButton()); + { + uint16_t prev_button = mapDpad(id, ((TouchDpad*)tcontrols[id][c])->getButton()); + device.button(prev_button, 0); + + res = tcontrols[id][c]->touch(fid, x, y); + + uint16_t button = mapDpad(id, ((TouchDpad*)tcontrols[id][c])->getButton()); + device.button(button, button); + } break; } @@ -87,34 +131,50 @@ namespace InputMapper } } - void mapTriggers(uint32_t values[2]) + void mapTriggers(uint32_t value[2]) { - uint8_t mapped_values[2]; + static const uint32_t max = 70; + static const uint32_t min = 2; - mapped_values[0] = 0; - - // 160 - 540 - if (values[1] < 160) - { - mapped_values[1] = 0; - } - else if(values[1] >= 540) + uint8_t mapped_value[2]; + + for (uint8_t i = 0; i < 2; ++i) { - mapped_values[1] = 0xFF; - } - else - { - mapped_values[1] = (values[1] - 160) * 0xFF / (540 - 160); + if (value[i] < min) + { + mapped_value[i] = 0; + } + else if (value[i] > max) + { + mapped_value[i] = 255; + } + else + { + mapped_value[i] = (value[i] - min) * 255 / (max - min); + } } - device.triggers(mapped_values); + device.triggers(mapped_value); } - void mapButtons(uint16_t buttons) + uint16_t button_map[] = { - uint16_t mappedButtons = buttons >> 4; + USB_Device::START, + USB_Device::SELECT, + USB_Device::JOYSTICK_LEFT, + USB_Device::JOYSTICK_RIGHT, + USB_Device::BUMPER_LEFT, + USB_Device::BUMPER_RIGHT, + USB_Device::HOME, + USB_Device::FACE_A, + USB_Device::FACE_B, + USB_Device::FACE_X, + USB_Device::FACE_Y, + }; - device.buttons(mappedButtons); + void mapButton(HardwareButtons button, bool value) + { + device.button(button_map[button], value? button_map[button] : 0); } void sendReport() diff --git a/src/main.cpp b/src/main.cpp index 69ab6d0..a82c7aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,25 @@ #include "trackpad.h" #include "input_mapper.h" -const uint8_t pin_trigger[2] = {0, PA3}; +const uint8_t pin_trigger[2] = {PA0, PA1}; -const uint8_t pin_trackpad_click[2] = {0, PB4}; - -const uint8_t pin_trackpad_data[2] = {PB7, PB9}; -const uint8_t pin_trackpad_clock[2] = {PB6, PB8}; +const uint8_t pin_button[] = +{ + PB12, 0, // START + PB13, 0, // SELECT + PC15, 0, // TRACKPAD_LEFT + PB3, 1, // TRACKPAD_RIGHT + PA2, 0, // BUMPER_LEFT + PA8, 1, // BUMPER_RIGHT + PB1, 0, // HOME + PB14, 1, // GRIP_A + PB15, 1, // GRIP_B + PA4, 0, // GRIP_X + PA3, 0, // GRIP_Y +}; + +const uint8_t pin_trackpad_data[2] = {PB5, PB9}; +const uint8_t pin_trackpad_clock[2] = {PB4, PB8}; TrackPad trackpad[2]; // 0 - left, 1 - right @@ -20,15 +33,23 @@ void setup() pinMode(PC13, OUTPUT); digitalWrite(PC13, LOW); - Serial.begin(256000); + Serial.begin(115200); // Init Hardware - pinMode(pin_trigger[1], INPUT_ANALOG); - pinMode(pin_trackpad_click[1], INPUT_PULLDOWN); - - trackpad[0].initialize(pin_trackpad_clock[0], pin_trackpad_data[0]); + for (uint8_t i = 0; i < sizeof(pin_trigger); ++i) + { + pinMode(pin_trigger[i], INPUT_ANALOG); + } - trackpad[1].initialize(pin_trackpad_clock[1], pin_trackpad_data[1]); + for (uint8_t i = 0; i < sizeof(pin_button); i += 2) + { + pinMode(pin_button[i], pin_button[i + 1]? INPUT_PULLDOWN : INPUT_PULLUP); + } + + for (uint8_t i = 0; i < sizeof(pin_trackpad_clock); ++i) + { + trackpad[i].initialize(pin_trackpad_clock[i], pin_trackpad_data[i]); + } trackpad_maxX = trackpad[0].getMaxX(); trackpad_maxY = trackpad[0].getMaxY(); @@ -86,14 +107,13 @@ void loop() } } - uint32_t triggers[] = {0, analogRead(pin_trigger[1])}; - //Serial.printf("T %u %u\n", triggers[0], triggers[1]); - + uint32_t triggers[] = {analogRead(pin_trigger[0]), analogRead(pin_trigger[1])}; InputMapper::mapTriggers(triggers); - uint16_t right_tp_click = digitalRead(pin_trackpad_click[1]); - InputMapper::mapButtons(right_tp_click << (5 + 8)); // B + for (uint8_t i = 0; i < sizeof(pin_button); i += 2) + { + InputMapper::mapButton((InputMapper::HardwareButtons)(i / 2), digitalRead(pin_button[i]) == pin_button[i + 1]); + } InputMapper::sendReport(); - }