Joystick
A circle within a circle: a knob that a finger drags inside a base. While the finger is held the knob follows it, and its X and Y follow the knob; let go, it springs back to the middle and X and Y go back to 0. Use it to steer, to pan a camera, to move something in a game or to drive a pointer.
| Setting | Default | What it does |
|---|---|---|
| Dead zone | 5 % | Near the middle X and Y stay 0, so a thumb resting on the knob does not drift. Past it they start from 0 again, with no jump. |
| Moves | Any way | Any way, Left and right only or Up and down only. |
| Knob size | 40 % | Of the base. The base and the knob are sized to fill the widget’s box between them, so the knob can ride the base’s edge without leaving the box. |
| Springs back to the middle | On | Let go, the knob goes back to the middle and X and Y back to 0. Off, it stays where it was let go, like a throttle. |
| Return time | 150 ms | How long the way back takes. 0 puts it back at once. |
How it moves. Only a press on the knob takes hold of it: a press on the base beside it does nothing. The knob is held where the finger took it, so it does not jump. Its centre can go from the middle to the edge of the base, where X or Y is 1. A finger that goes further leaves the knob on the edge, at the finger’s angle, for as long as it is held. A finger that is held still keeps it there. The widget’s box is square.
What it gives. Bind any of these to a variable, in Data bindings. They go one way, from the Joystick into the variable, as it moves:
- X and Y: decimals from -1 to 1. Right and up are plus, 0 is the middle and 1 is the knob at the edge, so pushed half way right X is 0.5. For another range, scale it where it is used: a formula of
JoyX * 100gives -100 to 100. - How far from the middle: 0 to 1.
- Angle: degrees from straight up, clockwise, 0 to 359.
- Held: 1 while a finger is on the knob.
- Way pushed: 0 in the middle, then 1 up, 2 right, 3 down, 4 left. These are the same numbers as a hardware joystick’s, so a design can swap one for the other.
A Joystick dropped on the screen brings two variables, JoyX and JoyY, decimals already bound to its X and Y. Its Value changed event comes each time what it gives changes, the last time with X and Y at 0.
Styling. The Base and the Knob are each a part with the whole of Style settings, and each takes Pressed while a finger holds the knob. They start as circles, and a radius of your own makes either a rounded square. Main is the box behind them, which is empty until it is styled.
Exports. Arduino, ESP-IDF and MicroPython. ESPHome has no joystick. Compiled, and driven by a finger in LVGL 9.4.0 and 8.3.11 to match the designer; run on an LCDWIKI ES3C35P steering a sprite (the example below).
Example: reading the stick
- Drag a Joystick onto the screen.
JoyXandJoyYappear on the Data tab. - Add a Label and bind its Text to
JoyXwith the format%.2f, and another toJoyY(see Variables and bindings). - Press Preview and drag the knob: the labels follow it, and go back to 0 when you let go.
Example: steering something round a box
- Add a Panel for the box, with its Padding at 0, and put the thing to move inside it - an Image of a small sprite, say 40 x 40.
- Add two decimal variables for where it is,
ShipXandShipY, and one for how fast,Speed(4 is 200 pixels a second at full stick). - Add an automation, Every interval of 20 ms, with two Set variable actions:
ShipXtoclamp(ShipX + JoyX * Speed, 0, 256)andShipYtoclamp(ShipY - JoyY * Speed, 0, 226). Y is taken away because the stick counts up as plus and the screen counts down. - Bind the sprite's X position to
ShipXand its Y position toShipY.
The clamp is what keeps it in the box: 256 and 226 are the inside of the box (a 300 x 270 panel less its 2 px border on each side) less the size of the sprite. Let go and the stick gives 0, so the sprite stops where it is. To make it face the way it goes, bind its Rotation to the stick's Angle through a variable. The whole build, with a picture of every step, is a tutorial in Tips and tricks on the forum, and the design is in the gallery.