From 1f3d61f67f775bd97034a5a07ba3973761dbb970 Mon Sep 17 00:00:00 2001 From: toptah Date: Sun, 31 Aug 2025 17:37:14 +0200 Subject: [PATCH] Initial source code --- src/Config.ino | 7 ++++ src/KeyPatch.ino | 84 +++++++++++++++++++++++++++++++++++++++++ src/NeoPixelHandler.ino | 48 +++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/Config.ino create mode 100644 src/KeyPatch.ino create mode 100644 src/NeoPixelHandler.ino diff --git a/src/Config.ino b/src/Config.ino new file mode 100644 index 0000000..07dae68 --- /dev/null +++ b/src/Config.ino @@ -0,0 +1,7 @@ + +//SCL D1 +//SCA D2 + +#define NEOPIXEL_PIN D6 +#define BRIGHTNESS 100 + diff --git a/src/KeyPatch.ino b/src/KeyPatch.ino new file mode 100644 index 0000000..b09d792 --- /dev/null +++ b/src/KeyPatch.ino @@ -0,0 +1,84 @@ +#include +#include + +#define AMOUNTOFPORTS 8 // currently max 16 ports are supported +#define EXPANDER1ADDRESS 0x21 // address of expander 1 + + +PCF8575 expander1(EXPANDER1ADDRESS); +uint8_t portStates[AMOUNTOFPORTS]; +uint8_t lastStates[AMOUNTOFPORTS] = {0}; +byte number=0; + +void setup() { + Serial.begin(9600); + Serial.println("Starte 16-Port IO Erweiterung..."); + //Set UP 12C Communication + Wire.begin(); + for (byte adress=8; adress<120; adress++) + { + Wire.beginTransmission(adress); + if (Wire.endTransmission()==0) + { + Serial.print(number+1); + Serial.print(". Device: 0x"); + Serial.println(adress, HEX); + number++; + } + } + Serial.print (number, DEC); + Serial.println (" device(s) found."); + //Setup CPF Expander + + Serial.print("Expander 1 at "); + Serial.print(EXPANDER1ADDRESS,HEX); + if (!expander1.begin()) + { + Serial.println(" could not initialize..."); + } + if (!expander1.isConnected()) + { + Serial.println(" => not connected"); + } + else + { + Serial.println(" => connected!!"); + } + + + //Setup NeoPixel + NeoPixel_init(AMOUNTOFPORTS); // Initialisierung der NeoPixel + for (uint8_t i = 0; i < AMOUNTOFPORTS; i++) { + portStates[i] = 0; + lastStates[i] = 0; + NeoPixel_setState(i,1); + delay(10); + NeoPixel_setState(i,0); + }; + + + Serial.println("Setup End..."); +} + +void loop() { + // Erst 16 Ports aus PCF8575 einlesen + for (uint8_t i = 0; i < AMOUNTOFPORTS; i++) { + portStates[i] = expander1.read(i); + } + + static uint8_t lastStates[AMOUNTOFPORTS] = {0}; + for (uint8_t i = 0; i < AMOUNTOFPORTS; i++) { + if (portStates[i] != lastStates[i]) { + // Ausgabe: geänderte Ports melden + Serial.print("Pin "); + Serial.print(i); + Serial.print(" hat sich geändert -> "); + Serial.println(portStates[i]); + lastStates[i] = portStates[i]; + // Bei änderung LED anpassen + NeoPixel_setState(i, portStates[i]); // Jede LED pro Pin aktualisieren + } + } + + delay(50); +} \ No newline at end of file diff --git a/src/NeoPixelHandler.ino b/src/NeoPixelHandler.ino new file mode 100644 index 0000000..e241047 --- /dev/null +++ b/src/NeoPixelHandler.ino @@ -0,0 +1,48 @@ +#include + +Adafruit_NeoPixel* pixels = nullptr; // Zeiger, um flexible Anzahl von LEDs zu erlauben + +void NeoPixel_init(uint8_t numPixels) { + if (pixels) delete pixels; // falls schon initialisiert + pixels = new Adafruit_NeoPixel(numPixels, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); + pixels->begin(); + pixels->setBrightness(BRIGHTNESS); + pixels->show(); // Alle Pixel aus +} + + + + +void NeoPixel_setState(uint8_t ledIndex, uint8_t state) { + Serial.print("LED Update "); + Serial.print(ledIndex); + Serial.print(" new State "); + Serial.println(state); + if (!pixels) return; // Schutz, falls init noch nicht erfolgt + if (!state) { + pixels->setPixelColor(ledIndex, pixels->Color(0, 150, 0)); // grün = HIGH + } else { + pixels->setPixelColor(ledIndex, pixels->Color(150, 0, 0)); // rot = LOW + } + pixels->show(); +} + +// Rainbow cycle along whole strip. Pass delay time (in ms) between frames. +void rainbow(int wait) { + // Hue of first pixel runs 5 complete loops through the color wheel. + // Color wheel has a range of 65536 but it's OK if we roll over, so + // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time + // means we'll make 5*65536/256 = 1280 passes through this loop: + for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) { + // strip.rainbow() can take a single argument (first pixel hue) or + // optionally a few extras: number of rainbow repetitions (default 1), + // saturation and value (brightness) (both 0-255, similar to the + // ColorHSV() function, default 255), and a true/false flag for whether + // to apply gamma correction to provide 'truer' colors (default true). + pixels->rainbow(firstPixelHue); + // Above line is equivalent to: + // strip.rainbow(firstPixelHue, 1, 255, 255, true); + pixels->show(); // Update strip with new contents + delay(wait); // Pause for a moment + } +} \ No newline at end of file