找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8802|回复: 0
打印 上一主题 下一主题
收起左侧

介绍一种特别的Arduino Uno模拟鼠标的方法

[复制链接]
跳转到指定楼层
楼主
ID:328014 发表于 2018-9-26 04:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Arduino模拟鼠标目前有三种方法(出自Arduin0中文社区):
1.  外部加电阻 USB母头等元件,然后烧录模拟程序,用 328P作为处理器。这种方法的缺点是:原件多,不容易调试,占用板载资源多,做出来之后基本上不能完成什么功能了(因为Usb低速设备传输要求 1.5Mb/s,而328P最高只有16MHz),有兴趣的朋友可以看一下之前我做的一个锁屏的装置【参考1】;

2.直接使用Leonardo 这样主控是ATmega32u4【参考2】  的板子。这种方法的好处是:Arduino 原生库支持,资料比较多,调试方便。个人推荐初学者如果有鼠标键盘的需要可以玩这个;

3.原版的Arduino Uno 上面使用的串口芯片是 16u2,可以给这个芯片刷写上一个特殊的Firmware,它和PC端用USB鼠标或者键盘通讯,然后和 328P 使用串口通讯。
本文介绍的就是第三种方法。

在玩第三种方法的时候,你需要特别准备一个烧写器。我用的是 USBTINY 这款。

本次实验的目标是将uno模拟成鼠标。参考的资料来自下面的页面:
hunt点net点nz/users/darran/weblog/cca39/Arduino_UNO_Mouse_HID.html
我刷写的工具是 AvrDudess 2.4,用法很简单,接线之后(建议选购下载器的时候直接选带完整线的,否则每次接线也是很麻烦的事情),按下 Detect按钮,软件需要检查到正确芯片的类型,比如,我的转接芯片是 16u2。如果无法侦测,那么请检查连线。如果折腾了很久都不行,那么请联系卖家所要驱动和刷写工具。刚开始的时候我就在这里折腾了很长时间。


这里是烧写Uno USB转串口芯片的位置

因为串口芯片被刷掉了,所以接下来也必须使用刷写器写入编译好的Arduino 程序。

输入程序,确定编译无误

  1. /* Arduino USB Mouse HID demo */

  2. /* Author: Darran Hunt
  3. * Release into the public domain.
  4. */

  5. struct {
  6.     uint8_t buttons;
  7.     int8_t x;
  8.     int8_t y;
  9.     int8_t wheel;        /* Not yet implemented */
  10. } mouseReport;

  11. uint8_t nullReport[4] = { 0, 0, 0, 0 };

  12. void setup();
  13. void loop();

  14. void setup()
  15. {
  16.     Serial.begin(9600);
  17.     delay(200);
  18. }

  19. /* Move the mouse in a clockwise square every 5 seconds */
  20. void loop()
  21. {
  22.     int ind;
  23.     delay(5000);

  24.     mouseReport.buttons = 0;
  25.     mouseReport.x = 0;
  26.     mouseReport.y = 0;
  27.     mouseReport.wheel = 0;

  28.     mouseReport.x = -2;
  29.     for (ind=0; ind<20; ind++) {
  30.         Serial.write((uint8_t *)&mouseReport, 4);
  31.         Serial.write((uint8_t *)&nullReport, 4);
  32.     }

  33.     mouseReport.x = 0;
  34.     mouseReport.y = -2;
  35.     for (ind=0; ind<20; ind++) {
  36.         Serial.write((uint8_t *)&mouseReport, 4);
  37.         Serial.write((uint8_t *)&nullReport, 4);
  38.     }

  39.     mouseReport.x = 2;
  40.     mouseReport.y = 0;
  41.     for (ind=0; ind<20; ind++) {
  42.         Serial.write((uint8_t *)&mouseReport, 4);
  43.         Serial.write((uint8_t *)&nullReport, 4);
  44.     }

  45.     mouseReport.x = 0;
  46.     mouseReport.y = 2;
  47.     for (ind=0; ind<20; ind++) {
  48.         Serial.write((uint8_t *)&mouseReport, 4);
  49.         Serial.write((uint8_t *)&nullReport, 4);
  50.     }
  51. }
复制代码
用 IDE 上传内容,需要一些设置,指定刷写工具


然后使用 File->Upload Using Programmer 来进行上传

上传成功:

成功之后,用Arduino Usb口连接电脑,你的鼠标每隔一段会自动旋转一圈,同时在设备管理器中会出现一个鼠标设备:

这个和16u2 Firmware source code(Descriptors.c)中定义是相同的

        .VendorID               = 0x03EB,
        .ProductID              = 0x2041,
        .ReleaseNumber          = 0x0000

从这里出发,可以让 Uno 玩出很多新花样,后面我会慢慢介绍。

参考:
1.  www点lab-z点com/20140101/ 用 Arduino 打造一个自动锁屏装置
2.  www点arduino点cn/thread-1205-1-1.html Arduino Leonardo 中文介绍




















分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表