Added deep sleep touch pad wake up.

This commit is contained in:
nitko12 2020-08-31 13:16:27 +02:00
parent 530788d895
commit cf9a1f16a5
4 changed files with 388 additions and 265 deletions

View File

@ -13,9 +13,9 @@
#include <Wire.h> #include <Wire.h>
#ifdef __AVR #ifdef __AVR
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#elif defined(ESP8266) #elif defined(ESP8266)
#include <pgmspace.h> #include <pgmspace.h>
#endif #endif
#include "Adafruit_MCP23017.h" #include "Adafruit_MCP23017.h"
@ -26,15 +26,17 @@
#endif #endif
// minihelper to keep Arduino backward compatibility // minihelper to keep Arduino backward compatibility
static inline void wiresend(uint8_t x) { static inline void wiresend(uint8_t x)
{
#if ARDUINO >= 100 #if ARDUINO >= 100
Wire.write((uint8_t) x); Wire.write((uint8_t)x);
#else #else
Wire.send(x); Wire.send(x);
#endif #endif
} }
static inline uint8_t wirerecv(void) { static inline uint8_t wirerecv(void)
{
#if ARDUINO >= 100 #if ARDUINO >= 100
return Wire.read(); return Wire.read();
#else #else
@ -45,21 +47,24 @@ static inline uint8_t wirerecv(void) {
/** /**
* Bit number associated to a give Pin * Bit number associated to a give Pin
*/ */
uint8_t Adafruit_MCP23017::bitForPin(uint8_t pin){ uint8_t Adafruit_MCP23017::bitForPin(uint8_t pin)
return pin%8; {
return pin % 8;
} }
/** /**
* Register address, port dependent, for a given PIN * Register address, port dependent, for a given PIN
*/ */
uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr){ uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr)
return(pin<8) ?portAaddr:portBaddr; {
return (pin < 8) ? portAaddr : portBaddr;
} }
/** /**
* Reads a given register * Reads a given register
*/ */
uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){ uint8_t Adafruit_MCP23017::readRegister(uint8_t addr)
{
// read the current GPINTEN // read the current GPINTEN
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(addr); wiresend(addr);
@ -68,11 +73,11 @@ uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){
return wirerecv(); return wirerecv();
} }
/** /**
* Writes a given register * 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 // Write the register
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(regAddr); wiresend(regAddr);
@ -80,22 +85,22 @@ void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue){
Wire.endTransmission(); Wire.endTransmission();
} }
/** /**
* Helper to update a single bit of an A/B register. * Helper to update a single bit of an A/B register.
* - Reads the current register value * - Reads the current register value
* - Writes the new 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 regValue;
uint8_t regAddr=regForPin(pin,portAaddr,portBaddr); uint8_t regAddr = regForPin(pin, portAaddr, portBaddr);
uint8_t bit=bitForPin(pin); uint8_t bit = bitForPin(pin);
regValue = readRegister(regAddr); regValue = readRegister(regAddr);
// set the value for the particular bit // 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. * Initializes the MCP23017 given its HW selected address, see datasheet for Address selection.
*/ */
void Adafruit_MCP23017::begin(uint8_t addr) { void Adafruit_MCP23017::begin(uint8_t addr)
if (addr > 7) { {
if (addr > 7)
{
addr = 7; addr = 7;
} }
i2caddr = addr; i2caddr = addr;
@ -113,28 +120,31 @@ void Adafruit_MCP23017::begin(uint8_t addr) {
// set defaults! // set defaults!
// all inputs on port A and B // all inputs on port A and B
writeRegister(MCP23017_IODIRA,0xff); writeRegister(MCP23017_IODIRA, 0xff);
writeRegister(MCP23017_IODIRB,0xff); writeRegister(MCP23017_IODIRB, 0xff);
} }
/** /**
* Initializes the default MCP23017, with 000 for the configurable part of the address * 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); begin(0);
} }
/** /**
* Sets the pin mode to either INPUT or OUTPUT * Sets the pin mode to either INPUT or OUTPUT
*/ */
void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d)
updateRegisterBit(p,(d==INPUT),MCP23017_IODIRA,MCP23017_IODIRB); {
updateRegisterBit(p, (d == INPUT), MCP23017_IODIRA, MCP23017_IODIRB);
} }
/** /**
* Reads all 16 pins (port A and B) into a single 16 bits variable. * 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; uint16_t ba = 0;
uint8_t a; 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. * 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. * 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 // read the current GPIO output latches
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
if (b == 0) if (b == 0)
wiresend(MCP23017_GPIOA); wiresend(MCP23017_GPIOA);
else { else
{
wiresend(MCP23017_GPIOB); wiresend(MCP23017_GPIOB);
} }
Wire.endTransmission(); 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. * 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); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
wiresend(MCP23017_GPIOA); wiresend(MCP23017_GPIOA);
wiresend(ba & 0xFF); wiresend(ba & 0xFF);
@ -182,30 +195,32 @@ void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) {
Wire.endTransmission(); 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 gpio;
uint8_t bit=bitForPin(pin); uint8_t bit = bitForPin(pin);
// read the current GPIO output latches // 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); gpio = readRegister(regAddr);
// set the pin and direction // set the pin and direction
bitWrite(gpio,bit,d); bitWrite(gpio, bit, d);
// write the new GPIO // write the new GPIO
regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB); regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB);
writeRegister(regAddr,gpio); writeRegister(regAddr, gpio);
} }
void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d) { void Adafruit_MCP23017::pullUp(uint8_t p, uint8_t d)
updateRegisterBit(p,d,MCP23017_GPPUA,MCP23017_GPPUB); {
updateRegisterBit(p, d, MCP23017_GPPUA, MCP23017_GPPUB);
} }
uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin) { uint8_t Adafruit_MCP23017::digitalRead(uint8_t pin)
uint8_t bit=bitForPin(pin); {
uint8_t regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB); uint8_t bit = bitForPin(pin);
uint8_t regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB);
return (readRegister(regAddr) >> bit) & 0x1; 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 * If you are connecting the INTA/B pin to arduino 2/3, you should configure the interupt handling as FALLING with
* the default configuration. * 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 // configure the port A
uint8_t ioconfValue=readRegister(MCP23017_IOCONA); uint8_t ioconfValue = readRegister(MCP23017_IOCONA);
bitWrite(ioconfValue,6,mirroring); bitWrite(ioconfValue, 6, mirroring);
bitWrite(ioconfValue,2,openDrain); bitWrite(ioconfValue, 2, openDrain);
bitWrite(ioconfValue,1,polarity); bitWrite(ioconfValue, 1, polarity);
writeRegister(MCP23017_IOCONA,ioconfValue); writeRegister(MCP23017_IOCONA, ioconfValue);
// Configure the port B // Configure the port B
ioconfValue=readRegister(MCP23017_IOCONB); ioconfValue = readRegister(MCP23017_IOCONB);
bitWrite(ioconfValue,6,mirroring); bitWrite(ioconfValue, 6, mirroring);
bitWrite(ioconfValue,2,openDrain); bitWrite(ioconfValue, 2, openDrain);
bitWrite(ioconfValue,1,polarity); bitWrite(ioconfValue, 1, polarity);
writeRegister(MCP23017_IOCONB,ioconfValue); 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. * 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); // 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 // 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 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. // 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 // 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; uint8_t intf;
// try port A // try port A
intf=readRegister(MCP23017_INTFA); intf = readRegister(MCP23017_INTFA);
for(int i=0;i<8;i++) if (bitRead(intf,i)) return i; for (int i = 0; i < 8; i++)
if (bitRead(intf, i))
return i;
// try port B // try port B
intf=readRegister(MCP23017_INTFB); intf = readRegister(MCP23017_INTFB);
for(int i=0;i<8;i++) if (bitRead(intf,i)) return i+8; for (int i = 0; i < 8; i++)
if (bitRead(intf, i))
return i + 8;
return MCP23017_INT_ERR; return MCP23017_INT_ERR;
} }
uint8_t Adafruit_MCP23017::getLastInterruptPinValue(){ uint8_t Adafruit_MCP23017::getLastInterruptPinValue()
uint8_t intPin=getLastInterruptPin(); {
if(intPin!=MCP23017_INT_ERR){ uint8_t intPin = getLastInterruptPin();
uint8_t intcapreg=regForPin(intPin,MCP23017_INTCAPA,MCP23017_INTCAPB); if (intPin != MCP23017_INT_ERR)
uint8_t bit=bitForPin(intPin); {
return (readRegister(intcapreg)>>bit) & (0x01); uint8_t intcapreg = regForPin(intPin, MCP23017_INTCAPA, MCP23017_INTCAPB);
uint8_t bit = bitForPin(intPin);
return (readRegister(intcapreg) >> bit) & (0x01);
} }
return MCP23017_INT_ERR; return MCP23017_INT_ERR;
} }

View File

@ -37,7 +37,7 @@ void Inkplate::begin(void)
if (_beginDone == 1) if (_beginDone == 1)
return; return;
Wire.begin(); Wire.begin();
mcpBegin(MCP23017_ADDR, mcpRegsInt); mcpBegin(MCP23017_ADDR, mcpRegsInt);
pinModeMCP(VCOM, OUTPUT); pinModeMCP(VCOM, OUTPUT);
pinModeMCP(PWRUP, OUTPUT); pinModeMCP(PWRUP, OUTPUT);
pinModeMCP(WAKEUP, OUTPUT); pinModeMCP(WAKEUP, OUTPUT);
@ -813,27 +813,29 @@ void Inkplate::cleanFast(uint8_t c, uint8_t rep)
data = B11111111; //Skip data = B11111111; //Skip
} }
uint32_t _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25);; 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 k = 0; k < rep; k++)
for (int i = 0; i < 600; i++) {
{ vscan_start();
hscan_start(_send); for (int i = 0; i < 600; i++)
GPIO.out_w1ts = (_send) | CL; {
GPIO.out_w1tc = DATA | CL; hscan_start(_send);
for (int j = 0; j < 99; j++) GPIO.out_w1ts = (_send) | CL;
{ GPIO.out_w1tc = DATA | CL;
GPIO.out_w1ts = (_send) | CL; for (int j = 0; j < 99; j++)
GPIO.out_w1tc = DATA | CL; {
GPIO.out_w1ts = (_send) | CL; GPIO.out_w1ts = (_send) | CL;
GPIO.out_w1tc = DATA | CL; GPIO.out_w1tc = DATA | CL;
} GPIO.out_w1ts = (_send) | CL;
GPIO.out_w1ts = (_send) | CL; GPIO.out_w1tc = DATA | CL;
GPIO.out_w1tc = DATA | CL; }
vscan_end(); 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) void Inkplate::cleanFast2(uint8_t c, uint8_t n, uint16_t d)
@ -917,122 +919,123 @@ void Inkplate::pinsAsOutputs()
} }
//----------------------------MCP23017 Functions---------------------------- //----------------------------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 _port = (_pin / 8) & 1;
uint8_t _p = _pin % 8; uint8_t _p = _pin % 8;
switch (_mode) switch (_mode)
{ {
case INPUT: case INPUT:
mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input
mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin 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_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]);
updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]);
break; break;
case INPUT_PULLUP: case INPUT_PULLUP:
mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input mcpRegsInt[MCP23017_IODIRA + _port] |= 1 << _p; //Set it to input
mcpRegsInt[MCP23017_GPPUA + _port] |= 1 << _p; //Enable pullup on that pin 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_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]);
updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]);
break; break;
case OUTPUT: case OUTPUT:
mcpRegsInt[MCP23017_IODIRA + _port] &= ~(1 << _p); //Set it to output mcpRegsInt[MCP23017_IODIRA + _port] &= ~(1 << _p); //Set it to output
mcpRegsInt[MCP23017_GPPUA + _port] &= ~(1 << _p); //Disable pullup on that pin 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_IODIRA + _port, mcpRegsInt[MCP23017_IODIRA + _port]);
updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]); updateRegister(MCP23017_ADDR, MCP23017_GPPUA + _port, mcpRegsInt[MCP23017_GPPUA + _port]);
break; break;
} }
} }
void Inkplate::digitalWriteMCP(uint8_t _pin, uint8_t _state) void Inkplate::digitalWriteMCP(uint8_t _pin, uint8_t _state)
{ {
uint8_t _port = (_pin / 8) & 1; uint8_t _port = (_pin / 8) & 1;
uint8_t _p = _pin % 8; uint8_t _p = _pin % 8;
if (mcpRegsInt[MCP23017_IODIRA + _port] & (1 << _p)) return; //Check if the pin is set as an output if (mcpRegsInt[MCP23017_IODIRA + _port] & (1 << _p))
_state ? (mcpRegsInt[MCP23017_GPIOA + _port] |= (1 << _p)) : (mcpRegsInt[MCP23017_GPIOA + _port] &= ~(1 << _p)); return; //Check if the pin is set as an output
updateRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt[MCP23017_GPIOA + _port]); _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 Inkplate::digitalReadMCP(uint8_t _pin)
{ {
uint8_t _port = (_pin / 8) & 1; uint8_t _port = (_pin / 8) & 1;
uint8_t _p = _pin % 8; uint8_t _p = _pin % 8;
readMCPRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt); readMCPRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt);
return (mcpRegsInt[MCP23017_GPIOA + _port] & (1 << _p)) ? HIGH : LOW; return (mcpRegsInt[MCP23017_GPIOA + _port] & (1 << _p)) ? HIGH : LOW;
} }
void Inkplate::setIntOutput(uint8_t intPort, uint8_t mirroring, uint8_t openDrain, uint8_t polarity) void Inkplate::setIntOutput(uint8_t intPort, uint8_t mirroring, uint8_t openDrain, uint8_t polarity)
{ {
intPort &= 1; intPort &= 1;
mirroring &= 1; mirroring &= 1;
openDrain &= 1; openDrain &= 1;
polarity &= 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 << mirroring);
mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << openDrain); mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << openDrain);
mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << polarity); mcpRegsInt[MCP23017_IOCONA + intPort] = (mcpRegsInt[MCP23017_IOCONA + intPort] & ~(1 << 6)) | (1 << polarity);
updateRegister(MCP23017_ADDR, MCP23017_IOCONA + intPort, mcpRegsInt[MCP23017_IOCONA + intPort]); updateRegister(MCP23017_ADDR, MCP23017_IOCONA + intPort, mcpRegsInt[MCP23017_IOCONA + intPort]);
} }
void Inkplate::setIntPin(uint8_t _pin, uint8_t _mode) void Inkplate::setIntPin(uint8_t _pin, uint8_t _mode)
{ {
uint8_t _port = (_pin / 8) & 1; uint8_t _port = (_pin / 8) & 1;
uint8_t _p = _pin % 8; uint8_t _p = _pin % 8;
switch (_mode) switch (_mode)
{ {
case CHANGE: case CHANGE:
mcpRegsInt[MCP23017_INTCONA + _port] &= ~(1 << _p); mcpRegsInt[MCP23017_INTCONA + _port] &= ~(1 << _p);
break; break;
case FALLING: case FALLING:
mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p);
mcpRegsInt[MCP23017_DEFVALA + _port] |= (1 << _p); mcpRegsInt[MCP23017_DEFVALA + _port] |= (1 << _p);
break; break;
case RISING: case RISING:
mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p); mcpRegsInt[MCP23017_INTCONA + _port] |= (1 << _p);
mcpRegsInt[MCP23017_DEFVALA + _port] &= ~(1 << _p); mcpRegsInt[MCP23017_DEFVALA + _port] &= ~(1 << _p);
break; break;
} }
mcpRegsInt[MCP23017_GPINTENA + _port] |= (1 << _p); mcpRegsInt[MCP23017_GPINTENA + _port] |= (1 << _p);
updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 6); updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 6);
} }
void Inkplate::removeIntPin(uint8_t _pin) void Inkplate::removeIntPin(uint8_t _pin)
{ {
uint8_t _port = (_pin / 8) & 1; uint8_t _port = (_pin / 8) & 1;
uint8_t _p = _pin % 8; uint8_t _p = _pin % 8;
mcpRegsInt[MCP23017_GPINTENA + _port] &= ~(1 << _p); mcpRegsInt[MCP23017_GPINTENA + _port] &= ~(1 << _p);
updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 2); updateRegister(MCP23017_ADDR, MCP23017_GPINTENA, mcpRegsInt, 2);
} }
uint16_t Inkplate::getINT() uint16_t Inkplate::getINT()
{ {
readMCPRegisters(MCP23017_ADDR, MCP23017_INTFA, mcpRegsInt, 2); readMCPRegisters(MCP23017_ADDR, MCP23017_INTFA, mcpRegsInt, 2);
return ((mcpRegsInt[MCP23017_INTFB] << 8) | mcpRegsInt[MCP23017_INTFA]); return ((mcpRegsInt[MCP23017_INTFB] << 8) | mcpRegsInt[MCP23017_INTFA]);
} }
uint16_t Inkplate::getINTstate() uint16_t Inkplate::getINTstate()
{ {
readMCPRegisters(MCP23017_ADDR, MCP23017_INTCAPA, mcpRegsInt, 2); readMCPRegisters(MCP23017_ADDR, MCP23017_INTCAPA, mcpRegsInt, 2);
return ((mcpRegsInt[MCP23017_INTCAPB] << 8) | mcpRegsInt[MCP23017_INTCAPA]); return ((mcpRegsInt[MCP23017_INTCAPB] << 8) | mcpRegsInt[MCP23017_INTCAPA]);
} }
void Inkplate::setPorts(uint16_t _d) void Inkplate::setPorts(uint16_t _d)
{ {
mcpRegsInt[MCP23017_GPIOA] = _d & 0xff; mcpRegsInt[MCP23017_GPIOA] = _d & 0xff;
mcpRegsInt[MCP23017_GPIOB] = (_d >> 8) & 0xff; mcpRegsInt[MCP23017_GPIOB] = (_d >> 8) & 0xff;
updateRegister(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); updateRegister(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2);
} }
uint16_t Inkplate::getPorts() uint16_t Inkplate::getPorts()
{ {
readMCPRegisters(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2); readMCPRegisters(MCP23017_ADDR, MCP23017_GPIOA, mcpRegsInt, 2);
return ((mcpRegsInt[MCP23017_GPIOB] << 8) | (mcpRegsInt[MCP23017_GPIOA])); return ((mcpRegsInt[MCP23017_GPIOB] << 8) | (mcpRegsInt[MCP23017_GPIOA]));
} }
//--------------------------PRIVATE FUNCTIONS-------------------------------------------- //--------------------------PRIVATE FUNCTIONS--------------------------------------------
@ -1125,32 +1128,33 @@ void Inkplate::display1b()
} }
delayMicroseconds(230); delayMicroseconds(230);
vscan_start(); vscan_start();
for (int i = 0; i < 600; i++) for (int i = 0; i < 600; i++)
{ {
dram = *(D_memory_new + _pos); dram = *(D_memory_new + _pos);
data = 0b00000000;; data = 0b00000000;
_send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25); ;
hscan_start(_send); _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25);
data = 0b00000000; hscan_start(_send);
GPIO.out_w1ts = (_send) | CL; data = 0b00000000;
GPIO.out_w1tc = DATA | CL; GPIO.out_w1ts = (_send) | CL;
for (int j = 0; j < 99; j++) 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_w1ts = (_send) | CL; GPIO.out_w1tc = DATA | 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; GPIO.out_w1ts = (_send) | CL;
vscan_end(); GPIO.out_w1tc = DATA | CL;
} vscan_end();
delayMicroseconds(230); }
delayMicroseconds(230);
vscan_start();
einkOff(); vscan_start();
_blockPartial = 0; 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). //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---------------------------- //----------------------------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); Wire.beginTransmission(_addr);
int error = Wire.endTransmission(); int error = Wire.endTransmission();
if (error) return false; if (error)
readMCPRegisters(_addr, _r); return false;
_r[0] = 0xff; readMCPRegisters(_addr, _r);
_r[1] = 0xff; _r[0] = 0xff;
updateAllRegisters(_addr, _r); _r[1] = 0xff;
return true; updateAllRegisters(_addr, _r);
return true;
} }
void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t *k) void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t *k)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(0x00); Wire.write(0x00);
Wire.endTransmission(); Wire.endTransmission();
Wire.requestFrom(_addr, (uint8_t)22); Wire.requestFrom(_addr, (uint8_t)22);
for (int i = 0; i < 22; i++) for (int i = 0; i < 22; i++)
{ {
k[i] = Wire.read(); k[i] = Wire.read();
} }
} }
void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n) void Inkplate::readMCPRegisters(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(_regName); Wire.write(_regName);
Wire.endTransmission(); Wire.endTransmission();
Wire.requestFrom(_addr, _n); Wire.requestFrom(_addr, _n);
for (int i = 0; i < _n; i++) for (int i = 0; i < _n; i++)
{ {
k[_regName + i] = Wire.read(); k[_regName + i] = Wire.read();
} }
} }
void Inkplate::readMCPRegister(uint8_t _addr, uint8_t _regName, uint8_t *k) void Inkplate::readMCPRegister(uint8_t _addr, uint8_t _regName, uint8_t *k)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(_regName); Wire.write(_regName);
Wire.endTransmission(); Wire.endTransmission();
Wire.requestFrom(_addr, (uint8_t)1); Wire.requestFrom(_addr, (uint8_t)1);
k[_regName] = Wire.read(); k[_regName] = Wire.read();
} }
void Inkplate::updateAllRegisters(uint8_t _addr, uint8_t *k) void Inkplate::updateAllRegisters(uint8_t _addr, uint8_t *k)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(0x00); Wire.write(0x00);
for (int i = 0; i < 22; i++) for (int i = 0; i < 22; i++)
{ {
Wire.write(k[i]); Wire.write(k[i]);
} }
Wire.endTransmission(); Wire.endTransmission();
} }
void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t _d) void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t _d)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(_regName); Wire.write(_regName);
Wire.write(_d); Wire.write(_d);
Wire.endTransmission(); Wire.endTransmission();
} }
void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n) void Inkplate::updateRegister(uint8_t _addr, uint8_t _regName, uint8_t *k, uint8_t _n)
{ {
Wire.beginTransmission(_addr); Wire.beginTransmission(_addr);
Wire.write(_regName); Wire.write(_regName);
for (int i = 0; i < _n; i++) for (int i = 0; i < _n; i++)
{ {
Wire.write(k[_regName + i]); Wire.write(k[_regName + i]);
} }
Wire.endTransmission(); Wire.endTransmission();
} }

View File

@ -0,0 +1,89 @@
#include <Inkplate.h>
// 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();
}

View File

@ -22,53 +22,62 @@
15 July 2020 by e-radionica.com 15 July 2020 by e-radionica.com
*/ */
#include "Inkplate.h" //Include Inkplate library to the sketch #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) 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 number = 0; //Variable that stores our number
int n = 0; //Variable that keeps track on how many times display is partially updated int n = 0; //Variable that keeps track on how many times display is partially updated
void setup() { void setup()
display.begin(); //Init Inkplate library (you should call this function ONLY ONCE) {
display.clearDisplay(); //Clear frame buffer of display display.begin(); //Init Inkplate library (you should call this function ONLY ONCE)
display.display(); //Put clear image on display display.clearDisplay(); //Clear frame buffer of display
display.setTextSize(5); //Set text scaling to five (text will be five times bigger) display.display(); //Put clear image on display
display.setTextColor(BLACK, WHITE); //Set text color to black and background color to white display.setTextSize(5); //Set text scaling to five (text will be five times bigger)
displayNumber(); //Call our function to display nubmer on screen 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() { void loop()
if (display.readTouchpad(PAD1)) { //Check if first pad has been touched. If it is, decrement the number and refresh the screen. {
if (display.readTouchpad(PAD1))
{ //Check if first pad has been touched. If it is, decrement the number and refresh the screen.
number--; number--;
displayNumber(); 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; number = 0;
displayNumber(); 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++; number++;
displayNumber(); 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 //Function that will write you number to screen
void displayNumber() { 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.clearDisplay(); //First, lets delete everything from frame buffer
display.print(number, DEC); //Print the number display.setCursor(385, 280); //Set print cursor at X=385, Y=280 (roughly in the middle of the screen)
display.setCursor(255, 560); //Set new print position (right above first touchpad) display.print(number, DEC); //Print the number
display.print('-'); //Print minus sign display.setCursor(255, 560); //Set new print position (right above first touchpad)
display.setCursor(385, 560); //Set new print position (right above second touchpad) display.print('-'); //Print minus sign
display.print('0'); //Print zero display.setCursor(385, 560); //Set new print position (right above second touchpad)
display.setCursor(520, 560); //Set new print position (right above third touchpad) display.print('0'); //Print zero
display.print('+'); //Print plus sign display.setCursor(520, 560); //Set new print position (right above third touchpad)
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.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(); display.display();
n = 0; n = 0;
} else { }
else
{
display.partialUpdate(); display.partialUpdate();
n++; n++;
} }