fixed multiple buttons mapped to one

gyro
NepEgor 2 years ago
parent 42b9ed80fb
commit f0ce651b9c

@ -3,6 +3,10 @@
#include "usb_device.h" #include "usb_device.h"
#include "touch_controls_all.h" #include "touch_controls_all.h"
#include <map>
#include <Arduino.h>
namespace InputMapper namespace InputMapper
{ {
USB_Device device; USB_Device device;
@ -25,7 +29,45 @@ namespace InputMapper
}; };
const uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*[2]); const uint8_t num_controls = sizeof(tcontrols) / sizeof(TouchControl*[2]);
uint16_t button_map[] =
{
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,
};
uint16_t dpad_left_map[] =
{
USB_Device::DPAD_UP,
USB_Device::DPAD_DOWN,
USB_Device::DPAD_LEFT,
USB_Device::DPAD_RIGHT,
};
uint16_t dpad_right_map[] =
{
USB_Device::FACE_Y,
USB_Device::FACE_A,
USB_Device::FACE_X,
USB_Device::FACE_B,
};
uint16_t* dpad_map[] =
{
dpad_left_map,
dpad_right_map,
};
std::map<uint16_t, uint8_t> xinput_counter;
void begin() void begin()
{ {
@ -60,44 +102,59 @@ namespace InputMapper
tdpad_left.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR_4); tdpad_left.init(pos_x, pos_y, pos_r, TouchDpad::DPAD_TYPE_SECTOR_4);
tdpad_left.setDeadZoneInner(dead_zone_inner); tdpad_left.setDeadZoneInner(dead_zone_inner);
for (uint8_t i = 0; i < sizeof(button_map) / sizeof(uint16_t); ++i)
{
auto search = xinput_counter.find(button_map[i]);
if (search == xinput_counter.end())
{
xinput_counter.insert(std::make_pair(button_map[i], 0));
}
}
for (uint8_t d = 0; d < 2; ++d)
{
for (uint8_t i = 0; i < 4; ++i)
{
auto search = xinput_counter.find(dpad_map[d][i]);
if (search == xinput_counter.end())
{
xinput_counter.insert(std::make_pair(dpad_map[d][i], 0));
}
}
}
device.begin(); device.begin();
} }
uint16_t dpad_left_map[] = void modifyCounter(uint16_t xinput_button, bool value)
{ {
USB_Device::DPAD_UP, auto search = xinput_counter.find(xinput_button);
USB_Device::DPAD_DOWN,
USB_Device::DPAD_LEFT,
USB_Device::DPAD_RIGHT,
};
uint16_t dpad_right_map[] = if (search != xinput_counter.end())
{ {
USB_Device::FACE_Y, if (value)
USB_Device::FACE_A, {
USB_Device::FACE_X, search->second++;
USB_Device::FACE_B, }
}; else
{
if (search->second > 0)
{
search->second--;
}
}
}
}
uint16_t* dpad_map[] = void mapDpad(uint8_t dpad, uint8_t direction, bool value)
{
dpad_left_map,
dpad_right_map,
};
uint16_t mapDpad(uint8_t dpad, TouchDpad::DpadType dpad_type, uint8_t direction)
{ {
uint16_t button = 0; for (uint8_t i = 0; i < 4; ++i)
for (uint8_t i = 0; i < dpad_type; ++i)
{ {
if (direction & (1 << i)) if (direction & (1 << i))
{ {
button |= dpad_map[dpad][i]; modifyCounter(dpad_map[dpad][i], value);
} }
} }
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)
@ -124,13 +181,11 @@ namespace InputMapper
{ {
TouchDpad* dpad = (TouchDpad*)tcontrols[id][c]; TouchDpad* dpad = (TouchDpad*)tcontrols[id][c];
uint16_t prev_button = mapDpad(id, dpad->getType(), dpad->getButton()); mapDpad(id, dpad->getButton(), 0);
device.button(prev_button, 0);
res = tcontrols[id][c]->touch(fid, x, y); res = tcontrols[id][c]->touch(fid, x, y);
uint16_t button = mapDpad(id, dpad->getType(), dpad->getButton()); mapDpad(id, dpad->getButton(), 1);
device.button(button, button);
} }
break; break;
} }
@ -169,28 +224,18 @@ namespace InputMapper
device.triggers(mapped_value); device.triggers(mapped_value);
} }
uint16_t button_map[] =
{
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,
};
void mapButton(HardwareButtons button, bool value) void mapButton(HardwareButtons button, bool value)
{ {
device.button(button_map[button], value? button_map[button] : 0); modifyCounter(button_map[button], value);
} }
void sendReport() void sendReport()
{ {
for (auto it = xinput_counter.begin(); it != xinput_counter.end(); ++it)
{
device.button(it->first, it->second > 0? it->first : 0);
}
device.sendReport(); device.sendReport();
} }
} }

@ -7,19 +7,21 @@ const uint8_t pin_trigger[2] = {PA1, PA0};
const uint8_t pin_button[] = const uint8_t pin_button[] =
{ {
PB12, 0, // START PB12, 0, // START
PB13, 0, // SELECT PB13, 0, // SELECT
PC15, 0, // TRACKPAD_LEFT PC15, 0, // TRACKPAD_LEFT
PB3, 1, // TRACKPAD_RIGHT PB3, 1, // TRACKPAD_RIGHT
PA2, 0, // BUMPER_LEFT PA2, 0, // BUMPER_LEFT
PA8, 1, // BUMPER_RIGHT PA8, 1, // BUMPER_RIGHT
PB1, 0, // HOME PB1, 0, // HOME
PB14, 1, // GRIP_A PB14, 1, // GRIP_A
PB15, 1, // GRIP_B PB15, 1, // GRIP_B
PA4, 0, // GRIP_X PA4, 0, // GRIP_X
PA3, 0, // GRIP_Y PA3, 0, // GRIP_Y
}; };
uint8_t button_state[sizeof(pin_button) / 2] = {0};
const uint8_t pin_trackpad_data[2] = {PB5, PB9}; const uint8_t pin_trackpad_data[2] = {PB5, PB9};
const uint8_t pin_trackpad_clock[2] = {PB4, PB8}; const uint8_t pin_trackpad_clock[2] = {PB4, PB8};
@ -112,7 +114,12 @@ void loop()
for (uint8_t i = 0; i < sizeof(pin_button); i += 2) 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]); uint8_t value = digitalRead(pin_button[i]);
if (value != button_state[i / 2])
{
button_state[i / 2] = value;
InputMapper::mapButton((InputMapper::HardwareButtons)(i / 2), value == pin_button[i + 1]);
}
} }
InputMapper::sendReport(); InputMapper::sendReport();

Loading…
Cancel
Save