fome-fw/firmware/hw_layer/sensors/joystick.cpp

127 lines
4.6 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 "pch.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"
#include "digital_input_exti.h"
2015-07-10 06:01:56 -07:00
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 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", engineConfiguration->joystickCenterPin)) {
2015-07-10 06:01:56 -07:00
joyCenter++;
button = JB_CENTER;
} else if (channel == getHwPin("joy", engineConfiguration->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", engineConfiguration->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", engineConfiguration->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", engineConfiguration->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() {
efiPrintf("total %d center=%d@%s", joyTotal, joyCenter,
hwPortname(engineConfiguration->joystickCenterPin));
efiPrintf("a=%d@%s", joyA, hwPortname(engineConfiguration->joystickAPin));
efiPrintf("b=%d@%s", joyB, hwPortname(engineConfiguration->joystickBPin));
efiPrintf("c=%d@%s", joyC, hwPortname(engineConfiguration->joystickCPin));
efiPrintf("d=%d@%s", joyD, hwPortname(engineConfiguration->joystickDPin));
2015-07-10 06:01:56 -07:00
}
2016-12-27 10:02:00 -08:00
static bool isJoystickEnabled() {
return (isBrainPinValid(engineConfiguration->joystickCenterPin) &&
isBrainPinValid(engineConfiguration->joystickAPin) &&
// not used so far isBrainPinValid(engineConfiguration->joystickBPin) &&
// not used so far isBrainPinValid(engineConfiguration->joystickCPin) &&
isBrainPinValid(engineConfiguration->joystickDPin));
2016-12-27 10:02:00 -08:00
}
void stopJoystickPins() {
// todo: should be 'efiExtiDisablePin' or smth?
efiSetPadUnused(activeConfiguration.joystickCenterPin);
efiSetPadUnused(activeConfiguration.joystickAPin);
efiSetPadUnused(activeConfiguration.joystickDPin);
}
void startJoystickPins() {
// todo: extract 'configurePalInputPin() method?
// input capture driver would claim pin ownership so we are not using 'efiSetPadMode' here
efiSetPadModeWithoutOwnershipAcquisition("joy center", engineConfiguration->joystickCenterPin, PAL_MODE_INPUT_PULLUP);
efiSetPadModeWithoutOwnershipAcquisition("joy A", engineConfiguration->joystickAPin, PAL_MODE_INPUT_PULLUP);
// not used so far efiSetPadModeWithoutOwnershipAcquisition("joy B", engineConfiguration->joystickBPin, PAL_MODE_INPUT_PULLUP);
// not used so far efiSetPadModeWithoutOwnershipAcquisition("joy C", engineConfiguration->joystickCPin, PAL_MODE_INPUT_PULLUP);
efiSetPadModeWithoutOwnershipAcquisition("joy D", engineConfiguration->joystickDPin, PAL_MODE_INPUT_PULLUP);
}
void initJoystick() {
int channel;
addConsoleAction("joystickinfo", joystickInfo);
2016-12-27 10:02:00 -08:00
if (!isJoystickEnabled())
2015-07-10 06:01:56 -07:00
return;
channel = getHwPin("joy", engineConfiguration->joystickCenterPin);
efiExtiEnablePin("joy", engineConfiguration->joystickCenterPin, PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
channel = getHwPin("joy", engineConfiguration->joystickAPin);
efiExtiEnablePin("joy", engineConfiguration->joystickAPin, PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
// not used so far applyPin(engineConfiguration->joystickBPin);
// not used so far applyPin(engineConfiguration->joystickCPin);
channel = getHwPin("joy", engineConfiguration->joystickDPin);
efiExtiEnablePin("joy", engineConfiguration->joystickDPin, PAL_EVENT_MODE_RISING_EDGE, (palcallback_t)(void *)extCallback, (void *)channel);
2015-07-10 06:01:56 -07:00
}
#endif /* HAL_USE_PAL && EFI_JOYSTICK */