A Clue and a Joystick:Bit

Posted by codepope on Saturday, March 21, 2020
Last Modified on Saturday, August 31, 2024

So I’ve been having a little bit of a hack (at HackWimbledon) on the Adafruit Clue alpha and having some of Elecfreaks Joystick:bits to hand I thought I’d get them working together. The Joystick:bit has been replaced by a version 2.

Apart from anything else, all the supporting code and libraries (what little there were) have been updated for the new Joystick:bit. Which is a pain as the original has the oddest buttons you’ve ever seen. To pack all six buttons into the controller, the Elecfreaks design gets each button to apply a different resistence to connection. No button mashing for you! There’s no button trigger or edges, just a change in resistance and multiple buttons, who really knows.

Anyway, I still wanted to have a hack with this and dug out some code that appeared to be appropriate. It was, kinda, but the button decoder gave me nightmares when I realised it was doing 60 plus scans of a button list to figure out which button was down. It was epic slow. Never mind; I did a rewrite of that code and got it tolerably fast enough. I pushed on and embedded the new functions into a variant of Adafruit’s demo code for the Clue.

from adafruit_clue import clue
import board
from analogio import AnalogIn

clue.sea_level_pressure = 1020
ap0=AnalogIn(board.P0)
ap1=AnalogIn(board.P1)
ap2=AnalogIn(board.P2)

clue_data = clue.simple_text_display(title="CLUE Sensor Data!", title_scale=2)

buttons = {0: 'A',
           516: 'B',
           684: 'C',
           768: 'D',
           853: 'E',
           819: 'F'}

def button_press():
    press = ap2.value / 64
    if press < 900:
        for key in buttons:
            if press>key-5 and press<key+5:
                return buttons[key]

def joystick_push():
    x = ap0.value
    y = ap1.value
    return x, y

while True:
    clue_data[0].text = "Acceleration: {:.2f} {:.2f} {:.2f}".format(*clue.acceleration)
    clue_data[1].text = "Gyro: {:.2f} {:.2f} {:.2f}".format(*clue.gyro)
    clue_data[2].text = "Magnetic: {:.3f} {:.3f} {:.3f}".format(*clue.magnetic)
    clue_data[3].text = "Pressure: {:.3f}hPa".format(clue.pressure)
    clue_data[4].text = "Altitude: {:.1f}m".format(clue.altitude)
    clue_data[5].text = "Temperature: {:.1f}C".format(clue.temperature)
    clue_data[6].text = "Humidity: {:.1f}%".format(clue.humidity)
    clue_data[7].text = "Proximity: {}".format(clue.proximity)
    clue_data[8].text = "Gesture: {}".format(clue.gesture)
    clue_data[9].text = "Color: R: {} G: {} B: {} C: {}".format(*clue.color)
    clue_data[10].text = "Joystick X,Y: {}".format(joystick_push())
    clue_data[11].text = "Buttons: {}".format(button_press())

    clue_data.show()

Things worth mentioning - The clue simple_text_display class is super useful. It lets you display multiple lines of text without worrying about how you are formatting each line relative to other lines. Instant dashboard, just add strings. Oh and you can have a nice big title too.

So that’s the code. In the process of putting it together, I started using VSCode and the super CircuitPython extension. It’s a very fresh/raw extension but already it makes coding on the Clue and other CircuitPython devices so much more mainstream. The Mu Editor is great for beginners but when you spend your day in VSCode, you really want to stick with it in the evening.

But this all puts me back to square one… the idea was to use the Joystick:Bit as a controller for a Tetvasion (game) prototype but the stick is quite squidgy and analogue and the buttons are in no shape to be launching pixellated death, being almost analogue. So, I have to wait till I can get my hands on version two, which will be a long wait as the price of shipping on two joystick:bit 2’s is twice the cost of the joysticks and … lets not mention the shipping.

Still, the Clue and VSCode CircuitPython are fun. Both recommended.