Skip to content

Commit

Permalink
fw: mcp23017-arduino-test - finish test firmware for the MCP23017, mo…
Browse files Browse the repository at this point in the history
…dify the library for ESP32
  • Loading branch information
JarekParal committed Jun 20, 2018
1 parent e82cbcf commit f58d9fc
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions fw/mcp23017-arduino-test/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
127 changes: 121 additions & 6 deletions fw/mcp23017-arduino-test/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit f58d9fc

Please sign in to comment.