Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code.py found on "Adafruit CircuitPython 6.2.0 on 2021-04-05; Adafruit ItsyBitsy nRF52840 Express with nRF52840" date 2020/04/14
###
# Dual BLE mouse jiggling:
# Advertise a first mouse as "CircuitPython HID1"
# Once we have a first connection
# Advertise a second mouse as "CircuitPython HID2"
#
# Once the first mouse is connected the jiggling start.
# Mouse1 should do Left-Right movement
# Mouse2 (once connected) should do Up-Down movement
#
# BUG?: Mouse2 does square move, so it gets the move for Mouse1.
###
import adafruit_ble
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_ble.services.standard.device_info import DeviceInfoService
from adafruit_hid.mouse import Mouse
import time
import board
hid1 = HIDService()
device_info = DeviceInfoService(software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries")
advertisement1 = ProvideServicesAdvertisement(hid1)
advertisement1.appearance = 961
scan_response1 = Advertisement()
scan_response1.complete_name = "CircuitPython HID1"
ble = adafruit_ble.BLERadio()
if not ble.connected:
print("advertising1")
ble.start_advertising(advertisement1, scan_response1)
else:
print("already connected")
print(ble.connections)
# This is a BLE mouse
mouse1 = Mouse(hid1.devices)
hid2 = HIDService()
advertisement2 = ProvideServicesAdvertisement(hid2)
advertisement2.appearance = 961
scan_response2 = Advertisement()
scan_response2.complete_name = "CircuitPython HID2"
# If I do the following before I get a connection on the first advertisement...
# ble.start_advertising(advertisement2, scan_response2)
# It produce the following error...
# _bleio.BluetoothError: Already advertising.
# This is a trick to wait for a first connection before making a new advertisement
while len(ble.connections) < 1:
pass
print("advertising2")
ble.start_advertising(advertisement2, scan_response2)
# This is a BLE mouse
mouse2 = Mouse(hid2.devices)
m=10
s=1
# This works even if mouse2 is not connected yet
while True:
while not ble.connected:
pass
print("Start shaking the mouse for you.")
while ble.connected:
time.sleep(s); mouse1.move(x=m);
time.sleep(s); mouse2.move(y=m);
time.sleep(s); mouse1.move(x=-m);
time.sleep(s); mouse2.move(y=-m);
# This is far from perfect, we only advertise 1 where the second is not there too.
ble.start_advertising(advertisement1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment