Skip to content

Commit

Permalink
Adapt flash storage to be used with C
Browse files Browse the repository at this point in the history
  • Loading branch information
TiboDevC committed Dec 8, 2022
1 parent ea5ceb7 commit 7b708cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 57 deletions.
20 changes: 14 additions & 6 deletions alarm_clock/alarm_flash_storage.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#include "Arduino.h"
#include <FlashStorage_SAMD.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "alarm_flash_storage.h"

#ifdef __cplusplus
}
#endif /* __cplusplus */

FlashStorage(alarm_0_flash, alarm_params_t);
FlashStorage(alarm_1_flash, alarm_params_t);

Expand All @@ -16,21 +24,21 @@ void init_alarm_flash_storage()
alarm_1_flash.read(alarm_1_ram);
}

void set_alarm_0(alarm_params_t &alarm)
void set_alarm_0(alarm_params_t *alarm)
{
alarm_0_ram = alarm;
alarm_0_flash.write(alarm);
alarm_0_ram = *alarm;
alarm_0_flash.write(*alarm);
}

alarm_params_t get_alarm_0()
{
return alarm_0_ram;
}

void set_alarm_1(alarm_params_t &alarm)
void set_alarm_1(alarm_params_t *alarm)
{
alarm_1_ram = alarm;
alarm_1_flash.write(alarm);
alarm_1_ram = *alarm;
alarm_1_flash.write(*alarm);
}

alarm_params_t get_alarm_1()
Expand Down
40 changes: 10 additions & 30 deletions alarm_clock/alarm_flash_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,17 @@ union days_u {
};

struct alarm_params_t {
bool is_set{false};
uint8_t alarm_hour{}; // when it should ring
uint8_t alarm_minute{}; // when it should ring
bool rings_tomorrow{true}; // is the user disable alarm for the next day
days_u alarm_days{};

friend bool operator!=(const alarm_params_t &a0, const alarm_params_t &a1)
{
if (a0.is_set != a1.is_set) {
return true;
}
if (a0.alarm_hour != a1.alarm_hour) {
return true;
}
if (a0.alarm_minute != a1.alarm_minute) {
return true;
}
if (a0.rings_tomorrow != a1.rings_tomorrow) {
return true;
}
if (a0.alarm_days.value != a1.alarm_days.value) {
return true;
}
return false;
}
uint8_t is_set;
uint8_t alarm_hour; // when it should ring
uint8_t alarm_minute; // when it should ring
uint8_t rings_tomorrow; // is the user disable alarm for the next day
union days_u alarm_days;
};

void init_alarm_flash_storage();
void set_alarm_0(alarm_params_t &alarm);
alarm_params_t get_alarm_0();
void set_alarm_1(alarm_params_t &alarm);
alarm_params_t get_alarm_1();
void init_alarm_flash_storage();
void set_alarm_0(struct alarm_params_t *alarm);
struct alarm_params_t get_alarm_0();
void set_alarm_1(struct alarm_params_t *alarm);
struct alarm_params_t get_alarm_1();

#endif // ALARM_CLOCK_FLASH_STORAGE_H
27 changes: 6 additions & 21 deletions alarm_clock/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@

#include <RTCZero.h>

#include "alarm_flash_storage.h"
#ifdef __cplusplus

extern "C" {
#endif /* __cplusplus */
#include "DEV_Config.h"

#include "EPD.h"
#include "GUI_Paint.h"
#include "alarm_flash_storage.h"
#include "button.h"
#include "rtc_tool.h"
#include "screen.h"
Expand Down Expand Up @@ -146,26 +144,13 @@ void screen_display_param()
EPD_3IN7_1Gray_Display(_blackImage);
}

static int weekday(int year, int month, int day)
/* Calculate day of week in proleptic Gregorian calendar. Sunday == 0. */
{
int adjustment, mm, yy;
if (year < 2000)
year += 2000;
adjustment = (14 - month) / 12;
mm = month + 12 * adjustment - 2;
yy = year - adjustment;
return (day + (13 * mm - 1) / 5 + yy + yy / 4 - yy / 100 + yy / 400) % 7;
}

void screen_update_clock()
{
const uint8_t day = rtc_get_day();
const uint8_t month = rtc_get_month();
const uint8_t year = rtc_get_year();
const uint8_t hours = rtc_get_hours();
const uint8_t minutes = rtc_get_minutes();
const uint8_t day_of_week = weekday(year, month, day);
const uint8_t day_of_week = rtc_get_weekday();
const char *days_buf[] = {"dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"};
const char *days_short_buf[] = {"dim.", "lun.", "mar.", "mer.", "jeu.", "vend.", "same."};
const char *months_buf[] = {"",
Expand Down Expand Up @@ -362,10 +347,10 @@ void ui_button_event(const struct button_evt_t *button_evt)
alarm.alarm_hour = clock_time / 60;
alarm.is_set = true;
if (_alarm_select == 0) {
set_alarm_0(alarm);
set_alarm_0(&alarm);
SerialUSB.print("[screen] Setting alarm 0\n");
} else {
set_alarm_1(alarm);
set_alarm_1(&alarm);
SerialUSB.print("[screen] Setting alarm 1\n");
}
SerialUSB.print("Monday ");
Expand All @@ -392,11 +377,11 @@ void ui_button_event(const struct button_evt_t *button_evt)
if (button_evt->button_id == B_MENU_CLOCK) {
alarm_params_t alarm_0 = get_alarm_0();
alarm_0.is_set = !alarm_0.is_set;
set_alarm_0(alarm_0);
set_alarm_0(&alarm_0);
} else if (button_evt->button_id == B_MENU_SETTINGS) {
alarm_params_t alarm_1 = get_alarm_1();
alarm_1.is_set = !alarm_1.is_set;
set_alarm_1(alarm_1);
set_alarm_1(&alarm_1);
}
}
}
Expand Down

0 comments on commit 7b708cd

Please sign in to comment.