FamilyPlanner/FamilyPlanner.ino

218 lines
7.1 KiB
C++

/*
Clock for e-radionica.com Inkplate 6
For this example you will need USB cable and Inkplate 6.
Select "Inkplate 6(ESP32)" from Tools -> Board menu.
Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it:
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
This example will show you how you can use low power functionality of Inkplate board.
In deep sleep, whole board will consume about 25uA from battery.
Inkplate will wake every 60 seconds change content on screen.
NOTE: Because we are using deep sleep, everytime the board wakes up,
it starts program from beginning. Also, whole content from RAM gets erased.
To use partial updates after wakeup, the old bitmap in RAM is recreated.
Want to learn more about Inkplate? Visit www.inkplate.io
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
15 July 2020 by e-radionica.com
*/
#include "Inkplate.h" //Include Inkplate library to the sketch
#include "driver/rtc_io.h" //ESP32 library used for deep sleep and RTC wake up pins
#include "driver/adc.h"
#include <esp_wifi.h>
#include <esp_bt.h>
//Including fonts
//#include "Fonts/FreeSerif18pt7b.h"
//#include "Fonts/FreeSansBold24pt7b.h"
#include "Fonts/RobotoCondensed-Regular18pt8b.h"
#include "Fonts/RobotoCondensed-Medium24pt8b.h"
//Including bitmaps (created with the LCD Image Converter)
typedef struct {
const uint8_t *data;
uint16_t width;
uint16_t height;
uint8_t dataSize;
} tImage;
#include "binary_icons/icon_char_colon.h"
#include "binary_icons/icon_digit_0.h"
#include "binary_icons/icon_digit_1.h"
#include "binary_icons/icon_digit_2.h"
#include "binary_icons/icon_digit_3.h"
#include "binary_icons/icon_digit_4.h"
#include "binary_icons/icon_digit_5.h"
#include "binary_icons/icon_digit_6.h"
#include "binary_icons/icon_digit_7.h"
#include "binary_icons/icon_digit_8.h"
#include "binary_icons/icon_digit_9.h"
const uint8_t *digits[10] = { image_data_icon_digit_0, image_data_icon_digit_1, image_data_icon_digit_2, image_data_icon_digit_3, image_data_icon_digit_4, image_data_icon_digit_5, image_data_icon_digit_6, image_data_icon_digit_7, image_data_icon_digit_8, image_data_icon_digit_9 };
//Includes
#include <ctime>
#include "Network.h"
//CHANGE HERE ---------------
char *ssid = "Freifunk";
char *pass = "";
char *calendarURL = "";
char *timeZone = "CET-1CEST,M3.5.0/02,M10.5.0/03";
//Set to 1 to flip the screen 180 degrees
#define ROTATION 3
//---------------------------
#define uS_TO_S_FACTOR 1000000 //Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60 //How long ESP32 will be in deep sleep (in seconds)
RTC_DATA_ATTR int timeDigits[4] = { 9, 5, 0, 0 }; // start time: 23:59
RTC_DATA_ATTR char calName[5][31];
RTC_DATA_ATTR char calStart[5][26];
RTC_DATA_ATTR char calTitle[5][81];
Inkplate display(INKPLATE_1BIT); //Create an object on Inkplate library and also set library into 1 Bit mode
Network network; //Our networking functions, see Network.cpp for info
//All our functions declared below setup and loop
void drawTime();
void drawCal();
void drawCalLine1(int entry);
void drawCalLine2(int entry);
void setup() {
bool networking = false;
//Initial display settings
display.begin(); //Before calling any display method you must call .begin()
display.clearDisplay(); //Clears all data in buffer.
display.setRotation(ROTATION);
//restore old bitmap (before the deep sleep)
drawTime();
drawCal();
display.load1b();
//Update data
if ((timeDigits[1] == 5) && (timeDigits[0] == 9)) {
network.begin(); // every hour set the time from internet
network.off();
networking = true;
}
network.getTime(timeDigits);
// create new bitmap
display.clearDisplay();
drawTime();
drawCal();
//Actually display all data
if (timeDigits[0]+timeDigits[1] == 0 || networking) {
display.display(); // full refresh at full hour or after network update
}
else
display.partialUpdate();
//Sleep
adc_power_off(); //don't forget to turn ADC back before using it (also after wake up from deep sleep)
esp_wifi_stop();
rtc_gpio_isolate(GPIO_NUM_12); //Isolate/disable GPIO12 on ESP32 (only to reduce power consumption in sleep)
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //Activate wake-up timer -- wake up after 20s here
esp_deep_sleep_start(); //Put ESP32 into deep sleep. Program stops here.
}
void loop() { }
//Drawing what time it is
void drawTime()
{
display.drawBitmap( 0, 0, digits[timeDigits[3]], 134, 227, WHITE, BLACK); //hours (tens)
display.drawBitmap(134, 0, digits[timeDigits[2]], 134, 227, WHITE, BLACK); //hours (units)
display.drawBitmap(268, 0, image_data_icon_char_colon, 64, 227, WHITE, BLACK); //colon
display.drawBitmap(332, 0, digits[timeDigits[1]], 134, 227, WHITE, BLACK); //minutes (tens)
display.drawBitmap(466, 0, digits[timeDigits[0]], 134, 227, WHITE, BLACK); //minutes (units)
}
void drawCal()
{
display.setCursor(5, 275);
drawCalLine1(0);
display.setCursor(20, 325);
drawCalLine2(0);
display.setCursor(5, 390);
drawCalLine1(1);
display.setCursor(20, 440);
drawCalLine2(1);
display.setCursor(5, 505);
drawCalLine1(2);
display.setCursor(20, 555);
drawCalLine2(2);
display.setCursor(5, 620);
drawCalLine1(3);
display.setCursor(20, 670);
drawCalLine2(3);
display.setCursor(5, 735);
drawCalLine1(4);
display.setCursor(20, 785);
drawCalLine2(4);
}
void drawCalLine1(int entry)
{
char year[5], month[3], day[3], hour[3], minute[3];
if (strlen(calStart[entry]) > 9)
{
strncpy(year, calStart[entry], 4);
year[4] = '\0';
strncpy(month, calStart[entry]+5, 2);
month[2] = '\0';
strncpy(day, calStart[entry]+8, 2);
day[2] = '\0';
display.setFont(&RobotoCondensed_Regular18pt8b);
display.setTextSize(1);
display.setTextWrap(false);
// date
display.print(day);
display.print(".");
display.print(month);
display.print(".");
display.print(year);
if (strlen(calStart[entry]) > 18)
{
strncpy(hour, calStart[entry]+11, 2);
hour[2] = '\0';
strncpy(minute, calStart[entry]+14, 2);
minute[2] = '\0';
// time
display.print(", ");
display.print(hour);
display.print(":");
display.print(minute);
}
display.print(" (");
display.print(calName[entry]);
display.print("):");
}
}
void drawCalLine2(int entry)
{
display.setFont(&RobotoCondensed_Medium24pt8b);
display.setTextSize(1);
display.setTextWrap(false);
display.print(calTitle[entry]);
}