From f58d9fc4af433167a59b5f2c607af4b43268f948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarek=20P=C3=A1ral?= Date: Wed, 20 Jun 2018 23:57:43 +0200 Subject: [PATCH] fw: mcp23017-arduino-test - finish test firmware for the MCP23017, modify the library for ESP32 --- .../Adafruit_MCP23017.cpp | 10 +- .../Adafruit_MCP23017.h | 2 +- fw/mcp23017-arduino-test/platformio.ini | 5 + fw/mcp23017-arduino-test/src/main.cpp | 127 +++++++++++++++++- 4 files changed, 134 insertions(+), 10 deletions(-) diff --git a/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.cpp b/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.cpp index 94f96a0..7e58a96 100644 --- a/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.cpp +++ b/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.cpp @@ -102,14 +102,18 @@ void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t p /** * Initializes the MCP23017 given its HW selected address, see datasheet for Address selection. */ -void Adafruit_MCP23017::begin(uint8_t addr) { +void Adafruit_MCP23017::begin(uint8_t addr, int sda, int scl, uint32_t frequency) { if (addr > 7) { addr = 7; } i2caddr = addr; - Wire.begin(); - + if((sda == -1) || (scl == -1)) { + Wire.begin(); + } else { + Wire.begin(sda, scl, frequency); + } + // set defaults! // all inputs on port A and B writeRegister(MCP23017_IODIRA,0xff); diff --git a/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.h b/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.h index 3793aec..10d3466 100644 --- a/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.h +++ b/fw/mcp23017-arduino-test/lib/Adafruit-MCP23017-Arduino-Library/Adafruit_MCP23017.h @@ -30,7 +30,7 @@ class Adafruit_MCP23017 { public: - void begin(uint8_t addr); + void begin(uint8_t addr, int sda=-1, int scl=-1, uint32_t frequency=100000); void begin(void); void pinMode(uint8_t p, uint8_t d); diff --git a/fw/mcp23017-arduino-test/platformio.ini b/fw/mcp23017-arduino-test/platformio.ini index e96065e..3afc0f7 100644 --- a/fw/mcp23017-arduino-test/platformio.ini +++ b/fw/mcp23017-arduino-test/platformio.ini @@ -13,4 +13,9 @@ platform = espressif32 board = esp32dev framework = arduino +upload_speed = 921600 + +monitor_speed = 115200 +monitor_port = COM5 + ; lib_deps = https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library \ No newline at end of file diff --git a/fw/mcp23017-arduino-test/src/main.cpp b/fw/mcp23017-arduino-test/src/main.cpp index 6e8f92e..d8aa17b 100644 --- a/fw/mcp23017-arduino-test/src/main.cpp +++ b/fw/mcp23017-arduino-test/src/main.cpp @@ -16,16 +16,131 @@ Adafruit_MCP23017 mcp; +static const int i2c_addr = 1; +static const int i2c_sda = 23; +static const int i2c_scl = 22; + +static const int ea0 = 0; +static const int ea1 = 1; +static const int ea2 = 2; +static const int ea3 = 3; +static const int ea4 = 4; +static const int ea5 = 5; +static const int ea6 = 6; +static const int ea7 = 7; + +static const int eb0 = 8; +static const int eb1 = 9; +static const int eb2 = 10; +static const int eb3 = 11; +static const int eb4 = 12; +static const int eb5 = 13; +static const int eb6 = 14; +static const int eb7 = 15; + +static const int sw1 = eb0; +static const int sw2 = eb1; +static const int sw3 = eb2; +static const int led1 = eb3; +static const int led2 = eb4; +static const int led3 = eb5; +static const int led4 = eb6; +static const int esp_shutdown = eb7; + +static const int delay_change = 500; + void setup() { - mcp.begin(); // use default address 0 + // Init + mcp.begin(i2c_addr, i2c_sda, i2c_scl); + + mcp.pinMode(ea0, OUTPUT); + mcp.pinMode(ea1, OUTPUT); + mcp.pinMode(ea2, OUTPUT); + mcp.pinMode(ea3, OUTPUT); + mcp.pinMode(ea4, OUTPUT); + mcp.pinMode(ea5, OUTPUT); + mcp.pinMode(ea6, OUTPUT); + mcp.pinMode(ea7, OUTPUT); + + mcp.pinMode(sw1, INPUT); + mcp.pullUp(sw1, HIGH); + mcp.pinMode(sw2, INPUT); + mcp.pullUp(sw2, HIGH); + mcp.pinMode(sw3, INPUT); + mcp.pullUp(sw3, HIGH); + mcp.pinMode(led1, OUTPUT); + mcp.pinMode(led2, OUTPUT); + mcp.pinMode(led3, OUTPUT); + mcp.pinMode(led4, OUTPUT); + + Serial.begin(115200); - mcp.pinMode(0, INPUT); - mcp.pullUp(0, HIGH); // turn on a 100K pullup internally + // Setup test + Serial.println("\n\n\tMCP23017 test\n"); + Serial.println("Led1"); + mcp.digitalWrite(led1, HIGH); + delay(delay_change); + mcp.digitalWrite(led1, LOW); + delay(delay_change); - pinMode(13, OUTPUT); // use the p13 LED as debugging + Serial.println("Led2"); + mcp.digitalWrite(led2, HIGH); + delay(delay_change); + mcp.digitalWrite(led2, LOW); + delay(delay_change); + + Serial.println("Led3"); + mcp.digitalWrite(led3, HIGH); + delay(delay_change); + mcp.digitalWrite(led3, LOW); + delay(delay_change); + + Serial.println("Led4"); + mcp.digitalWrite(led4, HIGH); + delay(delay_change); + mcp.digitalWrite(led4, LOW); + delay(delay_change); } void loop() { - // The LED will 'echo' the button - digitalWrite(13, mcp.digitalRead(0)); + mcp.digitalWrite(led2, LOW); + mcp.digitalWrite(led3, LOW); + mcp.digitalWrite(led4, LOW); + if(mcp.digitalRead(sw1) == false) { + mcp.digitalWrite(led4, HIGH); + } + if(mcp.digitalRead(sw2) == false) { + mcp.digitalWrite(led3, HIGH); + } + if(mcp.digitalRead(sw3) == false) { + mcp.digitalWrite(led2, HIGH); + } + + delay(100); + + Serial.println("High"); + mcp.digitalWrite(ea0, HIGH); + mcp.digitalWrite(ea1, HIGH); + mcp.digitalWrite(ea2, HIGH); + mcp.digitalWrite(ea3, HIGH); + mcp.digitalWrite(ea4, HIGH); + mcp.digitalWrite(ea5, HIGH); + mcp.digitalWrite(ea6, HIGH); + mcp.digitalWrite(ea7, HIGH); + + mcp.digitalWrite(led1, HIGH); + + delay(100); + + Serial.println("Low"); + mcp.digitalWrite(ea0, LOW); + mcp.digitalWrite(ea1, LOW); + mcp.digitalWrite(ea2, LOW); + mcp.digitalWrite(ea3, LOW); + mcp.digitalWrite(ea4, LOW); + mcp.digitalWrite(ea5, LOW); + mcp.digitalWrite(ea6, LOW); + mcp.digitalWrite(ea7, LOW); + + mcp.digitalWrite(led1, LOW); } \ No newline at end of file