diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index a75d45f1fe..f4de71db77 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -166,6 +166,8 @@ SRC_COMMON_HAL += \ pulseio/PulseIn.c \ pulseio/PulseOut.c \ pulseio/__init__.c \ + rotaryio/__init__.c \ + rotaryio/IncrementalEncoder.c \ supervisor/Runtime.c \ supervisor/__init__.c \ time/__init__.c \ diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c new file mode 100644 index 0000000000..a0cfd48d56 --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -0,0 +1,116 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Nick Moore 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 "common-hal/rotaryio/IncrementalEncoder.h" +#include "nrfx_gpiote.h" + +#include "py/runtime.h" + +#include + +// obj array to map pin number -> self since nrfx hide the mapping +static rotaryio_incrementalencoder_obj_t *_objs[NUMBER_OF_PINS]; + +static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { + rotaryio_incrementalencoder_obj_t *self = _objs[pin]; + if (!self) return; + + // reads a state 0 .. 3 *in order*. + uint8_t new_state = nrf_gpio_pin_read(self->pin_a); + new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b)); + + uint8_t change = (new_state - self->state) & 0x03; + if (change == 1) self->quarter++; + else if (change == 3) self->quarter--; + // ignore other state transitions + + self->state = new_state; + + // logic from the atmel-samd port: provides some damping and scales movement + // down by 4:1. + if (self->quarter >= 4) { + self->position++; + self->quarter = 0; + } else if (self->quarter <= -4) { + self->position--; + self->quarter = 0; + } +} + +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { + + self->pin_a = pin_a->number; + self->pin_b = pin_b->number; + + _objs[self->pin_a] = self; + _objs[self->pin_b] = self; + + nrfx_gpiote_in_config_t cfg = { + .sense = NRF_GPIOTE_POLARITY_TOGGLE, + .pull = NRF_GPIO_PIN_PULLUP, + .is_watcher = false, + .hi_accuracy = true, + .skip_gpio_setup = false + }; + nrfx_gpiote_in_init(self->pin_a, &cfg, _intr_handler); + nrfx_gpiote_in_init(self->pin_b, &cfg, _intr_handler); + nrfx_gpiote_in_event_enable(self->pin_a, true); + nrfx_gpiote_in_event_enable(self->pin_b, true); + + claim_pin(pin_a); + claim_pin(pin_b); +} + +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { + return self->pin_a == NO_PIN; +} + +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { + return; + } + _objs[self->pin_a] = NULL; + _objs[self->pin_b] = NULL; + + nrfx_gpiote_in_event_disable(self->pin_a); + nrfx_gpiote_in_event_disable(self->pin_b); + nrfx_gpiote_in_uninit(self->pin_a); + nrfx_gpiote_in_uninit(self->pin_b); + reset_pin_number(self->pin_a); + reset_pin_number(self->pin_b); + self->pin_a = NO_PIN; + self->pin_b = NO_PIN; +} + +mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { + return self->position; +} + +void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, + mp_int_t new_position) { + self->position = new_position; +} diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h new file mode 100644 index 0000000000..1d0fe41839 --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 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_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H +#define MICROPY_INCLUDED_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pin_a; + uint8_t pin_b; + uint8_t state; + int8_t quarter; + mp_int_t position; +} rotaryio_incrementalencoder_obj_t; + + +void incrementalencoder_interrupt_handler(uint8_t channel); + +#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H diff --git a/ports/nrf/common-hal/rotaryio/__init__.c b/ports/nrf/common-hal/rotaryio/__init__.c new file mode 100644 index 0000000000..0aae79c26a --- /dev/null +++ b/ports/nrf/common-hal/rotaryio/__init__.c @@ -0,0 +1 @@ +// No rotaryio module functions. diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index e97c13d817..6bcd5cfcf1 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -169,6 +169,7 @@ extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t board_module; extern const struct _mp_obj_module_t os_module; extern const struct _mp_obj_module_t random_module; +extern const struct _mp_obj_module_t rotaryio_module; extern const struct _mp_obj_module_t storage_module; extern const struct _mp_obj_module_t struct_module; extern const struct _mp_obj_module_t time_module; @@ -205,6 +206,7 @@ extern const struct _mp_obj_module_t touchio_module; { MP_OBJ_NEW_QSTR (MP_QSTR_bitbangio ), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_os ), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_random ), (mp_obj_t)&random_module }, \ + { MP_OBJ_NEW_QSTR (MP_QSTR_rotaryio ), (mp_obj_t)&rotaryio_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_storage ), (mp_obj_t)&storage_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_struct ), (mp_obj_t)&struct_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \