Skip to content

Commit 0ebd4e6

Browse files
authored
Merge pull request adafruit#3614 from microDev1/rotaryio-S2
ESP32S2: Support for RotaryIO
2 parents eec9821 + 7ba2c57 commit 0ebd4e6

File tree

6 files changed

+130
-3
lines changed

6 files changed

+130
-3
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
29+
30+
#include "py/obj.h"
31+
#include "peripherals/pcnt.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
uint8_t pin_a, pin_b;
36+
mp_int_t position;
37+
pcnt_unit_t unit;
38+
} rotaryio_incrementalencoder_obj_t;
39+
40+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No rotaryio module functions.

ports/esp32s2/common-hal/touchio/TouchIn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 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

ports/esp32s2/common-hal/touchio/TouchIn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 Scott Shawcroft
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

ports/esp32s2/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CIRCUITPY_CANIO = 1
2020
CIRCUITPY_COUNTIO = 1
2121
CIRCUITPY_FREQUENCYIO = 0
2222
CIRCUITPY_I2CPERIPHERAL = 0
23-
CIRCUITPY_ROTARYIO = 0
23+
CIRCUITPY_ROTARYIO = 1
2424
CIRCUITPY_NVM = 0
2525
# We don't have enough endpoints to include MIDI.
2626
CIRCUITPY_USB_MIDI = 0

0 commit comments

Comments
 (0)