Merge pull request #29 from nitko12/master

Added touch wake up example
This commit is contained in:
David Zovko 2020-09-01 12:50:57 +02:00 committed by GitHub
commit 9693bcb9ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 397 additions and 268 deletions

View File

@ -26,7 +26,8 @@
#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
@ -34,7 +35,8 @@ static inline void wiresend(uint8_t 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,13 +85,13 @@ 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);
@ -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;
@ -120,21 +127,24 @@ void Adafruit_MCP23017::begin(uint8_t addr) {
/** /**
* 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,11 +195,11 @@ 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);
@ -199,11 +212,13 @@ void Adafruit_MCP23017::digitalWrite(uint8_t pin, uint8_t d) {
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 bit = bitForPin(pin);
uint8_t regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB); uint8_t regAddr = regForPin(pin, MCP23017_GPIOA, MCP23017_GPIOB);
return (readRegister(regAddr) >> bit) & 0x1; return (readRegister(regAddr) >> bit) & 0x1;
@ -218,7 +233,8 @@ 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);
@ -241,7 +257,8 @@ 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);
@ -253,26 +270,31 @@ void Adafruit_MCP23017::setupInterruptPin(uint8_t pin, uint8_t mode) {
// 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(); uint8_t intPin = getLastInterruptPin();
if(intPin!=MCP23017_INT_ERR){ if (intPin != MCP23017_INT_ERR)
{
uint8_t intcapreg = regForPin(intPin, MCP23017_INTCAPA, MCP23017_INTCAPB); uint8_t intcapreg = regForPin(intPin, MCP23017_INTCAPA, MCP23017_INTCAPB);
uint8_t bit = bitForPin(intPin); uint8_t bit = bitForPin(intPin);
return (readRegister(intcapreg) >> bit) & (0x01); return (readRegister(intcapreg) >> bit) & (0x01);
@ -280,5 +302,3 @@ uint8_t Adafruit_MCP23017::getLastInterruptPinValue(){
return MCP23017_INT_ERR; return MCP23017_INT_ERR;
} }

View File

@ -813,8 +813,10 @@ 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++) { ;
for (int k = 0; k < rep; k++)
{
vscan_start(); vscan_start();
for (int i = 0; i < 600; i++) for (int i = 0; i < 600; i++)
{ {
@ -952,7 +954,8 @@ 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))
return; //Check if the pin is set as an output
_state ? (mcpRegsInt[MCP23017_GPIOA + _port] |= (1 << _p)) : (mcpRegsInt[MCP23017_GPIOA + _port] &= ~(1 << _p)); _state ? (mcpRegsInt[MCP23017_GPIOA + _port] |= (1 << _p)) : (mcpRegsInt[MCP23017_GPIOA + _port] &= ~(1 << _p));
updateRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt[MCP23017_GPIOA + _port]); updateRegister(MCP23017_ADDR, MCP23017_GPIOA + _port, mcpRegsInt[MCP23017_GPIOA + _port]);
} }
@ -1129,7 +1132,8 @@ void Inkplate::display1b()
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); _send = ((data & B00000011) << 4) | (((data & B00001100) >> 2) << 18) | (((data & B00010000) >> 4) << 23) | (((data & B11100000) >> 5) << 25);
hscan_start(_send); hscan_start(_send);
data = 0b00000000; data = 0b00000000;
@ -1976,7 +1980,8 @@ 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)
return false;
readMCPRegisters(_addr, _r); readMCPRegisters(_addr, _r);
_r[0] = 0xff; _r[0] = 0xff;
_r[1] = 0xff; _r[1] = 0xff;

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

@ -27,7 +27,8 @@ Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and a
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.begin(); //Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); //Clear frame buffer of display display.clearDisplay(); //Clear frame buffer of display
display.display(); //Put clear image on display display.display(); //Put clear image on display
@ -36,18 +37,22 @@ void setup() {
displayNumber(); //Call our function to display nubmer on screen 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();
} }
@ -55,7 +60,8 @@ void loop() {
} }
//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.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.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.print(number, DEC); //Print the number
@ -65,10 +71,13 @@ void displayNumber() {
display.print('0'); //Print zero display.print('0'); //Print zero
display.setCursor(520, 560); //Set new print position (right above third touchpad) display.setCursor(520, 560); //Set new print position (right above third touchpad)
display.print('+'); //Print plus sign 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 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++;
} }

View File

@ -92,7 +92,7 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
data = (char*)ps_malloc(100000); data = (char*)ps_malloc(2000000LL);
//Initial display settings //Initial display settings
display.begin(); display.begin();

View File

@ -18,7 +18,10 @@ void Network::begin()
delay(1000); delay(1000);
++cnt; ++cnt;
if (cnt == 20) WiFi.reconnect();
delay(5000);
if (cnt == 10)
{ {
Serial.println("Can't connect to WIFI, restarting"); Serial.println("Can't connect to WIFI, restarting");
delay(100); delay(100);
@ -67,7 +70,10 @@ bool Network::getData(char *data)
delay(1000); delay(1000);
++cnt; ++cnt;
if (cnt == 7) WiFi.reconnect();
delay(5000);
if (cnt == 10)
{ {
Serial.println("Can't connect to WIFI, restart initiated."); Serial.println("Can't connect to WIFI, restart initiated.");
delay(100); delay(100);