/*-----------------------------------------------------------------------------------
Pico_Rotary_SSD1306.ino
-----------------------------------------------------------------------------------
Raspberry Pi Pico, SSD1306 OLED, Rotary Encoder
https://forum.arduino.cc/t/pi-pico-and-ssd1306-oled-display/929498
Pico ---- SSD1306 OLED display (I2C communication)
3.3V ----- VCC physical pin 36
GND ----- GND physical pin 38
GP4 ----- SDA physical pin 6
GP5 ----- SCL physical pin 7
https://github.com/netguy204/OakOLED
https://github.com/mathertel/RotaryEncoder
// Abruf der aktuellen Position
long getPosition();
// Abrufen der Richtung, in die der Knopf zuletzt gedreht wurde.
// 0 = keine Drehung, 1 = im Uhrzeigersinn, -1 = gegen den Uhrzeigersinn
Direction getDirection();
// Eine Position einstellen
void setPosition(long newPosition);
// Diese Funktion alle paar Millisekunden aufrufen oder einen Interrupt
// zur Behandlung von Zustandsänderungen des Drehgebers verwenden.
void tick(void);
// Gibt die Zeit zwischen der Drehung in ms zurück
unsigned long getMillisBetweenRotations() const;
// Gibt die Drehzahl zurück
unsigned long getRPM();
*/
#define OAK_OLED
//#define ASCII_OLED
#include "RotaryEncoder.h"
#define PIN_IN1 2 // DT
#define PIN_IN2 3 // CLK
#define SW 6 // Button
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
#ifdef OAK_OLED
#include "OakOLED.h"
OakOLED oled;
#endif //OAK_OLED
/*-----------------------------------------------------------------------------------
encoderPosition - Interrupt Routine
-----------------------------------------------------------------------------------*/
void encoderPosition()
{
encoder.tick();
}
/*-----------------------------------------------------------------------------------
encoderButton - Interrupt Routine
-----------------------------------------------------------------------------------*/
void encoderButton()
{
Serial1.println("Button clicked");
}
/*-----------------------------------------------------------------------------------
----- S E T U P -----
-----------------------------------------------------------------------------------*/
void setup() {
Serial1.begin(9600);
// register interrupt routine
attachInterrupt(digitalPinToInterrupt(PIN_IN1), encoderPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_IN2), encoderPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(SW), encoderButton, CHANGE);
oled.begin();
oled.setTextSize(1);
//oled.setTextColor(1);
oled.setCursor(30, 40); oled.print("Hello Reini");
oled.setTextSize(2);
oled.setCursor(0, 0); oled.print("Pos: 0");
oled.display();
}
/*-----------------------------------------------------------------------------------
----- L O O P -----
-----------------------------------------------------------------------------------*/
void loop()
{
static int pos = 0;
int newPos = encoder.getPosition();
if (pos != newPos) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(0, 0); oled.print("Pos: ");
oled.setCursor(60, 0); oled.print(newPos);
oled.display();
// Serial1.print("pos:");
// Serial1.print(newPos);
// Serial1.print(" dir:");
// Serial1.println((int)(encoder.getDirection()));
pos = newPos;
}
}