以下代码用于使用 ProMicro(中文 Micro)模拟键盘。一切都很好,除了我想将代码上传到其他板上,因为我知道我不会有键盘功能。但主要问题是 Arduino IDE(V1 和 V2.beta)不会编译或上传代码,除非我选择了 Micro board。我尝试过(没有任何运气),是阅读使用的板的类型,并使用(或不使用)键盘特定功能。但是我忘记了,在草图读取它使用什么类型的板之前,必须对其进行编译。所以这是一个半身像。任何想法如何解决这个问题?
#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]);
}
}
}