diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h index 1c2c570..2331715 100644 --- a/IO_EXIOExpander.h +++ b/IO_EXIOExpander.h @@ -82,31 +82,58 @@ private: void _setupDevice() { // Send digital and analogue pin counts - I2CManager.write(_i2cAddress, 3, REG_EXIOINIT, _numDigitalPins, _numAnaloguePins); - // Enable digital ports - _digitalPinBytes = (_numDigitalPins + 7) / 8 + 1; - uint8_t _enableDigitalPins[_digitalPinBytes]; - _enableDigitalPins[0] = REG_EXIODPIN; - for (uint8_t byte = 1; byte < _digitalPinBytes; byte++) { - _enableDigitalPins[byte] = 0; + uint8_t _setupBuffer[3] = {EXIOINIT, _numDigitalPins, _numAnaloguePins}; + // I2CManager.write(_i2cAddress, 3, EXIOINIT, _numDigitalPins, _numAnaloguePins); + I2CManager.write(_i2cAddress, _setupBuffer, 3, &_i2crb); + _activity = EXIODPIN; + } + + void _loop(unsigned long currentMicros) override { + if (_i2crb.status == I2C_STATUS_PENDING) return; + if (_i2crb.status == I2C_STATUS_OK) { + switch(_activity) { + case EXIODPIN: + // Enable digital ports + _digitalPinBytes = (_numDigitalPins + 7) / 8 + 1; + // uint8_t _enableDigitalPins[_digitalPinBytes]; + // _enableDigitalPins[0] = EXIODPIN; + _digitalOutBuffer[0] = EXIODPIN; + for (uint8_t byte = 1; byte < _digitalPinBytes; byte++) { + // _enableDigitalPins[byte] = 0; + _digitalOutBuffer[byte] = 0; + } + for (uint8_t pin = 0; pin < _numDigitalPins; pin++) { + int pinByte = pin / 8; + // bitSet(_enableDigitalPins[pinByte + 1], pin - pinByte * 8); + bitSet(_digitalOutBuffer[pinByte + 1], pin - pinByte * 8); + } + // I2CManager.write(_i2cAddress, _enableDigitalPins, _digitalPinBytes, &_i2crb); + I2CManager.write(_i2cAddress, _digitalOutBuffer, _digitalPinBytes, &_i2crb); + _activity = EXIOAPIN; + break; + case EXIOAPIN: + // Enable analogue ports + _analoguePinBytes = (_numAnaloguePins + 7) / 8 + 1; + // uint8_t _enableAnaloguePins[_analoguePinBytes]; + // _enableAnaloguePins[0] = EXIOAPIN; + _analogueOutBuffer[0] = EXIOAPIN; + for (uint8_t byte = 1; byte < _analoguePinBytes; byte++) { + // _enableAnaloguePins[byte] = 0; + _analogueOutBuffer[byte] = 0; + } + for (uint8_t pin = 0; pin < _numAnaloguePins; pin++) { + int pinByte = pin / 8; + // bitSet(_enableAnaloguePins[pinByte + 1], pin - pinByte * 8); + bitSet(_analogueOutBuffer[pinByte + 1], pin - pinByte * 8); + } + // I2CManager.write(_i2cAddress, _enableAnaloguePins, _analoguePinBytes, &_i2crb); + I2CManager.write(_i2cAddress, _analogueOutBuffer, _analoguePinBytes, &_i2crb); + _activity = EXIORDY; + break; + default: + break; + } } - for (uint8_t pin = 0; pin < _numDigitalPins; pin++) { - int pinByte = pin / 8; - bitSet(_enableDigitalPins[pinByte + 1], pin - pinByte * 8); - } - I2CManager.write(_i2cAddress, _enableDigitalPins, _digitalPinBytes, &_i2crb); - // Enable analogue ports - _analoguePinBytes = (_numAnaloguePins + 7) / 8 + 1; - uint8_t _enableAnaloguePins[_analoguePinBytes]; - _enableAnaloguePins[0] = REG_EXIOAPIN; - for (uint8_t byte = 1; byte < _analoguePinBytes; byte++) { - _enableAnaloguePins[byte] = 0; - } - for (uint8_t pin = 0; pin < _numAnaloguePins; pin++) { - int pinByte = pin / 8; - bitSet(_enableAnaloguePins[pinByte + 1], pin - pinByte * 8); - } - I2CManager.write(_i2cAddress, _enableAnaloguePins, _analoguePinBytes, &_i2crb); } void _display() override { @@ -119,15 +146,20 @@ private: uint8_t _numAnaloguePins; int _digitalPinBytes; int _analoguePinBytes; + uint8_t _digitalOutBuffer[EXIO_NANO_DIGITAL_PINS + 1]; + uint8_t _digitalInBufer[EXIO_NANO_DIGITAL_PINS]; + uint8_t _analogueOutBuffer[EXIO_NANO_ANALOGUE_PINS + 1]; + uint8_t _analogueInBuffer[EXIO_NANO_ANALOGUE_PINS]; + uint8_t _activity; I2CRB _i2crb; enum { - REG_EXIOINIT = 0xE0, // Flag to initialise setup procedure - REG_EXIODPIN = 0xE1, // Flag we're sending digital pin assignments - REG_EXIOAPIN = 0xE2, // Flag we're sending analogue pin assignments - REG_EXIORDY = 0xE3, // Flag we have completed setup procedure, also for EX-IO to ACK setup - REG_EXIODDIR = 0xE4, // Flag we're sending digital pin direction configuration - REG_EXIODPUP = 0xE5, // Flag we're sending digital pin pullup configuration + EXIOINIT = 0xE0, // Flag to initialise setup procedure + EXIODPIN = 0xE1, // Flag we're sending digital pin assignments + EXIOAPIN = 0xE2, // Flag we're sending analogue pin assignments + EXIORDY = 0xE3, // Flag we have completed setup procedure, also for EX-IO to ACK setup + EXIODDIR = 0xE4, // Flag we're sending digital pin direction configuration + EXIODPUP = 0xE5, // Flag we're sending digital pin pullup configuration }; };