diff --git a/src/ayab/board.h b/src/ayab/board.h index b2027d4e..9228543b 100644 --- a/src/ayab/board.h +++ b/src/ayab/board.h @@ -32,12 +32,14 @@ constexpr uint8_t DBG_BTN_PIN = 7; // DEBUG BUTTON constexpr uint8_t I2Caddr_sol1_8 = 0x0U; ///< I2C Address of solenoids 1 - 8 constexpr uint8_t I2Caddr_sol9_16 = 0x1U; ///< I2C Address of solenoids 9 - 16 +constexpr uint8_t MCP23017_ADDR_0 = 0x0U; // I2C address of expander on ayab-esp32 (16-wide) -// TODO(Who?): Optimize Delay for various Arduino Models +// TODO(Who?): Optimize Delay for various Arduino Models (does ESP32 need this? Probably no?) constexpr uint16_t START_KNITTING_DELAY = 2000U; // ms // Determine board type #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) +#define HAS_MCP23008 // Arduino Uno // Pin Assignments constexpr uint8_t EOL_PIN_R = A0; // Analog @@ -54,6 +56,8 @@ constexpr uint8_t LED_PIN_B = 6; // yellow LED constexpr uint8_t PIEZO_PIN = 9; #elif defined(CONFIG_IDF_TARGET_ESP32S3)// ESP32-S3 (AYAB-ESP32) +#define HAS_MCP23017 + // Knitting machine control constexpr uint8_t EOL_PIN_R_N = 3; // Right EOL marker, each polarity. constexpr uint8_t EOL_PIN_R_S = 4; @@ -81,9 +85,6 @@ constexpr uint8_t I2C_PIN_SDA = 15; // External bus for user applications. constexpr uint8_t I2C_PIN_SCL = 16; // I2C1 // Misc I/O -#define LED_PIN_A LED_PIN_G -#define LED_PIN_B LED_PIN_R - constexpr uint8_t LED_PIN_R = 33; constexpr uint8_t LED_PIN_G = 34; constexpr uint8_t LED_PIN_B = 35; diff --git a/src/ayab/encoders.cpp b/src/ayab/encoders.cpp index 89ec47d9..8094f41c 100644 --- a/src/ayab/encoders.cpp +++ b/src/ayab/encoders.cpp @@ -23,9 +23,8 @@ * http://ayab-knitting.com */ -#include "board.h" #include - +#include "board.h" #include "encoders.h" diff --git a/src/ayab/solenoids.cpp b/src/ayab/solenoids.cpp index b74c8124..92df7b46 100644 --- a/src/ayab/solenoids.cpp +++ b/src/ayab/solenoids.cpp @@ -29,6 +29,7 @@ * \brief Initialize I2C connection for solenoids. */ void Solenoids::init() { + #ifdef HAS_MCP23008 mcp_0.begin(I2Caddr_sol1_8); mcp_1.begin(I2Caddr_sol9_16); @@ -36,6 +37,15 @@ void Solenoids::init() { mcp_0.pinMode(i, OUTPUT); mcp_1.pinMode(i, OUTPUT); } + + #elif HAS_MCP23017 + mcp.begin_I2C(MCP23017_ADDR_0); + + for (uint8_t i = 0; i < SOLENOID_BUFFER_SIZE; i++){ + mcp.pinMode(i, OUTPUT); + } + + #endif solenoidState = 0x0000U; } @@ -95,7 +105,26 @@ void Solenoids::setSolenoids(uint16_t state) { // GCOVR_EXCL_START void Solenoids::write(uint16_t newState) { (void)newState; + #if defined(HAS_MCP23008) mcp_0.writeGPIO(lowByte(newState)); mcp_1.writeGPIO(highByte(newState)); + + #elif defined(HAS_MCP23017) + // We need to shuffle the bits around due to hardware layout. + // GPA0..8 => solenoid 8-F + // GPB0..8 => solenoid 7-0 + // Adafruit mapping: GPA0...8, GPB0..8 => 0..15 + newState = (newState << 8); + + uint8_t reversedByte = 0; + for(uint8_t i = 0; i < 8; i++){ + reversedByte[i] = (highByte(newState) >> (7-i)) & 0x01; + } + + newState = newState & reversedByte; + + mcp.writeGPIOAB(newState); + + #endif } // GCOVR_EXCL_STOP diff --git a/src/ayab/solenoids.h b/src/ayab/solenoids.h index f5c89580..5a8aa062 100644 --- a/src/ayab/solenoids.h +++ b/src/ayab/solenoids.h @@ -28,6 +28,7 @@ #include "encoders.h" #include #include +#include #include // Different machines have a different number of solenoids. @@ -84,8 +85,12 @@ class Solenoids : public SolenoidsInterface { uint16_t solenoidState = 0x0000U; void write(uint16_t state); + #if defined(HAS_MCP23008) Adafruit_MCP23008 mcp_0 = Adafruit_MCP23008(); Adafruit_MCP23008 mcp_1 = Adafruit_MCP23008(); + #elif defined(HAS_MCP23017) + Adafruit_MCP23X17 mcp = Adafruit_MCP23X17(); + #endif }; #endif // SOLENOIDS_H_