summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-11-09 15:41:07 -0800
committerGitHub <noreply@github.com>2020-11-09 15:41:07 -0800
commit0ebd4e615e16515b7c1fb28f38f80d6fd920e0b1 (patch)
tree83572718d8f854b0090921448663e3495b10636d
parenteec9821fdc19ca81c2d0811a6b6c5909a54b8c81 (diff)
parent7ba2c5772ccbe48c5c52d62493f9c899a1d0963a (diff)
Merge pull request #3614 from microDev1/rotaryio-S2
ESP32S2: Support for RotaryIO
-rw-r--r--ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c86
-rw-r--r--ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.h40
-rw-r--r--ports/esp32s2/common-hal/rotaryio/__init__.c1
-rw-r--r--ports/esp32s2/common-hal/touchio/TouchIn.c2
-rw-r--r--ports/esp32s2/common-hal/touchio/TouchIn.h2
-rw-r--r--ports/esp32s2/mpconfigport.mk2
6 files changed, 130 insertions, 3 deletions
diff --git a/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c b/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
new file mode 100644
index 000000000..25529ac72
--- /dev/null
+++ b/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
@@ -0,0 +1,86 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "common-hal/rotaryio/IncrementalEncoder.h"
+#include "common-hal/microcontroller/Pin.h"
+
+#include "py/runtime.h"
+#include "supervisor/shared/translate.h"
+
+void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self,
+ const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) {
+ claim_pin(pin_a);
+ claim_pin(pin_b);
+
+ // Prepare configuration for the PCNT unit
+ const pcnt_config_t pcnt_config = {
+ // Set PCNT input signal and control GPIOs
+ .pulse_gpio_num = pin_a->number,
+ .ctrl_gpio_num = pin_b->number,
+ .channel = PCNT_CHANNEL_0,
+ // What to do on the positive / negative edge of pulse input?
+ .pos_mode = PCNT_COUNT_DEC, // Count up on the positive edge
+ .neg_mode = PCNT_COUNT_INC, // Keep the counter value on the negative edge
+ // What to do when control input is low or high?
+ .lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
+ .hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
+ };
+
+ // Initialize PCNT unit
+ const int8_t unit = peripherals_pcnt_init(pcnt_config);
+ if (unit == -1) {
+ mp_raise_RuntimeError(translate("All PCNT units in use"));
+ }
+
+ self->pin_a = pin_a->number;
+ self->pin_b = pin_b->number;
+ self->unit = (pcnt_unit_t)unit;
+}
+
+bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) {
+ return self->unit == PCNT_UNIT_MAX;
+}
+
+void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) {
+ if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
+ return;
+ }
+ reset_pin_number(self->pin_a);
+ reset_pin_number(self->pin_b);
+ peripherals_pcnt_deinit(&self->unit);
+}
+
+mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) {
+ int16_t count;
+ pcnt_get_counter_value(self->unit, &count);
+ return (count/2)+self->position;
+}
+
+void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self,
+ mp_int_t new_position) {
+ self->position = new_position;
+ pcnt_counter_clear(self->unit);
+}
diff --git a/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.h b/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.h
new file mode 100644
index 000000000..8a717b7b5
--- /dev/null
+++ b/ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 microDev
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
+#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
+
+#include "py/obj.h"
+#include "peripherals/pcnt.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint8_t pin_a, pin_b;
+ mp_int_t position;
+ pcnt_unit_t unit;
+} rotaryio_incrementalencoder_obj_t;
+
+#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ROTARYIO_INCREMENTALENCODER_H
diff --git a/ports/esp32s2/common-hal/rotaryio/__init__.c b/ports/esp32s2/common-hal/rotaryio/__init__.c
new file mode 100644
index 000000000..0aae79c26
--- /dev/null
+++ b/ports/esp32s2/common-hal/rotaryio/__init__.c
@@ -0,0 +1 @@
+// No rotaryio module functions.
diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.c b/ports/esp32s2/common-hal/touchio/TouchIn.c
index 3e3e4b551..b44234775 100644
--- a/ports/esp32s2/common-hal/touchio/TouchIn.c
+++ b/ports/esp32s2/common-hal/touchio/TouchIn.c
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ * Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.h b/ports/esp32s2/common-hal/touchio/TouchIn.h
index 585bb37bf..91de20931 100644
--- a/ports/esp32s2/common-hal/touchio/TouchIn.h
+++ b/ports/esp32s2/common-hal/touchio/TouchIn.h
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2016 Scott Shawcroft
+ * Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk
index bdb25a87b..4579b95ab 100644
--- a/ports/esp32s2/mpconfigport.mk
+++ b/ports/esp32s2/mpconfigport.mk
@@ -20,7 +20,7 @@ CIRCUITPY_CANIO = 1
CIRCUITPY_COUNTIO = 1
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
-CIRCUITPY_ROTARYIO = 0
+CIRCUITPY_ROTARYIO = 1
CIRCUITPY_NVM = 0
# We don't have enough endpoints to include MIDI.
CIRCUITPY_USB_MIDI = 0