rusefi-1/firmware/hw_layer/sensors/joystick.cpp

133 lines
4.2 KiB
C++
Raw Normal View History

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/
* 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
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
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
if (channel == getHwPin("joy", CONFIG(joystickCenterPin))) {
2015-07-10 06:01:56 -07:00
joyCenter++;
button = JB_CENTER;
} 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
} 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;
} 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;
*/
} 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);
#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(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() {
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
}
void stopJoystickPins() {
brain_pin_markUnused(activeConfiguration.joystickCenterPin);
brain_pin_markUnused(activeConfiguration.joystickAPin);
brain_pin_markUnused(activeConfiguration.joystickDPin);
}
void startJoystickPins() {
// todo: extract 'configurePalInputPin() method?
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);
}
2015-07-10 06:01:56 -07:00
void initJoystick(Logging *shared) {
int channel;
addConsoleAction("joystickinfo", joystickInfo);
2016-12-27 10:02:00 -08:00
if (!isJoystickEnabled())
2015-07-10 06:01:56 -07:00
return;
sharedLogger = shared;
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);
startJoystickPins();
2015-07-10 06:01:56 -07:00
}
#endif /* HAL_USE_PAL && EFI_JOYSTICK */