From 00341fa776ba37161a0ebd1962c6c311dc3a4a7e Mon Sep 17 00:00:00 2001 From: Porter Fencl Date: Mon, 29 Sep 2025 18:14:59 -0500 Subject: [PATCH] finished new member task 1 --- src/main.cpp | 11 +++++- src/mcp23017.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c58189e..bf59117 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ #endif // TODO: declare mcp23017 object - +Mcp23017 obj1(0x20); void setup() { // initialize Serial for printouts Serial.begin(115200); @@ -23,8 +23,17 @@ void setup() { //Make sure to check if it is an Active-High or Active-Low reset #endif // TODO: initialize I2C and mcp23017 object + Wire.begin(); + uint8_t numbers[8] = {0, 0, 0, 0, 0, 1, 0, 0}; + obj1.begin(numbers); } void loop() { // TODO: Write tests here + obj1.set_state(7, 1); + Serial.println(obj1.get_state(5)); + delay(1000); + obj1.set_state(7, 0); + delay(1000); + Serial.println(obj1.get_state(5)); } \ No newline at end of file diff --git a/src/mcp23017.cpp b/src/mcp23017.cpp index 9cb2515..79a8655 100644 --- a/src/mcp23017.cpp +++ b/src/mcp23017.cpp @@ -5,26 +5,95 @@ // TODO: Initialize i2cBus member Mcp23017::Mcp23017(int addr) { - + this->addr = addr; } uint8_t Mcp23017::get_dir(int pin) { - return 0; + + //gets information from Mcp23017 register 0x00 + Wire.beginTransmission(addr); + Wire.write(0x00); + Wire.endTransmission(); + + //requests 1 byte from register 0x00 in Mcp23017 + Wire.requestFrom(addr, 1); + + //reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0 + return (Wire.read() >> pin) & 1; + } // TODO: Read from state register uint8_t Mcp23017::get_state(int pin) { - return 0; + + Wire.beginTransmission(addr); + Wire.write(0x12); + Wire.endTransmission(); + + Wire.requestFrom(addr, 1); + + //reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0 + return (Wire.read() >> pin) & 1; + + + } // TODO: Write to directions register +// 1: input 0: output int Mcp23017::set_dir(int pin, uint8_t dir) { + // calls IODIRA for the read + Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0 + Wire.write(0x00); // writes the register offset + Wire.endTransmission(); + + //reads the byte value from the register + uint8_t number; + Wire.requestFrom(addr, 1); + number = Wire.read(); + + // changes the byte from the register that will convert the correct pin to a 1 or 0 + if (dir == 1) { + number = number | (1 << pin); // number is all 0s except a 1 in the input pin position + } else { + number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position + } + + //writes the new register data including the changed pin back to the register + Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0 + Wire.write(0x00); // writes the register offset + Wire.write(number); // writes the new data + Wire.endTransmission(); + return 0; } // TODO: Write to state register int Mcp23017::set_state(int pin, uint8_t val) { + // calls GPIO for the read + Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0 + Wire.write(0x12); // writes the register offset + Wire.endTransmission(); + + //reads the byte value from the register + uint8_t number; + Wire.requestFrom(addr, 1); + number = Wire.read(); + + // changes the byte from the register that will convert the correct pin to a 1 or 0 + if (val == 1) { + number = number | (1 << pin); // number is all 0s except a 1 in the input pin position + } else { + number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position + } + + //writes the new register data including the changed pin back to the register + Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0 + Wire.write(0x12); // writes the register offset + Wire.write(number); // writes the new data + Wire.endTransmission(); + return 0; } @@ -34,6 +103,23 @@ int Mcp23017::begin(uint8_t directions[8]) { int rc; // TODO: Add device ID check + Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0 + Wire.write(0x00); // writes the register offset + Wire.endTransmission(); + + // sets each bit in IODIRA according to the bits in directions + Wire.requestFrom(addr, 1); + uint8_t s = Wire.read(); + Serial.printf("%d, %d\n",s, addr); + if (s == 0xFF) { // if the read is all 1s then the default value is correct + for (int i = 0; i < 8; i++) { + this->set_dir(i, directions[i]); + } + return 0; + } else { + return 1; // returns 1 if the default value is wrong + } + - return 0; + }