Controllino MINI as USB HID keyboard

Community guides and tutorials
Post Reply
User avatar
Jasin
Posts: 6
Joined: Wed Jan 30, 2019 8:35 am

Controllino MINI as USB HID keyboard

Post by Jasin » Thu Jul 25, 2019 10:08 am

Turn your Controllino into a USB HID keyboard, and make buttons that do whatever you want.

The USB HID keyboard conforms to the standard USB specification, so is functional on all modern operating systems.
USB HID usage tables
Codes for keyboard, keypad from page 53.

At first, you need to program a simple sketch to send keyboard report buffer via serial.
e.g.

Code: Select all

#include <Controllino.h>

#define SUPPLY_PIN CONTROLLINO_D7 //Supply pin for buttons
#define KEY_MOD_LCTRL  0x01       // Keyboard left SHIFT
#define KEY_C 0x06                // Keyboard c and C
#define KEY_V 0x19                // Keyboard v and V
#define KEY_NONE 0x00             // No key pressed

int state = 0;

// Keyboard report buffer
uint8_t buf[8] = {
    0};

void setup()
{
  Serial.begin(9600);

  // enable voltage for buttons
  pinMode(SUPPLY_PIN, OUTPUT);
  digitalWrite(SUPPLY_PIN, HIGH);

  // set input pins
  pinMode(CONTROLLINO_A0, INPUT);
  pinMode(CONTROLLINO_A1, INPUT);


  delay(200);
}

void loop()

{

  state = digitalRead(CONTROLLINO_A0);
  if (state == 1)
  {
    buf[0] = KEY_MOD_LCTRL;
    buf[2] = KEY_C;
    Serial.write(buf, 8);
    
    delay(50);
    
    //key release
    buf[0] = KEY_NONE;
    buf[1] = KEY_NONE;
    buf[2] = KEY_NONE;
    buf[3] = KEY_NONE;
    buf[4] = KEY_NONE;
    buf[5] = KEY_NONE;
    buf[6] = KEY_NONE;
    buf[7] = KEY_NONE;
    Serial.write(buf, 8);
  }

  state = digitalRead(CONTROLLINO_A1);
  if (state == 1)
  {
    buf[0] = KEY_MOD_LCTRL;
    buf[2] = KEY_V;
    Serial.write(buf, 8);

    delay(50);
    //key release
    buf[0] = KEY_NONE;
    buf[1] = KEY_NONE;
    buf[2] = KEY_NONE;
    buf[3] = KEY_NONE;
    buf[4] = KEY_NONE;
    buf[5] = KEY_NONE;
    buf[6] = KEY_NONE;
    buf[7] = KEY_NONE;
    Serial.write(buf, 8);
  }
}
  
Then upload it to Controllino as usual.

To change Controllino to HID device it is necessary to update FW of Atmega16U2 chip. It can be done by the special USB protocol called Device Firmware Update (DFU).
To set Controllino to DFU mode it is necesarry to power it up and then connect these two pins:
Image
Then Controllino is no longer connected to PC like Controllino, but like ATmega 16U2 see:
Image

Now it is possible to update FW of ATmega16U2 same way as Arduino can be updated
Arduino procedure
For Windows user, the best way is to use FLIP to upload the HEX file
FLIP
As hex file I used one from here:
Arduino-keyboard-0.3.hex

And finally after reconnecting Controllino to PC, the PC recognizes Controllino as HID device and it is possible to copy-paste (CTRL-C CTRL-V) just by buttons connected between D7(support) and A0(CTRL-C input) and A1(CTRL-V input).

Just be careful with keyboard report buffer.

Code: Select all

// Keyboard report buffer
uint8_t buf[8] = {
    0};
The second byte buf[1] is reserved.
The First byte buf[0] can be used just for modifiers:

Code: Select all

/**
 * Modifier masks - used for the first byte in the HID report.
 */
#define KEY_MOD_LCTRL  0x01
#define KEY_MOD_LSHIFT 0x02
#define KEY_MOD_LALT   0x04
#define KEY_MOD_LMETA  0x08
#define KEY_MOD_RCTRL  0x10
#define KEY_MOD_RSHIFT 0x20
#define KEY_MOD_RALT   0x40
#define KEY_MOD_RMETA  0x80
Just last six bytes can be used for KEY press.
And don't forget to RELEASE key or it will remain pressed.

User avatar
Jasin
Posts: 6
Joined: Wed Jan 30, 2019 8:35 am

Re: Controllino MINI as USB HID keyboard

Post by Jasin » Thu Jul 25, 2019 10:12 am

To return Controllino to the original state, just switch it to DFU mode again and upload the latest ATmega16U2 Arduino HEX file:
Arduino ATmega HEX files

Post Reply