Leonardo是Arduino开发板的一种型号,可以像其他Arduino板一样对Leonardo进行编程和使用。但是,有一些重要的区别:
The Leonardo differ from other Arduino boards in that they use a single microcontroller to both run your sketches and for USB communication with the computer. The Uno and other boards use separate microcontrollers for these two functions, meaning that the USB connection to the computer remains established regardless of the state of the main microcontroller. By combining these two functions onto a single processor, the Leonardo allows for more flexibility in its communication with the computer. It also helps to lower the cost of the board by removing the need for an additional processor.
Arduino Leonardo不同于之前全部的Arduino控制器,他直接使用了ATmega32u4的USB通信功能,取消了USB转UART芯片。这使得Leonardo不仅可以作为一个虚拟的COM端口,还可以作为鼠标或者键盘连接到计算机。
今天用Arduino Leonardo做的东西很花里胡哨,先来看看效果吧:
虚拟键盘演示(badusb)
先在Arduino IDE里新建一个文件,导入资源库:
#include
这是键盘操作的库,只能在Arduino Leonardo系列的开发板上使用,如果在Arduino UNO 上编译的话,会报错
接下来我们需要学习一些虚拟键盘的语句:
开始键盘通讯:
Keyboard.begin();//开始键盘通讯
按键操作:
Keyboard.press();
与按键对应的是释放:
Keyboard.release();
当然还可以输入:
Keyboard.println("");
最后是结束键盘通讯:
Keyboard.end();//结束键盘通讯
有了这些基础,就可以大胆地发挥想象啦!
比如说,我想要的效果是:把开发板插入电脑后,打开命令行,给命令行变颜色
for(int i=0;i<10;i++)
{
Keyboard.println("color 1");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 2");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 3");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 4");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 5");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 6");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 7");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 8");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
Keyboard.println("color 9");
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(100);
}
Keyboard.println("telnet towel.blinkenlights.nl");
除此之外,还有视频里的代码雨:
@echo off
color 0a
setlocal ENABLEDELAYEDEXPANSION
for /l %%i in (0) do (
set "line="
for /l %%j in (1,1,80) do (
set /a Down%%j-=2
set "x=!Down%%j!"
if !x! LSS 0 (
set /a Arrow%%j=!random!%%3
set /a Down%%j=!random!%%15+10 )
set "x=!Arrow%%j!"
if "!x!" == "2" (
set "line=!line!!random:~-1! "
) else (set "line=!line! ")
)
set /p=!line!<nul
)
代码雨的代码要提前写在电脑上,保存为 .bat 文件,运行时直接拖到命令行即可.
还有很多花里胡哨的玩法,大家可以自己琢磨一下,总结一下,加上打开命令行的代码:
#include
void setup() {//初始化
Keyboard.begin();//开始键盘通讯
delay(1000);//延时
Keyboard.press(KEY_LEFT_GUI);//win键
Keyboard.press('r');//r键
Keyboard.release('r');
delay(1000);
Keyboard.release(KEY_LEFT_GUI);
Keyboard.press(KEY_CAPS_LOCK);//利用开大写输小写绕过输入法
Keyboard.release(KEY_CAPS_LOCK);
delay(1000);
Keyboard.println("CMD");
delay(500);
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
/*
插入你想实现的功能
*/
Keyboard.end();//结束键盘通讯
}
void loop()//循环
{}
将代码编译上传后,把Arduino Leonardo开发板插到其他电脑上试试看吧!