From d4f46e1ce85af7fb2d0095844362374aee737a35 Mon Sep 17 00:00:00 2001 From: Renzo Mischianti Date: Mon, 5 Oct 2020 08:13:38 +0200 Subject: [PATCH] Add digital write all and fix all examples with test of pcf8574 start --- PCF8574.cpp | 16 +-- .../Arduino4Leds4ButtonsWithInterrupt.ino | 7 +- examples/blinkOnPin0/blinkOnPin0.ino | 9 +- .../encoderWithBasicLibraryFunction.ino | 7 +- .../encoderWithFullLibraryFunction.ino | 7 +- examples/interruptWemos/interruptWemos.ino | 9 +- examples/keyPressedPin1/keyPressedPin1.ino | 8 +- .../keyPressedPin1_storedOnBuffer_async.ino | 8 +- .../keyPressed_withInterrupt.ino | 8 +- .../ledEsp32OnTheSecondI2C.ino | 9 +- examples/ledWemos/ledWemos.ino | 8 +- .../readAll_Interrupt/readAll_Interrupt.ino | 9 +- .../readAll_Interrupt_lowMemory.ino | 8 +- examples/writeAllEsp8266/writeAllEsp8266.ino | 101 ++++++++++++++++++ 14 files changed, 194 insertions(+), 20 deletions(-) create mode 100644 examples/writeAllEsp8266/writeAllEsp8266.ino diff --git a/PCF8574.cpp b/PCF8574.cpp index 4a7c1cc..ff9f0e1 100644 --- a/PCF8574.cpp +++ b/PCF8574.cpp @@ -697,14 +697,14 @@ bool PCF8574::digitalWrite(uint8_t pin, uint8_t value){ } bool PCF8574::digitalWriteAll(PCF8574::DigitalInput digitalInput){ - setVal(0, digitalInput.p0); - setVal(1, digitalInput.p1); - setVal(2, digitalInput.p2); - setVal(3, digitalInput.p3); - setVal(4, digitalInput.p4); - setVal(5, digitalInput.p5); - setVal(6, digitalInput.p6); - setVal(7, digitalInput.p7); + setVal(P0, digitalInput.p0); + setVal(P1, digitalInput.p1); + setVal(P2, digitalInput.p2); + setVal(P3, digitalInput.p3); + setVal(P4, digitalInput.p4); + setVal(P5, digitalInput.p5); + setVal(P6, digitalInput.p6); + setVal(P7, digitalInput.p7); return digitalWriteAllBytes(writeByteBuffered); } diff --git a/examples/Arduino4Leds4ButtonsWithInterrupt/Arduino4Leds4ButtonsWithInterrupt.ino b/examples/Arduino4Leds4ButtonsWithInterrupt/Arduino4Leds4ButtonsWithInterrupt.ino index 8ca50a7..5dc9502 100644 --- a/examples/Arduino4Leds4ButtonsWithInterrupt/Arduino4Leds4ButtonsWithInterrupt.ino +++ b/examples/Arduino4Leds4ButtonsWithInterrupt/Arduino4Leds4ButtonsWithInterrupt.ino @@ -31,7 +31,12 @@ void setup() pcf8574.pinMode(P5, OUTPUT); pcf8574.pinMode(P4, OUTPUT, LOW); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } timeElapsed = millis(); } diff --git a/examples/blinkOnPin0/blinkOnPin0.ino b/examples/blinkOnPin0/blinkOnPin0.ino index b1dcf98..4e4023e 100644 --- a/examples/blinkOnPin0/blinkOnPin0.ino +++ b/examples/blinkOnPin0/blinkOnPin0.ino @@ -14,11 +14,18 @@ PCF8574 pcf8574(0x39); void setup() { Serial.begin(115200); + delay(1000); // Set pinMode to OUTPUT pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); - pcf8574.begin(); + + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } void loop() diff --git a/examples/encoderWithBasicLibraryFunction/encoderWithBasicLibraryFunction.ino b/examples/encoderWithBasicLibraryFunction/encoderWithBasicLibraryFunction.ino index 56c009f..1f383e8 100644 --- a/examples/encoderWithBasicLibraryFunction/encoderWithBasicLibraryFunction.ino +++ b/examples/encoderWithBasicLibraryFunction/encoderWithBasicLibraryFunction.ino @@ -49,7 +49,12 @@ void setup() pcf8574.setLatency(0); // Start library - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } bool changed = false; diff --git a/examples/encoderWithFullLibraryFunction/encoderWithFullLibraryFunction.ino b/examples/encoderWithFullLibraryFunction/encoderWithFullLibraryFunction.ino index 0a08486..6b1919d 100644 --- a/examples/encoderWithFullLibraryFunction/encoderWithFullLibraryFunction.ino +++ b/examples/encoderWithFullLibraryFunction/encoderWithFullLibraryFunction.ino @@ -44,7 +44,12 @@ void setup() pcf8574.pinMode(P2, INPUT); // Start library - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } diff --git a/examples/interruptWemos/interruptWemos.ino b/examples/interruptWemos/interruptWemos.ino index a9def81..afad55c 100644 --- a/examples/interruptWemos/interruptWemos.ino +++ b/examples/interruptWemos/interruptWemos.ino @@ -42,13 +42,20 @@ void keyPressedOnPCF8574(){ void setup() { Serial.begin(9600); + delay(1000); + pinMode(ESP8266_INTERRUPTED_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(ESP8266_INTERRUPTED_PIN), keyPressedOnPCF8574, FALLING); for(int i=0;i<8;i++) { pcf8574.pinMode(i, INPUT); } - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } void loop() diff --git a/examples/keyPressedPin1/keyPressedPin1.ino b/examples/keyPressedPin1/keyPressedPin1.ino index 8dc2cdc..d1618a0 100644 --- a/examples/keyPressedPin1/keyPressedPin1.ino +++ b/examples/keyPressedPin1/keyPressedPin1.ino @@ -14,11 +14,17 @@ unsigned long timeElapsed; void setup() { Serial.begin(115200); + delay(1000); pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } void loop() diff --git a/examples/keyPressedPin1_storedOnBuffer_async/keyPressedPin1_storedOnBuffer_async.ino b/examples/keyPressedPin1_storedOnBuffer_async/keyPressedPin1_storedOnBuffer_async.ino index b96c046..451cf58 100644 --- a/examples/keyPressedPin1_storedOnBuffer_async/keyPressedPin1_storedOnBuffer_async.ino +++ b/examples/keyPressedPin1_storedOnBuffer_async/keyPressedPin1_storedOnBuffer_async.ino @@ -14,11 +14,17 @@ unsigned long timeElapsed; void setup() { Serial.begin(115200); + delay(1000); pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } timeElapsed = millis(); } diff --git a/examples/keyPressed_withInterrupt/keyPressed_withInterrupt.ino b/examples/keyPressed_withInterrupt/keyPressed_withInterrupt.ino index 2292b17..08a3be5 100644 --- a/examples/keyPressed_withInterrupt/keyPressed_withInterrupt.ino +++ b/examples/keyPressed_withInterrupt/keyPressed_withInterrupt.ino @@ -20,10 +20,16 @@ unsigned long timeElapsed; void setup() { Serial.begin(115200); + delay(1000); pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } timeElapsed = millis(); diff --git a/examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino b/examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino index 87f7945..b6b71ec 100644 --- a/examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino +++ b/examples/ledEsp32OnTheSecondI2C/ledEsp32OnTheSecondI2C.ino @@ -46,12 +46,19 @@ void setup() Serial.begin(112560); I2Cone.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency + delay(1000); // Set pinMode to OUTPUT for(int i=0;i<8;i++) { pcf8574.pinMode(i, OUTPUT); } - pcf8574.begin(); + + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } void loop() diff --git a/examples/ledWemos/ledWemos.ino b/examples/ledWemos/ledWemos.ino index ea5e8cb..bfa87e7 100644 --- a/examples/ledWemos/ledWemos.ino +++ b/examples/ledWemos/ledWemos.ino @@ -31,12 +31,18 @@ PCF8574 pcf8574(0x20); void setup() { Serial.begin(9600); + delay(1000); // Set pinMode to OUTPUT for(int i=0;i<8;i++) { pcf8574.pinMode(i, OUTPUT); } - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } } void loop() diff --git a/examples/readAll_Interrupt/readAll_Interrupt.ino b/examples/readAll_Interrupt/readAll_Interrupt.ino index bd9df96..9b8d004 100644 --- a/examples/readAll_Interrupt/readAll_Interrupt.ino +++ b/examples/readAll_Interrupt/readAll_Interrupt.ino @@ -20,11 +20,18 @@ unsigned long timeElapsed; void setup() { Serial.begin(115200); + delay(1000); pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P1, INPUT); pcf8574.pinMode(P2, INPUT); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } + Serial.println("START"); diff --git a/examples/readAll_Interrupt_lowMemory/readAll_Interrupt_lowMemory.ino b/examples/readAll_Interrupt_lowMemory/readAll_Interrupt_lowMemory.ino index 776b748..568a6ea 100644 --- a/examples/readAll_Interrupt_lowMemory/readAll_Interrupt_lowMemory.ino +++ b/examples/readAll_Interrupt_lowMemory/readAll_Interrupt_lowMemory.ino @@ -24,11 +24,17 @@ unsigned long timeElapsed; void setup() { Serial.begin(115200); + delay(1000); pcf8574.pinMode(P0, INPUT); pcf8574.pinMode(P1, INPUT); pcf8574.pinMode(P2, INPUT); - pcf8574.begin(); + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } Serial.println("START"); diff --git a/examples/writeAllEsp8266/writeAllEsp8266.ino b/examples/writeAllEsp8266/writeAllEsp8266.ino new file mode 100644 index 0000000..6bdcbff --- /dev/null +++ b/examples/writeAllEsp8266/writeAllEsp8266.ino @@ -0,0 +1,101 @@ +/* + KeyPressed with interrupt and digital write all + from P4 to P7 + by Mischianti Renzo + + https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/ +*/ + +#include "Arduino.h" +#include "PCF8574.h" + +// For arduino uno only pin 1 and 2 are interrupted +#define ARDUINO_UNO_INTERRUPTED_PIN D3 + +// Function interrupt +void ICACHE_RAM_ATTR keyPressedOnPCF8574(); + +// Set i2c address +PCF8574 pcf8574(0x38, ARDUINO_UNO_INTERRUPTED_PIN, keyPressedOnPCF8574); +unsigned long timeElapsed; +void setup() +{ + Serial.begin(115200); + delay(1000); + Serial.println("INIT"); + + pcf8574.pinMode(P0, INPUT); + pcf8574.pinMode(P1, INPUT_PULLUP); + pcf8574.pinMode(P2, INPUT); + pcf8574.pinMode(P3, INPUT); + + pcf8574.pinMode(P7, OUTPUT); + pcf8574.pinMode(P6, OUTPUT, HIGH); + pcf8574.pinMode(P5, OUTPUT, LOW); + pcf8574.pinMode(P4, OUTPUT, LOW); + + Serial.print("Init pcf8574..."); + if (pcf8574.begin()){ + Serial.println("OK"); + }else{ + Serial.println("KO"); + } + + Serial.println("START"); + timeElapsed = millis(); +} +unsigned long lastSendTime = 0; // last send time +unsigned long interval = 3000; // interval between sends + +bool startVal = HIGH; + +bool keyPressed = false; +void loop() +{ + if (keyPressed){ + uint8_t val0 = pcf8574.digitalRead(P0); + uint8_t val1 = pcf8574.digitalRead(P1); + uint8_t val2 = pcf8574.digitalRead(P2); + uint8_t val3 = pcf8574.digitalRead(P3); + Serial.print("P0 "); + Serial.print(val0); + Serial.print(" P1 "); + Serial.println(val1); + Serial.print("P2 "); + Serial.print(val2); + Serial.print(" P3 "); + Serial.println(val3); + keyPressed= false; + + + } + + if (millis() - lastSendTime > interval) { + Serial.print("WRITE VALUE FROM P4 TO P7 "); + Serial.println(startVal); + +// pcf8574.digitalWrite(P7, startVal); + bool startVal2 = LOW; + if (startVal==HIGH) { + startVal = LOW; + startVal2 = HIGH; + }else{ + startVal = HIGH; + startVal2 = LOW; + } + PCF8574::DigitalInput digitalInput; + digitalInput.p4 = startVal2; + digitalInput.p5 = startVal; + digitalInput.p6 = startVal2; + digitalInput.p7 = startVal; + + pcf8574.digitalWriteAll(digitalInput); + lastSendTime = millis(); + } +} + +void keyPressedOnPCF8574(){ + // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library) + keyPressed = true; + +}