This may just be me sucking at C++, but I’m having a terrible time attempting to Init() a MCP23017 class with non-default pin assignments.
My understanding of transports and I2C handles is shaky at best, and most of my coding expertise is C# and Python (VFX and game dev) so I’m probably missing something obvious and could use a little help connecting the dots. This is what I’m trying to do:
- Instantiate a Mcp23017 object with SCL and SDA on Daisy Seed pins D11 and D12 respectively.
2 Write a bool blinkState to MCP pin GPA7 that goes between true/false every 200ms.
This is the code:
/*
Simple blink test for mcp23017.
*/
#include "daisy_seed.h"
#include "daisysp.h"
#define SCL_PIN {DSY_GPIOB, 11}
#define SDA_PIN {DSY_GPIOB, 12}
using namespace daisy;
using namespace daisysp;
using namespace daisy::seed;
DaisySeed hw;
Mcp23017 mcp; // Do I init pin numbers and other I2CHandle params here?
const uint8_t LED7 = 7;
const uint8_t SW = 6; // encoder button, not yet implemented
bool blinkState = false;
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size)
{
for (size_t i = 0; i < size; i++)
{
out[0][i] = in[0][i];
out[1][i] = in[1][i];
}
}
int main(void)
{
hw.Init();
mcp.Init();
mcp.PortMode(MCPPort::A, 0xff, false); //All inputs, no invert, GPA7 is output only
mcp.PortMode(MCPPort::B, 0xff, false);
mcp.PinMode(LED7, MCPMode::OUTPUT, true);
// mcp.PinMode(SW, MCPMode::INPUT, false );
bool cur_blinkState = false;
//Init before or after all the stuff is set?
hw.StartLog(true);
// hw.SetAudioBlockSize(4); // number of samples handled per callback
// hw.SetAudioSampleRate(SaiHandle::Config::SampleRate::SAI_48KHZ);
// // hw.StartAudio(AudioCallback);
// Logger<LOGGER_INTERNAL>::PrintLine("mcp: 0x%#02x", mcp.GetPin(LED7));
while(1) {
mcp.Read();
System::Delay(200);
blinkState = !blinkState;
// blinkState = mcp.ReadPin(SW);
// if (blinkState != cur_blinkState)
// mcp.WritePin(LED7, blinkState); // Also not implemented.
// With oscilloscope probes connected to LED7 and GND,
// I would expect to see a square wave on the screen.
// If I hack the .h file's defaults to the pins I want, that happens.
mcp.WritePin(LED7, blinkState);
}
}
The only thing that made even something remotely interesting happen was hacking the Mcp23017.h file and setting the defaults to the pins I needed:
class Mcp23017Transport
{
public:
struct Config
{
I2CHandle::Config i2c_config;
uint8_t i2c_address;
void Defaults()
{
i2c_config.periph = I2CHandle::Config::Peripheral::I2C_1;
i2c_config.speed = I2CHandle::Config::Speed::I2C_1MHZ;
i2c_config.mode = I2CHandle::Config::Mode::I2C_MASTER;
i2c_config.pin_config.scl = {DSY_GPIOB, 11};
i2c_config.pin_config.sda = {DSY_GPIOB, 12};
i2c_address = 0x27;
}
};
...
And here is where I hit the wall of my C++ skills. I should be able to set the public struct members to whatever I want, right? The eventual goal of this exercise is a eurorack module with 4 pushbutton/encoders and a OLED display.
I’ve successfully implemented (software and hardware) 5-pin DIN MIDI and eurorack-level audio with TL074’s, but I’m stumped on this one and I need it to stand up the 4 encoders.
Thanks in advance