#include "header.h"
//------------- OLED ----------------------
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
//----------- OLED ---------------
SSD1306AsciiWire oled;
//-------------- TASK SCHEDULER ------------------------------
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
#include <TaskScheduler.h>
StatusRequest btn1St,btn2St; //globals
Scheduler ts;
//------------ RTC -----------------------
RTC_DS1307 rtc;
void rtcCB();
Task tRtc(500,TASK_FOREVER,&rtcCB,&ts,true); //get time and date from RTC, display info at oled
//--------------- BUTTONS ------------------------
#include <ezButton.h>
#define BTN1PIN 7
ezButton button1(BTN1PIN);
#define BTN2PIN 6
ezButton button2(BTN2PIN);
void buttonsCB();
Task tButtons(20,TASK_FOREVER,&buttonsCB,&ts,true); //monitorea estado de botones
//-------------- GLOBALS -------------------------
//-------------- SWITCH PINES --------------------
int SW1PIN=4;
int SW2PIN=2;
//------------ AlARM --------------------------
String strAlarmHour="06";
String strAlarmMinute="47";
//------------ TIMERS TASKS ---------------
StatusRequest tmr2sSt,tmrHsSt; //globals
//------------ TIMER 2s -------------------
void tmr2sCB();
Task tTimer2s(2000,TASK_ONCE,&tmr2sCB,&ts);
//------------ TIMER HALFs -------------------
void tmrHsCB();
Task tTimerHs(500,TASK_ONCE,&tmrHsCB,&ts);
//====================== SETUP ==========================
void setup () {
Serial.begin(57600);
//------------------- OLED -----------------------------------
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.setFont(System5x7);
oled.clear();
//-------------------- RTC ------------------------------------
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
//--------- CONFIG PINS -------------
pinMode(BTN1PIN, INPUT_PULLUP);
pinMode(BTN2PIN, INPUT_PULLUP);
pinMode(SW1PIN, INPUT);
pinMode(SW2PIN, INPUT);
//------- BUTTONS --------------------
tButtons.enable();
btn1St.setWaiting(1);
btn2St.setWaiting(1);
}
//================= LOOP ================================
void loop() {
//-------- SCHEDULER ---------------
ts.execute();
}
//========== CALLBACK BUTTONS ==================================
void buttonsCB()
{
button1.loop();
button2.loop();
if(button1.isPressed())
{
btn1St.signal();
}
if(button2.isPressed())
{
btn2St.signal();
}
}
//========== CALLBACK RTC ==================================
DateTime now; //now : global
void rtcCB()
{
now = rtc.now();
char buf1[] = "hh:mm:ss";
oled.set2X(); // Make font double
oled.setCursor(20,3); //20 pixeles hacia la derecha; 3x8 = 24 pixeles hacia abjo
oled.print(now.toString(buf1));
char buf3[] = "DDD, MMM DD YYYY";
oled.set1X();
oled.setCursor(20,0);
oled.print(now.toString(buf3));
oled.setCursor(20,6);
oled.print("Alarm:");
oled.setCursor(60,6);
oled.print(strAlarmHour);
oled.print(":");
oled.print(strAlarmMinute);
oled.setCursor(100,6);
if(digitalRead(SW2PIN)==HIGH)
{
oled.print("ON ");
}
else
{
oled.print("OFF");
}
}
//===================== CALLBACKS TIMERS =======================
void tmr2sCB()
{
tmr2sSt.signal();
//BREAK();
}
void tmrHsCB()
{
tmrHsSt.signal();
}