Skip to content

Commit

Permalink
Merge pull request #9 from gabrielosorio/backlight_dimming
Browse files Browse the repository at this point in the history
Backlight dimming
  • Loading branch information
gabrielosorio authored May 29, 2023
2 parents 7f1127a + 8714c13 commit f8ae3b6
Showing 1 changed file with 71 additions and 17 deletions.
88 changes: 71 additions & 17 deletions wakie.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const char *weekdayNames[7] = {
bool alarmDeactivated = false;

#define LCD_LED 10
uint8_t lcdLedState = HIGH;
int backlightDimTimeout = 6000; // Milliseconds
unsigned long nextBacklightDimTimestamp = 0;

#define PIEZO 11
const uint8_t noteBitmapRows = 2;
Expand All @@ -68,46 +71,58 @@ void setup() {

pinMode(BUTTON, INPUT);
pinMode(LCD_LED, OUTPUT);
digitalWrite(LCD_LED, HIGH);
enableLcdBacklight();
}

void loop() {
tmElements_t tm;
readRtc();

if (RTC.read(tm)) {
currentHour = tm.Hour;
currentMinute = tm.Minute;
currentWeekday = tm.Wday;
currentDay = tm.Day;
currentMonth = tm.Month;
currentYear = tm.Year;
} else {
Serial.print("[DS1307] Read error!");
}
handleLcdBacklightDimming();

if (currentMinute != lastMinute) {
lastMinute = currentMinute;
renderDisplay();
}

// Current duration is within the specified alarmMinute
if (alarmTimeIsReached(currentHour, currentMinute)) {
if (alarmTimeIsReached()) {
if (!alarmDeactivated) {
soundAlarm();
}
} else {
// Outside of the alarm window reset the
// alarmDeactivated check if it's enabled
if (alarmDeactivated) {
Serial.println("Resetting button");
alarmDeactivated = false;
}
}
}

void buttonInterrupt() {
Serial.println("Button pressed");
alarmDeactivated = true;

// Use button to deactivate alarm if currently sounding
if (alarmTimeIsReached()) {
alarmDeactivated = true;
} else {
// Otherwise use button to toggle backlight on/off
toggleLcdBacklight();
}
}

void readRtc() {
tmElements_t tm;

if (RTC.read(tm)) {
currentHour = tm.Hour;
currentMinute = tm.Minute;
currentWeekday = tm.Wday;
currentDay = tm.Day;
currentMonth = tm.Month;
currentYear = tm.Year;
} else {
Serial.print("[DS1307] Read error!");
}
}

void renderDisplay() {
Expand Down Expand Up @@ -161,14 +176,17 @@ void numberToDoubleDigitChar(uint8_t number, char *output) {
}
}

bool alarmTimeIsReached(uint8_t hour, uint8_t minute) {
if (hour == alarmHour && minute == alarmMinute) {
bool alarmTimeIsReached() {
if (currentHour == alarmHour && currentMinute == alarmMinute) {
return true;
}
return false;
}

void soundAlarm() {
// Keep LCD backlight on during the alarm
enableLcdBacklight();

// Go through the note bitmap rows
for (uint8_t row = 0; row < noteBitmapRows; row++) {
// Go through the columns in the note bitmap row
Expand Down Expand Up @@ -198,3 +216,39 @@ void handleCurrentNoteOff() {
Serial.println("Note Off");
delay(noteDuration);
}

void handleLcdBacklightDimming() {
if (lcdBacklightIsOn()) {
if (nextBacklightDimTimestamp == 0) {
// Arm a timeout to dim the backlight after the set interval
nextBacklightDimTimestamp = millis() + backlightDimTimeout;
}

// Check if the backlight timeout was reached
if (millis() >= nextBacklightDimTimestamp) {
disableLcdBacklight();
}
} else {
// Disarm dimming timeout
nextBacklightDimTimestamp = 0;
}
}

bool lcdBacklightIsOn() {
return lcdLedState == HIGH;
}

void enableLcdBacklight() {
lcdLedState = HIGH;
digitalWrite(LCD_LED, lcdLedState);
}

void disableLcdBacklight() {
lcdLedState = LOW;
digitalWrite(LCD_LED, lcdLedState);
}

void toggleLcdBacklight() {
lcdLedState = !lcdLedState;
digitalWrite(LCD_LED, lcdLedState);
}

0 comments on commit f8ae3b6

Please sign in to comment.