diff --git a/Adafruit_MCP23017.cpp b/Adafruit_MCP23017.cpp index ee99eb8..30734b3 100755 --- a/Adafruit_MCP23017.cpp +++ b/Adafruit_MCP23017.cpp @@ -13,9 +13,9 @@ #include #ifdef __AVR - #include +#include #elif defined(ESP8266) - #include +#include #endif #include "Adafruit_MCP23017.h" @@ -26,15 +26,17 @@ #endif // minihelper to keep Arduino backward compatibility -static inline void wiresend(uint8_t x) { +static inline void wiresend(uint8_t x) +{ #if ARDUINO >= 100 - Wire.write((uint8_t) x); + Wire.write((uint8_t)x); #else Wire.send(x); #endif } -static inline uint8_t wirerecv(void) { +static inline uint8_t wirerecv(void) +{ #if ARDUINO >= 100 return Wire.read(); #else @@ -45,21 +47,24 @@ static inline uint8_t wirerecv(void) { /** * Bit number associated to a give Pin */ -uint8_t Adafruit_MCP23017::bitForPin(uint8_t pin){ - return pin%8; +uint8_t Adafruit_MCP23017::bitForPin(uint8_t pin) +{ + return pin % 8; } /** * Register address, port dependent, for a given PIN */ -uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr){ - return(pin<8) ?portAaddr:portBaddr; +uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr) +{ + return (pin < 8) ? portAaddr : portBaddr; } /** * Reads a given register */ -uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){ +uint8_t Adafruit_MCP23017::readRegister(uint8_t addr) +{ // read the current GPINTEN Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(addr); @@ -68,11 +73,11 @@ uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){ return wirerecv(); } - /** * Writes a given register */ -void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue){ +void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue) +{ // Write the register Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(regAddr); @@ -80,22 +85,22 @@ void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue){ Wire.endTransmission(); } - /** * Helper to update a single bit of an A/B register. * - Reads the current register value * - Writes the new register value */ -void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t portAaddr, uint8_t portBaddr) { +void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t portAaddr, uint8_t portBaddr) +{ uint8_t regValue; - uint8_t regAddr=regForPin(pin,portAaddr,portBaddr); - uint8_t bit=bitForPin(pin); + uint8_t regAddr = regForPin(pin, portAaddr, portBaddr); + uint8_t bit = bitForPin(pin); regValue = readRegister(regAddr); // set the value for the particular bit - bitWrite(regValue,bit,pValue); + bitWrite(regValue, bit, pValue); - writeRegister(regAddr,regValue); + writeRegister(regAddr, regValue); } //////////////////////////////////////////////////////////////////////////////// @@ -103,8 +108,10 @@ void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t p /** * Initializes the MCP23017 given its HW selected address, see datasheet for Address selection. */ -void Adafruit_MCP23017::begin(uint8_t addr) { - if (addr > 7) { +void Adafruit_MCP23017::begin(uint8_t addr) +{ + if (addr > 7) + { addr = 7; } i2caddr = addr; @@ -113,28 +120,31 @@ void Adafruit_MCP23017::begin(uint8_t addr) { // set defaults! // all inputs on port A and B - writeRegister(MCP23017_IODIRA,0xff); - writeRegister(MCP23017_IODIRB,0xff); + writeRegister(MCP23017_IODIRA, 0xff); + writeRegister(MCP23017_IODIRB, 0xff); } /** * Initializes the default MCP23017, with 000 for the configurable part of the address */ -void Adafruit_MCP23017::begin(void) { +void Adafruit_MCP23017::begin(void) +{ begin(0); } /** * Sets the pin mode to either INPUT or OUTPUT */ -void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { - updateRegisterBit(p,(d==INPUT),MCP23017_IODIRA,MCP23017_IODIRB); +void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) +{ + updateRegisterBit(p, (d == INPUT), MCP23017_IODIRA, MCP23017_IODIRB); } /** * Reads all 16 pins (port A and B) into a single 16 bits variable. */ -uint16_t Adafruit_MCP23017::readGPIOAB() { +uint16_t Adafruit_MCP23017::readGPIOAB() +{ uint16_t ba = 0; uint8_t a; @@ -156,13 +166,15 @@ uint16_t Adafruit_MCP23017::readGPIOAB() { * Read a single port, A or B, and return its current 8 bit value. * Parameter b should be 0 for GPIOA, and 1 for GPIOB. */ -uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) { +uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) +{ // read the current GPIO output latches Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); if (b == 0) wiresend(MCP23017_GPIOA); - else { + else + { wiresend(MCP23017_GPIOB); } Wire.endTransmission(); @@ -174,7 +186,8 @@ uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) { /** * Writes all the pins in one go. This method is very useful if you are implementing a multiplexed matrix and want to get a decent refresh rate. */ -void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) { +void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) +{ Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPIOA); wiresend(ba & 0xFF); @@ -182,30 +195,32 @@ void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) { Wire.endTransmission(); } -void Adafruit_MCP23017::digitalWrite(uint8_t pin, uint8_t d) { +void Adafruit_MCP23017::digitalWrite(uint8_t pin, uint8_t d) +{ uint8_t gpio; - uint8_t bit=bitForPin(pin); - + uint8_t bit = bitForPin(pin); // read the current GPIO output latches - uint8_t regAddr=regForPin(pin,MCP23017_OLATA,MCP23017_OLATB); + uint8_t regAddr = regForPin(pin, MCP23017_OLATA, MCP23017_OLATB); gpio = readRegister(regAddr); // set the pin and direction - bitWrite(gpio,bit,d); + bitWrite(gpio, bit, d); // write the new GPIO - regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB); - writeRegister(regAddr,gpio); + regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB); + writeRegister(regAddr, gpio); } -void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) { - updateRegisterBit(p,d,MCP23017_GPPUA,MCP23017_GPPUB); +void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) +{ + updateRegisterBit(p, d, MCP23017_GPPUA, MCP23017_GPPUB); } -uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin) { - uint8_t bit=bitForPin(pin); - uint8_t regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB); +uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin) +{ + uint8_t bit = bitForPin(pin); + uint8_t regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB); return (readRegister(regAddr) >> bit) & 0x1; } @@ -218,20 +233,21 @@ uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin) { * If you are connecting the INTA/B pin to arduino 2/3, you should configure the interupt handling as FALLING with * the default configuration. */ -void Adafruit_MCP23017::setupInterrupts(uint8_t mirroring, uint8_t openDrain, uint8_t polarity){ +void Adafruit_MCP23017::setupInterrupts(uint8_t mirroring, uint8_t openDrain, uint8_t polarity) +{ // configure the port A - uint8_t ioconfValue=readRegister(MCP23017_IOCONA); - bitWrite(ioconfValue,6,mirroring); - bitWrite(ioconfValue,2,openDrain); - bitWrite(ioconfValue,1,polarity); - writeRegister(MCP23017_IOCONA,ioconfValue); + uint8_t ioconfValue = readRegister(MCP23017_IOCONA); + bitWrite(ioconfValue, 6, mirroring); + bitWrite(ioconfValue, 2, openDrain); + bitWrite(ioconfValue, 1, polarity); + writeRegister(MCP23017_IOCONA, ioconfValue); // Configure the port B - ioconfValue=readRegister(MCP23017_IOCONB); - bitWrite(ioconfValue,6,mirroring); - bitWrite(ioconfValue,2,openDrain); - bitWrite(ioconfValue,1,polarity); - writeRegister(MCP23017_IOCONB,ioconfValue); + ioconfValue = readRegister(MCP23017_IOCONB); + bitWrite(ioconfValue, 6, mirroring); + bitWrite(ioconfValue, 2, openDrain); + bitWrite(ioconfValue, 1, polarity); + writeRegister(MCP23017_IOCONB, ioconfValue); } /** @@ -241,44 +257,48 @@ void Adafruit_MCP23017::setupInterrupts(uint8_t mirroring, uint8_t openDrain, ui * that caused the interrupt or you read the port itself. Check the datasheet can be confusing. * */ -void Adafruit_MCP23017::setupInterruptPin(uint8_t pin, uint8_t mode) { +void Adafruit_MCP23017::setupInterruptPin(uint8_t pin, uint8_t mode) +{ // set the pin interrupt control (0 means change, 1 means compare against given value); - updateRegisterBit(pin,(mode!=CHANGE),MCP23017_INTCONA,MCP23017_INTCONB); + updateRegisterBit(pin, (mode != CHANGE), MCP23017_INTCONA, MCP23017_INTCONB); // if the mode is not CHANGE, we need to set up a default value, different value triggers interrupt // In a RISING interrupt the default value is 0, interrupt is triggered when the pin goes to 1. // In a FALLING interrupt the default value is 1, interrupt is triggered when pin goes to 0. - updateRegisterBit(pin,(mode==FALLING),MCP23017_DEFVALA,MCP23017_DEFVALB); + updateRegisterBit(pin, (mode == FALLING), MCP23017_DEFVALA, MCP23017_DEFVALB); // enable the pin for interrupt - updateRegisterBit(pin,HIGH,MCP23017_GPINTENA,MCP23017_GPINTENB); - + updateRegisterBit(pin, HIGH, MCP23017_GPINTENA, MCP23017_GPINTENB); } -uint8_t Adafruit_MCP23017::getLastInterruptPin(){ +uint8_t Adafruit_MCP23017::getLastInterruptPin() +{ uint8_t intf; // try port A - intf=readRegister(MCP23017_INTFA); - for(int i=0;i<8;i++) if (bitRead(intf,i)) return i; + intf = readRegister(MCP23017_INTFA); + for (int i = 0; i < 8; i++) + if (bitRead(intf, i)) + return i; // try port B - intf=readRegister(MCP23017_INTFB); - for(int i=0;i<8;i++) if (bitRead(intf,i)) return i+8; + intf = readRegister(MCP23017_INTFB); + for (int i = 0; i < 8; i++) + if (bitRead(intf, i)) + return i + 8; return MCP23017_INT_ERR; - } -uint8_t Adafruit_MCP23017::getLastInterruptPinValue(){ - uint8_t intPin=getLastInterruptPin(); - if(intPin!=MCP23017_INT_ERR){ - uint8_t intcapreg=regForPin(intPin,MCP23017_INTCAPA,MCP23017_INTCAPB); - uint8_t bit=bitForPin(intPin); - return (readRegister(intcapreg)>>bit) & (0x01); +uint8_t Adafruit_MCP23017::getLastInterruptPinValue() +{ + uint8_t intPin = getLastInterruptPin(); + if (intPin != MCP23017_INT_ERR) + { + uint8_t intcapreg = regForPin(intPin, MCP23017_INTCAPA, MCP23017_INTCAPB); + uint8_t bit = bitForPin(intPin); + return (readRegister(intcapreg) >> bit) & (0x01); } return MCP23017_INT_ERR; } - - diff --git a/Inkplate.cpp b/Inkplate.cpp index 74f36eb..2dc2bc4 100644 --- a/Inkplate.cpp +++ b/Inkplate.cpp @@ -37,7 +37,7 @@ void Inkplate::begin(void) if (_beginDone == 1) return; Wire.begin(); - mcpBegin(MCP23017_ADDR, mcpRegsInt); + mcpBegin(MCP23017_ADDR, mcpRegsInt); pinModeMCP(VCOM, OUTPUT); pinModeMCP(PWRUP, OUTPUT); pinModeMCP(WAKEUP, OUTPUT); @@ -813,27 +813,29 @@ void Inkplate::cleanFast(uint8_t c, uint8_t rep) data = B11111111; //Skip } - uint32_t _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25);; - for (int k = 0; k < rep; k++) { - vscan_start(); - for (int i = 0; i < 600; i++) - { - hscan_start(_send); - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - for (int j = 0; j < 99; j++) - { - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - } - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - vscan_end(); + uint32_t _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25); + ; + for (int k = 0; k < rep; k++) + { + vscan_start(); + for (int i = 0; i < 600; i++) + { + hscan_start(_send); + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + for (int j = 0; j < 99; j++) + { + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + } + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + vscan_end(); + } + delayMicroseconds(230); } - delayMicroseconds(230); - } } void Inkplate::cleanFast2(uint8_t c, uint8_t n, uint16_t d) @@ -917,122 +919,123 @@ void Inkplate::pinsAsOutputs() } //----------------------------MCP23017 Functions---------------------------- -void Inkplate::pinModeMCP(uint8_t _pin, uint8_t _mode) +void Inkplate::pinModeMCP(uint8_t _pin, uint8_t _mode) { - uint8_t _port = (_pin / 8) & 1; - uint8_t _p = _pin % 8; + uint8_t _port = (_pin / 8) & 1; + uint8_t _p = _pin % 8; - switch (_mode) - { - case INPUT: - mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input - mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin - updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); - updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); - break; + switch (_mode) + { + case INPUT: + mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input + mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin + updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); + updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); + break; - case INPUT_PULLUP: - mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input - mcpRegsInt[MCP23017_GPPUA + _port] |= 1 << _p; //Enable pullup on that pin - updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); - updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); - break; + case INPUT_PULLUP: + mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input + mcpRegsInt[MCP23017_GPPUA + _port] |= 1 << _p; //Enable pullup on that pin + updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); + updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); + break; - case OUTPUT: - mcpRegsInt[MCP23017_IODIRA + _port] &= ~(1 << _p); //Set it to output - mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin - updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); - updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); - break; - } + case OUTPUT: + mcpRegsInt[MCP23017_IODIRA + _port] &= ~(1 << _p); //Set it to output + mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin + updateRegister(MCP23017_ADDR, MCP23017_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]); + updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); + break; + } } void Inkplate::digitalWriteMCP(uint8_t _pin, uint8_t _state) { - uint8_t _port = (_pin / 8) & 1; - uint8_t _p = _pin % 8; + uint8_t _port = (_pin / 8) & 1; + uint8_t _p = _pin % 8; - if (mcpRegsInt[MCP23017_IODIRA + _port] & (1 << _p)) return; //Check if the pin is set as an output - _state ? (mcpRegsInt[MCP23017_GPIOA + _port] |= (1 << _p)) : (mcpRegsInt[MCP23017_GPIOA + _port] &= ~(1 << _p)); - updateRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt[MCP23017_GPIOA + _port]); + if (mcpRegsInt[MCP23017_IODIRA + _port] & (1 << _p)) + return; //Check if the pin is set as an output + _state ? (mcpRegsInt[MCP23017_GPIOA + _port] |= (1 << _p)) : (mcpRegsInt[MCP23017_GPIOA + _port] &= ~(1 << _p)); + updateRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt[MCP23017_GPIOA + _port]); } uint8_t Inkplate::digitalReadMCP(uint8_t _pin) { - uint8_t _port = (_pin / 8) & 1; - uint8_t _p = _pin % 8; - readMCPRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt); - return (mcpRegsInt[MCP23017_GPIOA + _port] & (1 << _p)) ? HIGH : LOW; + uint8_t _port = (_pin / 8) & 1; + uint8_t _p = _pin % 8; + readMCPRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt); + return (mcpRegsInt[MCP23017_GPIOA + _port] & (1 << _p)) ? HIGH : LOW; } void Inkplate::setIntOutput(uint8_t intPort, uint8_t mirroring, uint8_t openDrain, uint8_t polarity) { - intPort &= 1; - mirroring &= 1; - openDrain &= 1; - polarity &= 1; - mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << mirroring); - mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << openDrain); - mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << polarity); - updateRegister(MCP23017_ADDR, MCP23017_IOCONA + intPort, mcpRegsInt[MCP23017_IOCONA + intPort]); + intPort &= 1; + mirroring &= 1; + openDrain &= 1; + polarity &= 1; + mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << mirroring); + mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << openDrain); + mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << polarity); + updateRegister(MCP23017_ADDR, MCP23017_IOCONA + intPort, mcpRegsInt[MCP23017_IOCONA + intPort]); } void Inkplate::setIntPin(uint8_t _pin, uint8_t _mode) { - uint8_t _port = (_pin / 8) & 1; - uint8_t _p = _pin % 8; + uint8_t _port = (_pin / 8) & 1; + uint8_t _p = _pin % 8; - switch (_mode) - { - case CHANGE: - mcpRegsInt[MCP23017_INTCONA + _port] &= ~(1 << _p); - break; + switch (_mode) + { + case CHANGE: + mcpRegsInt[MCP23017_INTCONA + _port] &= ~(1 << _p); + break; - case FALLING: - mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); - mcpRegsInt[MCP23017_DEFVALA + _port] |= (1 << _p); - break; + case FALLING: + mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); + mcpRegsInt[MCP23017_DEFVALA + _port] |= (1 << _p); + break; - case RISING: - mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); - mcpRegsInt[MCP23017_DEFVALA + _port] &= ~(1 << _p); - break; - } - mcpRegsInt[MCP23017_GPINTENA + _port] |= (1 << _p); - updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 6); + case RISING: + mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); + mcpRegsInt[MCP23017_DEFVALA + _port] &= ~(1 << _p); + break; + } + mcpRegsInt[MCP23017_GPINTENA + _port] |= (1 << _p); + updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 6); } void Inkplate::removeIntPin(uint8_t _pin) { - uint8_t _port = (_pin / 8) & 1; - uint8_t _p = _pin % 8; - mcpRegsInt[MCP23017_GPINTENA + _port] &= ~(1 << _p); - updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 2); + uint8_t _port = (_pin / 8) & 1; + uint8_t _p = _pin % 8; + mcpRegsInt[MCP23017_GPINTENA + _port] &= ~(1 << _p); + updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 2); } uint16_t Inkplate::getINT() { - readMCPRegisters(MCP23017_ADDR, MCP23017_INTFA, mcpRegsInt, 2); - return ((mcpRegsInt[MCP23017_INTFB] << 8) | mcpRegsInt[MCP23017_INTFA]); + readMCPRegisters(MCP23017_ADDR, MCP23017_INTFA, mcpRegsInt, 2); + return ((mcpRegsInt[MCP23017_INTFB] << 8) | mcpRegsInt[MCP23017_INTFA]); } uint16_t Inkplate::getINTstate() { - readMCPRegisters(MCP23017_ADDR, MCP23017_INTCAPA, mcpRegsInt, 2); - return ((mcpRegsInt[MCP23017_INTCAPB] << 8) | mcpRegsInt[MCP23017_INTCAPA]); + readMCPRegisters(MCP23017_ADDR, MCP23017_INTCAPA, mcpRegsInt, 2); + return ((mcpRegsInt[MCP23017_INTCAPB] << 8) | mcpRegsInt[MCP23017_INTCAPA]); } void Inkplate::setPorts(uint16_t _d) { - mcpRegsInt[MCP23017_GPIOA] = _d & 0xff; - mcpRegsInt[MCP23017_GPIOB] = (_d >> 8) & 0xff; - updateRegister(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); + mcpRegsInt[MCP23017_GPIOA] = _d & 0xff; + mcpRegsInt[MCP23017_GPIOB] = (_d >> 8) & 0xff; + updateRegister(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); } uint16_t Inkplate::getPorts() { - readMCPRegisters(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); - return ((mcpRegsInt[MCP23017_GPIOB] << 8) | (mcpRegsInt[MCP23017_GPIOA])); + readMCPRegisters(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); + return ((mcpRegsInt[MCP23017_GPIOB] << 8) | (mcpRegsInt[MCP23017_GPIOA])); } //--------------------------PRIVATE FUNCTIONS-------------------------------------------- @@ -1125,32 +1128,33 @@ void Inkplate::display1b() } delayMicroseconds(230); - vscan_start(); + vscan_start(); for (int i = 0; i < 600; i++) - { - dram = *(D_memory_new + _pos); - data = 0b00000000;; - _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25); - hscan_start(_send); - data = 0b00000000; - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - for (int j = 0; j < 99; j++) - { - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - } - GPIO.out_w1ts = (_send) | CL; - GPIO.out_w1tc = DATA | CL; - vscan_end(); - } - delayMicroseconds(230); - - vscan_start(); - einkOff(); - _blockPartial = 0; + { + dram = *(D_memory_new + _pos); + data = 0b00000000; + ; + _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25); + hscan_start(_send); + data = 0b00000000; + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + for (int j = 0; j < 99; j++) + { + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + } + GPIO.out_w1ts = (_send) | CL; + GPIO.out_w1tc = DATA | CL; + vscan_end(); + } + delayMicroseconds(230); + + vscan_start(); + einkOff(); + _blockPartial = 0; } //Display content from RAM to display (3 bit per pixel,. 8 level of grayscale, STILL IN PROGRESSS, we need correct wavefrom to get good picture, use it only for pictures not for GFX). @@ -1972,78 +1976,79 @@ void Inkplate::precalculateGamma(uint8_t *c, float gamma) } //----------------------------MCP23017 Low Level Functions---------------------------- -bool Inkplate::mcpBegin(uint8_t _addr, uint8_t* _r) +bool Inkplate::mcpBegin(uint8_t _addr, uint8_t *_r) { - Wire.beginTransmission(_addr); - int error = Wire.endTransmission(); - if (error) return false; - readMCPRegisters(_addr, _r); - _r[0] = 0xff; - _r[1] = 0xff; - updateAllRegisters(_addr, _r); - return true; + Wire.beginTransmission(_addr); + int error = Wire.endTransmission(); + if (error) + return false; + readMCPRegisters(_addr, _r); + _r[0] = 0xff; + _r[1] = 0xff; + updateAllRegisters(_addr, _r); + return true; } void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t *k) { - Wire.beginTransmission(_addr); - Wire.write(0x00); - Wire.endTransmission(); - Wire.requestFrom(_addr, (uint8_t)22); - for (int i = 0; i < 22; i++) - { - k[i] = Wire.read(); - } + Wire.beginTransmission(_addr); + Wire.write(0x00); + Wire.endTransmission(); + Wire.requestFrom(_addr, (uint8_t)22); + for (int i = 0; i < 22; i++) + { + k[i] = Wire.read(); + } } void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n) { - Wire.beginTransmission(_addr); - Wire.write(_regName); - Wire.endTransmission(); + Wire.beginTransmission(_addr); + Wire.write(_regName); + Wire.endTransmission(); - Wire.requestFrom(_addr, _n); - for (int i = 0; i < _n; i++) - { - k[_regName + i] = Wire.read(); - } + Wire.requestFrom(_addr, _n); + for (int i = 0; i < _n; i++) + { + k[_regName + i] = Wire.read(); + } } void Inkplate::readMCPRegister(uint8_t _addr, uint8_t _regName, uint8_t *k) { - Wire.beginTransmission(_addr); - Wire.write(_regName); - Wire.endTransmission(); - Wire.requestFrom(_addr, (uint8_t)1); - k[_regName] = Wire.read(); + Wire.beginTransmission(_addr); + Wire.write(_regName); + Wire.endTransmission(); + Wire.requestFrom(_addr, (uint8_t)1); + k[_regName] = Wire.read(); } void Inkplate::updateAllRegisters(uint8_t _addr, uint8_t *k) { - Wire.beginTransmission(_addr); - Wire.write(0x00); - for (int i = 0; i < 22; i++) - { - Wire.write(k[i]); - } - Wire.endTransmission(); + Wire.beginTransmission(_addr); + Wire.write(0x00); + for (int i = 0; i < 22; i++) + { + Wire.write(k[i]); + } + Wire.endTransmission(); } void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t _d) { - Wire.beginTransmission(_addr); - Wire.write(_regName); - Wire.write(_d); - Wire.endTransmission(); + Wire.beginTransmission(_addr); + Wire.write(_regName); + Wire.write(_d); + Wire.endTransmission(); } void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n) { - Wire.beginTransmission(_addr); - Wire.write(_regName); - for (int i = 0; i < _n; i++) - { - Wire.write(k[_regName + i]); - } - Wire.endTransmission(); + Wire.beginTransmission(_addr); + Wire.write(_regName); + for (int i = 0; i < _n; i++) + { + Wire.write(k[_regName + i]); + } + Wire.endTransmission(); } \ No newline at end of file diff --git a/examples/2. Advanced Inkplate Features/11-Wake_up_on_touchpads/11-Wake_up_on_touchpads.ino b/examples/2. Advanced Inkplate Features/11-Wake_up_on_touchpads/11-Wake_up_on_touchpads.ino new file mode 100644 index 0000000..2ec2cae --- /dev/null +++ b/examples/2. Advanced Inkplate Features/11-Wake_up_on_touchpads/11-Wake_up_on_touchpads.ino @@ -0,0 +1,89 @@ +#include + +// Conversion factor for micro seconds to seconds +#define uS_TO_S_FACTOR 1000000 +// Time ESP32 will go to sleep (in seconds) +#define TIME_TO_SLEEP 30 + +// Initiate Inkplate object +Inkplate display(INKPLATE_1BIT); + +byte touchPadPin = 10; + +// Store int in rtc data, to remain persistent during deep sleep +RTC_DATA_ATTR int bootCount = 0; + +void setup() +{ + Serial.begin(115200); + display.begin(); + + // Setup mcp interrupts + display.pinModeMCP(touchPadPin, INPUT); + display.setIntOutput(1, true, true, HIGH); + display.setIntPin(touchPadPin, RISING); + + ++bootCount; + + // Our function declared below + displayInfo(); + + // Go to sleep for TIME_TO_SLEEP seconds, but also enable wake up from gpio 34 + // Gpio 34 is where the mcp interrupt is connected, check + // https://github.com/e-radionicacom/Inkplate-6-hardware/blob/master/Schematics%2C%20Gerber%2C%20BOM/Inkplate6%20Schematics.pdf + // for more detail + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + esp_sleep_enable_ext0_wakeup(GPIO_NUM_34, 1); + + // Go to sleep + esp_deep_sleep_start(); +} + +void loop() +{ + // Never here +} + +// Function that will write number of boots and boot reason to screen +void displayInfo() +{ + // First, lets delete everything from frame buffer + display.clearDisplay(); + + // Set text cursor and size + display.setCursor(10, 280); + display.setTextSize(2); + + display.print(F("Boot count: ")); + display.println(bootCount, DEC); //Print the number + + // Set next line cursor position + display.setCursor(10, 320); + + // Display wake up reason + esp_sleep_wakeup_cause_t wakeup_reason; + wakeup_reason = esp_sleep_get_wakeup_cause(); + switch (wakeup_reason) + { + case ESP_SLEEP_WAKEUP_EXT0: + display.println("Wakeup caused by external signal using RTC_IO"); + break; + case ESP_SLEEP_WAKEUP_EXT1: + display.println("Wakeup caused by external signal using RTC_CNTL"); + break; + case ESP_SLEEP_WAKEUP_TIMER: + display.println("Wakeup caused by timer"); + break; + case ESP_SLEEP_WAKEUP_TOUCHPAD: + display.println("Wakeup caused by touchpad"); + break; + case ESP_SLEEP_WAKEUP_ULP: + display.println("Wakeup caused by ULP program"); + break; + default: + display.println("Wakeup was not caused by deep sleep"); + break; + } + + display.display(); +} diff --git a/examples/2. Advanced Inkplate Features/4-Inkplate_Touchpads/4-Inkplate_Touchpads.ino b/examples/2. Advanced Inkplate Features/4-Inkplate_Touchpads/4-Inkplate_Touchpads.ino index ed59ac8..5f6355d 100644 --- a/examples/2. Advanced Inkplate Features/4-Inkplate_Touchpads/4-Inkplate_Touchpads.ino +++ b/examples/2. Advanced Inkplate Features/4-Inkplate_Touchpads/4-Inkplate_Touchpads.ino @@ -22,53 +22,62 @@ 15 July 2020 by e-radionica.com */ -#include "Inkplate.h" //Include Inkplate library to the sketch -Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) +#include "Inkplate.h" //Include Inkplate library to the sketch +Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode (Monochrome) -int number = 0; //Variable that stores our number -int n = 0; //Variable that keeps track on how many times display is partially updated -void setup() { - display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) - display.clearDisplay(); //Clear frame buffer of display - display.display(); //Put clear image on display - display.setTextSize(5); //Set text scaling to five (text will be five times bigger) - display.setTextColor(BLACK, WHITE); //Set text color to black and background color to white - displayNumber(); //Call our function to display nubmer on screen +int number = 0; //Variable that stores our number +int n = 0; //Variable that keeps track on how many times display is partially updated +void setup() +{ + display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) + display.clearDisplay(); //Clear frame buffer of display + display.display(); //Put clear image on display + display.setTextSize(5); //Set text scaling to five (text will be five times bigger) + display.setTextColor(BLACK, WHITE); //Set text color to black and background color to white + displayNumber(); //Call our function to display nubmer on screen } -void loop() { - if (display.readTouchpad(PAD1)) { //Check if first pad has been touched. If it is, decrement the number and refresh the screen. +void loop() +{ + if (display.readTouchpad(PAD1)) + { //Check if first pad has been touched. If it is, decrement the number and refresh the screen. number--; displayNumber(); } - if (display.readTouchpad(PAD2)) { //If you touched second touchpad, set number to zero and refresh screen by calling our displayNumber() function + if (display.readTouchpad(PAD2)) + { //If you touched second touchpad, set number to zero and refresh screen by calling our displayNumber() function number = 0; displayNumber(); } - if (display.readTouchpad(PAD3)) { //If you touched third touchpad, incerement the number and refresh the screen. + if (display.readTouchpad(PAD3)) + { //If you touched third touchpad, incerement the number and refresh the screen. number++; displayNumber(); } - delay(100); //Wait a little bit between readings. + delay(100); //Wait a little bit between readings. } //Function that will write you number to screen -void displayNumber() { - display.clearDisplay(); //First, lets delete everything from frame buffer - display.setCursor(385, 280); //Set print cursor at X=385, Y=280 (roughly in the middle of the screen) - display.print(number, DEC); //Print the number - display.setCursor(255, 560); //Set new print position (right above first touchpad) - display.print('-'); //Print minus sign - display.setCursor(385, 560); //Set new print position (right above second touchpad) - display.print('0'); //Print zero - display.setCursor(520, 560); //Set new print position (right above third touchpad) - display.print('+'); //Print plus sign - if (n > 20) { //Chech if screen has been partially refreshed more than 20 times. If it is, do a full refresh. If is not, do a partial refresh +void displayNumber() +{ + display.clearDisplay(); //First, lets delete everything from frame buffer + display.setCursor(385, 280); //Set print cursor at X=385, Y=280 (roughly in the middle of the screen) + display.print(number, DEC); //Print the number + display.setCursor(255, 560); //Set new print position (right above first touchpad) + display.print('-'); //Print minus sign + display.setCursor(385, 560); //Set new print position (right above second touchpad) + display.print('0'); //Print zero + display.setCursor(520, 560); //Set new print position (right above third touchpad) + display.print('+'); //Print plus sign + if (n > 20) + { //Chech if screen has been partially refreshed more than 20 times. If it is, do a full refresh. If is not, do a partial refresh display.display(); n = 0; - } else { + } + else + { display.partialUpdate(); n++; } diff --git a/examples/3. Projects/4-Google_calendar_example/4-Google_calendar_example.ino b/examples/3. Projects/4-Google_calendar_example/4-Google_calendar_example.ino index ac48991..100659d 100644 --- a/examples/3. Projects/4-Google_calendar_example/4-Google_calendar_example.ino +++ b/examples/3. Projects/4-Google_calendar_example/4-Google_calendar_example.ino @@ -92,7 +92,7 @@ void setup() { Serial.begin(115200); - data = (char*)ps_malloc(100000); + data = (char*)ps_malloc(2000000LL); //Initial display settings display.begin(); diff --git a/examples/3. Projects/4-Google_calendar_example/Network.cpp b/examples/3. Projects/4-Google_calendar_example/Network.cpp index 0b7b364..c49682f 100644 --- a/examples/3. Projects/4-Google_calendar_example/Network.cpp +++ b/examples/3. Projects/4-Google_calendar_example/Network.cpp @@ -18,7 +18,10 @@ void Network::begin() delay(1000); ++cnt; - if (cnt == 20) + WiFi.reconnect(); + delay(5000); + + if (cnt == 10) { Serial.println("Can't connect to WIFI, restarting"); delay(100); @@ -67,7 +70,10 @@ bool Network::getData(char *data) delay(1000); ++cnt; - if (cnt == 7) + WiFi.reconnect(); + delay(5000); + + if (cnt == 10) { Serial.println("Can't connect to WIFI, restart initiated."); delay(100);