Zum Hauptinhalt springen

RTC alarm with an interrupt

What this example shows​

React to the RTC alarm with an interrupt instead of polling. The sketch sets the clock from an epoch value (seconds since 1 January 1970) and sets an alarm 10 seconds ahead. The RTC pulls its interrupt line low when the alarm fires. The sketch watches that line on GPIO39, which its comments name as the RTC interrupt pin, and sets a flag from the interrupt handler alarmISR().

The handler is marked IRAM_ATTR and has to be defined above setup() and loop().

Example code​

In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.

Select Board
Inkplate10_RTC_Interrupt_Alarm.ino
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

#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 (BW)

volatile int _alarmFlag = 0; // Variable to store alarm flag

void IRAM_ATTR alarmISR() // This function will be called when alarm interrupt event happens
{ // NOTE: Function must be above setup() and loop()!
_alarmFlag = 1; // Set alarm flag
}

void setup()
{
pinMode(39, INPUT_PULLUP); // Set RTC INT pin on ESP32 GPIO39 as input with pullup resistor enabled

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 to be 5 times bigger than classic 5x7 px text

display.rtc.setEpoch(1589610300);
display.rtc.setAlarmEpoch(display.rtc.getEpoch() + 10, RTC_ALARM_MATCH_DHHMMSS);

attachInterrupt(39, alarmISR, FALLING); // Set interrupt function and interrupt mode
}

// Variable that keeps count on how much screen has been partially updated
int n = 0;
void loop()
{
display.clearDisplay(); // Clear frame buffer of display
display.setCursor(100, 100); // Set position of the text
display.rtc.getRtcData(); // Get the time and date from RTC

// Print the time on screen
printTime(display.rtc.getHour(), display.rtc.getMinute(), display.rtc.getSecond(), display.rtc.getDay(), display.rtc.getWeekday(), display.rtc.getMonth(), display.rtc.getYear());

if (_alarmFlag) // Check alarm flag
{
// _alarmFlag = 0; // Uncomment if you want to clear this flag
display.rtc.clearAlarmFlag(); // It's recommended to clear alarm flag after alarm has occurred
display.setCursor(200, 200); // Set position of the text
display.print("ALARM"); // Print text
}

if (n > 9) // Check if you need to do full refresh or you can do partial update
{
display.display(true); // Do a full refresh
n = 0;
}
else
{
display.partialUpdate(false, true); // Do partial update and keep e-papr power supply on
n++; // Keep track on how many times screen has been partially updated
}

delay(700); // Delay between refreshes.
}

void printTime(uint8_t _hour, uint8_t _minutes, uint8_t _seconds, uint8_t _day, uint8_t _weekday, uint8_t _month,
uint16_t _year)
{
// Write time and date info on screen
char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

print2Digits(_hour);
display.print(':');
print2Digits(_minutes);
display.print(':');
print2Digits(_seconds);

display.print(' ');

display.print(wday[_weekday]);
display.print(", ");
print2Digits(_day);
display.print('/');
print2Digits(_month);
display.print('/');
display.print(_year, DEC);
}

void print2Digits(uint8_t _d)
{
if (_d < 10)
display.print('0');
display.print(_d, DEC);
}

Trimmed from Inkplate10_RTC_Interrupt_Alarm.ino: the file header comment is left out. Three commented-out lines are also left out, because they call functions that the pinned library does not declare (rtcSetTime(), rtcSetDate(), rtcSetAlarm() on the display object). The code is otherwise unchanged.

Functions used​

Inkplate(uint8_t mode)returns None

Creates the Inkplate object and chooses the display mode. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` is grayscale and only supports full updates.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tmodeDisplay mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1).
pinMode(uint8_t pin, uint8_t mode)returns void

Arduino function that sets the mode of an ESP32 pin. The sketch sets GPIO 39, the RTC interrupt pin, as an input with the pull-up resistor enabled.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tpinGPIO number. The sketch passes 39.
uint8_tmodePin mode. The sketch passes INPUT_PULLUP.
display.begin()returns void

Initializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.

ReturnsNothing.

display.clearDisplay()returns void

Clears the frame buffer in memory. It does not clear the panel itself, call `display()` afterwards to do that.

ReturnsNothing.

display.display(bool _leaveOn = 0)returns void

Refreshes the whole panel from the frame buffer (a full update).

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnIf true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0.
display.setTextSize(uint8_t s)returns void

Sets the text magnification. Size 1 is the classic 5x7 pixel font, size 5 is five times bigger. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsText size multiplier.
display.rtc.setEpoch(uint32_t _epoch)returns void

Sets the RTC time and date from an epoch value (seconds since 1 January 1970).

ReturnsNothing.

Parameters

TypeNameDescription
uint32_t_epochEpoch to write to the RTC.
display.rtc.setAlarmEpoch(uint32_t _epoch, uint8_t _match)returns void

Sets the RTC alarm from an epoch value and enables it. The match argument chooses which fields of that time have to match for the alarm to fire.

ReturnsNothing.

Parameters

TypeNameDescription
uint32_t_epochEpoch of the alarm time.
uint8_t_matchWhich fields to match, for example RTC_ALARM_MATCH_DHHMMSS (day, hour, minute, second). The library also defines RTC_ALARM_MATCH_SS, RTC_ALARM_MATCH_MMSS, RTC_ALARM_MATCH_HHMMSS and RTC_ALARM_MATCH_WHHMMSS.
display.rtc.getEpoch()returns uint32_t

Reads the RTC and returns the current time and date as an epoch value.

ReturnsCurrent epoch (seconds since 1 January 1970).

attachInterrupt(uint8_t pin, void (*handler)(void), int mode)returns void

Arduino function that runs a handler function when a pin changes. The sketch calls `alarmISR` on a `FALLING` edge of GPIO 39.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tpinGPIO number. The sketch passes 39.
void (*)(void)handlerFunction to call. The sketch passes alarmISR.
intmodeTrigger edge. The sketch passes FALLING.
display.setCursor(int16_t x, int16_t y)returns void

Sets where the next printed text starts, in pixels from the top left corner. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txX coordinate in pixels.
int16_tyY coordinate in pixels.
display.rtc.getRtcData()returns void

Reads the time and date from the RTC.

ReturnsNothing.

display.rtc.getSecond()returns uint8_t

Returns the current second from the RTC.

ReturnsSeconds.

display.rtc.getMinute()returns uint8_t

Returns the current minute from the RTC.

ReturnsMinutes.

display.rtc.getHour()returns uint8_t

Returns the current hour from the RTC.

ReturnsHours.

display.rtc.getDay()returns uint8_t

Returns the current day of the month from the RTC.

ReturnsDay of the month.

display.rtc.getWeekday()returns uint8_t

Returns the current day of the week from the RTC.

ReturnsDay of the week, 0 to 6.

display.rtc.getMonth()returns uint8_t

Returns the current month from the RTC.

ReturnsMonth, 1 to 12.

display.rtc.getYear()returns uint16_t

Returns the current year from the RTC.

ReturnsFull year, for example `2022`.

display.rtc.clearAlarmFlag()returns void

Clears the RTC alarm flag. Call it after an alarm has gone off so the next one can be detected.

ReturnsNothing.

display.print(value, format)returns size_t

Prints text, a character or a number at the cursor position, in the frame buffer. The display object builds on the Adafruit GFX base class, which extends the Arduino `Print` class, so it prints like `Serial` does. Call `display()` or `partialUpdate()` to show the result.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print.
intformatOptional. Number base for integers, for example DEC.
display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_t

Updates only the parts of the screen that changed, without a full refresh. It works in 1-bit mode only, in 3-bit mode it does nothing and returns 0. A full refresh every 5 to 10 partial updates keeps the image quality good.

ReturnsNumber of pixels that changed from black to white, which are the ones that leave visible blur.

Parameters

TypeNameDescription
bool_forcedFor advanced use with deep sleep: forces a partial update. Defaults to false.
boolleaveOnIf true, the e-paper supply stays on after the update. Defaults to false.
delay(unsigned long ms)returns void

Arduino function that pauses the sketch.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longmsTime to wait, in milliseconds.

What to expect​

The time and date refresh about every 700 milliseconds, starting from the epoch set in setup(). About 10 seconds after start the interrupt fires and ALARM appears below the time. It stays on screen, because the sketch has the line that resets _alarmFlag commented out. The sketch does not print to Serial.