Recent Posts

2018-05-12

How to get Mac High Sierra Time Machine backups working on an Ubuntu 18.04 server

There are lot's of howtos out there how to get Linux servers to support TimeMachine networked backups for Mac clients. But lately they all stopped working due to latest Mac High Sierra updates. Apple moves away from Netatalk protocol and recommends using SMB. Unfortunately the latest Samba you will get on Ubuntu 18.04 is 4.7, and this does not support so called fruit extensions needed by Mac to have Time Machine writing it's data to the destinations. We need to get Samba 4.8 that was released not so long ago.

So let's start. I'm assuming you do not have any other samba installed. The backup folder will pop up as "backup on <your linux server name>" and you will be using user name backups with the password you'll set on the way. I strongly recommend to read the howto I've posted at the end. It allows for a multi user setup.

Install samba:
sudo apt-get-repository ppa:linux-schools/samba-latest
sudo apt update
sudo apt install samba
This installs samba 4.8 that is a hard requirement to get SMB based TimeMachine backups working.

Let's get the old samba config out of the way
mv /etc/samba/smb.conf /etc/samba/smb.confORG
Now create and edit /etc/samba/smb.conf

[global]
server role = standalone server
passdb backend = tdbsam
obey pam restrictions = yes
security = user
printcap name = /dev/null
load printers = no
socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=524288 SO_SNDBUF=524288
server string = Samba Server %v
dns proxy = no
wide links = yes
follow symlinks = yes
unix extensions = no
acl allow execute always = yes
# Special configuration for Apple's Time Machine
fruit:model = MacPro
fruit:advertise_fullsync = true
fruit:aapl = yes
fruit:time machine = yes
## Definde your shares here
[backup]
path = /space/backups
valid users = backups
writable = yes
durable handles = yes
kernel oplocks = no
kernel share modes = no
posix locking = no
vfs objects = catia fruit streams_xattr
ea support = yes
browseable = yes
read only = No
inherit acls = yes

Create and edit /etc/avahi/services/timemachine.service

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
 <name replace-wildcards="yes">%h</name>
 <service>
   <type>_smb._tcp</type>
   <port>445</port>
 </service>
 <service>
   <type>_device-info._tcp</type>
   <port>0</port>
   <txt-record>model=RackMac</txt-record>
 </service>
 <service>
   <type>_adisk._tcp</type>
   <txt-record>sys=waMa=0,adVF=0x100</txt-record>
   <txt-record>dk0=adVN=backup,adVF=0x82</txt-record>
 </service>
</service-group>
Create and edit /etc/rc.local as it looks like that samba 4.8 does not come with startup scripts.
#!/bin/bash
echo " # starting samba from rc.local"
smbd
exit 0
Add the backup directory, backup user and set permissions on the directory. This also sets the password for SMB/CIFS access:
useradd -m backups
smbpasswd -a backups
mkdir -p /space/backups
chown backups /space/backups
chmod 700 /space/backups
And let's start this (no reboot required):
/etc/init.d/avahi-daemon restart
smbd
And there, the new backup destination should show up in all macs connected to this network. REMEMBER KIDS, DO ENCRYPTED BACKUPS!

ps. how to view you mac backups progress:
log stream --style syslog --predicate 'senderImagePath contains[cd] "TimeMachine"' --info

Credits:
https://www.reddit.com/r/homelab/comments/83vkaz/howto_make_time_machine_backups_on_a_samba/

2017-06-26

Sim racing and game controllers - part 4 - Arduino leonardo project - TENSIOMETERS

Check the previous entries about the project: part1part2, part3 and the video and problems.

So I'm still using the Arduino racing pad since 2 years. I'm still racing in LFS, now in Assetto Corsa with it. I've complained about what's bad about it. And now I've solved another issue that I've had - the crappy joysticks. I've used FSR402 tensiometers for both accelerator and braking. This seems much more natural than using the lever sticks. And precise. Now I can drive powerful cars with no ABS and TC in Assetto Corsa :)

Why tensiometers?


When I've started thinking about that crappy joysticks, I've looked at what the game consoles are doing for the trigger buttons on the pads:

  • X360 pad uses a potentiometer with clever levers,
  • Playstation uses tensiometers,
  • X One uses Hall Effect sensors.
I wanted to buy the replacement parts for the X360 to check the levers, but the tensiometers are just cheaper. The Hall Effect sensors seem to be a very cool idea, but you really need to have an idea how to sort out the mechanics.

How does it look:

Bad. Ugly. But works.









I've placed the tensiometers along another edge of this beautiful lunch box. And now I can use both methods of controlling the speed of the car by just remapping controls. Frankly speaking - I haven't used the levers since I've got the tensiometers running.

Wiring FSR402 to Arduino:

Check this for examples of wiring and code: https://learn.adafruit.com/force-sensitive-resistor-fsr/using-an-fsr. After wiring the tensiometer properly you use it within the code like any other potentiometer.

Video:


Summary:

This just works. I'm using this for gaming weekly or biweekly (you know, 3 kids) for the last 3-4 months.

Using tensiometers for game controlers has another potential application. People making pedals for racing always look for methods to measure travel for acceleration and pressure for braking - to simulate real car hydraulics. All not ridiculously expensive steering wheels on the market use potentiometers for all pedals. And with a very simple mechanism (1 swing, some foam) you can get a pressure measuring tensiometer based pedal - you'll need a bigger tensiometer thou.

Code:

The requirements for having this running are described in the part3 of this "journey".

#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
const int pinButton = 2;

// this is for printing to COM - set to 1 to get printouts
const int printuj = 0 ;

void setup() {
  pinMode(pinLed, OUTPUT);
  pinMode(pinButton, INPUT_PULLUP);
  Gamepad.begin();

  if ( printuj == 1 ) {
    Serial.begin(9600);
  }
}

void loop() {
  // limits for steering
  float steeringmin=0;
  float steeringmax=1023;
  // limits for joystick braking
  float brakemin=0;
  float brakemax=1023;
  // limits for joystick accelerating
  float accelmax=1023;
  // limits for tensometer acceleration and braking
  float brakemaxb=910;
  float accelmaxb=910;
  float accelmin=0;
  float min=-32767;
  float max=32767;
  float diff=max-min;
  float minB=-123;
  float maxB=+123;
  float diffB=maxB-minB;

  // steering
  float steer = (float)analogRead(0)/(steeringmax-steeringmin)*diff + min;

  // acceleration, braking for joysticks and for tensometers
  float accel = ((float)analogRead(2)/(accelmax-accelmin))*diff + min;
  float brake = ((float)analogRead(1)/(brakemax-brakemin))*diff + min;
  float accelb = ((float)analogRead(4)/(accelmaxb-accelmin))*diffB + minB;
  float brakeb = ((float)analogRead(3)/(brakemaxb-brakemin))*diffB + minB;

  // all 4 buttons readouts
  int gearup = digitalRead(2);
  int geardown = digitalRead(3);

  int gearupb = digitalRead(4);
  int geardownb = digitalRead(5);

  // idiotic deadzones for joystick
  if ((accel <= 3) and (accel >= -3)) {
    accel=0;
  }
  if ((brake <= 3) and (brake >= -3)) {
    brake=0;
  }

  // limits for tensometers
  if (accelb > maxB) {
    accelb=maxB;
  }
  if (brakeb > maxB) {
    brakeb=maxB;
  }

  // this is the COM printing
  if ( printuj == 1 ) {
        Serial.print ((int)steer);
        Serial.print (' ');
        Serial.print (analogRead(0));
        Serial.print (' ');
        Serial.print ((int)accel);
        Serial.print (' ');
        Serial.print (analogRead(2));
        Serial.print (' ');
        Serial.print ((int)brake);
        Serial.print (' ');
        Serial.print (analogRead(1));
        Serial.print (' ');
        Serial.print (gearup);
        Serial.print (' ');
        Serial.print (geardown);
        Serial.print (' ');
        Serial.print (gearupb);
        Serial.print (' ');
        Serial.print (geardownb);
                Serial.print (' ');
                Serial.print (analogRead(3));
                Serial.print (' ');
                Serial.print ((int)brakeb);
            Serial.print (' ');
                Serial.print (analogRead(4));
                Serial.print (' ');
                 Serial.print ((int)accelb);
        Serial.println (' ');
  }

 \\ button switching
 if (gearup == 0)
 {
   Gamepad.press(1);
 } else {
   Gamepad.release(1);
 }

 if (geardown == 0)
 {
   Gamepad.press(2);
 } else {
   Gamepad.release(2);
 }
 if (gearupb == 0)
 {
   Gamepad.press(3);
 } else {
   Gamepad.release(3);
 }

 if (geardownb == 0)
 {
   Gamepad.press(4);
 } else {
   Gamepad.release(4);
 }


 \\ output to axises. xA is 16bit, the rest I don't remember and it does not matter.
 Gamepad.xAxis((int)steer);
 Gamepad.yAxis((int)accel);
 Gamepad.rxAxis((int)brake);
 Gamepad.zAxis((int)accelb);
 Gamepad.rzAxis((int)brakeb);


 Gamepad.write();

}

Next step: getting some better chassis for the device.

2016-12-18

Headless linux google photos sync (nasty and dirty)

Intro

Google Photos is a marvelous service! You can store all of your photos for free (if you'd agree to compress them) and have them on the go on every device. It has it's downsides: sharing all photos to your wife is not an option and managing them (folders, mass actions, etc.) is not there. But hey, it comes at the low, low, low price of FREE.

Google did release a simple and effective tool for Mac and Windows that syncs all the photos you have available on your computer to their service. They did not release this for Linux. So people who have thousands of photos on their drives are left only with the possibility of dragging them to the browser window. And if you want to do this for 35000 images - this is going to be a wild ride.

Of course you can have a virtual machine with Windows and put the Google Sync Tool there, but let's ignore this option :) It needs at least 2G of ram and a Windows licence.

You can run the sync tool on Wine, as discussed here: https://www.reddit.com/r/linux/comments/37ptsw/any_got_the_new_google_photos_backup_desktop/, but you still run this manually and need X. If you wanted to run this on a server that you use to store all of your photos you were out of luck.

BUT NOT ANY MORE.

Ingredients:

Let's get the authentication key first

So just do what the howto mentioned above tells you to. You can use a friends Windows PC or download Windows ISO from Microsoft pages and run this as a trial under KVM or other virtualization solution of your choice. 

Install the Google Photos Sync Tool, fully authorize and export the regedit key that should contain the data below:
Location: HKEY_CURRENT_USER\Software\Google\Picasa\Picasa2\Preferences
[HKEY_CURRENT_USER\Software\Google\Picasa\Picasa2\Preferences]"GoogleOAuth"=hex:xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,\ xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx,xx"GoogleOAuthEmail"="youremail@gmail.com""GoogleOAuthServices"="mail,lh2,cp,cp.manager,mailrelay,plus.stream.write,plus.circles.read,plus.profiles.read,plus.me,plus.media.upload,plus.media.readonly,plus.settings,youtube,""GoogleOAuthVersion"=dword:00000005
Get this file (lets call it gphotos.reg) to the server and you can delete the Windows virtual machine.

Let's install wine and prepare

We won't be needing X on the remote server, so login there with SSH and X forwarding:
ssh user@server -X
if you run now (remotely)
xclock
you should see the xclock window showing up on your workstation. If it does not you'd need to google to enable this, but this should work out of the box on most recent Linux distros.

Steps to be carried out:
  • install wine (as root)
sudo apt install wine
  • create a separate user for the sync tool (let's call it gphotos)
  • upload the sync tool exe and registry file from windows to the gphotos home directory
  • as the gphotos user start winecfg (using a remote SSH connection with X forwarding)
winecfg
  • switch the Windows version to Windows 7

Let's install Google Photos Sync Tool

Steps to be carried out:
  • simply run (using a remote SSH connection with X forwarding)
wine gpautobackup_setup.exe
  • proceed with default installation options until you will see that the installer is trying to open something up and it will just keep a blank white window open. (it tries to start up IE to let you log in to Google Photos),
  • kill the wine processes, shut the window
  • run regedit to install the gphotos.reg file and import
regedit
  • run the Google Photos Sync Tool again (but installed on your drive)
cd "/home/gphotos/.wine/dosdevices/c:/users/gphotos/Local Settings/Application Data/Programs/Google/Google Photos Backup"
wine "Google Photos Backup.exe"
  • it will now run from the moment after the authorization tool place and you will be able to select the folders you'd want to sync (be sure to check read permissions before this step).
  • finish, the sync tool started syncing your photos
Now the sync tool runs, but is still connected with your X session over SSH. Kill it and move on.

Start up at boot time

We'll be using xvfb-run, so be sure to make sure you have it on your system. This is used to start up a X session, without any X on the server.

  • Let's create a startup file for the service, located in /home/gphotos/gphotossync with the following contents:
cd "/home/gphotos/.wine/dosdevices/c:/users/gphotos/Local Settings/Application Data/Programs/Google/Google Photos Backup"xvfb-run -a -e /home/gphotos/log.log -s "-screen 0 1280x800x16" wine "Google Photos Backup.exe" & &>/tmp/gphotos.log

  • a few remarks here:
    • this assumes you did not customize any paths and run this as gphoto user,
    • -a makes xvfb-run select a display ID if the default one is busy,
    • /home/gphotos/log.log is a log for xfvb-run issues,
    • /tmp/gphotos.log for any issues wine might pop up with,
  • this script just runs the Google Photo Sync tool in the background and allows it to do it's job.
  • Let's just add this to the /etc/rc.local file, by adding the following line BEFORE the line with exit 0:
sudo -u gphotos -i /home/gphotos/gphotossync
  • and that's it. After reboot you should see your Google Photos Sync tool running and syncing photos.

Logs and monitoring

You might want to know what Google Photos Sync is actually doing, so:
  • you can see this in the process list or htop or other tools of choice if it is running or not,
  • you can access the logs to see what JPG files were processed when:
grep -i jpg "/home/gphotos/.wine/dosdevices/c:/users/gphotos/Local Settings/Application Data/Google/Google Photos Backup/network.log"
And that's it. I've managed to sync nearly 40 thousand family pictures, using Google's free Photos, from my Linux's server drive. If I add anything to the pictures folder, the Sync tool picks up straight away and uploads.

So good luck! 

2016-04-15

Sim racing and game controllers - part 3 - Arduino leonardo project

The issue

I've posted these articles on the Arduino racing pad that I've built: part1, part2 and the video and problems. I've complained a bit on the fact that what I have done has only 8bits of information for the steering wheel. That should be enough for steering, but when driving straight in LFS, you could see that the steering wheel "jumps" between positions. 8bits of information is 256 possible positions of the wheel and when you get 270 degrees of rotation on a potentiometer, that is visible - you cannot turn the wheel by a half of a degree.

My third kid started sleeping better, so now I have bits of time here and there to play around with toys. After a beer or two I've said to my self "8bits steering improvement - how hard can it be?".

The ingredients

Back then, one year ago, you needed to copy the custom HID.cpp and USBAPI.h files into the Arduino IDE libraries to get the Joystick definition. Now you don't have to. Arduino 1.6.8 IDE allows for libraries to be added and guy named Nico prepared marvelous library called Arduino HID Project 2.4.3. This library contains Gamepad definition with 32 buttons, 2 dpads, 4 16bit axis and 2 8bit axis.

So I've used:
  • Arduino Leonardo with the tupperware racing pad I've build in the previous posts, 
  • Arduino IDE 1.6.8,
  • Arduino HID Project 2.4.3,
  • moderate amount of alcohol.

The solution

The new Arduino HID project makes it very tidy and simple to set up the controler I needed. The code for the Arduino Sketch is given below. When someone will try to copy and play with that, be sure to check which axis has how many bits. X, Y, RX, RY are 16bits, Z and RZ are 8bits.

The Arduino HID needs to be downloaded and copied into the libraries directory of your Arduino IDE installation. I'm sure there are other places you can put it in, but I guess I'm too lazy :)

The results

Now I've got all axis on 16 bits. Arduino Leonardo A/D has 10 bits resolution, so that is an overkill. The USB device is 64 times more precise than what Arduino can return, but it does not matter. I got what I needed on a very simple set of libraries. Now when I turn the wheel knob everything is super smooth. 

Now the only thing keeping me from winning every LFS game is my set of mediocre virtual driving skills.

Next improvement - some other mechanical potentiometers for the throttle and brakes.

The Code


#include "HID-Project.h"

const int pinLed = LED_BUILTIN;
const int pinButton = 2;

void setup() {
  pinMode(pinLed, OUTPUT);
  pinMode(pinButton, INPUT_PULLUP);
  Gamepad.begin();
}

void loop() {
  float steeringmin=0;
  float steeringmax=1023;
  float brakemin=0;
  float brakemax=1023;
  float accelmax=1023;
  float accelmin=0;
  float min=-32767;
  float max=32767;
  float diff=max-min;


  float steer = (float)analogRead(0)/(steeringmax-steeringmin)*diff + min;
  float accel = ((float)analogRead(2)/(accelmax-accelmin))*diff + min;
  float brake = ((float)analogRead(1)/(brakemax-brakemin))*diff + min;
  // on second thought, I just could have multiplied one thing by 64 to get the result
 
  int gearup = digitalRead(2);
  int geardown = digitalRead(3);
 
  if ((accel <= 3) and (accel >= -3)) {
    accel=0;
  }
  if ((brake <= 3) and (brake >= -3)) {
    brake=0;
  }



 
 if (gearup == 0)
 {
   Gamepad.press(1);
 } else {
   Gamepad.release(1);
 }

 if (geardown == 0)
 {
   Gamepad.press(2);
 } else {
   Gamepad.release(2);
 }
 Gamepad.xAxis((int)steer);
 Gamepad.yAxis((int)accel);
 Gamepad.rxAxis((int)brake);
 Gamepad.write();

 // zAxis is 8bit, I wanted to use 16bits on all

}

2015-12-30

Czy na klawiaturze mechanicznej pisze się lepiej?

Nie.

Ale od początku. Od około pół roku używam w pracy, codziennie, klawiatury mechanicznej opartej o przełączniki Cherry MX Blue. Model Razer BlackWidow "expert". Klawiatura przygotowana ewidentnie dla graczy - świeci się, błyszczy, obudowa ma ostre kąty, na przyciskach jest bardzo nowoczesne liternictwo. Żeby dało się tego używać musiałem przykleić podkładkę pod nadgarstki od innej klawiatury taśmą klejącą.

Na pewno ktoś zwróci uwagę na to, że w powyższym akapicie znalazły się jednocześnie terminy "Cherry MX Blue" oraz "w pracy". Wersja niebieska przełączników Cherry MX jest uważana za najlepszą do pisania tekstów, ale z drugiej strony jest to najgłośniejsza z popularnych mechanicznych konstrukcji. Wyróżnia się tym, że w momencie rejestracji naciśnięcia klawisza, słychać "skrobnięcie" blaszki o blaszkę.

Osoby lubiące korzystać z klawiatur mechanicznych odradzają ten ich rodzaj, głównie ze względu na uciążliwość dla osób postronnych. Ja siedzę dzielnie w pokoju z czterema innymi osobami i każdy kto mnie nie zna i chce się dowiedzieć kto to jest K., dowiaduje się, że to ten "co tak głośno na klawiaturze nap....". Faktycznie, taka klawiatura jest głośniejsza niż cokolwiek innego o konstrukcji membranowej lub klawiatura nożycowa z laptopa. Ostatnio zauważyłem, że mi samemu łatwiej się pracuje jak założę słuchawki i słucham muzyki.

Skąd więc początkowa teza, że na klawiaturze mechanicznej nie pisze się lepiej? zauważyłem, że mniej błędów popełniam używając standardowej klawiatury laptopa Lenovo X220.

Klawiatury z laptopów Lenovo, a wcześniej IBM Thinkpad, zawsze uchodziły za bardzo dobre. Cenię w nich bardzo dobry kształt przycisków, bardzo dobrą wyczuwalność nacisku oraz bardzo poprawny układ, bez dodatkowych przycisków na prawo od entera. Obudowa laptopa stanowiąca podkładkę pod nadgarstki idealnie uzupełnia całość. Korzystałem z przerwami z tego typu komputerów od wczesnych lat dwutysięcznych, kiedy to za pożyczone od rodziców pieniądze nabyłem w Warszawie pod Głównym Urzędem Statystycznym model IBM 600x za 3500 złotych polskich brutto.

To co wcześniej uznawałem za przewagę klawiatur mechanicznych, teraz zaczęło mi przeszkadzać. Chodzi o wysoki skok klawisza. Żeby w pełni docenić to, że klawiatura mechaniczna, a szczególnie przełączniki Cherry MX Blue rejestrują naciśnięcie przycisku w połowie jego drogi, trzeba się naprawdę nauczyć na tym pisać. Jeśli użytkownik (ja) co i raz przesiada się między laptopem, klawiaturą mechaniczną a wynalazkiem z odwróconymi gumkami (Fujitsu z innego artykułu), nie będzie w stanie w pełni wykorzystać potencjału zabytkowej konstrukcji. Dobijając przyciskami "do dna" nie będziemy w stanie pisać szybciej niż na innych urządzeniach. Żeby nauczyć się porządnie używać mojego Razera, musiałbym przez ostatnie pół roku pisać wyłącznie na tej klawiaturze lub mieć ich kilka.

Oczywiście klawiatura mechaniczna ma masę zalet. Jest to urządzenie lepszej jakości niż to co zwykle dodaje się do komputerów. Można się również delektować każdym naciśniętym klawiszem - przesiadka na coś takiego po taniej klawiaturze logitecha to doznanie niemal mistyczne. Jednak niniejszy wpis chciałem poświęcić wyłącznie tematowi użyteczności klawiatur mechanicznych w zadaniach do których zostały stworzone - czyli czy rzeczywiście pisze się na nich szybciej, wygodniej i popełnia mniej błędów.

Nikt przy zdrowych zmysłach nie uważa, że jak kupi sobie notatnik Moleskine (polecam) to od razu zacznie pisać powieści jak Ernest Hemingway. "Mistrz długiej prostej i lekkiego łuku" za kierownicą bolidu F1 nie będzie brał zakrętów jak Kubica. Tak samo jest z klawiaturami. Czy piszesz powieść, posty na Facebooku, sztukę której nikt nie zrozumie czy po prostu kod w Javie i komentarze do ticketów w Jirze - to czy klawiatura ma taką czy inną konstrukcję, ma znaczenie niewielkie. Co najwyżej jeśli jest niewygodna mogą człowieka boleć nadgarstki, łokcie albo palce. Szybciej i z mniejszą ilością błędów będzie pisać dopiero jak się tego nauczy - niezależnie od konstrukcji mechaniki.

(ludzie na forach fotograficznych chyba z powyższym akapitem się nie zgodzą)

(niniejszy artykuł sponsoruje whisky oraz lód)

(moje trzecie dziecko - Aniela - skończyło właśnie pół roku :) )

2015-06-16

My first Arduino racing pad - video and conclusions

Hi!

For future reference, check out the video with the Arduino racing pad, that I've built here: http://jakkul.blogspot.com/2015/06/sim-racing-and-game-controllers-part-2_3.html

My first Arduino racing pad in full glory!


There are some problems with the design that need working on:
  • The ergonomics are not the best. It's hard to use one hand for acceleration, brakes and gears.
  • I think, that 8bits of data fed by Arduino to the PC for steering are not enough.
  • You cannot drink beer while driving. Two hands need to be always on the pad. This is not the case when driving with a mouse or a wheel.
  • The gaming-pad-like joysticks are crap. About half of the axis travel is analogue, the rest is in full on state. 
I have some ideas for the next design. But I guess that will have to wait until autumn, since my 3rd kid is due to be born within next 2-3 weeks :)

2015-06-03

Sim racing and game controllers - part 2 - Arduino leonardo project

I've written here about the need to build a customized pad-like controller for my racing games: http://jakkul.blogspot.com/2015/05/sim-racing-and-game-controllers-part-1.html Now I've managed to find 3 hours yesterday to put the thing together. But lets start from the beginning.


The Plan

The goal is to have the right hand operate the steering wheel potentiometer, the left hand's index and middle finger to operate accelerator and brakes, left thumb to operate gears up and down. After a long debate I came up with this:


The Plan

The ingredients

The thing that looked the most like the case for the device was a tupperware-like food container. It's also easily available, easy to drill and put things on and has a low low price of zero.

I also bought:
  • Arduino Leonardo (clone I guess),
  • two thumb joysticks,
  • four buttons (two used),
  • one linear 10k potentiometer,
  • some cables.

The stuff

Building

Building was a fun and easy part. I've screwed everything to the container, made a hole for cabling and connected together.


 Connecting it all was easy:
  • 5V and ground everywhere,
  • digital pins 2 and 3 to the gear switches,
  • X axis of the thumb joysticks (both) to analog lines 1 and 2,
  • potentiometer to analog line 0.
I haven't connected the rest of the stuff available in the joysticks - that was not needed.




Starting up

I've used the following excellent howto on how to get Arduino Leonardo to behave like a Joystick: http://www.instructables.com/id/Add-USB-Game-Controller-to-Arduino-LeonardoMicro/?ALLSTEPS. This is also very helpful: http://www.imaginaryindustries.com/blog/?p=80&cpage=1. There are two files that you need to get into your Arduino IDE environment (HID.cpp and USBAPI.h) that define the Joystick, that is not defined in Arduino IDE standard distribution.

I've used Arduino IDE 1.6.4 and Windows 7 64bit. I'm a linux addict, but since this is to be used in racing games, I wanted to test it straight away.

The step 2 of the howto mentioned above provides a test sketch (Arduino program) that allows for the arduino board to send signals as a joystick to your PC. The code that I've made basing on that and other examples in the net is presented below. You can switch displaying all values on COM port for double checking if it works. You can also tune your potentiometers and joysticks using max-min ranges. Keep in mind, that Arduino reads potentiometers and joysticks, returning and integer ranging from 0 to 1023, and the Joystick class requires the values to be between -127 and 127 for each axis.

Summary

It just works! I can now play my racing games with a full analogue controller without the hassle of a steering wheel. It's a different kind of fun, but still fun :)

The code

int useserial = 1;

float steeringmin = 0;
float steeringmax = 1023;
float brakemin = 501; //0;
float brakemax = 1023;
float accelmax = 1023;
float accelmin = 520; //0;
// 0 steer 1 brake 2 accel
float min = -127;
float max = 127;
float diff = max - min;

void setup() {
  Joystick.begin();
  if (useserial == 1 )
  {
    Serial.begin(9600);
  }
}

void loop() {

  float steer = (max - min) / (steeringmax - steeringmin) * ((float)analogRead(0) - steeringmin) + min;
  float accel = (max - min) / (accelmax - accelmin) * ((float)analogRead(2) - accelmin) + min;
  float brake = (max - min) / (brakemax - brakemin) * ((float)analogRead(1) - brakemin) + min;
  int gearup = digitalRead(2);
  int geardown = digitalRead(3);

  if (steer > max) {
    steer = max;
  }
  if (steer < min) {
    steer = min;
  }
  if (accel > max) {
    accel = max;
  }
  if (accel < min) {
    accel = min;
  }
  if (brake < min) {
    brake = min;
  }
  if (brake > max) {
    brake = max;
  }

  if (useserial == 1 )
  {

    Serial.print ((int)steer);
    Serial.print (' ');
    Serial.print (analogRead(0));
    Serial.print (' ');
    Serial.print (accel);
    Serial.print (' ');
    Serial.print (analogRead(2));
    Serial.print (' ');
    Serial.print (brake); Serial.print (' ');
    Serial.print (analogRead(1));
    Serial.print (' ');
    Serial.print (gearup);
    Serial.print (' ');
    Serial.println (geardown);
  }
  if (gearup == 0)
  {
    Joystick.pressButton(0);
  } else {
    Joystick.releaseButton(0);
  }

  if (geardown == 0)
  {
    Joystick.pressButton(1);
  } else {
    Joystick.releaseButton(1);
  }
  Joystick.setXAxis((int)steer);
  Joystick.setYAxis((int)accel);
  Joystick.setZAxis((int)brake);
  Joystick.sendState();
}