|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Nick Moore for Adafruit Industries |
| 7 | + * Copyright (c) 2023 Jeff Epler for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "common-hal/digitalio/DigitalInOut.h" |
| 29 | +#include "common-hal/rotaryio/IncrementalEncoder.h" |
| 30 | +#include "shared-module/rotaryio/IncrementalEncoder.h" |
| 31 | +#include "shared-bindings/microcontroller/Pin.h" |
| 32 | +#include "shared-bindings/rotaryio/IncrementalEncoder.h" |
| 33 | + |
| 34 | +#include "py/runtime.h" |
| 35 | + |
| 36 | +#include "sdk/drivers/igpio/fsl_gpio.h" |
| 37 | +static void encoder_change(void *self_in) { |
| 38 | + rotaryio_incrementalencoder_obj_t *self = self_in; |
| 39 | + |
| 40 | + bool value_a = GPIO_PinRead(self->pin_a->gpio, self->pin_a->number); |
| 41 | + bool value_b = GPIO_PinRead(self->pin_b->gpio, self->pin_b->number); |
| 42 | + uint8_t new_state = (value_a << 1) | value_b; |
| 43 | + shared_module_softencoder_state_update(self, new_state); |
| 44 | +} |
| 45 | + |
| 46 | +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self, |
| 47 | + const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) { |
| 48 | + |
| 49 | + self->pin_a = pin_a; |
| 50 | + self->pin_b = pin_b; |
| 51 | + |
| 52 | + // GPIO is always IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 until proven otherwise |
| 53 | +#define IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5 5U |
| 54 | + IOMUXC_SetPinMux(pin_a->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0); |
| 55 | + IOMUXC_SetPinMux(pin_b->mux_reg, IOMUXC_SW_MUX_CTL_PAD_MUX_MODE_ALT5, 0, 0, 0, 0); |
| 56 | + |
| 57 | + const gpio_pin_config_t config = { kGPIO_DigitalInput, 0, kGPIO_IntRisingOrFallingEdge }; |
| 58 | + GPIO_PinInit(pin_a->gpio, pin_a->number, &config); |
| 59 | + GPIO_PinInit(pin_b->gpio, pin_b->number, &config); |
| 60 | + |
| 61 | + enable_pin_change_interrupt(pin_a, encoder_change, self); |
| 62 | + enable_pin_change_interrupt(pin_b, encoder_change, self); |
| 63 | + |
| 64 | + pin_config(pin_a, false, PULL_UP); |
| 65 | + pin_config(pin_b, false, PULL_UP); |
| 66 | + |
| 67 | + claim_pin(pin_a); |
| 68 | + claim_pin(pin_b); |
| 69 | +} |
| 70 | + |
| 71 | +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t *self) { |
| 72 | + return !self->pin_a; |
| 73 | +} |
| 74 | + |
| 75 | +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t *self) { |
| 76 | + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + disable_pin_change_interrupt(self->pin_a); |
| 80 | + disable_pin_change_interrupt(self->pin_b); |
| 81 | + |
| 82 | + common_hal_reset_pin(self->pin_a); |
| 83 | + common_hal_reset_pin(self->pin_b); |
| 84 | + |
| 85 | + self->pin_a = NULL; |
| 86 | + self->pin_b = NULL; |
| 87 | +} |
0 commit comments