I have mentioned the Minimus AVR before on this blog. It is a very low-cost ATMEL micrcontroller board with a USB bootloader.

This post describes using the device as a HID (Human Interface Device) such as a keyboard or mouse. A challenge was set by a friend to take serial commands and, depending upon the command, output different strings to a computer via USB as if it was a keyboard plugged in.

This post gives some detail about issues and basic code for you to use.

Programming the Minimus

My blog post and work done by Spencer Owen gives info on getting the programming flow using the Arduino IDE, the TeensyDuino Arduino add-on and ATMEL flip to upload the .hex file.

The workflow is:

  1. Create code in the Arduino IDE.
  2. Upload using the Teensy 1.0 board. This will create the .hex file to upload to the Minimus.
  3. Put the Minimus into bootloader mode.
  4. Upload the .hex code to the Minimus using Atmel Flip.
  5. Reset the Minimus and it should run the code.

This workflow is OK for me but is a bit of a hassle. One day I’ll sort out some kind of makefile for this.

Note: This only works for Arduino IDE version 1.0.4. The Teensy is not yet supported on 1.0.5.

The challenge

The code needed to constantly monitor a serial port, wait for a certain set of characters (in this case ###~~~) and then write different lines as a keyboard, depending upon the number ofter the character set.

For example: if the received code was “###~~~3” then the keyboard should write “BUTTON 3 PRESSED”

Minimus as HID device

First I wanted to write as if the Minimus was a keyboard. The Teensy add-on gives some examples including a keyboard write example. This can be found by looking at the following example:

alt

Here is the simple USB keyboard example.

It will write “Hello World ” to the keyboard every 5 seconds or so.

To upload it to the Minimus you must create the .hex file but telling the Arduino IDE that the output device will act as an HID device. This is done by setting Tools -> USB Type to “Keyboard + Mouse”, as shown here:

alt

Ensure that you set this back to “Serial” before you use the serial monitor, or else you will not get any data and will think its not working (there is no error message).

This worked great and (slightly annoyingly) started writing “Hello World” anywhere where the cursor was active.

Problems encountered

I had already produced some code which worked on an Arduino to monitor the serial lines and perform actions depending upon the strings received. This had been working fine on the Arduino (ATMega328 IC). I changed this code to perform Keyboard.print() functions when different serial strings were sent. This code compiled OK with the work flow above to put it onto the Minimus. The problem was that it did not seem to perform correctly.

I was using the string functions including the .substring function to do the comparison on the serial data. This has worked fine for me before. The problem was that with one or two string searches then it worked fine, with three or more string searches then it did not work.

This was strange to me as the code compiled and uploaded correctly and no warnings were given.

When going through the problem with fellow Nottingham hackspace member Mouse, he suggested that the sting functions were very memory intensive and this could be the problem.

We re-wrote the code using a char array (rather than a sting) and used the strncmp function to compare a number of chars within the array. This is far less memory intensive.

It worked perfectly. So this is a bit of a warning to try and write memory efficient code for this device. I was using the MinimusAVR v1 with 16kB, rather than the newer version which have 32kB.

Final code

alt

The final code worked well as can be seen in these screen shots:

alt

This was the input data sent over the serial port.

alt

The final code is available to download as a sketch here, or written here.

Please feel free to use and adapt for your own purposes.

2 responses to “Minimus, Arduino IDE and USB

Leave a Reply

Your email address will not be published. Required fields are marked *