Search code examples
arduinokeyboard

Compiling Arduino code with Keyboard.h lib on other boards (UNO, Nano, etc)


The following code is used for emulating a Keyboard using a ProMicro (chinese Micro). All works well, except I want to upload the code to other boards, knowing that I won't have the Keyboard functionality. But the main issue is that the Arduino IDE (both V1 and V2.beta) does not compile or upload the code unless I have the Micro board selected. What I have tried (without any luck), is to read the type of board used, and use (or not) the Keyboard specific functions. But I forgot that before the sketch would read what type of board it's used, it must be compiled. So it's a bust. Any ideeas how to work arround this issue?

#include <mcp2515.h>
#include <Keyboard.h>

#if   defined(ARDUINO_AVR_ADK)       
        #define BOARD "Mega Adk"
    #elif defined(ARDUINO_AVR_BT)    // Bluetooth
        #define BOARD "Bt"
    #elif defined(ARDUINO_AVR_DUEMILANOVE)       
        #define BOARD "Duemilanove"
    #elif defined(ARDUINO_AVR_ESPLORA)       
        #define BOARD "Esplora"
    #elif defined(ARDUINO_AVR_ETHERNET)       
        #define BOARD "Ethernet"
    #elif defined(ARDUINO_AVR_FIO)       
        #define BOARD "Fio"
    #elif defined(ARDUINO_AVR_GEMMA)
        #define BOARD "Gemma"
    #elif defined(ARDUINO_AVR_LEONARDO)       
        #define BOARD "Leonardo"
    #elif defined(ARDUINO_AVR_LILYPAD)
        #define BOARD "Lilypad"
    #elif defined(ARDUINO_AVR_LILYPAD_USB)
        #define BOARD "Lilypad Usb"
    #elif defined(ARDUINO_AVR_MEGA)       
        #define BOARD "Mega"
    #elif defined(ARDUINO_AVR_MEGA2560)       
        #define BOARD "Mega 2560"
    #elif defined(ARDUINO_AVR_MICRO)       
        #define BOARD "Micro"
    #elif defined(ARDUINO_AVR_MINI)       
        #define BOARD "Mini"
    #elif defined(ARDUINO_AVR_NANO)       
        #define BOARD "Nano"
    #elif defined(ARDUINO_AVR_NG)       
        #define BOARD "NG"
    #elif defined(ARDUINO_AVR_PRO)       
        #define BOARD "Pro"
    #elif defined(ARDUINO_AVR_ROBOT_CONTROL)       
        #define BOARD "Robot Ctrl"
    #elif defined(ARDUINO_AVR_ROBOT_MOTOR)       
        #define BOARD "Robot Motor"
    #elif defined(ARDUINO_AVR_UNO)       
        #define BOARD "Uno"
    #elif defined(ARDUINO_AVR_YUN)       
        #define BOARD "Yun"

    // These boards must be installed separately:
    #elif defined(ARDUINO_SAM_DUE)       
        #define BOARD "Due"
    #elif defined(ARDUINO_SAMD_ZERO)       
        #define BOARD "Zero"
    #elif defined(ARDUINO_ARC32_TOOLS)       
        #define BOARD "101"
    #else
       #error "Unknown board"
    #endif

void setup() {
  Serial.begin(250000);
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
}

///Do stuff here (read signals - output awesomeness)

void keyboardButtons(char firstBit){
      //KEY 1
      if(firstBit == 0x00){
        Keyboard.press('1');
        //Serial.println("1");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 2
      if(firstBit == 0x01){
        Keyboard.press(KEY_ESC);
        //Serial.println("KEY_ESC");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 3
      if(firstBit == 0x02){
        Keyboard.press('3');
        //Serial.println("3");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 5
      if(firstBit == 0x03){
        Keyboard.press('5');
        //Serial.println("5");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 9
      if(firstBit == 0x04){
        Keyboard.press('9');
        //Serial.println("9");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 8
      if(firstBit == 0x05){
        Keyboard.press('8');
        //Serial.println("8");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 7
      if(firstBit == 0x06){
        Keyboard.press('7');
        //Serial.println("7");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 6
      if(firstBit == 0x07){
        Keyboard.press('6');
        //Serial.println("6");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 4
      if(firstBit == 0x08){
        Keyboard.press('4');
        //Serial.println("4");
        delay(delayTime);
        Keyboard.releaseAll();
      }  
}

void keyboardJoystick(char firstBit, char secondBit){
      //KEY 10
      if(firstBit == 0x00 && secondBit == 0x06){
       // Keyboard.press(KEY_UP_ARROW);
       // Serial.println("KEY_UP_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 11
      if(firstBit == 0x10 && secondBit == 0x05){
       // Keyboard.press(KEY_UP_ARROW);
       // Serial.println("KEY_UP_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 12
      if(firstBit == 0x10 && secondBit == 0x00){
        Keyboard.press(KEY_UP_ARROW);
        //Serial.println("KEY_UP_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 13
      if(firstBit == 0x20 && secondBit == 0x00){
        Keyboard.press(KEY_DOWN_ARROW);
        //Serial.println("KEY_DOWN_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 14
      if(firstBit == 0x30 && secondBit == 0x00){
        Keyboard.press(KEY_LEFT_ARROW);
        //Serial.println("KEY_LEFT_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 15
      if(firstBit == 0x40 && secondBit == 0x00){
        Keyboard.press(KEY_RIGHT_ARROW);
        //Serial.println("KEY_RIGHT_ARROW");
        delay(delayTime);
        Keyboard.releaseAll();
      }
      //KEY 16
      if(firstBit == 0x01 && secondBit == 0x00){
        Keyboard.press(0xE0);
        //Serial.println("ENTER_KEY");
        delay(delayTime);
        Keyboard.releaseAll();
      }
}
void loop(){
  String messageRecv ;
  String incomingByte;
  if (Serial.available()){
    char c = Serial.read();
    incomingByte.concat(c);
  }
  if (incomingByte == "reqMsg"){
    Serial.println(lastSource);
    Serial.println(lastVolume);
    Serial.println(lastMessage);
  }
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK){
    if (BOARD == "Leonardo" && canMsg.can_id == 1597){
      keyboardButtons(canMsg.data[0]);   
    }

    if (BOARD == "Leonardo" && canMsg.can_id == 1598){
      keyboardJoystick(canMsg.data[0], canMsg.data[1]);      
    }
  }
}

Solution

  • After multiple searches, I've haven't found a way to compile only parts of the code, depending on the type of the board used. So I'm ending this topic with an answer.