Initial source code

This commit is contained in:
toptah 2025-08-31 17:37:14 +02:00
parent 17664ffe21
commit 1f3d61f67f
3 changed files with 139 additions and 0 deletions

7
src/Config.ino Normal file
View File

@ -0,0 +1,7 @@
//SCL D1
//SCA D2
#define NEOPIXEL_PIN D6
#define BRIGHTNESS 100

84
src/KeyPatch.ino Normal file
View File

@ -0,0 +1,84 @@
#include <Wire.h>
#include <PCF8575.h>
#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);
}

48
src/NeoPixelHandler.ino Normal file
View File

@ -0,0 +1,48 @@
#include <Adafruit_NeoPixel.h>
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
}
}