2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file joystick.cpp
|
|
|
|
*
|
2017-12-04 15:30:44 -08:00
|
|
|
* See lcd_controller.cpp for more information
|
|
|
|
* See void onJoystick(joystick_button_e button)
|
|
|
|
*
|
2015-07-10 06:01:56 -07:00
|
|
|
* @date Jan 2, 2015
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "engine_configuration.h"
|
|
|
|
#include "joystick.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "pin_repository.h"
|
|
|
|
|
|
|
|
#if HAL_USE_EXT || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
|
|
|
static int joyTotal = 0;
|
|
|
|
static int joyCenter;
|
|
|
|
static int joyA = 0;
|
|
|
|
static int joyB = 0;
|
|
|
|
static int joyC = 0;
|
|
|
|
static int joyD = 0;
|
|
|
|
|
2017-01-05 17:04:02 -08:00
|
|
|
// 50ms
|
|
|
|
#define NT_EVENT_GAP US2NT(50 *1000)
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static Logging *sharedLogger;
|
|
|
|
static efitick_t lastEventTime = 0;
|
|
|
|
|
|
|
|
static void extCallback(EXTDriver *extp, expchannel_t channel) {
|
|
|
|
UNUSED(extp);
|
|
|
|
efitick_t now = getTimeNowNt();
|
|
|
|
if (now - lastEventTime < NT_EVENT_GAP)
|
|
|
|
return; // two consecutive events are probably just jitter
|
|
|
|
lastEventTime = now;
|
|
|
|
joyTotal++;
|
|
|
|
joystick_button_e button;
|
|
|
|
// todo: I guess it's time to reduce code duplication and start working with an array
|
2017-07-28 11:27:37 -07:00
|
|
|
if (channel == getHwPin("joy", boardConfiguration->joystickCenterPin)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyCenter++;
|
|
|
|
button = JB_CENTER;
|
2017-07-28 11:27:37 -07:00
|
|
|
} else if (channel == getHwPin("joy", boardConfiguration->joystickAPin)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyA++;
|
|
|
|
button = JB_BUTTON_A;
|
2017-07-28 11:27:37 -07:00
|
|
|
} else if (channel == getHwPin("joy", boardConfiguration->joystickBPin)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyB++;
|
|
|
|
button = JB_BUTTON_C;
|
2017-07-28 11:27:37 -07:00
|
|
|
} else if (channel == getHwPin("joy", boardConfiguration->joystickCPin)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyC++;
|
|
|
|
button = JB_BUTTON_B;
|
2017-07-28 11:27:37 -07:00
|
|
|
} else if (channel == getHwPin("joy", boardConfiguration->joystickDPin)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyD++;
|
|
|
|
button = JB_BUTTON_D;
|
|
|
|
} else {
|
|
|
|
// unexpected channel
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if EFI_HD44780_LCD || defined(__DOXYGEN__)
|
|
|
|
onJoystick(button);
|
2017-05-02 10:34:01 -07:00
|
|
|
#else
|
|
|
|
UNUSED(button);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void joystickInfo(void) {
|
|
|
|
scheduleMsg(sharedLogger, "total %d center=%d@%s", joyTotal, joyCenter,
|
|
|
|
hwPortname(boardConfiguration->joystickCenterPin));
|
|
|
|
scheduleMsg(sharedLogger, "a=%d@%s", joyA, hwPortname(boardConfiguration->joystickAPin));
|
|
|
|
scheduleMsg(sharedLogger, "b=%d@%s", joyB, hwPortname(boardConfiguration->joystickBPin));
|
|
|
|
scheduleMsg(sharedLogger, "c=%d@%s", joyC, hwPortname(boardConfiguration->joystickCPin));
|
|
|
|
scheduleMsg(sharedLogger, "d=%d@%s", joyD, hwPortname(boardConfiguration->joystickDPin));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EXTI is a funny thing: you can only use same pin on one port. For example, you can use
|
|
|
|
* PA0 PB5 PE2 PD7
|
|
|
|
* but you cannot use
|
|
|
|
* PA0 PB0 PE2 PD7
|
|
|
|
* because pin '0' would be used on two different ports
|
|
|
|
*/
|
|
|
|
|
|
|
|
static EXTConfig extcfg = { {
|
|
|
|
/* CH#00 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#01 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#02 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#03 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#04 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#05 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#06 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#07 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#08 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#09 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#10 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#11 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#12 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#13 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#14 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#15 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#16 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#17 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#18 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#19 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#20 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#21 */{ EXT_CH_MODE_DISABLED, NULL },
|
|
|
|
/* CH#22 */{ EXT_CH_MODE_DISABLED, NULL } } };
|
|
|
|
|
2016-02-04 09:01:41 -08:00
|
|
|
static uint32_t getExtMode(ioportid_t port) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (port == GPIOA) {
|
|
|
|
return EXT_MODE_GPIOA;
|
|
|
|
} else if (port == GPIOB) {
|
|
|
|
return EXT_MODE_GPIOB;
|
|
|
|
} else if (port == GPIOC) {
|
|
|
|
return EXT_MODE_GPIOC;
|
|
|
|
} else if (port == GPIOD) {
|
|
|
|
return EXT_MODE_GPIOD;
|
|
|
|
} else if (port == GPIOE) {
|
|
|
|
return EXT_MODE_GPIOE;
|
|
|
|
} else if (port == GPIOF) {
|
|
|
|
return EXT_MODE_GPIOF;
|
|
|
|
}
|
2017-03-08 22:10:33 -08:00
|
|
|
firmwareError(CUSTOM_ERR_EXT_MODE, "Unsupported %d", port);
|
2015-07-10 06:01:56 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applyPin(brain_pin_e pin) {
|
|
|
|
if (pin == GPIO_UNASSIGNED)
|
|
|
|
return;
|
|
|
|
|
2017-07-28 11:27:37 -07:00
|
|
|
int index = getHwPin("joy", pin);
|
|
|
|
ioportid_t port = getHwPort("joy", pin);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
extcfg.channels[index].mode = EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | getExtMode(port);
|
|
|
|
extcfg.channels[index].cb = extCallback;
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:02:00 -08:00
|
|
|
static bool isJoystickEnabled() {
|
|
|
|
return boardConfiguration->joystickCenterPin != GPIO_UNASSIGNED ||
|
|
|
|
boardConfiguration->joystickAPin != GPIO_UNASSIGNED ||
|
|
|
|
boardConfiguration->joystickBPin != GPIO_UNASSIGNED ||
|
|
|
|
boardConfiguration->joystickCPin != GPIO_UNASSIGNED ||
|
|
|
|
boardConfiguration->joystickDPin != GPIO_UNASSIGNED;
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initJoystick(Logging *shared) {
|
2016-12-27 10:02:00 -08:00
|
|
|
if (!isJoystickEnabled())
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
sharedLogger = shared;
|
|
|
|
|
|
|
|
applyPin(boardConfiguration->joystickCenterPin);
|
|
|
|
applyPin(boardConfiguration->joystickAPin);
|
|
|
|
applyPin(boardConfiguration->joystickBPin);
|
|
|
|
applyPin(boardConfiguration->joystickCPin);
|
|
|
|
applyPin(boardConfiguration->joystickDPin);
|
|
|
|
|
2017-05-15 05:40:54 -07:00
|
|
|
efiSetPadMode("joy center", boardConfiguration->joystickCenterPin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy A", boardConfiguration->joystickAPin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy B", boardConfiguration->joystickBPin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy C", boardConfiguration->joystickCPin, PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy D", boardConfiguration->joystickDPin, PAL_MODE_INPUT_PULLUP);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
addConsoleAction("joystickinfo", joystickInfo);
|
|
|
|
|
|
|
|
extStart(&EXTD1, &extcfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|