Register / Login | Desktop view |
#include <IoAbstraction.h>
#include <IoAbstractionWire.h>
#include <TaskManagerIO.h>
#include <Wire.h>
// to make life easier, probably define each expander
#define EXPANDER1 100
// Multi IO generieren mit Pin 0 - 99 für ESP8266
MultiIoAbstractionRef multiIo = multiIoExpander(EXPANDER1);
//Encoder
const uint8_t encoderAPin = 101;
const uint8_t encoderBPin = 102;
// the maximum value (starting from 0) that we want the encoder to represent. This range is inclusive.
const int maximumEncoderValue = 1000;
// Each time the encoder value changes, this function runs, as we registered it as a callback
//
void onEncoderChange(int newValue) {
Serial.print("Encoder change ");
Serial.println(newValue);
}
void onSwitchPressed(uint8_t key, bool held)
{
ioDeviceSync(multiIo); // force another sync
Serial.print("Switch ");
Serial.print(key);
Serial.println(held ? " Held down" : " Pressed");
}
class RepeatButtonListener : public SwitchListener {
private:
int counter;
public:
void onPressed(pinid_t pin, bool held) override {
++counter;
Serial.print("Repeat button pressed ");
Serial.println(counter);
}
void onReleased(pinid_t pin, bool held) override {
Serial.print("Released after presses ");
Serial.println(counter);
counter = 0;
}
} repeatListener;
void setup()
{
Wire.begin();
//Serielle Sxchnittstelle und Monitor
Serial.begin(9600);
Serial.println(" Start Setup");
//8574 Expander added, 10 Pin 100 - 109
multiIoAddExpander ( multiIo, ioFrom8574(0x20), 10);
Serial.println("expander 8574: Pin 100 bis 109 hinzu über multiIoAddExpander");
// Pin 8574 definition of Pins
ioDevicePinMode(multiIo, EXPANDER1, INPUT); //P0
ioDevicePinMode(multiIo, EXPANDER1+0, INPUT); //P1 Pin 100 ( P0 auf 8574) Taster
ioDevicePinMode(multiIo, EXPANDER1+1, INPUT); //P2 Pin 101 Encoder Pin A
ioDevicePinMode(multiIo, EXPANDER1+2, INPUT); //P3 Pin 102 Encoder Pin B
ioDevicePinMode(multiIo, EXPANDER1+3, INPUT); //P4 Pin 103 ( P3 auf 8574) Taster Drehimpulsgeber
Serial.println("ioDevicePinMode für Pin 100 + 103 hinzu");
Serial.println("Multi IoExpander example");
Serial.println("Io is setup, adding switch");
switches.initialise(multiIo, true);
switches.addSwitch(EXPANDER1 +0, onSwitchPressed);
switches.addSwitch(EXPANDER1 +3, onSwitchPressed);
/*
switches.addSwitch(EXPANDER1 +1, onSwitchPressed); //Test CLK / PinA as switch
switches.addSwitch(EXPANDER1 +2, onSwitchPressed); //Test DT / PinB
*/
setupRotaryEncoderWithInterrupt(encoderAPin, encoderBPin, onEncoderChange, HWACCEL_REGULAR);
switches.changeEncoderPrecision(0, 0);
Serial.println("setup is done!");
}
void loop()
{
taskManager.runLoop();
}