mapped all the buttons; todo fix multiple buttons mapped to one

gyro
NepEgor 2 years ago
parent ad1f845d66
commit d8f1771713

@ -1,7 +1,10 @@
{ {
// See http://go.microsoft.com/fwlink/?LinkId=827846 // See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
] ],
} "unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

@ -9,15 +9,26 @@ namespace InputMapper
void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y); void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y);
void mapTriggers(uint32_t values[2]); void mapTriggers(uint32_t value[2]);
// 0, 1 - triggers dunno if triggers will be dual stage enum HardwareButtons: uint8_t
// 4, 5 - start, select = xinput {
// 6, 7 - trackpads = xinput L3 & R3 START,
// 0, 1 << 8 - bumpers = xinput SELECT,
// 2 << 8 - home = xinput TRACKPAD_LEFT,
// 4 - 7 << 8 - grips = xinput ABXY TRACKPAD_RIGHT,
void mapButtons(uint16_t buttons); 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(); // void mapGyro();

@ -15,8 +15,7 @@ class USB_Device
uint8_t msg_type:8; // byte 0 uint8_t msg_type:8; // byte 0
uint8_t msg_len:8; // byte 1 uint8_t msg_len:8; // byte 1
uint8_t dpad:4; // byte 2 first nibble uint16_t buttons:16; // byte 2 and byte 3
uint16_t buttons:12; // byte 2 second nibble and byte 3
uint8_t trigger_left:8; // byte 4 uint8_t trigger_left:8; // byte 4
uint8_t trigger_right:8; // byte 5 uint8_t trigger_right:8; // byte 5
int16_t lx:16; // byte 6 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_y = 0;
static const int16_t usb_joystick_r = 0x7FFF; 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() {} USB_Device() {}
void begin(); void begin();
void end(); void end();
void buttons(uint16_t buttons); void button(uint16_t button, uint16_t value);
void button(uint8_t button, bool val);
void dpad(uint8_t dir);
void joystick(uint8_t id, int16_t x, int16_t y); void joystick(uint8_t id, int16_t x, int16_t y);

@ -26,26 +26,10 @@ void USB_Device::end()
HID_Custom_DeInit(); HID_Custom_DeInit();
} }
void USB_Device::buttons(uint16_t buttons) void USB_Device::button(uint16_t button, uint16_t value)
{ {
xinput_report.buttons = buttons; xinput_report.buttons = ((xinput_report.buttons | button) & value) |
} ((xinput_report.buttons & ~button) & ~value);
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;
} }
void USB_Device::joystick(uint8_t id, int16_t x, int16_t y) void USB_Device::joystick(uint8_t id, int16_t x, int16_t y)

@ -8,12 +8,15 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env]
lib_extra_dirs = F:\stm32\libraries
[env:blackpill_f411ce] [env:blackpill_f411ce]
platform = ststm32 platform = ststm32
board = blackpill_f411ce board = blackpill_f411ce
upload_protocol = stlink upload_protocol = stlink
monitor_port = COM[4] monitor_port = COM[3]
monitor_speed = 256000 monitor_speed = 115200
framework = arduino framework = arduino
build_flags = build_flags =
-D PIO_FRAMEWORK_ARDUINO_ENABLE_HID -D PIO_FRAMEWORK_ARDUINO_ENABLE_HID

@ -54,28 +54,72 @@ namespace InputMapper
tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4); tdpad_right.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR4);
tdpad_right.setDeadZoneInner(dead_zone_inner); 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(); 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) void mapTrackpad(uint8_t id, uint8_t fid, int32_t x, int32_t y)
{ {
for (uint8_t c = 0; c < num_controls; ++c) 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()) switch(tcontrols[id][c]->getControlType())
{ {
case TouchControl::CT_NONE: case TouchControl::CT_NONE:
break; break;
case TouchControl::CT_JOYSTICK: case TouchControl::CT_JOYSTICK:
//Serial.printf("%i, %i\n", ((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY()); res = tcontrols[id][c]->touch(fid, x, y);
//device.joystick_left(((TouchJoystick*)tcontrols[c])->getX(), ((TouchJoystick*)tcontrols[c])->getY());
device.joystick(id, ((TouchJoystick*)tcontrols[id][c])->getX(), ((TouchJoystick*)tcontrols[id][c])->getY()); device.joystick(id, ((TouchJoystick*)tcontrols[id][c])->getX(), ((TouchJoystick*)tcontrols[id][c])->getY());
break; break;
case TouchControl::CT_DPAD: 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; 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; uint8_t mapped_value[2];
// 160 - 540 for (uint8_t i = 0; i < 2; ++i)
if (values[1] < 160)
{
mapped_values[1] = 0;
}
else if(values[1] >= 540)
{ {
mapped_values[1] = 0xFF; if (value[i] < min)
} {
else mapped_value[i] = 0;
{ }
mapped_values[1] = (values[1] - 160) * 0xFF / (540 - 160); 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() void sendReport()

@ -3,12 +3,25 @@
#include "trackpad.h" #include "trackpad.h"
#include "input_mapper.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_button[] =
{
const uint8_t pin_trackpad_data[2] = {PB7, PB9}; PB12, 0, // START
const uint8_t pin_trackpad_clock[2] = {PB6, PB8}; 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 TrackPad trackpad[2]; // 0 - left, 1 - right
@ -20,15 +33,23 @@ void setup()
pinMode(PC13, OUTPUT); pinMode(PC13, OUTPUT);
digitalWrite(PC13, LOW); digitalWrite(PC13, LOW);
Serial.begin(256000); Serial.begin(115200);
// Init Hardware // Init Hardware
pinMode(pin_trigger[1], INPUT_ANALOG); for (uint8_t i = 0; i < sizeof(pin_trigger); ++i)
pinMode(pin_trackpad_click[1], INPUT_PULLDOWN); {
pinMode(pin_trigger[i], INPUT_ANALOG);
trackpad[0].initialize(pin_trackpad_clock[0], pin_trackpad_data[0]); }
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_maxX = trackpad[0].getMaxX();
trackpad_maxY = trackpad[0].getMaxY(); trackpad_maxY = trackpad[0].getMaxY();
@ -86,14 +107,13 @@ void loop()
} }
} }
uint32_t triggers[] = {0, analogRead(pin_trigger[1])}; uint32_t triggers[] = {analogRead(pin_trigger[0]), analogRead(pin_trigger[1])};
//Serial.printf("T %u %u\n", triggers[0], triggers[1]);
InputMapper::mapTriggers(triggers); InputMapper::mapTriggers(triggers);
uint16_t right_tp_click = digitalRead(pin_trackpad_click[1]); for (uint8_t i = 0; i < sizeof(pin_button); i += 2)
InputMapper::mapButtons(right_tp_click << (5 + 8)); // B {
InputMapper::mapButton((InputMapper::HardwareButtons)(i / 2), digitalRead(pin_button[i]) == pin_button[i + 1]);
}
InputMapper::sendReport(); InputMapper::sendReport();
} }

Loading…
Cancel
Save