From fd0c67dd94534a29f3615581bb972f2c2bcec5c4 Mon Sep 17 00:00:00 2001 From: Wenmiaojia Date: Mon, 29 Sep 2025 18:24:17 -0500 Subject: [PATCH 1/2] new member task1 --- src/main.cpp | 39 ++++++++++++++++++++++++-- src/mcp23017.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c58189e..f8bc808 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,30 +1,63 @@ #include #include "mcp23017.h" +#include //Ignore the compiler instruction related code (the ones that start with #) if you are not attempting the optional section //Uncomment the statement below this line to work on the optional section -// #define EXTERNAL_RESET_ENABLED +//#define EXTERNAL_RESET_ENABLED #ifdef EXTERNAL_RESET_ENABLED #define RESET_PIN ______ //TODO: Add the pin thats doing the reset #endif // TODO: declare mcp23017 object +Mcp23017 mcp(0x20); // Example declaration, modify as needed void setup() { // initialize Serial for printouts Serial.begin(115200); printf("Test begins\n"); + #ifdef EXTERNAL_RESET_ENABLED //TODO: Configure the digital pin acting as a reset an output pin and reset the IOExpander registers to their default values. //Make sure to check if it is an Active-High or Active-Low reset #endif // TODO: initialize I2C and mcp23017 object + Wire.begin(); // Initialize I2C + // Initialize MCP23017 + uint8_t directions[8] = { + 1, // pin 0 = input + 1, // pin 1 = input (switch) + 1, + 1, + 1, + 1, // pin 5 = input + 1, + 0 // pin 7 = output (LED) + }; + mcp.begin(directions); // Set pin directions } + void loop() { - // TODO: Write tests here -} \ No newline at end of file + mcp.set_state(7, HIGH); + Serial.println("LED ON"); + delay(1000); + + mcp.set_state(7, LOW); + Serial.println("LED OFF"); + delay(1000); + + // Read switch on pin 1 + int switchState = mcp.get_state(5); + Serial.print("Switch state: "); + Serial.println(switchState); + + delay(500); +} + + + diff --git a/src/mcp23017.cpp b/src/mcp23017.cpp index 9cb2515..4685e43 100644 --- a/src/mcp23017.cpp +++ b/src/mcp23017.cpp @@ -1,30 +1,68 @@ #include "mcp23017.h" // TODO (optional): Define macros for useful register below: - +#define IODIRREG 0x00 // I/O Direction Register +#define GPIOREG 0x12 // GPIO Register // TODO: Initialize i2cBus member Mcp23017::Mcp23017(int addr) { + this->addr = addr; + + } uint8_t Mcp23017::get_dir(int pin) { - return 0; + Wire.beginTransmission(addr); + Wire.write(IODIRREG); // Point to IODIR register + Wire.endTransmission(); + Wire.requestFrom(addr, 1); // Request 1 byte + uint8_t diReg = Wire.read(); + + return (diReg >> pin) & 0x01; } // TODO: Read from state register uint8_t Mcp23017::get_state(int pin) { - return 0; + Wire.beginTransmission(addr); // Point to GPIO register + Wire.write(GPIOREG); + Wire.endTransmission(); + Wire.requestFrom(addr, 1); // Request 1 byte + uint8_t gpioreg = Wire.read(); + return (gpioreg >> pin) & 0x01; } + // TODO: Write to directions register int Mcp23017::set_dir(int pin, uint8_t dir) { + uint8_t diReg = get_dir(pin); // read current direction register + if(dir ==1) { + diReg |= (1 << pin); // set pin to input + } else { + diReg &= ~(1 << pin); // set pin to output + } + Wire.beginTransmission(addr); + Wire.write(IODIRREG); // Point to IODIR register + Wire.write(diReg); // Write updated direction + Wire.endTransmission(); + return 0; } // TODO: Write to state register int Mcp23017::set_state(int pin, uint8_t val) { + uint8_t gpioreg = get_state(pin); // read current state register + if(val ==1) { + gpioreg |= (1 << pin); // set pin high + } else { + gpioreg &= ~(1 << pin); // set pin low + + } + Wire.beginTransmission(addr); + Wire.write(GPIOREG); // Point to GPIO register + Wire.write(gpioreg); // Write updated state + Wire.endTransmission(); return 0; } @@ -34,6 +72,35 @@ int Mcp23017::begin(uint8_t directions[8]) { int rc; // TODO: Add device ID check + uint8_t iodira_val = 0; + for(int i=0; i<8; i++) { + iodira_val |= (get_dir(i) << i); + //iodira_val = (iodira_val << 1) | (get_dir(i)); + } + + Serial.printf("IODIRA register value: 0x%02X\n", iodira_val); + + if(iodira_val != 0xFF) { + return 1; // Device ID mismatch + } + + uint8_t dirByte = 0; + for(int i=0; i<8; i++) { + dirByte |= (directions[i] << i); + } + Serial.printf("Direction byte: 0x%02X\n", dirByte); + Wire.beginTransmission(addr); + Wire.write(IODIRREG); // Point to IODIR register + Wire.write(dirByte); // Set directions + Wire.endTransmission(); + + + iodira_val = 0; + for(int i=0; i<8; i++) { + iodira_val |= (get_dir(i) << i); + //iodira_val = (iodira_val << 1) | (get_dir(i)); + } + Serial.printf("MCP23017 initialized at address 0x%02X\n", iodira_val); return 0; } From 2b3336e73d4ad28c77ad83334c156ffba6fb3331 Mon Sep 17 00:00:00 2001 From: Wenmiaojia Date: Mon, 29 Sep 2025 18:33:09 -0500 Subject: [PATCH 2/2] Add tft-display-tutorial project --- tft-display-tutorial | 1 + 1 file changed, 1 insertion(+) create mode 160000 tft-display-tutorial diff --git a/tft-display-tutorial b/tft-display-tutorial new file mode 160000 index 0000000..3173dd6 --- /dev/null +++ b/tft-display-tutorial @@ -0,0 +1 @@ +Subproject commit 3173dd637ee6655e6ac6ed7a78c816eb2e3e5285