|
| 1 | +# MicroPython Human Interface Device library |
| 2 | +# Copyright (C) 2021 H. Groefsema |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | + |
| 18 | +# Implements a BLE HID keyboard |
| 19 | +import time |
| 20 | +from machine import SoftSPI, Pin |
| 21 | +from hid_services import Keyboard |
| 22 | + |
| 23 | +class Device: |
| 24 | + def __init__(self): |
| 25 | + # Define state |
| 26 | + self.key0 = 0x00 |
| 27 | + self.key1 = 0x00 |
| 28 | + self.key2 = 0x00 |
| 29 | + self.key3 = 0x00 |
| 30 | + |
| 31 | + # Define buttons |
| 32 | + self.pin_forward = Pin(5, Pin.IN) |
| 33 | + self.pin_reverse = Pin(23, Pin.IN) |
| 34 | + self.pin_right = Pin(19, Pin.IN) |
| 35 | + self.pin_left = Pin(18, Pin.IN) |
| 36 | + |
| 37 | + # Create our device |
| 38 | + self.keyboard = Keyboard("Keyboard") |
| 39 | + # Set a callback function to catch changes of device state |
| 40 | + self.keyboard.set_state_change_callback(self.keyboard_state_callback) |
| 41 | + # Start our device |
| 42 | + self.keyboard.start() |
| 43 | + |
| 44 | + # Function that catches device status events |
| 45 | + def keyboard_state_callback(self): |
| 46 | + if self.keyboard.get_state() is Keyboard.DEVICE_IDLE: |
| 47 | + return |
| 48 | + elif self.keyboard.get_state() is Keyboard.DEVICE_ADVERTISING: |
| 49 | + return |
| 50 | + elif self.keyboard.get_state() is Keyboard.DEVICE_CONNECTED: |
| 51 | + return |
| 52 | + else: |
| 53 | + return |
| 54 | + |
| 55 | + def keyboard_event_callback(self, bytes): |
| 56 | + print("Keyboard state callback with bytes: ", bytes) |
| 57 | + |
| 58 | + def advertise(self): |
| 59 | + self.keyboard.start_advertising() |
| 60 | + |
| 61 | + def stop_advertise(self): |
| 62 | + self.keyboard.stop_advertising() |
| 63 | + |
| 64 | + # Main loop |
| 65 | + def start(self): |
| 66 | + while True: |
| 67 | + # Read pin values and update variables |
| 68 | + if self.pin_forward.value(): |
| 69 | + self.key0 = 0x1A # W |
| 70 | + else: |
| 71 | + self.key0 = 0x00 |
| 72 | + |
| 73 | + if self.pin_left.value(): |
| 74 | + self.key1 = 0x04 # A |
| 75 | + else: |
| 76 | + self.key1 = 0x00 |
| 77 | + |
| 78 | + if self.pin_reverse.value(): |
| 79 | + self.key2 = 0x16 # S |
| 80 | + else: |
| 81 | + self.key2 = 0x00 |
| 82 | + |
| 83 | + if self.pin_right.value(): |
| 84 | + self.key3 = 0x07 # D |
| 85 | + else: |
| 86 | + self.key3 = 0x00 |
| 87 | + |
| 88 | + # If the variables changed do something depending on the device state |
| 89 | + if (self.key0 != 0x00) or (self.key1 != 0x00) or (self.key2 != 0x00) or (self.key3 != 0x00): |
| 90 | + # If connected set keys and notify |
| 91 | + # If idle start advertising for 30s or until connected |
| 92 | + if self.keyboard.get_state() is Keyboard.DEVICE_CONNECTED: |
| 93 | + self.keyboard.set_keys(self.key0, self.key1, self.key2, self.key3) |
| 94 | + self.keyboard.notify_hid_report() |
| 95 | + elif self.keyboard.get_state() is Keyboard.DEVICE_IDLE: |
| 96 | + self.keyboard.start_advertising() |
| 97 | + i = 10 |
| 98 | + while i > 0 and self.keyboard.get_state() is Keyboard.DEVICE_ADVERTISING: |
| 99 | + time.sleep(3) |
| 100 | + i -= 1 |
| 101 | + if self.keyboard.get_state() is Keyboard.DEVICE_ADVERTISING: |
| 102 | + self.keyboard.stop_advertising() |
| 103 | + |
| 104 | + if self.keyboard.get_state() is Keyboard.DEVICE_CONNECTED: |
| 105 | + time.sleep_ms(20) |
| 106 | + else: |
| 107 | + time.sleep(2) |
| 108 | + |
| 109 | + def send_char(self, char): |
| 110 | + if char == " ": |
| 111 | + mod = 0 |
| 112 | + code = 0x2C |
| 113 | + elif ord("a") <= ord(char) <= ord("z"): |
| 114 | + mod = 0 |
| 115 | + code = 0x04 + ord(char) - ord("a") |
| 116 | + elif ord("A") <= ord(char) <= ord("Z"): |
| 117 | + mod = 1 |
| 118 | + code = 0x04 + ord(char) - ord("A") |
| 119 | + else: |
| 120 | + assert 0 |
| 121 | + |
| 122 | + self.keyboard.set_keys(code) |
| 123 | + self.keyboard.set_modifiers(left_shift=mod) |
| 124 | + self.keyboard.notify_hid_report() |
| 125 | + time.sleep_ms(2) |
| 126 | + |
| 127 | + self.keyboard.set_keys() |
| 128 | + self.keyboard.set_modifiers() |
| 129 | + self.keyboard.notify_hid_report() |
| 130 | + time.sleep_ms(2) |
| 131 | + |
| 132 | + |
| 133 | + def send_string(self, st): |
| 134 | + for c in st: |
| 135 | + self.send_char(c) |
| 136 | + |
| 137 | + # Only for test |
| 138 | + def stop(self): |
| 139 | + self.keyboard.stop() |
| 140 | + |
| 141 | + # Test routine |
| 142 | + def test(self): |
| 143 | + time.sleep(5) |
| 144 | + self.keyboard.set_battery_level(50) |
| 145 | + self.keyboard.notify_battery_level() |
| 146 | + time.sleep_ms(2) |
| 147 | + |
| 148 | + # Press Shift+W |
| 149 | + self.keyboard.set_keys(0x1A) |
| 150 | + self.keyboard.set_modifiers(right_shift=1) |
| 151 | + self.keyboard.notify_hid_report() |
| 152 | + |
| 153 | + # release |
| 154 | + self.keyboard.set_keys() |
| 155 | + self.keyboard.set_modifiers() |
| 156 | + self.keyboard.notify_hid_report() |
| 157 | + time.sleep_ms(500) |
| 158 | + |
| 159 | + # Press a |
| 160 | + self.keyboard.set_keys(0x04) |
| 161 | + self.keyboard.notify_hid_report() |
| 162 | + |
| 163 | + # release |
| 164 | + self.keyboard.set_keys() |
| 165 | + self.keyboard.notify_hid_report() |
| 166 | + time.sleep_ms(500) |
| 167 | + |
| 168 | + # Press s |
| 169 | + self.keyboard.set_keys(0x16) |
| 170 | + self.keyboard.notify_hid_report() |
| 171 | + |
| 172 | + # release |
| 173 | + self.keyboard.set_keys() |
| 174 | + self.keyboard.notify_hid_report() |
| 175 | + time.sleep_ms(500) |
| 176 | + |
| 177 | + # Press d |
| 178 | + self.keyboard.set_keys(0x07) |
| 179 | + self.keyboard.notify_hid_report() |
| 180 | + |
| 181 | + # release |
| 182 | + self.keyboard.set_keys() |
| 183 | + self.keyboard.notify_hid_report() |
| 184 | + time.sleep_ms(500) |
| 185 | + |
| 186 | + self.send_string(" Hello World") |
| 187 | + |
| 188 | + self.keyboard.set_battery_level(100) |
| 189 | + self.keyboard.notify_battery_level() |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + d = Device() |
| 193 | + d.start() |
0 commit comments