Skip to content

Feature Request: Soft Reset, Reading Power Button Presses, LCD Power Off, Processor Sleep, Touch Interrupt, Dual Core Support. #129

@JasonPittenger

Description

@JasonPittenger

I wrote a few features that I thought might be useful to incorporate into this library.
Of course, modify the names and code as you see fit.

Soft Reset:
This allows a reset based on software rather than the physical reset button

esp_sleep_enable_timer_wakeup(1);
esp_deep_sleep(1);

Read Power Button Presses:
The chip that reads the power button has built in interrupts for detecting pressing or holding the power button.
I have used this to put the m5 to sleep with a short press, or soft power down with a long press.
The short press is anything less than 3 seconds.
The long press is 3+ seconds.
Of course, holding the button for ~7 seconds forces a hard power down.

Wire1.beginTransmission(0x34);
Wire1.write(0x46);
Wire1.endTransmission();
Wire1.requestFrom(0x34, 1);
u8_PowerWasPressed = Wire1.read();
if((u8_PowerWasPressed & 0x01) == 0x01)
{
	//Power button was long pressed!
}
if((u8_PowerWasPressed & 0x02) == 0x02)
{
	//Power button was short pressed!
}
if(u8_PowerWasPressed & 0x03)
{
	//Clear IRQ
	Wire1.beginTransmission(0x34);
	Wire1.write(0x46);
	Wire1.write(0x03);
	Wire1.endTransmission();
}

LCD Power Off:
This completely turns off the LCD to reduce power usage.
The screen must be redrawn on power up.

M5.Axp.SetDCDC3(false);			//Turn LCD backlight off
M5.Lcd.sleep();					//Turn off LCD logic

LCD Power On:
The screen must be redrawn on power up.

M5.Lcd.wakeup();					//Wake up LCD logic
M5.Axp.SetDCDC3(true);				//Turn LCD backlight on

Processor Sleep:
The sleep mode does not effect what's displayed on the LCD.

void SleepProcessor(uint64_t time_in_us)
{
	if (time_in_us > 0) 
	{
		esp_sleep_enable_timer_wakeup(time_in_us);
	}
	else
	{
		esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
	}
	esp_light_sleep_start();
}

Touch Interrupt:
By default, the touch controller uses an interrupt whenever there is a touch detected.
I found the touch screen to be much more responsive if the interrupt pin is polled.
This goes it pin 39.
Whenever the pin is low, there is an unread touch.

#define TOUCH_PANEL_INTERRUPT_PIN		39

void setup() 
{
	pinMode(TOUCH_PANEL_INTERRUPT_PIN, INPUT);    // sets the digital pin as input to detect a touch input
}

void loop() 
{
	if(!digitalRead(TOUCH_PANEL_INTERRUPT_PIN))
	{
		M5.update();
	}
}

Dual-Core Support:
Here is all that is needed to set up a main task loop on the 2nd core.
You need to set "Tools->Arduino Runs On: Core 1" and "Tools->Events Run On: Core 0" for this to work.
Also, interfaces like I2C cannot be accessed by both loops or there will be data corruption on the bus.

void setup() 
{
	xTaskCreatePinnedToCore	(
								LoopCore0, 		//TaskFunction_t pvTaskCode,
								"LoopCore0", 		//const char *const pcName,
								4096, 			//const uint32_t usStackDepth,
								NULL, 			//void *const pvParameters,
								1,  				//UBaseType_t uxPriority,
								NULL, 			//TaskHandle_t *const pvCreatedTask
								0				//Core
							);
}

void loop() 
{
	//Main Loop Tasks running on Core 1
}

void LoopCore0(void *pvParameters)
{
	uint32_t WatchDogTimer;
	
	while(1)
	{
		//Second Loop Tasks running on Core 0

		if((millis() - WatchDogTimer) > 500)
		{
			delay(1);	//This must be called every 500ms to trigger the watchdog timer
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions