Interesting bug with controllermap

Interesting bug with controllermap

Running a Rust program to show gamepad controls

So I’ve been playing with a Rust program that uses the config string generated by controllermap. It shows all the keys and the text of a particular button turns green when you press it and it shows PRESSED/RELEASED.

But it got A and B buttons mixed up and also X and Y. I’d press A, it highlighted B etc.

I checked my code, it wasn’t there. I ran controllermap again and it generated a similar config string.

I then went back to the Pi version of Asteroids, put it on a Pi, started debugging it, pressed the fire button and it hit the line of code for the A button. D’oh.

The only conclusion I can come up with is that controllermap gets it wrong for my gamepad. I had somehow missed it when I added gamepad control for the Asteroid Pi.

It’s quite easy to fix.  This is what the full text in gamepad.txt look like.

030000001008000001e5000010010000,usb gamepad,platform:Linux,crc:417e,a:b2,b:b1,x:b3,y:b0,back:b8,start:b9,leftshoulder:b4,rightshoulder:b5,leftx:a0,lefty:a1,

but we’re only interesed in the button mappings for a,b, x and y which are in bold.

a:b2,b:b1,x:b3,y:b0,

Just swap the a b mappings and the x and y so it looks like

a:b1,b:b2,x:b0,y,b3

Then save the file and it now works correctly.

 

New tutorial posted – how to build controllermap in Linux

New tutorial posted – how to build controllermap in Linux

Controllermap utility

Remember controllermap? The utility that creates a configuration string for a game pad etc. I found that it is no longer installed by default when you install libsdl2-dev on Linux.  I found the hard way, trying to use it on my new Raspbverry Piu 5.

But the source code is available for it in the libsdl 2 releases.

So I wrote a quick tutorial that explains how to get the source code and build it using clang on VS Code on a Pi or any Linux box.  It’s linked here or you can find it on the tutorials menu.

How fast is a Raspberry Pi 5?

How fast is a Raspberry Pi 5?

Raspberry PI 5I recently bought one with 8 GB RAM pictured above. I haven’t got an SSD for it yet so am still pottering about with an SD Card.

I’ve installed VS Code, clang. and Rust along with the VS Code extensions Rust-Analyzer and codelldb.

Out of curiosity I compiled and ran a Rust program that reads in a text file of 1,000 lines of text with each line holding 7 random cards like this: AH 6D 2C 4S JD QH QC.

The idea is to load the file into RAM then process each line and determine the best poker hand.

On my PC 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz (which runs Windows 11), I compared running the Rust program in Windows 11, on Ubuntu 24.04 LTS running in a Hyper-V VM on the same PC and on the Raspberry Pi 5 using it’s own Debian (Bookworm) version.

Here are the times per hand.

  • Windows: 175 ns.
  • Ubuntu on Hyper-V 125 ns
  • Raspberry Pi 5: 175 ns.

You can download the project with the test cards from GitHub. This link is a zip file containing the project file.

To run it in release in VS Code, in the terminal type in

cargo run --release 1000_card_hands.txt
Timings running under Ubuntu

 

 

 

Or you can view the test cards which include the results with commemts in the test file. No need to run it release.

cargo run --features=show_cards test_card_hands.txt

Showing cards with test data

What is so suprising is that the time on Windows and Rapberry Pi 5 are the same. Windows is running a virus checker (Windows Defender) and I guess that might slow it a bit.

Yet another fix

Yet another fix

But no AI generated image this time.

The problem seems to be with Electron which is the technology that drives VS Code.

Normally I use code or code . when I’ve opened a terminal and done a cd to the folder containing the project.

What fiuxes the VS Code crash with Error 5 is adding this on the end of the command

--js-flags="--nodecommit_pooled_pages"

so use either of these.

code --js-flags="--nodecommit_pooled_pages" or 

code . --js-flags="--nodecommit_pooled_pages"

I’ve tested it with my Pi 4 and it has been rock solid. The bug report and the fix are here on GitHub.

So I bought a Raspberry PI 5 and VS Code crashes!

So I bought a Raspberry PI 5 and VS Code crashes!

Crash on VS Code running on a Reaspberry Pi 5.

And the first thing I did after assembling it, was install VS Code and Rust.  And irritatingly, there’s some kind of a bug with the current version of VS Code. 1.97.

Open a project- say for a Rust application and view the source code. After a few seconds or so it crashes with a The window terminated unexpectedly (reason: ‘crashed’, code 5).

I also updated my Raspberry Pi 4B, installed Rust and got the same crash with it.

Thankfully Raspis come with grim which lets you do screenshots on Wayland.

Want to know if your system is running Wayland? From a terminal run this command:

echo “$XDG_SESSION_TYPE”

And you’ll either get wayland or x11.

So I hope the VS Code issue gets fixed soon.

Bit of a curiosity – Rust runs slower on Windows than Linux

Bit of a curiosity – Rust runs slower on Windows than Linux

And it is a fair comparison because the Linux I’m running (Ubuntu 24.04 LTS) runs on my Windows 11 box in a hyper-V VM. I wrote a Poker hand evaluation program which loaded a text file containing lines of seven playing cards in text format TS 2C 4D sort of thing. It was seven cards because there’s two in your hand and five on the table.

There were 1,000 lines of these and the program loaded the file into memory, holding the cards in a Vec<Vec<Card>>. Then it loops through the 1,000 elements and figures out the best hand for each set.  The bit that was timed was the loop not the loading the file into memory.

The source file is on GitHub. It has the project files plus some test cards. You can try it yourself. Note at the bottom of this article is a link to a much faster version.

https://github.com/David-H-Bolton/Projects/blob/main/rust_pokerhand.zip

On my PC, the Windows one average time per hand is 768 ns. On Ubuntu is 540ns. Mad eh! The same program runs in 70% of the time on Linux compared to Windows. Both are run with this command from a terminal/command line.

cargo run --release 1000_card_hads.txt

You can also try the test_card_hands.txt but you need to enable the show_cards feature. That shows the cards but doesn’t do timing. The default is do the timing but don’t show the cards or the evaluation.

cargo run --release --features show_card test_card_hands.txt

Shows the Rust program with the feature

 

 

 

 

 

 

 

 

 

 

 

 

The test cards say what each hand is and the output at the bottom is the program working out each hand.
The file

https://github.com/David-H-Bolton/Projects/blob/main/rust_pokerhand_faster.zip

contains a much faster version. On Linux it takes about 127 ns per hand. On Windows it’s about 190 ns.

So I found the fix

So I found the fix

It's fixed text on a technology backgroundWell actually I asked Microsoft’s CoPilot AI and it sorted it!

This is what you need in the dependencies section in cargo.toml

[dependencies]
sdl2-sys = "*"
sdl2 = { version = "0.37", features = ["image","ttf"] }

It compiled with no problems after saving that.

A slight problem with SDL2 and Rust

A slight problem with SDL2 and Rust

VS Code Rust SDL Copikle problemThis is on Ubuntu 24.04 LTS. I’ve installed the dev versions of SDL2, including images and TTF.  You can read how to install them in this tutorial.

Once that’s done you add them to the rust project with commands like this

cargo add sdl2

You can see these instruction in the relevant crates pages. For instance SDL2 crate, SDL2 Image and SDL2 TTF.

So far so good. The cargo add added the crates into cargo.toml as expected but when I compiled it. Not a compile error as such but a version of dll hell.

The text in that image says:

Updating crates.io index
error: failed to select a version for `sdl2-sys`.
... required by package `sdl2_image v0.25.0`
... which satisfies dependency `sdl2_image = "^0.25.0"` of package `sdl1 v0.1.0 (/home/david/rust/sdl1)`
versions that meet the requirements `^0.25.0` are: 0.25.0

the package `sdl2-sys` links to the native library `SDL2`, but it conflicts with a previous package which links to `SDL2` as well:
package `sdl2-sys v0.37.0`
... which satisfies dependency `sdl2-sys = "^0.37.0"` of package `sdl1 v0.1.0 (/home/david/rust/sdl1)`
Only one package in the dependency graph may specify the same links value. This helps ensure that only one copy of a native library is linked in the final binary. Try to adjust your dependencies so that only one package uses the `links = "SDL2"` value. For more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links.

failed to select a version for `sdl2-sys` which could resolve this conflict

The problem appears to be a clash between sdl-sys and sdl2-ttf or sdl2-image. If anyone knows a fix…

 

Rust’s way of importing from other modules is bizarre

Rust’s way of importing from other modules is bizarre

I come from a background of Pascal and Delphi. From about 1989 Turbo Pascal 4 and subsequently Delphi had a really useful unit system for breaking a program down into multiple source files. A unit is the equivalent of Rust’s mod.  Anything you wish to export from a unit is put in the interface section of the source file- consts, types, functions etc. Like making them pub in Rust. Then in the source file that wants to use these, you just put using name-of-unit. No hassles, very easy to use and it just works.

Now in Rust, they have a cockermanie system.  I had a main.rs that had several Structs (Card and Game) and decided to put it in another file poker.rs inside a mod poker {.

I made both Structs pub.  When compiling, there’s no errors in poker.rs but in the main.rs which has

pub mod poker;
use poker::{Card, Game};

There’s red lines under Card and Game.

note: struct `crate::poker::poker::Card` exists but is inaccessible etc. Grrr.

Rust compile errors

 

 

 

 

 

 

 

Now, no doubt there is soime way to make this work but why make it so difficult?