Skip to content

Commit 55e0e2c

Browse files
committed
Update rotaryio implementation
1 parent 4795670 commit 55e0e2c

File tree

2 files changed

+29
-59
lines changed

2 files changed

+29
-59
lines changed

ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2020 microDev
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -25,91 +25,62 @@
2525
*/
2626

2727
#include "common-hal/rotaryio/IncrementalEncoder.h"
28+
#include "common-hal/microcontroller/Pin.h"
2829

2930
#include "py/runtime.h"
3031
#include "supervisor/shared/translate.h"
3132

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-
}
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);
4237

43-
static void pcnt_init(int unit, rotaryio_incrementalencoder_obj_t* self) {
4438
// Prepare configuration for the PCNT unit
45-
pcnt_config_t pcnt_config = {
39+
const pcnt_config_t pcnt_config = {
4640
// Set PCNT input signal and control GPIOs
47-
.pulse_gpio_num = self->pin_a->number,
48-
.ctrl_gpio_num = self->pin_b->number,
41+
.pulse_gpio_num = pin_a->number,
42+
.ctrl_gpio_num = pin_b->number,
4943
.channel = PCNT_CHANNEL_0,
50-
.unit = unit,
5144
// What to do on the positive / negative edge of pulse input?
5245
.pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
5346
.neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
5447
// What to do when control input is low or high?
5548
.lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
5649
.hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
5750
};
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;
8351

84-
self->position = 0;
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+
}
8557

86-
pcnt_init(PCNT_UNIT_0, self);
58+
self->pin_a = pin_a->number;
59+
self->pin_b = pin_b->number;
60+
self->unit = (pcnt_unit_t)unit;
8761
}
8862

8963
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) {
90-
return self->pin_a == NULL;
64+
return self->unit == PCNT_UNIT_MAX;
9165
}
9266

9367
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) {
9468
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
9569
return;
9670
}
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;
71+
reset_pin_number(self->pin_a);
72+
reset_pin_number(self->pin_b);
73+
peripherals_pcnt_deinit(&self->unit);
10374
}
10475

10576
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;
77+
int16_t count;
78+
pcnt_get_counter_value(self->unit, &count);
79+
return (count/2)+self->position;
10980
}
11081

11182
void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self,
11283
mp_int_t new_position) {
11384
self->position = new_position;
114-
pcnt_reset(PCNT_UNIT_0);
85+
pcnt_counter_clear(self->unit);
11586
}

ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2020 microDev
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -27,15 +27,14 @@
2727
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
2828
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
2929

30-
#include "common-hal/microcontroller/Pin.h"
31-
3230
#include "py/obj.h"
31+
#include "peripherals/pcnt.h"
3332

3433
typedef struct {
3534
mp_obj_base_t base;
36-
const mcu_pin_obj_t * pin_a;
37-
const mcu_pin_obj_t * pin_b;
35+
uint8_t pin_a, pin_b;
3836
mp_int_t position;
37+
pcnt_unit_t unit;
3938
} rotaryio_incrementalencoder_obj_t;
4039

4140
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H

0 commit comments

Comments
 (0)