|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 microDev |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "common-hal/rotaryio/IncrementalEncoder.h" |
| 28 | +#include "common-hal/microcontroller/Pin.h" |
| 29 | + |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "supervisor/shared/translate.h" |
| 32 | + |
| 33 | +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, |
| 34 | + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { |
| 35 | + claim_pin(pin_a); |
| 36 | + claim_pin(pin_b); |
| 37 | + |
| 38 | + // Prepare configuration for the PCNT unit |
| 39 | + const pcnt_config_t pcnt_config = { |
| 40 | + // Set PCNT input signal and control GPIOs |
| 41 | + .pulse_gpio_num = pin_a->number, |
| 42 | + .ctrl_gpio_num = pin_b->number, |
| 43 | + .channel = PCNT_CHANNEL_0, |
| 44 | + // What to do on the positive / negative edge of pulse input? |
| 45 | + .pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge |
| 46 | + .neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge |
| 47 | + // What to do when control input is low or high? |
| 48 | + .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low |
| 49 | + .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high |
| 50 | + }; |
| 51 | + |
| 52 | + // Initialize PCNT unit |
| 53 | + const int8_t unit = peripherals_pcnt_init(pcnt_config); |
| 54 | + if (unit == -1) { |
| 55 | + mp_raise_RuntimeError(translate("All PCNT units in use")); |
| 56 | + } |
| 57 | + |
| 58 | + self->pin_a = pin_a->number; |
| 59 | + self->pin_b = pin_b->number; |
| 60 | + self->unit = (pcnt_unit_t)unit; |
| 61 | +} |
| 62 | + |
| 63 | +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { |
| 64 | + return self->unit == PCNT_UNIT_MAX; |
| 65 | +} |
| 66 | + |
| 67 | +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { |
| 68 | + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + reset_pin_number(self->pin_a); |
| 72 | + reset_pin_number(self->pin_b); |
| 73 | + peripherals_pcnt_deinit(&self->unit); |
| 74 | +} |
| 75 | + |
| 76 | +mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { |
| 77 | + int16_t count; |
| 78 | + pcnt_get_counter_value(self->unit, &count); |
| 79 | + return (count/2)+self->position; |
| 80 | +} |
| 81 | + |
| 82 | +void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, |
| 83 | + mp_int_t new_position) { |
| 84 | + self->position = new_position; |
| 85 | + pcnt_counter_clear(self->unit); |
| 86 | +} |
0 commit comments