|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
| 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 | + |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "supervisor/shared/translate.h" |
| 31 | + |
| 32 | +#include "driver/pcnt.h" |
| 33 | + |
| 34 | +static void pcnt_reset(int unit) { |
| 35 | + // Initialize PCNT's counter |
| 36 | + pcnt_counter_pause(unit); |
| 37 | + pcnt_counter_clear(unit); |
| 38 | + |
| 39 | + // Everything is set up, now go to counting |
| 40 | + pcnt_counter_resume(unit); |
| 41 | +} |
| 42 | + |
| 43 | +static void pcnt_init(int unit, rotaryio_incrementalencoder_obj_t* self) { |
| 44 | + // Prepare configuration for the PCNT unit |
| 45 | + pcnt_config_t pcnt_config = { |
| 46 | + // Set PCNT input signal and control GPIOs |
| 47 | + .pulse_gpio_num = self->pin_a->number, |
| 48 | + .ctrl_gpio_num = self->pin_b->number, |
| 49 | + .channel = PCNT_CHANNEL_0, |
| 50 | + .unit = unit, |
| 51 | + // What to do on the positive / negative edge of pulse input? |
| 52 | + .pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge |
| 53 | + .neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge |
| 54 | + // What to do when control input is low or high? |
| 55 | + .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low |
| 56 | + .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high |
| 57 | + }; |
| 58 | + // Initialize PCNT unit |
| 59 | + pcnt_unit_config(&pcnt_config); |
| 60 | + |
| 61 | + // Configure channel 1 |
| 62 | + pcnt_config.pulse_gpio_num = self->pin_b->number; |
| 63 | + pcnt_config.ctrl_gpio_num = self->pin_a->number; |
| 64 | + pcnt_config.channel = PCNT_CHANNEL_1; |
| 65 | + pcnt_config.pos_mode = PCNT_COUNT_INC; |
| 66 | + pcnt_config.neg_mode = PCNT_COUNT_DEC; |
| 67 | + pcnt_unit_config(&pcnt_config); |
| 68 | + |
| 69 | + // Configure and enable the input filter |
| 70 | + pcnt_set_filter_value(unit, 100); |
| 71 | + pcnt_filter_enable(unit); |
| 72 | + |
| 73 | + pcnt_reset(unit); |
| 74 | +} |
| 75 | + |
| 76 | +void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, |
| 77 | + const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) { |
| 78 | + claim_pin(pin_a); |
| 79 | + claim_pin(pin_b); |
| 80 | + |
| 81 | + self->pin_a = pin_a; |
| 82 | + self->pin_b = pin_b; |
| 83 | + |
| 84 | + self->position = 0; |
| 85 | + |
| 86 | + pcnt_init(PCNT_UNIT_0, self); |
| 87 | +} |
| 88 | + |
| 89 | +bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) { |
| 90 | + return self->pin_a == NULL; |
| 91 | +} |
| 92 | + |
| 93 | +void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) { |
| 94 | + if (common_hal_rotaryio_incrementalencoder_deinited(self)) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + reset_pin_number(self->pin_a->number); |
| 99 | + self->pin_a = NULL; |
| 100 | + |
| 101 | + reset_pin_number(self->pin_b->number); |
| 102 | + self->pin_b = NULL; |
| 103 | +} |
| 104 | + |
| 105 | +mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) { |
| 106 | + int16_t count = 0; |
| 107 | + pcnt_get_counter_value(PCNT_UNIT_0, &count); |
| 108 | + return self->position+count; |
| 109 | +} |
| 110 | + |
| 111 | +void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self, |
| 112 | + mp_int_t new_position) { |
| 113 | + self->position = new_position; |
| 114 | + pcnt_reset(PCNT_UNIT_0); |
| 115 | +} |
0 commit comments