Wednesday, February 21, 2018

PiGPIOPin - when you want to use pin numbers and not BCM on the Raspberry Pi in Python

A while back I was doing some bare Arduino compatible boards using the ATMega328 chip. From this I saw that Arduino did not map the pins on the IC to the same numbers on the Arduino.  They used a simple order on the Arduino board and the Arduino ID so it was easy to find the right pin for wiring and also for your code.

Only problem is when you use the chip with the bootloader the mappings are different to the pin number so you need a lookup tool to know when you want to use Arduino Pin 13 that it's ATMega Pin 19.
After a while this became a bit annoying so I put together an .h file with a bunch of constants that got around this.

On the Raspberry Pi there is a similar situation.  The default numbering mechanism used is the BCM numbering which is great if you're operating at chip level, but at a board level it means things like:

BCM GPIO 5 is board pin 29

I still have to count pins to get to board pin 29, so having to also know it's then GPIO 5 in my code can sometimes be a bit annoying.

The original RPi.GPIO library allows you to choose BCM or Board numbers

 import RPi.GPIO as GPIO  


Giving the user the choice.

With the fantastic GPIOZero BCM numbering is enforced and there is no option to use board numbering.
GPIOZero is a great library and makes controlling motors, working with MCP3008 (analog input) and distance sensors a lot easier.

For those who want to use board pin numbers and want to take advantage of GPIOZero the use of the BCM numbering can be annoying.

To make it easier I decided to do a similar lookup table like I did for the Arduino.
Mapping Pin number variables to BCM numbers.
The code and an example is on GitHub.

It's straight forward to use.

Have in the same folder as your code or in the python path.

Put the following line at the top

from PiGPIOPin import *

Then instead of using something like

red = LED(17) # 17 is the BCM number that is at board pin 11


red = LED(PIN11) 

Each BCM number is mapped a PIN* variable.

The rest of the code then works as normal.

Using this gives the option back to use board pin numbers and not BCM numbers.

Thursday, February 8, 2018

More cheap stuff from eBay - Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy

I'm drawn to the underside of eBay. The placed where strange bits of electronics are sold.  This interest comes from making things and needs odd bits of electronics to realise my (awful) creations.

On one of my regular trawls of ebay I found this.

Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy

Looked interesting.  OK the display is obviously mono-colour LED and not back lit but for £1.39 I thought it was worth a punt.

From the Listing the dimension are: Product size: 14*8*2cm
Bigger than the Sony PSP Go 12.8*6.9*1.64cm
If Sony can squeeze a 4.3" widescreen display in the PSP Go then this could be a very useful case to work from. A nice sized case with 10 buttons.

For reference a Raspberry Pi Zero is 6.5cm x 3 cm.  An easy fit and with dimensions greater than the PSP Go one of the many 3.2" LCD displays I see running from a Pi should fit nicely.

£1.39 for a case with 10 buttons that should easily take a Raspberry Pi Zero and 3.2 " screen with space for other bits is a bargain.

I see a plan coming together so I ordered one two weeks ago and it arrived today.

The packaging is the same as the listing which is always a good start.

Opened the box and inside was the actual console. OK a bit rough and the sticker isn't straight.  Not a problem as I plan to remove all that anyway.

But there was something else not right.  I checked the listing again and confirmed the dimension Product size: 14*8*2cm

The dimensions on the actual one I received seem a bit off.  Annoyingly I don't have a ruler with me at the moment so I'm using AAA (not AA) as the standard measure.

An AAA battery according to Wikipedia is 4.45cm long.
From the picture below it looks like this may not be 8cm wide or 14 cm in length.

It is more like 9cm in length so a Raspberry Pi Zero may still fit inside and 5cm in width.
There goes the plans for a 3.2" LCD display. Maybe one of the  0.96" OLED displays might fit.

I almost forgot.  What about the 999 games.
It does have Tetris, a variety of Breakout games, a Tank combat game and a Driving game.

The driving game is especially bad as there are only 2 lanes and you just move from left to right as the cars come down the screen.

In summary how does it compare to the listing title.

Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy
It is a game playing handheld video Tetris Game Console.
No kids, children or boys included, but it definitely is portable and a toy.

As an added bonus you also get the amazing driving game, breakout and tank combat games, so in a way it over delivers on the gaming promise.  I didn't count whether there are 9999 but I suspect we all know the answer to that.

But, if your reason for getting it is to have a 14cm x 8cm x 2cm case with an opening to put a 3.2" display and a Raspberry Pi Zero you will be very disappointed as it's most definitely a lot smaller than the listing.

It is held together with screws so maybe a teardown is needed to see what's inside and if there is anything I can salvage from it.

I think I may have found an actual correct listing for the one I purchased
Listing states the dimensions are: 10 x 4.5 x 2.4cm

Then there's also this listing for £1.79
With the dimension listed as: 15.5*6.5*2.5cm

If the larger one really is those dimensions then a project may be on to use it as a shell for something more fun.

Thursday, November 9, 2017

White Wii Nunchuck shaped Bluetooth Joystick - £1.85/£1.99 on eBay

When I attended PiWars (a great event for people who like to build robots using the Raspberry Pi) earlier this year I saw a whole range of controllers being used for controlling Raspberry Pi based robots. With many using USB dongle ones and others using Bluetooth or even apps on phones.  Expecting to see some BlueDot next year a great Android App for controlling things over Bluetooth.

Since my phone is iPhone (too long to explain) BlueDot isn't an option for me and with my continuing goal of finding cheap solutions to a problem I found a £1.85 Bluetooth controller from eBay - 3D Bluetooth Virtual Reality Glasses VR BOX Game Remote Control Gamepad Handles

What I liked about it is that it uses AAA batteries so if at an event the batteries died I could swap them out. No need to wait for recharging.

It's billed as being for mobile gaming and virtual reality and no mention of working with a regular computer or even Raspberry Pi, so I took a punt for £1.85, well actually £3.70, as I bought two. (walking on the wild side). 

eBay order confirmation

NOTE: The listing I bought from has ended but the same seller has a new listing at £1.99

I've seen some similar listing with switches on the side.  This is not the model I have bought. Mine has no switch.

What it looks like on eBay listing

They arrived and actually look good.  Plastic is definitely cheap, but they don't feel flimsy.
Below are a couple of pictures of the real thing.  A little different to the render but I'm happy..

Top. Joystick, power button, mode change button and 4 facing buttons.

Front has 2 additional buttons for when held straight rather than sideways
Time to start testing.

Setting up a new Raspbian image and using Raspberry Pi ZeroW I added the two AAA batteries and it came on. Blue light flashes to say ready to pair.

Using the Bluetooth Add Device in Raspbian the device VR BOX was found and paired successfully.  The unusal name actuallymakes sense as the listing on eBay is for "3D Bluetooth Virtual Reality Glasses VR BOX Game Remote Control Gamepad Handles"

Bluetooth on Raspbian paired with VR BOX

From the eBay listing and the single sheet of instructions the Gamepad/Nunchuck has 4 modes that you select with a combination of the @ and the relevant button.

A : Function Select (Music Video Mode.)
B : Function Select (game mode)
C : Function Selection (VR video self-timer mode.)
D : Function Select (mouse self-timer mode.)

It starts up in Music mode, so to use as a gamepad you have to change this after it pairs.

As I wanted to test in game mode I did [@]+[B]

I previously created a small python/pygame program to help with the testing.  It's available on GitHub.

I downloaded it to the PiZeroW (one of these days I'll do the proper git clone thing) and ran the program.
I run from the command line as the terminal window prints the details in text while the pygame window gives a graphic representation of the gamepad so I can see if multiple button presses are possible and which button numbers are pressed.  The code includes an analogue joystick so it will show the full range of movement for the gamepad joystick.

python2/pygame gamepad tester

Pressing the buttons the appropriate red square appeared and the terminal window stated which button was pressed or released.  Success! It's being detected as a gamepad and the buttons are working.

I then used the joystick which looks like an analogue stick you see on many projects. Only it's not. The joystick is digital.  Up, Down,Left,Right.  No range in values.  Except for a strange thing.
The positive values are 0.999969482422 and the negative values are -1, so you can't test for 1 for down and right when in gamepad (sideways) mode as 0.999969482422 is not 1. A little gotcha for coding.

The different modes

As listed above there are 4 different modes for the controller.

[@]+[A] for music
Nothing happened on the Pi. I haven't figured out what Bluetooth Music Mode is yet.
I tried this mode with my phone and it does provide volume up/down, mute an play so does what is expected with the target device.

[@]+[B] game mode
This is gamepad in horizontal mode, like a traditional controller with the joystick on the left and the buttons on the right.
For example, if you push up the Up joystick is detected

[@]+[C] VR video self-timer mode
This is a gamepad in Nunchuck mode. So vertical.
For example, if you have the controller held vertically and press up then Up Joystick is detected.

All the buttons are the same as for game mode.

[@]+[D] mouse self-timer mode
Guess what the joystick operates like a mouse held like a Nunchuch (vertical) with the lower front button (trigger) being left click and the higher front button (trigger) being right click.
In mouse mode buttons A and B still get recognised as gamepad buttons so that could be useful.Not sure how or when but extra buttons are always useful. 

Button combinations

In game mode some of the buttons could be pressed at the same time while others could not.

@ and Power are reserved so have no button function.

Bottom trigger and top trigger could not be pressed at the same time. Lower trigger always took priority even if upper was pressed first.

Bottom trigger could be pressed with any of the face bottoms (A,B,C,D). Same for Top Trigger.

A could be pressed with C, but not B or D. A stays active and the others do not show.
If you press B or D first and then press A, A will be activated and the other will be released.

B and C can be pressed at the same time

D is over ridden by all face buttons, so if you have D pressed and then press any of the other their function is activated and D is released.

Also, either trigger and two button face combinations that were noted to work above will also work.
So, if you need 2 buttons at the same time and one is the master and the second one needs to be one of 4 I'd recommend using a trigger as the master and the face buttons as the secondary as this works for all face buttons.
If you need 3 buttons then there are a small number of combinations that will work.
Top button, A and C worked for me.

Future Note: I have a feeling this section may need a chart of some nature. Need to figure out how to represent it.

For Python/Pygame and I expect other system the buttons are numbered:

Gamepad Mode

Face button arrangement
C   D

Top Trigger: button 7
Bottom Trigger: button 6
A: button 4
B: button 0
C: button 3
D: button 1

VR mode (Nunchuck)

Face button arrangement
B   A

Top Trigger: button 7
Bottom Trigger: button 6
A: button 1
B: button 3
C: button 0
D: button 4

When I saw it changed I thought the button values would have turned clockwise 90 degrees so the buttons were in the same position (top, left, right, bottom) would have the same values but they don't. Top and bottom shift correctly, but left and right are mirrored.

Where did 2 and 5 go.  I wonder if they're on the board and not broken out. Might be a tear down thing


For £1.85, or £1.99, last time I checked if your need is digital controls then I think this is a hit.  It paired easily. The instructions though really sparse are enough to change modes.
With a phone it does the media things promised from mode A and on the Raspberry Pi and expect and device that supports a Bluetooth Gamepad and Mouse it does exactly what it says on the tin.

For controlling robots it's ideal (except for no analog).
Joystick for direction control and then buttons for other functions. Maybe take a picture, fire a missile, change mode from piloted to auto mode for different challenges at an event like PiWars.
With the advantage of using regular AAA batteries there is no fear that at an even the controller will run out of power and need to be charged.

A little but extra

As each controller having a unique Bluetooth Device ID it's possible to set up a cronjob on Linux to  pair with a specific gamepad when the Raspberry Pi (ZeroW) is booted/rebooted so  at events if you had 5 robots, 5 controllers each controller would be set up to work with a specific controller
It's not too complicated to do.

Get the device ID for the VR Box using the command bluetoothctl
Mine was FF:FF:70:00:76:8B

Run crontab -e
Select your editor if running for the first time. I use Nano

Go to the end and add the line
@reboot echo "connect FF:FF:70:00:76:8B" | bluetoothctl

If using Nano do a [ctrl]-[x] to exist and 'y' to save.
Reboot an you're done.

This will try to auto pair when the Pi boots.

You can also do a cronjob to continuously try to reconnect and not just at boot.
This is useful if the Pi has completed booting before the gampad has been turned on or if the controller hasn't been used for 10 minutes or in pairing mode fails to pair after 3 minutes and so needs to repair when the controller is turned back on