Skip to content

Commit

Permalink
Implement MCP23017 expander
Browse files Browse the repository at this point in the history
  • Loading branch information
VIPQualityPost committed Feb 5, 2024
1 parent 74d7c7a commit b46fc2d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/ayab/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/ayab/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
* http://ayab-knitting.com
*/

#include "board.h"
#include <Arduino.h>

#include "board.h"
#include "encoders.h"


Expand Down
29 changes: 29 additions & 0 deletions src/ayab/solenoids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@
* \brief Initialize I2C connection for solenoids.
*/
void Solenoids::init() {
#ifdef HAS_MCP23008
mcp_0.begin(I2Caddr_sol1_8);
mcp_1.begin(I2Caddr_sol9_16);

for (uint8_t i = 0; i < SOLENOID_BUFFER_SIZE / 2; i++) {
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;
}

Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/ayab/solenoids.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "encoders.h"
#include <Arduino.h>
#include <Adafruit_MCP23008.h>
#include <Adafruit_MCP23X17.h>
#include <Wire.h>

// Different machines have a different number of solenoids.
Expand Down Expand Up @@ -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_

0 comments on commit b46fc2d

Please sign in to comment.