|
| 1 | +import time |
| 2 | +import board |
| 3 | +import digitalio |
| 4 | +import adafruit_lsm6ds.lsm6ds33 |
| 5 | +import adafruit_apds9960.apds9960 |
| 6 | +from adafruit_hid.mouse import Mouse |
| 7 | + |
| 8 | +import adafruit_ble |
| 9 | +from adafruit_ble.advertising import Advertisement |
| 10 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 11 | +from adafruit_ble.services.standard.hid import HIDService |
| 12 | +from adafruit_ble.services.standard.device_info import DeviceInfoService |
| 13 | + |
| 14 | +# setup I2C |
| 15 | +i2c = board.I2C() |
| 16 | + |
| 17 | +# setup accelerometer |
| 18 | +lsm6ds33 = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c) |
| 19 | +# setup proximity sensor |
| 20 | +apds9960 = adafruit_apds9960.apds9960.APDS9960(i2c) |
| 21 | + |
| 22 | +# enable proximity sensor |
| 23 | +apds9960.enable_proximity = True |
| 24 | + |
| 25 | +# x and y axis setup |
| 26 | +x_axis = 0 |
| 27 | +y_axis = 0 |
| 28 | + |
| 29 | +# setup for onboard button |
| 30 | +click = digitalio.DigitalInOut(board.SWITCH) |
| 31 | +click.direction = digitalio.Direction.INPUT |
| 32 | +click.pull = digitalio.Pull.UP |
| 33 | + |
| 34 | +# rounding algorhythm used for mouse movement |
| 35 | +# as used in the HID mouse CircuitPython example |
| 36 | +mouse_min = -9 |
| 37 | +mouse_max = 9 |
| 38 | +step = (mouse_max - mouse_min) / 20.0 |
| 39 | + |
| 40 | +def steps(axis): |
| 41 | + return round((axis - mouse_min) / step) |
| 42 | + |
| 43 | +# time.monotonic() variable |
| 44 | +clock = 0 |
| 45 | + |
| 46 | +# variable for distance for proximity scrolling |
| 47 | +distance = 245 |
| 48 | + |
| 49 | +# setup for HID and BLE |
| 50 | +hid = HIDService() |
| 51 | + |
| 52 | +device_info = DeviceInfoService(software_revision=adafruit_ble.__version__, |
| 53 | + manufacturer="Adafruit Industries") |
| 54 | +advertisement = ProvideServicesAdvertisement(hid) |
| 55 | +advertisement.appearance = 961 |
| 56 | +scan_response = Advertisement() |
| 57 | +scan_response.complete_name = "CircuitPython HID" |
| 58 | + |
| 59 | +ble = adafruit_ble.BLERadio() |
| 60 | + |
| 61 | +if not ble.connected: |
| 62 | + print("advertising") |
| 63 | + ble.start_advertising(advertisement, scan_response) |
| 64 | +else: |
| 65 | + print("already connected") |
| 66 | + print(ble.connections) |
| 67 | + |
| 68 | +# setup for mouse |
| 69 | +mouse = Mouse(hid.devices) |
| 70 | + |
| 71 | +while True: |
| 72 | + while not ble.connected: |
| 73 | + pass |
| 74 | + while ble.connected: |
| 75 | + # sets x and y values for accelerometer x and y values |
| 76 | + x = lsm6ds33.acceleration[1] |
| 77 | + y = lsm6ds33.acceleration[0] |
| 78 | + |
| 79 | + # if onboard button is pressed, sends left mouse click |
| 80 | + if click.value is False: |
| 81 | + mouse.click(Mouse.LEFT_BUTTON) |
| 82 | + time.sleep(0.2) |
| 83 | + # debugging print for x and y values |
| 84 | + # time.monotonic() is used so that the |
| 85 | + # code is not delayed with time.sleep |
| 86 | + if (clock + 2) < time.monotonic(): |
| 87 | + print("x", steps(x)) |
| 88 | + print("y", steps(y)) |
| 89 | + clock = time.monotonic() |
| 90 | + # mouse movement left and right |
| 91 | + if steps(x) > 11.0: |
| 92 | + mouse.move(x=1) |
| 93 | + if steps(x) < 9.0: |
| 94 | + mouse.move(x=-1) |
| 95 | + |
| 96 | + if steps(x) > 19.0: |
| 97 | + mouse.move(x=8) |
| 98 | + if steps(x) < 1.0: |
| 99 | + mouse.move(x=-8) |
| 100 | + # mouse movement up and down |
| 101 | + # and mouse scrolling using |
| 102 | + # proximity sensor |
| 103 | + if steps(y) > 11.0: |
| 104 | + if apds9960.proximity > distance: |
| 105 | + # scroll down |
| 106 | + mouse.move(wheel=1) |
| 107 | + time.sleep(0.1) |
| 108 | + else: |
| 109 | + # move down |
| 110 | + mouse.move(y=-1) |
| 111 | + if steps(y) < 9.0: |
| 112 | + if apds9960.proximity > distance: |
| 113 | + # scroll up |
| 114 | + mouse.move(wheel=-1) |
| 115 | + time.sleep(0.1) |
| 116 | + else: |
| 117 | + # move up |
| 118 | + mouse.move(y=1) |
| 119 | + |
| 120 | + if steps(y) > 15.0: |
| 121 | + if apds9960.proximity > distance: |
| 122 | + # scroll down |
| 123 | + mouse.move(wheel=3) |
| 124 | + else: |
| 125 | + # move down |
| 126 | + mouse.move(y=-8) |
| 127 | + if steps(y) < 1.0: |
| 128 | + if apds9960.proximity > distance: |
| 129 | + # scroll up |
| 130 | + mouse.move(wheel=-3) |
| 131 | + else: |
| 132 | + # move up |
| 133 | + mouse.move(y=8) |
| 134 | + #print(apds9960.proximity) |
| 135 | + ble.start_advertising(advertisement) |
0 commit comments