ESP32 – micropython蓝牙触控空鼠制作记录 – 哔哩哔哩https://www.bilibili.com/read/mobile?id=14619064&share_token=be27260f-1303-4941-88f5-72006e0df1da
ESP32 – micropython蓝牙触控空鼠制作记录
学习 2021-12-29 13:4398阅读
ESP32 – micropython蓝牙触控空鼠制作记录
一、ESP32-CAM固件下载和烧写
(过程请看视频:蓝牙鼠标制作记录一,micropython固件和HID库配置篇)
micropython固件 :
http://www.micropython.org/download/esp32/
下载:Releases v1.17 (20210902) .bin
打开win控制台:
安装:pip install esptool
擦除:esptool –chip esp32 –port com3 erase_flash
烧写:esptool –chip esp32 –port com3 –baud 460800 write_flash -z 0x1000 esp32-20210902-v1.17.bin
查看支持模块:help(‘modules’)
microPython蓝牙键鼠模拟库:https://github.com/Heerkog/MicroPythonBLEHID
二、GY60JY60姿态传感器简介与测试篇
(过程请看视频:蓝牙鼠标制作记录二,JY60姿态传感器简介与测试篇)
JY60姿态传感器 别称: JY60六轴姿态传感器
简介:
http://www.wit-motion.cn/liuzhoumokuai/81.html
https://dl.wit-motion.com:2103/index.html#/wit-service/productLiterature/details?productId=ca8764fea5af4958836bb4ab4d1a0c38
上位机下载页面:
https://dl.wit-motion.com:2103/index.html#/wit-service/productLiterature/course?literatureId=cd8717842edd4c5aa52be9e4695bfb88
microPython示例代码:
https://blog.csdn.net/Fred_1986/article/details/114415548?spm=1001.2014.3001.5501
三、esp32蓝牙空鼠的micropython代码:
在ESP32写入三个修改过的python代码文件(请仔细看:视频过程):
1、蓝牙鼠标初始化程序:mouse_simple.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# MicroPython Human Interface Device library # <span class="token function">Copyright</span> <span class="token punctuation">(</span><span class="token constant">C</span><span class="token punctuation">)</span> <span class="token number">2021</span> <span class="token constant">H</span><span class="token punctuation">.</span> Groefsema # # This program is free software<span class="token operator">:</span> you can redistribute it and<span class="token operator">/</span>or modify # it under the terms <span class="token keyword">of</span> the <span class="token constant">GNU</span> General Public License <span class="token keyword">as</span> published by # the Free Software Foundation<span class="token punctuation">,</span> either version <span class="token number">3</span> <span class="token keyword">of</span> the License<span class="token punctuation">,</span> or # <span class="token punctuation">(</span>at your option<span class="token punctuation">)</span> any later version<span class="token punctuation">.</span> # # This program is distributed <span class="token keyword">in</span> the hope that it will be useful<span class="token punctuation">,</span> # but <span class="token constant">WITHOUT</span> <span class="token constant">ANY</span> <span class="token constant">WARRANTY</span><span class="token punctuation">;</span> without even the implied warranty <span class="token keyword">of</span> # <span class="token constant">MERCHANTABILITY</span> or <span class="token constant">FITNESS</span> <span class="token constant">FOR</span> <span class="token constant">A</span> <span class="token constant">PARTICULAR</span> <span class="token constant">PURPOSE</span><span class="token punctuation">.</span> See the # <span class="token constant">GNU</span> General Public License <span class="token keyword">for</span> more details<span class="token punctuation">.</span> # # You should have received a copy <span class="token keyword">of</span> the <span class="token constant">GNU</span> General Public License # along <span class="token keyword">with</span> <span class="token keyword">this</span> program<span class="token punctuation">.</span> If not<span class="token punctuation">,</span> see <span class="token operator"><</span>https<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>gnu<span class="token punctuation">.</span>org<span class="token operator">/</span>licenses<span class="token operator">/</span><span class="token operator">></span><span class="token punctuation">.</span> # Implements a <span class="token constant">BLE</span> <span class="token constant">HID</span> mouse <span class="token keyword">import</span> time <span class="token keyword">from</span> machine <span class="token keyword">import</span> SoftSPI<span class="token punctuation">,</span> Pin <span class="token keyword">from</span> hid_services <span class="token keyword">import</span> Mouse <span class="token keyword">class</span> <span class="token class-name">Device</span><span class="token operator">:</span> def <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token operator">:</span> # Define state self<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token number">0</span> self<span class="token punctuation">.</span>y <span class="token operator">=</span> <span class="token number">0</span> self<span class="token punctuation">.</span>prev_x <span class="token operator">=</span> <span class="token number">0</span> self<span class="token punctuation">.</span>prev_y <span class="token operator">=</span> <span class="token number">0</span> # Define buttons # Create our device self<span class="token punctuation">.</span>mouse <span class="token operator">=</span> <span class="token function">Mouse</span><span class="token punctuation">(</span><span class="token string">"Mouse123"</span><span class="token punctuation">)</span> # Set a callback <span class="token keyword">function</span> to catch changes <span class="token keyword">of</span> device state self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">set_state_change_callback</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>mouse_state_callback<span class="token punctuation">)</span> # Start our device self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> # Function that catches device status events def <span class="token function">mouse_state_callback</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">if</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_IDLE</span><span class="token operator">:</span>#设备闲置,代码<span class="token operator">:</span><span class="token number">1</span> <span class="token keyword">return</span> elif self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_ADVERTISING</span><span class="token operator">:</span>#设备广播,代码<span class="token operator">:</span><span class="token number">2</span> <span class="token keyword">return</span> elif self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_CONNECTED</span><span class="token operator">:</span>#设备连接中,代码<span class="token operator">:</span><span class="token number">3</span> <span class="token keyword">return</span> <span class="token keyword">else</span><span class="token operator">:</span> <span class="token keyword">return</span> def <span class="token function">advertise</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token operator">:</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">start_advertising</span><span class="token punctuation">(</span><span class="token punctuation">)</span> def <span class="token function">stop_advertise</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token operator">:</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">stop_advertising</span><span class="token punctuation">(</span><span class="token punctuation">)</span> # Main loop def <span class="token function">start</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token operator">:</span> # If connected <span class="token keyword">set</span> axes and notify # If idle start advertising <span class="token keyword">for</span> <span class="token number">30</span>s or until connected <span class="token keyword">if</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_IDLE</span><span class="token operator">:</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">start_advertising</span><span class="token punctuation">(</span><span class="token punctuation">)</span> i <span class="token operator">=</span> <span class="token number">10</span> <span class="token keyword">while</span> i <span class="token operator">></span> <span class="token number">0</span> and self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_ADVERTISING</span><span class="token operator">:</span> time<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> i <span class="token operator">-=</span> <span class="token number">1</span> <span class="token keyword">if</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_ADVERTISING</span><span class="token operator">:</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">stop_advertising</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">if</span> self<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">get_state</span><span class="token punctuation">(</span><span class="token punctuation">)</span> is Mouse<span class="token punctuation">.</span><span class="token constant">DEVICE_CONNECTED</span><span class="token operator">:</span> time<span class="token punctuation">.</span><span class="token function">sleep_ms</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token operator">:</span> time<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token operator">:</span> d <span class="token operator">=</span> <span class="token function">Device</span><span class="token punctuation">(</span><span class="token punctuation">)</span> d<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> |
2、主程序:mouse.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
#<span class="token number">2022</span><span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span><span class="token number">5</span>版,使用中 <span class="token keyword">from</span> machine <span class="token keyword">import</span> TouchPad<span class="token punctuation">,</span><span class="token constant">UART</span><span class="token punctuation">,</span>Pin <span class="token keyword">import</span> utime <span class="token keyword">from</span> mouse_simple <span class="token keyword">import</span> Device ACCData<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">0.0</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token number">8</span> GYROData<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">0.0</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token number">8</span> AngleData<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">0.0</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token number">8</span> FrameState <span class="token operator">=</span> <span class="token number">0</span> #通过<span class="token number">0</span>x后面的值判断属于哪一种情况 Bytenum <span class="token operator">=</span> <span class="token number">0</span> #读取到这一段的第几位 CheckSum <span class="token operator">=</span> <span class="token number">0</span> #求和校验位 a <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">)</span> w <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">)</span> Angle <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">,</span><span class="token number">0.0</span><span class="token punctuation">)</span> def <span class="token function">DueData</span><span class="token punctuation">(</span>inputdata<span class="token punctuation">)</span><span class="token operator">:</span> #新增的核心程序,对读取的数据进行划分,各自读到对应的数组里 global FrameState #在局部修改全局变量,要进行global的定义 global Bytenum global CheckSum global a global w global Angle <span class="token keyword">for</span> data <span class="token keyword">in</span> inputdata<span class="token operator">:</span> #在输入的数据进行遍历 #Python2软件版本这里需要插入 data <span class="token operator">=</span> <span class="token function">ord</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">**</span><span class="token operator">*</span> <span class="token keyword">if</span> FrameState<span class="token operator">==</span><span class="token number">0</span><span class="token operator">:</span> #当未确定状态的时候,进入以下判断 <span class="token keyword">if</span> data<span class="token operator">==</span><span class="token number">0x55</span> and Bytenum<span class="token operator">==</span><span class="token number">0</span><span class="token operator">:</span> #<span class="token number">0x55</span>位于第一位时候,开始读取数据,增大bytenum CheckSum<span class="token operator">=</span>data Bytenum<span class="token operator">=</span><span class="token number">1</span> <span class="token keyword">continue</span> elif data<span class="token operator">==</span><span class="token number">0x51</span> and Bytenum<span class="token operator">==</span><span class="token number">1</span><span class="token operator">:</span>#在byte不为<span class="token number">0</span> 且 识别到 <span class="token number">0x51</span> 的时候,改变frame CheckSum<span class="token operator">+=</span>data FrameState<span class="token operator">=</span><span class="token number">1</span> Bytenum<span class="token operator">=</span><span class="token number">2</span> elif data<span class="token operator">==</span><span class="token number">0x52</span> and Bytenum<span class="token operator">==</span><span class="token number">1</span><span class="token operator">:</span> #同理 CheckSum<span class="token operator">+=</span>data FrameState<span class="token operator">=</span><span class="token number">2</span> Bytenum<span class="token operator">=</span><span class="token number">2</span> elif data<span class="token operator">==</span><span class="token number">0x53</span> and Bytenum<span class="token operator">==</span><span class="token number">1</span><span class="token operator">:</span> CheckSum<span class="token operator">+=</span>data FrameState<span class="token operator">=</span><span class="token number">3</span> Bytenum<span class="token operator">=</span><span class="token number">2</span> elif FrameState<span class="token operator">==</span><span class="token number">1</span><span class="token operator">:</span> # acc #已确定数据代表加速度 <span class="token keyword">if</span> Bytenum<span class="token operator"><</span><span class="token number">10</span><span class="token operator">:</span> # 读取<span class="token number">8</span>个数据 ACCData<span class="token punctuation">[</span>Bytenum<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">=</span>data # 从<span class="token number">0</span>开始 CheckSum<span class="token operator">+=</span>data Bytenum<span class="token operator">+=</span><span class="token number">1</span> <span class="token keyword">else</span><span class="token operator">:</span> <span class="token keyword">if</span> data <span class="token operator">==</span> <span class="token punctuation">(</span>CheckSum<span class="token operator">&</span><span class="token number">0xff</span><span class="token punctuation">)</span><span class="token operator">:</span> #假如校验位正确 a <span class="token operator">=</span> <span class="token function">get_acc</span><span class="token punctuation">(</span>ACCData<span class="token punctuation">)</span> CheckSum<span class="token operator">=</span><span class="token number">0</span> #各数据归零,进行新的循环判断 Bytenum<span class="token operator">=</span><span class="token number">0</span> FrameState<span class="token operator">=</span><span class="token number">0</span> elif FrameState<span class="token operator">==</span><span class="token number">2</span><span class="token operator">:</span> # gyro <span class="token keyword">if</span> Bytenum<span class="token operator"><</span><span class="token number">10</span><span class="token operator">:</span> GYROData<span class="token punctuation">[</span>Bytenum<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">=</span>data CheckSum<span class="token operator">+=</span>data Bytenum<span class="token operator">+=</span><span class="token number">1</span> <span class="token keyword">else</span><span class="token operator">:</span> <span class="token keyword">if</span> data <span class="token operator">==</span> <span class="token punctuation">(</span>CheckSum<span class="token operator">&</span><span class="token number">0xff</span><span class="token punctuation">)</span><span class="token operator">:</span> w <span class="token operator">=</span> <span class="token function">get_gyro</span><span class="token punctuation">(</span>GYROData<span class="token punctuation">)</span> CheckSum<span class="token operator">=</span><span class="token number">0</span> Bytenum<span class="token operator">=</span><span class="token number">0</span> FrameState<span class="token operator">=</span><span class="token number">0</span> elif FrameState<span class="token operator">==</span><span class="token number">3</span><span class="token operator">:</span> # angle <span class="token keyword">if</span> Bytenum<span class="token operator"><</span><span class="token number">10</span><span class="token operator">:</span> AngleData<span class="token punctuation">[</span>Bytenum<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">=</span>data CheckSum<span class="token operator">+=</span>data Bytenum<span class="token operator">+=</span><span class="token number">1</span> <span class="token keyword">else</span><span class="token operator">:</span> <span class="token keyword">if</span> data <span class="token operator">==</span> <span class="token punctuation">(</span>CheckSum<span class="token operator">&</span><span class="token number">0xff</span><span class="token punctuation">)</span><span class="token operator">:</span> Angle <span class="token operator">=</span> <span class="token function">get_angle</span><span class="token punctuation">(</span>AngleData<span class="token punctuation">)</span> d <span class="token operator">=</span> a<span class="token operator">+</span>w<span class="token operator">+</span>Angle # <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"a(g):%10.3f %10.3f %10.3f w(deg/s):%10.3f %10.3f %10.3f Angle(deg):%10.3f %10.3f %10.3f"</span><span class="token operator">%</span>d<span class="token punctuation">)</span> CheckSum<span class="token operator">=</span><span class="token number">0</span> Bytenum<span class="token operator">=</span><span class="token number">0</span> FrameState<span class="token operator">=</span><span class="token number">0</span> def <span class="token function">get_acc</span><span class="token punctuation">(</span>datahex<span class="token punctuation">)</span><span class="token operator">:</span> axl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> axh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> ayl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> ayh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> azl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> azh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> k_acc <span class="token operator">=</span> <span class="token number">16.0</span> acc_x <span class="token operator">=</span> <span class="token punctuation">(</span>axh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> axl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_acc acc_y <span class="token operator">=</span> <span class="token punctuation">(</span>ayh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> ayl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_acc acc_z <span class="token operator">=</span> <span class="token punctuation">(</span>azh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> azl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_acc <span class="token keyword">if</span> acc_x <span class="token operator">>=</span> k_acc<span class="token operator">:</span> acc_x <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_acc <span class="token keyword">if</span> acc_y <span class="token operator">>=</span> k_acc<span class="token operator">:</span> acc_y <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_acc <span class="token keyword">if</span> acc_z <span class="token operator">>=</span> k_acc<span class="token operator">:</span> acc_z<span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_acc <span class="token keyword">return</span> acc_x<span class="token punctuation">,</span>acc_y<span class="token punctuation">,</span>acc_z def <span class="token function">get_gyro</span><span class="token punctuation">(</span>datahex<span class="token punctuation">)</span><span class="token operator">:</span> wxl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> wxh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> wyl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> wyh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> wzl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> wzh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> k_gyro <span class="token operator">=</span> <span class="token number">2000.0</span> gyro_x <span class="token operator">=</span> <span class="token punctuation">(</span>wxh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> wxl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_gyro gyro_y <span class="token operator">=</span> <span class="token punctuation">(</span>wyh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> wyl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_gyro gyro_z <span class="token operator">=</span> <span class="token punctuation">(</span>wzh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> wzl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_gyro <span class="token keyword">if</span> gyro_x <span class="token operator">>=</span> k_gyro<span class="token operator">:</span> gyro_x <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_gyro <span class="token keyword">if</span> gyro_y <span class="token operator">>=</span> k_gyro<span class="token operator">:</span> gyro_y <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_gyro <span class="token keyword">if</span> gyro_z <span class="token operator">>=</span>k_gyro<span class="token operator">:</span> gyro_z<span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_gyro <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">abs</span><span class="token punctuation">(</span>gyro_z<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">4</span> or <span class="token function">abs</span><span class="token punctuation">(</span>gyro_y<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">4</span> <span class="token punctuation">)</span><span class="token operator">:</span><span class="token function">moveMouse</span><span class="token punctuation">(</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token operator">-</span>gyro_z<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">round</span><span class="token punctuation">(</span>gyro_y<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> gyro_x<span class="token punctuation">,</span>gyro_y<span class="token punctuation">,</span>gyro_z def <span class="token function">get_angle</span><span class="token punctuation">(</span>datahex<span class="token punctuation">)</span><span class="token operator">:</span> rxl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> rxh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> ryl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> ryh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> rzl <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> rzh <span class="token operator">=</span> datahex<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> k_angle <span class="token operator">=</span> <span class="token number">180.0</span> angle_x <span class="token operator">=</span> <span class="token punctuation">(</span>rxh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> rxl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_angle angle_y <span class="token operator">=</span> <span class="token punctuation">(</span>ryh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> ryl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_angle angle_z <span class="token operator">=</span> <span class="token punctuation">(</span>rzh <span class="token operator"><<</span> <span class="token number">8</span> <span class="token operator">|</span> rzl<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">32768.0</span> <span class="token operator">*</span> k_angle <span class="token keyword">if</span> angle_x <span class="token operator">>=</span> k_angle<span class="token operator">:</span> angle_x <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_angle <span class="token keyword">if</span> angle_y <span class="token operator">>=</span> k_angle<span class="token operator">:</span> angle_y <span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_angle <span class="token keyword">if</span> angle_z <span class="token operator">>=</span>k_angle<span class="token operator">:</span> angle_z<span class="token operator">-=</span> <span class="token number">2</span> <span class="token operator">*</span> k_angle <span class="token keyword">return</span> angle_x<span class="token punctuation">,</span>angle_y<span class="token punctuation">,</span>angle_z def <span class="token function">moveMouse</span><span class="token punctuation">(</span>xx<span class="token punctuation">,</span>yy<span class="token punctuation">)</span><span class="token operator">:</span>#移动相对位置 d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">set_axes</span><span class="token punctuation">(</span>xx<span class="token punctuation">,</span>yy<span class="token punctuation">)</span> d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">notify_hid_report</span><span class="token punctuation">(</span><span class="token punctuation">)</span> def <span class="token function">mouseClickScan</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> global prvflag flag <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">try</span><span class="token operator">:</span> mouse_right <span class="token operator">=</span> touch_right<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> mouse_left <span class="token operator">=</span> touch_left<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">#print</span><span class="token punctuation">(</span>mouse_right<span class="token punctuation">,</span>mouse_left<span class="token punctuation">)</span>#串口输出这两个触摸键的值,用于观察已设置按键状态 #flag<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">if</span> touch_right<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">390</span> <span class="token keyword">else</span> <span class="token number">0</span> flag<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">if</span> touch_right<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">360</span> <span class="token keyword">else</span> <span class="token number">0</span>#若读取的值大于阀值,则为没触摸 flag<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">if</span> touch_left<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">380</span> <span class="token keyword">else</span> <span class="token number">0</span>#若读取的值大于阀值,则为没触摸 # <span class="token keyword">if</span> touch_right<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">330</span><span class="token operator">:</span>#若读取的值大于阀值,则为没触摸 # flag<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span> # <span class="token keyword">else</span><span class="token operator">:</span> # flag<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span> # <span class="token keyword">if</span> touch_left<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">380</span><span class="token operator">:</span>#若读取的值大于阀值,则为没触摸 # flag<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1</span> # <span class="token keyword">else</span><span class="token operator">:</span> # flag<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">if</span> flag <span class="token operator">!=</span> prvflag<span class="token operator">:</span> <span class="token keyword">if</span> flag<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">:</span> d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">set_buttons</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>#鼠标左键按下 elif flag<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">:</span> d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">set_buttons</span><span class="token punctuation">(</span>b2<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> #鼠标右键按下 <span class="token keyword">else</span><span class="token operator">:</span> d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">set_buttons</span><span class="token punctuation">(</span><span class="token punctuation">)</span>#鼠标键放开 d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span><span class="token function">notify_hid_report</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token function">print</span><span class="token punctuation">(</span>flag<span class="token punctuation">,</span>d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span>button1<span class="token punctuation">,</span>d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span>button2<span class="token punctuation">,</span>d<span class="token punctuation">.</span>mouse<span class="token punctuation">.</span>button3<span class="token punctuation">)</span> prvflag <span class="token operator">=</span> flag<span class="token punctuation">.</span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> except<span class="token operator">:</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"error"</span><span class="token punctuation">)</span> prvflag <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> d <span class="token operator">=</span> <span class="token function">Device</span><span class="token punctuation">(</span><span class="token punctuation">)</span>#引用鼠标蓝牙驱动 d<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> # 初始化一个<span class="token constant">UART</span>对象 uart <span class="token operator">=</span> <span class="token constant">UART</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> baudrate<span class="token operator">=</span><span class="token number">9600</span><span class="token punctuation">,</span> rx<span class="token operator">=</span><span class="token number">13</span><span class="token punctuation">,</span>tx<span class="token operator">=</span><span class="token number">15</span><span class="token punctuation">,</span>timeout<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span>#重新定义一个串口用于接收<span class="token constant">JY60</span>的数据,<span class="token number">13</span>、<span class="token number">15</span>引脚 touch_right <span class="token operator">=</span> <span class="token function">TouchPad</span><span class="token punctuation">(</span><span class="token function">Pin</span><span class="token punctuation">(</span><span class="token number">14</span><span class="token punctuation">)</span><span class="token punctuation">)</span> ##鼠标左键<span class="token punctuation">,</span>定义<span class="token number">14</span>脚为触摸脚,esp32<span class="token operator">-</span>cam电容触摸输入<span class="token constant">IO</span>口只有两个能用?<span class="token operator">:</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">14</span> touch_left <span class="token operator">=</span> <span class="token function">TouchPad</span><span class="token punctuation">(</span><span class="token function">Pin</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">)</span> ##鼠标右键<span class="token punctuation">,</span>定义<span class="token number">12</span>脚为触摸脚,因为esp32<span class="token operator">-</span>cam的板增加了很多外设,所以就剩下<span class="token number">12</span>和<span class="token number">4</span>引脚能用触摸按键,但可以改造增加<span class="token constant">IO</span>口 #就像我视频里用脚控制的蓝牙键盘一下,也是用esp32<span class="token operator">-</span>cam改造增加的 <span class="token keyword">while</span> True<span class="token operator">:</span> <span class="token function">mouseClickScan</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">if</span> uart<span class="token punctuation">.</span><span class="token function">any</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> datahex <span class="token operator">=</span> uart<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token number">33</span><span class="token punctuation">)</span> # 将收到的信息打印在终端 # <span class="token function">print</span><span class="token punctuation">(</span>datahex<span class="token punctuation">)</span> <span class="token function">DueData</span><span class="token punctuation">(</span>datahex<span class="token punctuation">)</span> |
3、引导文件:boot.py
1 |
<span class="token function">exec</span><span class="token punctuation">(</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">'./mouse.py'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">globals</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> |
写入后重启ESP32,用上位机蓝牙搜索添加即可正常控制了。
在此有个问题,初次连接蓝牙成功后,此时可以使用,但是一旦重启ESP32就需要在PC或上位机端删除该蓝牙设备进行重连才可以正常运行,不论是micropython还是Arduino的程序都是如此,可能是我对蓝牙连接的过程不太明白,不知道有否大佬可以解答一下,谢谢了!