USB-Rubber Ducky scripts on Arduino/Leostick

The other day I was at Jaycar and saw that they are now selling small USB sticks that are arduino compatible.
It is called the LeoStick and is made by Freetronics down in Melbourne.
Seeing that it could pretend to be a USB HID device (ie keyboard/mouse) I wondered if I could do the sort of thing that the USB Rubber Ducky from Hak5 can do
As it turns out the answer is YES 🙂

Since it was possible I spent an hour or two writing a quick shell script which can convert ducky script payloads into a sketch suitable for uploading to the LeoStick (or any arduino that has USB-HID capability)   The end result is a small bash script which can be downloaded from

Usage is fairly simple – you run the script with two options – the first being the payload file, and the second being the arduino script output.

ie: compile_payload lock_prank.txt lock_prank.ino

Various payloads can be found linked from the USB-Rubber-Ducky wiki

As a bit of fun I changed the lock_prank payload to work on Gnome/Linux and it also plays the mission impossible theme once done 😉
Grab it from lock_prank.ino

Also note that to get this working you need to edit the arduino libraries so that the sendReport function is marked as public.

To to this edit the USBAPI.h file which can be found in ${ARDUINO_DIR}/hardware/arduino/cores/arduino directory.
This may be /usr/share/arduino/hardware/arduino/cores/arduino/USBAPI.h or similar
If you installed the LeoStick board stuff from their website then it will be under your sketches directory as hardware/LeoStick/cores/arduino/USBAPI.h

Open that file and find

    KeyMap* _keyMap;
    void sendReport(KeyReport* keys);
    void setKeyMap(KeyMap* keyMap);
    virtual size_t write(uint8_t);

Then change that to

    KeyMap* _keyMap;
    void setKeyMap(KeyMap* keyMap);
    void sendReport(KeyReport* keys);
    virtual size_t write(uint8_t);

Then everything should work fine.

Freetronics DMD – Games

I had a little while free and so ported a bunch of games I wrote for the Peggy2 to work with a pair of DMD’s
In doing so the games now run at 32×32 and are controlled by a Wii nunchuck.
The games are snake, pong, breakout and race.
The Arduino code is available at
The two screens are daisy-chained but as the cable connecting them is quite short the top one is upside-down.
To get this working the modified DMD library is setup to handle rows of displays where odd-numbered rows are upside down.
See this diagram for how it is laid out (for 2×2 case)

Playing is fairly straight-forward.  Only thing to remember is that the ‘z-button’ on the nunchuck is used to select and the ‘c-button’ is used to exit back to the menu

Have a look at the video below to see them in action.
[youtube_sc url=”5WW9ZmvjSA4″]

Freetronics DMD – updated library

I recently got two Dot Matrix Displays from Freetronics –
They are 32×16 LED panels that can be daisychained via SPI.
It came with a quick library which worked okay but could do with a few enhancements 😉
My modified library supports multiple displays (tested with 2×1 and 1×2 layout – but should work with other layouts)
It also supports multiple fonts, marquee text, scrolling the display around and grayscale (grayscale sort of works – but not well 🙁 )
I have updated the examples included in the original library to use these new functions.

The library can be downloaded from
See freetronics forum for my DMD library
[youtube_sc url=”5DLmWrpV-3M”]

MusicXML/MIDI for Arduino

For a recent project I wanted to add music.

Being that I am not musically talented in any way I figured the best way to do this was to generate the music from a midi file.

To help with this I wrote a converter that would take a MusicXML file and output an include file.

It simply reads the XML and outputs an array where each note is stored in two uint16 values – the first is the note name as a defined in pitches.h (ie NOTE_C4), the second is the duration in milliseconds.  The entire array is stored in PROGMEM so as to not use up too much ram on the AVR.

The script is as follows (or download from here)

# Convert a single-channel musicxml file to include file for arduino
# Pass the xml filename on the commandline and a .h file will be generated
# with the same name (replacing .xml with .h)

# Needs XML::Mini perlmodule (libxml-mini-perl package in debian/ubuntu)
use XML::Mini::Document;
my $size = 0;

my $name = $ARGV[0];
$name =~ s/\.xml$//;
open(OUT, ">".$name.".h");
print OUT "PROGMEM prog_uint16_t ".$name."Tune[] = {\n";

my $xmldoc = XML::Mini::Document->new();
my $xmlHash = $xmldoc->toHash();
my $measures = $xmlHash->{'score-partwise'}->{'part'}->{'measure'};
foreach my $measure (@$measures) {
  my $notes=$measure->{'note'};
  foreach (@$notes) {
    my $note = "";
    if ($_->{'pitch'}) {
      if (defined $_->{'accidental'}) {
        if ($_->{'accidental'} eq "sharp") {
        } elsif ($_->{'accidental'} eq "flat") {
          $_->{'pitch'}->{'step'} =~ tr/A-F/FA-E/;
      $note = "NOTE_".$_->{'pitch'}->{'step'}.$_->{'pitch'}->{'octave'};
    } else {
      $note = "NOTE_00";
    my $duration = $_->{'duration'};
    print OUT $note.", ".$duration.", ";
  print OUT "\n";
print OUT "};\n";
print OUT "byte ".$name."TuneSteps = ".$size.";\n";
print "total memory usage:".(($size*4)+1)." bytes\n";
close OUT;

To actually use this generated include file I created a function ‘playMelody’ which is called regularly during the running of a sketch.

Each time it runs it checks if it is time to play the next note, if so then it reads the next note from the array,calls ‘tone’ to play it and then sets a variable to say when the next note should be played.  If it is not yet time to play the next note then it quickly returns (there is not much latency added by calling the function so it’s okay to call too often)

boolean playMelody()
  boolean retVal=true;
  if (millis() > time) {
    int toneVal = 0;
    int duration = 0;
    if (tuneStep >= SmoothCriminalTuneSteps) {
    duration = pgm_read_word_near(SmoothCriminalTune+(tuneStep*2)+1);
    if (toneVal) tone(speakerPin,toneVal,duration*2);
  return retVal;

I have a sample sketch which uses this to play a short melody loop as

Peggy 2 multi-game sketch v0.2

I’ve done a lot more work on the multi-game sketch for my Peggy2 – mostly to add functionality and fix up a few ‘issues’
See the original post here for more details on what is included
Download it from here When compiled it uses 13808 bytes so there only a bit of room to spare.
I’ve added a two player version of pong (use up/down buttons to control player two – I’ve made a separate plug-in controller for player two to make things easier to use)
I’ve fixed up the ‘breakout’ clone so it actually can be considered a ‘game’ 🙂
I also added a ‘demo’ game which just lights up all leds with a nice grayscale pattern than can be moved around via the direction buttons (press select to stop the motion)
The other main thing is that it now has sound!
With the release of arduino-0018 they added a function ‘tone’ to generate square waves on a digital pin. I connected a piezo speaker (from a headphone) to ADC5 and generated the tones on that pin.
There is intro music at the game select screen, music when displaying the score and various bips and beeps while playing the games.
To make the music I wrote a small perl script to convert MusicXML files to a suitable include file (will be documented in a separate post)
Adding the music and extra games meant I was hitting the space limits in the AVR so there is a bit of ‘dodgyness’ in the code so that it would fit.

See the original post here for more details on what is included