125 lines
3.1 KiB
C++
125 lines
3.1 KiB
C++
#include <array>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <vector>
|
|
#include "alarm.h"
|
|
|
|
|
|
|
|
alarm::alarm(uint8_t hours, uint8_t minutes, const std::array<bool, 7> &days_enabled, bool every_other_week, bool even_week){
|
|
this->minutes_store = minutes;
|
|
|
|
// Initialize scheduled field
|
|
this->scheduled = 0;
|
|
|
|
// Set bits 0-6 for days of the week
|
|
for (int i = 0; i < 7; i++) {
|
|
if (days_enabled[i]) {
|
|
this->scheduled |= (1 << i);
|
|
}
|
|
}
|
|
|
|
// Set bit 7 for every other week functionality
|
|
if (every_other_week) {
|
|
this->scheduled |= (1 << 7);
|
|
}
|
|
|
|
// Initialize hours field
|
|
this->hours_and_stuff_store = 0;
|
|
|
|
// Set bit 0 for even/odd week (only relevant if every_other_week is true)
|
|
if (every_other_week && even_week) {
|
|
this->hours_and_stuff_store |= (1 << 0);
|
|
}
|
|
|
|
// Set bit 1 as disabled flag
|
|
this->hours_and_stuff_store &= ~(1 << 1);
|
|
|
|
// Bit 2 is reserved (left as 0)
|
|
|
|
// Set bits 3-7 with actual hours value (shift left by 3)
|
|
this->hours_and_stuff_store |= (hours & 0x1F) << 3; // 0x1F masks to 5 bits (0-31)
|
|
};
|
|
|
|
uint8_t alarm::minutes() const {
|
|
return minutes_store;
|
|
}
|
|
|
|
uint8_t alarm::hours() const {
|
|
return (hours_and_stuff_store >> 3) & 0x1F; // Extract bits 3-7 and mask to 5 bits
|
|
}
|
|
|
|
std::array<bool, 7> alarm::days_enabled() const {
|
|
std::array<bool, 7> days;
|
|
for (int i = 0; i < 7; i++) {
|
|
days[i] = (scheduled & (1 << i)) != 0;
|
|
}
|
|
return days;
|
|
}
|
|
|
|
bool alarm::every_other_week() const {
|
|
return (scheduled & (1 << 7)) != 0;
|
|
}
|
|
|
|
bool alarm::even_week() const {
|
|
return (hours_and_stuff_store & (1 << 0)) != 0;
|
|
}
|
|
|
|
bool alarm::enabled() const {
|
|
return (hours_and_stuff_store & (1 << 1)) != 0;
|
|
}
|
|
|
|
// Enable/disable methods
|
|
void alarm::enable() {
|
|
hours_and_stuff_store |= (1 << 1); // Set bit 1
|
|
}
|
|
|
|
void alarm::disable() {
|
|
hours_and_stuff_store &= ~(1 << 1); // Clear bit 1
|
|
}
|
|
|
|
void alarm::set_state(bool enabled) {
|
|
if (enabled) {
|
|
enable();
|
|
} else {
|
|
disable();
|
|
}
|
|
}
|
|
|
|
void alarm::set_minutes(uint8_t minutes) {
|
|
this->minutes_store = minutes;
|
|
}
|
|
|
|
void alarm::set_hours(uint8_t hours) {
|
|
this->hours_and_stuff_store &= ~(0x1F << 3); // Clear bits 3-7
|
|
this->hours_and_stuff_store |= (hours & 0x1F) << 3; // Set new hours
|
|
}
|
|
|
|
void alarm::set_days_enabled(const std::array<bool, 7>& days_enabled) {
|
|
this->scheduled &= (1 << 7); // Keep only bit 7 (every_other_week)
|
|
|
|
// Set new day bits
|
|
for (int i = 0; i < 7; i++) {
|
|
if (days_enabled[i]) {
|
|
this->scheduled |= (1 << i);
|
|
}
|
|
}
|
|
}
|
|
|
|
void alarm::set_every_other_week(bool every_other_week) {
|
|
if (every_other_week) {
|
|
this->scheduled |= (1 << 7); // Set bit 7
|
|
} else {
|
|
this->scheduled &= ~(1 << 7); // Clear bit 7
|
|
}
|
|
}
|
|
|
|
void alarm::set_even_week(bool even_week) {
|
|
if (even_week) {
|
|
this->hours_and_stuff_store |= (1 << 0); // Set bit 0
|
|
} else {
|
|
this->hours_and_stuff_store &= ~(1 << 0); // Clear bit 0
|
|
}
|
|
}
|
|
|
|
std::vector<alarm> alarms; |