-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to use interrupts on every pulse and set a callback (#52
) * interrupts on every encoder change. * refine always interrupt * interrupt callback. * cleanup. * whitespace. * Add example. * Add enc_isr_cb_data * Rename example to INO * Make it Arduino example. * Complete ctor initialization of members. * delete old example.
- Loading branch information
Showing
3 changed files
with
157 additions
and
56 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
examples/Encoder_interrupt_display/Encoder_interrupt_display.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <Arduino.h> | ||
#include <ESP32Encoder.h> | ||
#include <Adafruit_GFX.h> | ||
#include <Adafruit_SSD1306.h> | ||
#include "esp_task_wdt.h" | ||
|
||
static IRAM_ATTR void enc_cb(void* arg) { | ||
ESP32Encoder* enc = (ESP32Encoder*) arg; | ||
//Serial.printf("enc cb: count: %d\n", enc->getCount()); | ||
static bool leds = false; | ||
digitalWrite(LED_BUILTIN, (int)leds); | ||
leds = !leds; | ||
} | ||
|
||
extern bool loopTaskWDTEnabled; | ||
extern TaskHandle_t loopTaskHandle; | ||
|
||
ESP32Encoder encoder(true, enc_cb); | ||
Adafruit_SSD1306 display(128, 32, &Wire); | ||
|
||
static const char* LOG_TAG = "main"; | ||
|
||
void setup(){ | ||
loopTaskWDTEnabled = true; | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
Serial.begin(115200); | ||
// Encoder A and B pins connected with 1K series resistor to pins 4 and 5, common pin to ground. | ||
// |- A --- 1K --- pin 4 | ||
// >=[enc]|- GND | ||
// |- B --- 1K --- pin 5 | ||
|
||
ESP32Encoder::useInternalWeakPullResistors=UP; | ||
encoder.attachSingleEdge(4, 5); | ||
encoder.clearCount(); | ||
encoder.setFilter(1023); | ||
|
||
if (! display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) | ||
{ | ||
for (;;) { | ||
Serial.println("display init failed"); | ||
} | ||
} | ||
display.cp437(true); | ||
display.clearDisplay(); | ||
display.setTextSize(2); | ||
display.setTextColor(WHITE); | ||
display.setCursor(0,0); | ||
display.display(); | ||
esp_log_level_set("*", ESP_LOG_DEBUG); | ||
esp_log_level_set("main", ESP_LOG_DEBUG); | ||
esp_log_level_set("ESP32Encoder", ESP_LOG_DEBUG); | ||
esp_task_wdt_add(loopTaskHandle); | ||
} | ||
|
||
void loop(){ | ||
|
||
display.clearDisplay(); | ||
display.setCursor(0,0); | ||
display.printf("E: %lld\n", encoder.getCount()); | ||
display.display(); | ||
Serial.printf("Enc count: %d\n", encoder.getCount()); | ||
delay(500); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters