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)
|
|
|
|
*
|
2018-12-09 13:46:53 -08:00
|
|
|
* TODO: separate EXTI layer from joystick logic
|
2018-12-09 13:50:22 -08:00
|
|
|
* You cannot use two pins with same index for EXTI (for instance PA5 and PE5) since these would
|
|
|
|
* be using same EXTI line. See https://stm32f4-discovery.net/2014/08/stm32f4-external-interrupts-tutorial/
|
2019-11-11 20:32:09 -08:00
|
|
|
* See also comments in digital_input_icu.cpp
|
2018-12-09 13:46:53 -08:00
|
|
|
*
|
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.h"
|
2019-01-03 21:16:08 -08:00
|
|
|
|
2019-04-12 17:52:51 -07:00
|
|
|
#if (HAL_USE_PAL && EFI_JOYSTICK)
|
2018-12-18 20:50:29 -08:00
|
|
|
#include "joystick.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "pin_repository.h"
|
2018-12-18 20:50:29 -08:00
|
|
|
#include "digital_input_exti.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-12-12 15:02:00 -08:00
|
|
|
static bool isJitter() {
|
2015-07-10 06:01:56 -07:00
|
|
|
efitick_t now = getTimeNowNt();
|
|
|
|
if (now - lastEventTime < NT_EVENT_GAP)
|
2018-12-12 15:02:00 -08:00
|
|
|
return true; // two consecutive events are probably just jitter
|
2015-07-10 06:01:56 -07:00
|
|
|
lastEventTime = now;
|
2018-12-12 15:02:00 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-14 10:52:27 -07:00
|
|
|
static void extCallback(ioportmask_t channel) {
|
2018-12-12 15:02:00 -08:00
|
|
|
if (isJitter())
|
|
|
|
return;
|
2015-07-10 06:01:56 -07:00
|
|
|
joyTotal++;
|
|
|
|
joystick_button_e button;
|
|
|
|
// todo: I guess it's time to reduce code duplication and start working with an array
|
2019-12-11 14:48:55 -08:00
|
|
|
if (channel == getHwPin("joy", CONFIG(joystickCenterPin))) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyCenter++;
|
|
|
|
button = JB_CENTER;
|
2019-12-11 14:48:55 -08:00
|
|
|
} else if (channel == getHwPin("joy", CONFIG(joystickAPin))) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyA++;
|
|
|
|
button = JB_BUTTON_A;
|
2018-12-12 15:02:00 -08:00
|
|
|
/* not used so far
|
2019-12-11 14:48:55 -08:00
|
|
|
} else if (channel == getHwPin("joy", CONFIG(joystickBPin))) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyB++;
|
2018-12-12 15:02:00 -08:00
|
|
|
button = JB_BUTTON_B;
|
2019-12-11 14:48:55 -08:00
|
|
|
} else if (channel == getHwPin("joy", CONFIG(joystickCPin))) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyC++;
|
2018-12-12 15:02:00 -08:00
|
|
|
button = JB_BUTTON_C;
|
|
|
|
*/
|
2019-12-11 14:48:55 -08:00
|
|
|
} else if (channel == getHwPin("joy", CONFIG(joystickDPin))) {
|
2015-07-10 06:01:56 -07:00
|
|
|
joyD++;
|
|
|
|
button = JB_BUTTON_D;
|
|
|
|
} else {
|
|
|
|
// unexpected channel
|
|
|
|
return;
|
|
|
|
}
|
2019-04-12 17:52:51 -07:00
|
|
|
#if EFI_HD44780_LCD
|
2015-07-10 06:01:56 -07:00
|
|
|
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,
|
2019-12-11 14:48:55 -08:00
|
|
|
hwPortname(CONFIG(joystickCenterPin)));
|
|
|
|
scheduleMsg(sharedLogger, "a=%d@%s", joyA, hwPortname(CONFIG(joystickAPin)));
|
|
|
|
scheduleMsg(sharedLogger, "b=%d@%s", joyB, hwPortname(CONFIG(joystickBPin)));
|
|
|
|
scheduleMsg(sharedLogger, "c=%d@%s", joyC, hwPortname(CONFIG(joystickCPin)));
|
|
|
|
scheduleMsg(sharedLogger, "d=%d@%s", joyD, hwPortname(CONFIG(joystickDPin)));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 10:02:00 -08:00
|
|
|
static bool isJoystickEnabled() {
|
2019-12-11 14:48:55 -08:00
|
|
|
return CONFIG(joystickCenterPin) != GPIO_UNASSIGNED ||
|
|
|
|
CONFIG(joystickAPin) != GPIO_UNASSIGNED ||
|
|
|
|
// not used so far CONFIG(joystickBPin) != GPIO_UNASSIGNED ||
|
|
|
|
// not used so far CONFIG(joystickCPin) != GPIO_UNASSIGNED ||
|
|
|
|
CONFIG(joystickDPin) != GPIO_UNASSIGNED;
|
2016-12-27 10:02:00 -08:00
|
|
|
}
|
|
|
|
|
2019-04-19 09:42:21 -07:00
|
|
|
void stopJoystickPins() {
|
2019-12-11 14:48:55 -08:00
|
|
|
brain_pin_markUnused(activeConfiguration.joystickCenterPin);
|
|
|
|
brain_pin_markUnused(activeConfiguration.joystickAPin);
|
|
|
|
brain_pin_markUnused(activeConfiguration.joystickDPin);
|
2019-04-19 09:42:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void startJoystickPins() {
|
2019-04-20 07:59:07 -07:00
|
|
|
// todo: extract 'configurePalInputPin() method?
|
2019-12-11 14:48:55 -08:00
|
|
|
efiSetPadMode("joy center", CONFIG(joystickCenterPin), PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy A", CONFIG(joystickAPin), PAL_MODE_INPUT_PULLUP);
|
|
|
|
// not used so far efiSetPadMode("joy B", CONFIG(joystickBPin), PAL_MODE_INPUT_PULLUP);
|
|
|
|
// not used so far efiSetPadMode("joy C", CONFIG(joystickCPin), PAL_MODE_INPUT_PULLUP);
|
|
|
|
efiSetPadMode("joy D", CONFIG(joystickDPin), PAL_MODE_INPUT_PULLUP);
|
2019-04-19 09:42:21 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void initJoystick(Logging *shared) {
|
2019-04-21 06:28:49 -07:00
|
|
|
int channel;
|
2019-02-05 15:36:25 -08:00
|
|
|
addConsoleAction("joystickinfo", joystickInfo);
|
2016-12-27 10:02:00 -08:00
|
|
|
if (!isJoystickEnabled())
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
sharedLogger = shared;
|
|
|
|
|
2019-12-11 14:48:55 -08:00
|
|
|
channel = getHwPin("joy", CONFIG(joystickCenterPin));
|
|
|
|
efiExtiEnablePin("joy", CONFIG(joystickCenterPin), PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
|
|
|
|
channel = getHwPin("joy", CONFIG(joystickAPin));
|
|
|
|
efiExtiEnablePin("joy", CONFIG(joystickAPin), PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
|
|
|
|
// not used so far applyPin(CONFIG(joystickBPin));
|
|
|
|
// not used so far applyPin(CONFIG(joystickCPin));
|
|
|
|
channel = getHwPin("joy", CONFIG(joystickDPin));
|
|
|
|
efiExtiEnablePin("joy", CONFIG(joystickDPin), PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
|
2019-01-09 19:57:33 -08:00
|
|
|
|
2019-04-19 09:42:21 -07:00
|
|
|
startJoystickPins();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:46:10 -07:00
|
|
|
#endif /* HAL_USE_PAL && EFI_JOYSTICK */
|