Motor Driver control with Display and Joystick helper classes

I find myself constantly copy/pasting my simplay display code and having the re-work my joystick control methods each time I want to reuse them.  Today, I moved all that logic in two helper classes.  The JoystickHelper class is particularly useful and is much better than the built in GHI joystick methods. To demonstrate their usage, I created a simple example using the motor driver module.  The goal is to use the joytick module to control a  small motor.  The engine will spin forward or reverse when the joystick is moved up or down.  Also, the motor will spin faster or slower depending on how far the joystick is moved.  The JoystickHelper is entirely event driven.  Therefore, you don’t have to determine if the joystick was actually moved.  The event will only fire when the position has actually changed. This makes programming the joysitck trivial.

The SimpleDisplayHelper, simply shows the position of the joystick on the screen. It contains all the logic to clear the screen when full and display the next text at the top of the screen.

This full solution can be found on my Gadgeteer GitHub repository.

Direct links to the helpers:

As usual, the setup is easy.

Motor Driver Setup

For assembly, I set my power supply to 7V and a 500mA cutoff. There seems to be some voltage dropout from the motor driver.  I haven’t really investigated it yet. However, if I set my power supply to 3V, the max for my motor, the driver barely give enough power to the motor for it to spin.  7V seems to be equivalent.

Motor Driver Assembly

Here’s a closeup of the assembly.

Motor Driver Assembly Closeup

Notice how the screen is full?  The next time the joystick is moved, the screen will clear and the next data row will appear at the top.  Let’s handle the SimpleDisplayHelper first.

Using the code is very simple. Create a class level instance of the helper with the display and font you want to use. Then, call DisplayText with the text you want to display. All the other logic to calculate screen height and font height are encapsulated in the helper.

The JoystickHelper is also very easy to use. Create an instance of the helper and subscribe to the JoystickMoved event.

The JoystickMoved event give you a JoystickEventsArgs

The JoystickEventArgs has the new X and Y joystick positins.

Here is my JoystickMoved event handler for the motor driver example.

The best part of this code is that I don’t have to determine “if” the joystick was moved. The helper take care of all that for me.

I hope you find this useful. It’s already saved me time and I hope it does for you too.

Share this Post