Joystick 4-Position Component
Joystick
Section titled “Joystick”Path: src/components/Joystick.cpp
Revision History: Initial documentation
The Joystick component provides an interface for a 4-position joystick (up, down, left, right) with support for both local physical operation and remote control via Modbus. It features position debouncing for reliable readings, position state tracking, and seamless switching between local and remote operation modes.
REQUIREMENTS
Section titled “REQUIREMENTS”- Four GPIO pins for joystick inputs (up, down, left, right)
- Pull-up resistors (internal or external) for each input
- Modbus support for remote operation
FEATURES
Section titled “FEATURES”- 5 distinct positions: UP, DOWN, LEFT, RIGHT, and CENTER
- Configurable debouncing for reliable position readings
- Position state tracking with timing information
- Switchable operation modes:
- LOCAL: reads physical joystick position
- REMOTE: accepts position commands via Modbus
- Complete Modbus integration
- Position holding time tracking
DEPENDENCIES
Section titled “DEPENDENCIES”BEHAVIOUR
Section titled “BEHAVIOUR”PERFORMANCE
Section titled “PERFORMANCE”- Consider optimizing the debouncing algorithm for specific application needs
- Evaluate if READ_INTERVAL_MS (25ms) can be adjusted based on application requirements
SECURITY
Section titled “SECURITY”- Implement input validation for Modbus commands
- Consider adding authentication for remote mode switching
COMPLIANCE
Section titled “COMPLIANCE”- Ensure compliance with relevant industrial control standards
- Review electrical safety requirements for the connected joystick hardware
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- Use hardware debouncing circuits when possible to reduce CPU load
- Implement additional position combinations if needed (e.g., diagonal positions)
- Consider adding filter capacitors on input pins for noisy environments
EXAMPLE
Section titled “EXAMPLE”#ifdef PIN_JOYSTICK_UP joystick = new Joystick( this, // owner PIN_JOYSTICK_UP, // pinUp PIN_JOYSTICK_DOWN, // pinDown PIN_JOYSTICK_LEFT, // pinLeft PIN_JOYSTICK_RIGHT, // pinRight JOYSTICK_MB_ADDR // modbusAddress ); if (joystick) { components.push_back(joystick); Log.infoln(F("Joystick initialized. Pins - Up:%d Down:%d Left:%d Right:%d, MB:%d"), PIN_JOYSTICK_UP, PIN_JOYSTICK_DOWN, PIN_JOYSTICK_LEFT, PIN_JOYSTICK_RIGHT, JOYSTICK_MB_ADDR); } else { Log.errorln(F("Joystick initialization failed.")); }#endif