diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-02-03 09:41:04 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-02-27 15:43:58 +0100 |
| commit | d598c2a919e9d3dc550092a4ac7e7ed68dc27f86 (patch) | |
| tree | 81e69f8ea90c6c1c11a772a3b9f2ae60753fae41 /shared-bindings | |
| parent | 062fac1d43d42b35e0ecdaaec124c29b65db7c68 (diff) | |
atmel-samd: Add USB HID mouse and keyboard support.
Diffstat (limited to 'shared-bindings')
| -rw-r--r-- | shared-bindings/usb_hid/Device.c | 124 | ||||
| -rw-r--r-- | shared-bindings/usb_hid/Device.h | 38 | ||||
| -rw-r--r-- | shared-bindings/usb_hid/__init__.c | 70 | ||||
| -rw-r--r-- | shared-bindings/usb_hid/__init__.h | 39 |
4 files changed, 271 insertions, 0 deletions
diff --git a/shared-bindings/usb_hid/Device.c b/shared-bindings/usb_hid/Device.c new file mode 100644 index 000000000..3a3cc5170 --- /dev/null +++ b/shared-bindings/usb_hid/Device.c @@ -0,0 +1,124 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/objproperty.h" +#include "shared-bindings/usb_hid/Device.h" + +//| .. currentmodule:: usb_hid +//| +//| :class:`Device` -- HID Device +//| ============================================ +//| +//| Usage:: +//| +//| import usb_hid +//| +//| mouse = usb_hid.devices[0] +//| +//| mouse.send_report() +//| + +//| .. class:: Device() +//| +//| Not currently dynamically supported. +//| +STATIC mp_obj_t usb_hid_device_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + return mp_const_none; +} + +//| .. method:: send_report(buf) +//| +//| Send a HID report. +//| +STATIC mp_obj_t usb_hid_device_send_report(mp_obj_t self_in, mp_obj_t buffer) { + usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ); + + common_hal_usb_hid_device_send_report(self, ((uint8_t*) bufinfo.buf), bufinfo.len); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(usb_hid_device_send_report_obj, usb_hid_device_send_report); + +//| .. attribute:: usage_page +//| +//| The usage page of the device. Can be thought of a category. +//| +//| :return: the device's usage page +//| :rtype: int +//| +STATIC mp_obj_t usb_hid_device_obj_get_usage_page(mp_obj_t self_in) { + usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_usb_hid_device_get_usage_page(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(usb_hid_device_get_usage_page_obj, usb_hid_device_obj_get_usage_page); + +const mp_obj_property_t usb_hid_device_usage_page_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&usb_hid_device_get_usage_page_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +//| .. attribute:: usage +//| +//| The functionality of the device. For example Keyboard is 0x06 within the +//| generic desktop usage page 0x01. Mouse is 0x02 within the same usage +//| page. +//| +//| :return: the usage within the usage page +//| :rtype: int +//| +STATIC mp_obj_t usb_hid_device_obj_get_usage(mp_obj_t self_in) { + usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(common_hal_usb_hid_device_get_usage(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(usb_hid_device_get_usage_obj, + usb_hid_device_obj_get_usage); + +const mp_obj_property_t usb_hid_device_usage_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&usb_hid_device_get_usage_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t usb_hid_device_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_send_report), MP_ROM_PTR(&usb_hid_device_send_report_obj) }, + { MP_ROM_QSTR(MP_QSTR_usage_page), MP_ROM_PTR(&usb_hid_device_usage_page_obj)}, + { MP_ROM_QSTR(MP_QSTR_usage), MP_ROM_PTR(&usb_hid_device_usage_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(usb_hid_device_locals_dict, usb_hid_device_locals_dict_table); + +const mp_obj_type_t usb_hid_device_type = { + { &mp_type_type }, + .name = MP_QSTR_Device, + .make_new = usb_hid_device_make_new, + .locals_dict = (mp_obj_t)&usb_hid_device_locals_dict, +}; diff --git a/shared-bindings/usb_hid/Device.h b/shared-bindings/usb_hid/Device.h new file mode 100644 index 000000000..08713874e --- /dev/null +++ b/shared-bindings/usb_hid/Device.h @@ -0,0 +1,38 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ + +#include "common-hal/usb_hid/types.h" + +const mp_obj_type_t usb_hid_device_type; + +void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len); +uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self); +uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self); + +#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__ diff --git a/shared-bindings/usb_hid/__init__.c b/shared-bindings/usb_hid/__init__.c new file mode 100644 index 000000000..a870cb4a1 --- /dev/null +++ b/shared-bindings/usb_hid/__init__.c @@ -0,0 +1,70 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/obj.h" +#include "py/mphal.h" +#include "py/runtime.h" + +#include "shared-bindings/usb_hid/__init__.h" +#include "shared-bindings/usb_hid/Device.h" + +//| :mod:`usb_hid` --- USB Human Interface Device +//| =========================================================== +//| +//| .. module:: usb_hid +//| :synopsis: USB Human Interface Device +//| :platform: SAMD21 +//| +//| The `usb_hid` module allows you to output data as a HID device. +//| + +//| .. warning:: This module may be dropped from builds without external flash +//| in the future. Please file an issue if this a problem and explain why. +//| + +//| .. attribute:: usb_hid.devices +//| +//| Tuple of all active HID device interfaces. +//| + +//| Libraries +//| +//| .. toctree:: +//| :maxdepth: 3 +//| +//| Device +STATIC const mp_rom_map_elem_t usb_hid_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid) }, + { MP_ROM_QSTR(MP_QSTR_devices), MP_ROM_PTR(&common_hal_usb_hid_devices) }, + { MP_ROM_QSTR(MP_QSTR_Device), MP_ROM_PTR(&usb_hid_device_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(usb_hid_module_globals, usb_hid_module_globals_table); + +const mp_obj_module_t usb_hid_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&usb_hid_module_globals, +}; diff --git a/shared-bindings/usb_hid/__init__.h b/shared-bindings/usb_hid/__init__.h new file mode 100644 index 000000000..576a3301b --- /dev/null +++ b/shared-bindings/usb_hid/__init__.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef SHARED_BINDINGS_USB_HID_H +#define SHARED_BINDINGS_USB_HID_H + +#include <stdint.h> +#include <stdbool.h> + +#include "common-hal/nativeio/types.h" + +// TODO(tannewt): Make this a mp_obj_tuple_t when it is dynamically allocated. +// until then we hard code it to two entries so LTO is happy. +extern mp_obj_tuple2_t common_hal_usb_hid_devices; + +#endif // SHARED_BINDINGS_USB_HID_H |
