def __init__(self): """Create a Keyboard object that will send USB keyboard HID reports.""" self.hid_keyboard = None ''' for device in usb_hid.devices: if device.usage_page == 0x1 and device.usage == 0x06: self.hid_keyboard = device break if not self.hid_keyboard: raise IOError("Could not find an HID keyboard device.") # Reuse this bytearray to send keyboard reports. ''' self.hid_keyboard = pyb.USB_HID() self.report = bytearray(8) # report[0] modifiers # report[1] unused # report[2:8] regular key presses # View onto byte 0 in report. self.report_modifier = memoryview(self.report)[0:1] # List of regular keys currently pressed. # View onto bytes 2-7 in report. self.report_keys = memoryview(self.report)[2:]
def run(self): while True: self._scan_matrix() if self.hid is not None: pyb.LED(1).on() self.hid.send(self.send_buf) pyb.LED(1).off() else: if 'HID' in pyb.usb_mode(): self.hid = pyb.USB_HID()
def main(): sw_state = False led = pyb.LED(1) switch = pyb.Switch() switch.callback(set_sw_state) accel = STAccel() hid = pyb.USB_HID() while True: if sw_state: x, y, z = accel.xyz() hid.send((0, int(x * MAG), int(-y * MAG), 0)) pyb.delay(int(1000 / FREQ))
def osc(n, d): for i in range(n): hid.send((0, int(20 * math.sin(i / 10)), 0, 0)) pyb.delay(d) osc(100, 50) # Connect to accel - NOTE you will need to use safe mode to stop - see webapge import pyb switch = pyb.Switch() accel = pyb.Accel() hid = pyb.USB_HID() while not switch(): hid.send((0, accel.x(), accel.y(), 0)) pyb.delay(20)
def __init__(self): self.hid = pyb.USB_HID() if 'HID' in pyb.usb_mode() else None if self.hid is not None: _thread.start_new_thread(thread_entry, (self,)) self.send_buf = bytearray(SEND_SIZE) self.recv_buf = bytearray(RECV_SIZE) self.fn = False self.row_pins = [pyb.Pin(x) for x in ROW_PINS] self.col_pins = [pyb.Pin(x) for x in COL_PINS] for row_pin in self.row_pins: row_pin.init(pyb.Pin.OUT_OD) row_pin.high() for col_pin in self.col_pins: col_pin.init(pyb.Pin.IN, pull=pyb.Pin.PULL_UP)
def __init__(self): """Create a Mouse object that will send USB mouse HID reports.""" self.hid_mouse = None ''' for device in usb_hid.devices: if device.usage_page == 0x1 and device.usage == 0x02: self.hid_mouse = device break if not self.hid_mouse: raise IOError("Could not find an HID mouse device.") ''' self.hid_mouse = pyb.USB_HID() # Reuse this bytearray to send mouse reports. # report[0] buttons pressed (LEFT, MIDDLE, RIGHT) # report[1] x movement # report[2] y movement # report[3] wheel movement self.report = bytearray(4)
def __init__(self): self.dev = pyb.USB_HID() # We keep a running hash over whatever has been uploaded # - reset at offset zero, can be read back anytime self.file_checksum = tcc.sha256() # handle simulator self.blockable = getattr(self.dev, 'pipe', self.dev) #self.msg = bytearray(MAX_MSG_LEN) from sram2 import usb_buf self.msg = usb_buf assert len(self.msg) == MAX_MSG_LEN self.encrypted_req = False # these will be tcc.AES objects later self.encrypt = None self.decrypt = None
import pyb from time import sleep_ms # Activer le mode VCP+HID dans boot.py switch = pyb.Switch() accel = pyb.Accel() hid = pyb.USB_HID() while not switch(): hid.send((0, accel.x(), accel.y(), 0)) sleep_ms(20)
import pyb from keys import KEYS, KEYS_SHIFT, MODS KEYBOARD = pyb.USB_HID() KEY_UP = bytearray(8) LINUX_TERMINAL = bytearray(8) LINUX_TERMINAL[0], LINUX_TERMINAL[2] = MODS["LCTRL"] + MODS["LALT"], KEYS["t"] CHANGE_WINDOW = bytearray(8) CHANGE_WINDOW[0], CHANGE_WINDOW[2] = MODS["LALT"], KEYS["TAB"] CTRL_Z = bytearray(8) CTRL_Z[0], CTRL_Z[2] = MODS["LCTRL"], KEYS["z"] LALT_F4 = bytearray(8) LALT_F4[0], LALT_F4[2] = MODS["LALT"], KEYS["F4"] def windows_terminal(): run = bytearray(8) run[0], run[2] = MODS["LMETA"], KEYS["r"] send_array(run) pyb.delay(100) send_string("cmd\\ENTER\\") pyb.delay(100) def map_char(char): array = bytearray(8) if char in KEYS_SHIFT: array[0] = MODS["LSHIFT"] array[2] = KEYS_SHIFT[char] else: array[2] = KEYS[char]
import pyb from utils import kbmap, ALT, GUI led = pyb.LED(1) sw = pyb.Switch() kb = pyb.USB_HID() buf = bytearray(8) def sendchr(char): if not char in kbmap.keys(): print("Unknow char") return # key down buf[2], buf[0] = kbmap[char] kb.send(buf) pyb.delay(20) #注意 delay时间太短可能出问题 # key up buf[2], buf[0] = 0x00, 0x00 kb.send(buf) pyb.delay(20) def sendstr(str): for c in str: sendchr(c) def call_termial(): # Ubuntu: alt + contrl +T # windows: GUi + r then cmd\n
#hardware platform: pyboard V1.1 # use this demo you should do: # 1. open boot.py # 2. enable pyb.usb_mode('VCP+HID') # 3. close uPyCraft and reconnet pyboard for PC # 4. open uPyCraft and run this demo # 5. ctrl+c or stop could stop this demo # Restoring your pyboard to normal # 1. Hold down the USR switch. # 2. While still holding down USR, press and release the RST switch. # 3. The LEDs will then cycle green to orange to green+orange and back again. # 4. Keep holding down USR until only the orange LED is lit, and then let go of the USR switch. # 5. The orange LED should flash quickly 4 times, and then turn off. # 6. You are now in safe mode. import pyb switch = pyb.Switch() accel = pyb.Accel() #Accel is an object that controls the accelerometer hid = pyb.USB_HID( ) #create USB_HID object.it can be used to emulate a peripheral such as a mouse or keyboard. while not switch(): hid.send( (0, accel.x(), accel.y(), 0)) #Send data over the USB HID interface
# main.py import pyb # importing the board switch = pyb.Switch() # assigning the switch USR accel = pyb.Accel() # assigning the sensor to act as movement detector… hid = pyb.USB_HID() # assigning the HID (the USB mouse) while not switch(): # while loop, looping while switch is not pressed… hid.send( (0, accel.x(), accel.y(), 0) ) # map moment on monitor based on the movement of the board pyb.delay( 20 ) # Delay by 20 microseconds to allow visible movement (Not too fast!)
def osc(n, d): hid = pyb.USB_HID() if hid is not None: for i in range(n): hid.send((0, int(20 * math.sin(i/10)), - int(20 * math.cos(i/10)), 0)) pyb.delay(d)
def __init__(self): self.h = pyb.USB_HID() self.CID = b'\xff\xff\xff\xff'