{"id":3259,"date":"2020-04-11T03:01:41","date_gmt":"2020-04-10T18:01:41","guid":{"rendered":"https:\/\/programresource.net\/?p=3259"},"modified":"2020-04-11T03:01:44","modified_gmt":"2020-04-10T18:01:44","slug":"m5stack-and-mpu9250-as-bluetooth-gyro-mouse","status":"publish","type":"post","link":"https:\/\/programresource.net\/en\/2020\/04\/11\/3259.html","title":{"rendered":"M5Stack and MPU9250 as Bluetooth gyro mouse"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP-1024x577.jpg\" alt=\"\" class=\"wp-image-3254\" srcset=\"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP-1024x577.jpg 1024w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP-300x169.jpg 300w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP-768x433.jpg 768w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP.jpg 1277w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Thanks to author of library, Bluetooth mouse library is available for ESP32 devices. I&#8217;ll be making Bluetooth mouse with M5Stack this time.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/T-vK\/ESP32-BLE-Mouse\">https:\/\/github.com\/T-vK\/ESP32-BLE-Mouse<\/a><\/p>\n\n\n\n<p>Download and install library from above site.<\/p>\n\n\n\n<p>There are many ideas, such us using joystick, to control mouse. This time, I&#8217;ll use MPU9250 gyro sensor. Connect sensor with I2C wiring.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto.jpg\"><img decoding=\"async\" src=\"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto-1024x577.jpg\" alt=\"\" class=\"wp-image-3256\" srcset=\"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto-1024x577.jpg 1024w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto-300x169.jpg 300w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto-768x433.jpg 768w, https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013913_vHDR_Auto.jpg 1277w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>There are M5Stack models which has gyro sensor inside. For those M5Stack models, only thing you need is M5Stack itself only.<\/p>\n\n\n\n<p>Tweet from M5Stack official below shows good table of difference between models. Check MEMS row.<\/p>\n\n\n\n<figure class=\"wp-block-embed-twitter wp-block-embed is-type-rich is-provider-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"500\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">See the difference of M5Stack <a href=\"https:\/\/t.co\/DuxmeXGGK7\">pic.twitter.com\/DuxmeXGGK7<\/a><\/p>&mdash; M5Stack (@M5Stack) <a href=\"https:\/\/twitter.com\/M5Stack\/status\/1095266372702089216?ref_src=twsrc%5Etfw\">February 12, 2019<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n\n\n\n<p>Sketch is as follows. M5Stack is used upside down. Button C for left click, A for right click, hold down B and tilt for scroll.<\/p>\n\n\n\n<p>Gyro sensor are also simplified with library; sketch is kept neat and simple.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#define M5STACK_MPU9250\n#include &lt;M5Stack.h&gt;\n#include &lt;BleMouse.h&gt;\n\n#define GYRO_CHECK_INTERVAL\t\t10\n#define GYRO_CHECK_INTERVAL_WHEEL\t30\n\nBleMouse bleMouse(&quot;M5 GyroMouse&quot;);\nbool blestate = false;\nbool wheelmode = false;\nint gyrointerval = GYRO_CHECK_INTERVAL;\nsigned char mousex, mousey, mwheel;\nint ipitch, iroll, iyaw;\nfloat pitch, roll, yaw;\nchar chbuff&#91;32];\nunsigned long current_millis, last_millis = 0;\n\nvoid showstate(char *txt) {\n\tM5.Lcd.setCursor(0, 120);\n\tM5.Lcd.fillRect(0, 120, 320, 20, BLACK);\n\tM5.Lcd.printf(txt);\n}\n\nvoid showgyro(char *txt) {\n\tM5.Lcd.setCursor(0, 140);\n\tM5.Lcd.printf(txt);\n}\n\nvoid setup() {\n\tM5.begin();\n\tM5.Power.begin();\n\tbleMouse.begin();\n\n\tM5.Lcd.clear(BLACK);\n\tM5.Lcd.setRotation(3);\n\tM5.Lcd.setTextSize(2);\n\tM5.Lcd.println(&quot;Gyro Mouse&quot;);\n\tM5.Lcd.println(&quot;C: Left&quot;);\n\tM5.Lcd.println(&quot;B: Wheel (Hold)&quot;);\n\tM5.Lcd.println(&quot;A: Right&quot;);\n\tshowstate(&quot;Disconnected&quot;);\n\n\tM5.IMU.Init();\n}\n\nvoid loop() {\n\tM5.update();\n\n\tcurrent_millis = millis();\n\n\tif (bleMouse.isConnected()) {\n\t\tif (!blestate) {\n\t\t\tblestate = true;\n\t\t\tshowstate(&quot;Connected&quot;);\n\t\t}\n\n\t\tif (current_millis - last_millis &gt; gyrointerval) {\n\t\t\tM5.IMU.getAhrsData(&amp;pitch, &amp;roll, &amp;yaw);\n\t\t\tsprintf(chbuff, &quot;%.1f %.1f %.1f           &quot;, pitch, roll, yaw);\n\t\t\tipitch = (int)pitch;\n\t\t\tiroll = (int)roll;\n\t\t\tiyaw = (int)yaw;\n\t\t\tshowgyro(chbuff);\n\t\t\tif (wheelmode) {\n\t\t\t\tmousex = 0;\n\t\t\t\tmousey = 0;\n\t\t\t\tif (abs(ipitch) &gt;= 10)\n\t\t\t\t\tmwheel = (signed char)(ipitch \/ 10);\n\t\t\t} else {\n\t\t\t\tmousex = (signed char)((iroll &lt; 0 ? min(0, iroll + 10) : max(0, iroll - 10)) \/ 10);\n\t\t\t\tmousey = -1 * (signed char)((ipitch &lt; 0 ? min(0, ipitch + 10) : max(0, ipitch - 10)) \/ 10);\n\t\t\t\tmwheel = 0;\n\t\t\t}\n\t\t\tbleMouse.move(mousex, mousey, mwheel);\n\t\t\tlast_millis = current_millis;\n\t\t}\n\n\t\tif (M5.BtnA.wasPressed()) {\n\t\t\tbleMouse.press(MOUSE_RIGHT);\n\t\t} else if (M5.BtnA.wasReleased()) {\n\t\t\tbleMouse.release(MOUSE_RIGHT);\n\t\t} else if (M5.BtnB.wasPressed()) {\n\t\t\twheelmode = true;\n\t\t\tgyrointerval = GYRO_CHECK_INTERVAL_WHEEL;\n\t\t} else if (M5.BtnB.wasReleased()) {\n\t\t\twheelmode = false;\n\t\t\tgyrointerval = GYRO_CHECK_INTERVAL;\n\t\t} else if (M5.BtnC.wasPressed()) {\n\t\t\tbleMouse.press(MOUSE_LEFT);\n\t\t} else if (M5.BtnC.wasReleased()) {\n\t\t\tbleMouse.release(MOUSE_LEFT);\n\t\t}\n\t} else {\n\t\tif (blestate) {\n\t\t\tblestate = false;\n\t\t\tshowstate(&quot;Disconnected&quot;);\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>Uploaded youtube video showing M5Stack paired with PC. Somehow, I was unable to pair with GPD MicroPC.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<span class=\"embed-youtube\" style=\"text-align:center; display: block;\"><iframe loading=\"lazy\" class=\"youtube-player\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/d8MRYMekMUM?version=3&#038;rel=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;fs=1&#038;hl=en-US&#038;autohide=2&#038;wmode=transparent\" allowfullscreen=\"true\" style=\"border:0;\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-presentation\"><\/iframe><\/span>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Thanks to author of library, Bluetooth mouse library is available for ESP32 devices. I&#8217;ll be making Bluetooth mouse with M5Stack this time. https:\/\/github.com\/T-vK\/ESP32-BLE-Mouse Download and install library from above site. There are many ideas, such us using joystick, to control mouse. This time, I&#8217;ll use MPU9250 gyro sensor. Connect sensor with I2C wiring. There are M5Stack models which has gyro sensor inside. For those M5Stack models, only thing you need is M5Stack itself only. Tweet from M5Stack official below shows good table of difference between models. Check MEMS row. Sketch is as follows. M5Stack is used upside down. Button C for left click, A for right click, hold down B &#8230;<\/p>\n","protected":false},"author":2,"featured_media":3255,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[866],"tags":[982,989,883,685,988],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/programresource.net\/images\/2020\/04\/P_20200411_013805_vHDR_Auto_HP.jpg","jetpack_shortlink":"https:\/\/wp.me\/p3pJyQ-Qz","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3259"}],"collection":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/comments?post=3259"}],"version-history":[{"count":2,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3259\/revisions"}],"predecessor-version":[{"id":3263,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/posts\/3259\/revisions\/3263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media\/3255"}],"wp:attachment":[{"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/media?parent=3259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/categories?post=3259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programresource.net\/en\/wp-json\/wp\/v2\/tags?post=3259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}