Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to make default time on the watch be PC clock's during … #414

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
78 changes: 62 additions & 16 deletions make.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ BUILD = ./build-sim
endif
BIN = watch

ifndef COLOR
$(error Set the COLOR variable to RED, BLUE, GREEN or PRO depending on what board you have.)
endif

COLOR_VALID := $(filter $(COLOR),RED BLUE GREEN PRO)

ifeq ($(COLOR_VALID),)
$(error COLOR must be RED, BLUE, GREEN or PRO)
endif

ifeq ($(COLOR), PRO)
override BOARD = OSO-SWAT-C1-00
else
override BOARD = OSO-SWAT-A1-05
endif

##############################################################################
.PHONY: all directory clean size

Expand Down Expand Up @@ -215,6 +199,29 @@ SRCS += \

endif

ifeq ($(filter $(MAKECMDGOALS),clean analyze size directory install),)

ifeq ($(LED), BLUE)
CFLAGS += -DWATCH_IS_BLUE_BOARD
endif

ifndef COLOR
COLOR := NONE # Change to your board's color to not need to set the COLOR parameter on every make
$(info COLOR is set to $(COLOR) by default.)
endif

COLOR_VALID := $(filter $(COLOR),RED BLUE GREEN)

ifeq ($(COLOR_VALID),)
$(error Set the COLOR variable to RED, BLUE, GREEN or PRO depending on what board you have.)
endif

ifeq ($(COLOR), PRO)
override BOARD = OSO-SWAT-C1-00
else
override BOARD = OSO-SWAT-A1-05
endif

ifeq ($(COLOR), BLUE)
CFLAGS += -DWATCH_IS_BLUE_BOARD
endif
Expand All @@ -238,3 +245,42 @@ endif
ifdef CLOCK_FACE_24H_ONLY
CFLAGS += -DCLOCK_FACE_24H_ONLY
endif

# DATE = X
# YEAR = Sets the year and timezone to the PC's
# DAY = Sets the default time down to the day (year, month, day, timezone)
# MIN = Sets the default time down to the minute (year, month, day, timezone, hour, minute)
ifdef DATE
ifneq ($(DETECTED_OS), LINUX)
$(error DATE parameter only works with Linux.)
endif
TIMEZONE := $(shell date +%z | awk '{print substr($$0, 1, 3) * 60 + substr($$0, 4, 2)}')
CURRENT_YEAR := $(shell echo $$(($(shell date +"%Y") - 2020)))
CURRENT_MONTH := $(shell date +"%-m")
CURRENT_DAY := $(shell date +"%-d")
CURRENT_HOUR := $(shell date +"%-H")
CURRENT_MINUTE := $(shell date +"%-M")
Comment on lines +259 to +262
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds a direct dependency on GNU coreutils for building the firmware. I consider that to be perfectly OK but then again I've been running Linux for over a decade. I have no idea how this impacts Windows users. Maintainers might want to double check this.

ifeq ($(DATE), YEAR)
CFLAGS += -DINITIAL_TIMEZONE=$(TIMEZONE)
CFLAGS += -DINITIAL_YEAR=$(CURRENT_YEAR)
$(info Default year and timezone are set to $(shell date +"%Y") $(shell date +%Z))
else ifeq ($(DATE), DAY)
CFLAGS += -DINITIAL_TIMEZONE=$(TIMEZONE)
CFLAGS += -DINITIAL_YEAR=$(CURRENT_YEAR)
CFLAGS += -DINITIAL_MONTH=$(CURRENT_MONTH)
CFLAGS += -DINITIAL_DAY=$(CURRENT_DAY)
$(info Default date set to $(shell date +"%b") $(CURRENT_DAY) $(shell date +"%Y") $(shell date +%Z))
else ifeq ($(DATE), MIN)
CFLAGS += -DINITIAL_TIMEZONE=$(TIMEZONE)
CFLAGS += -DINITIAL_YEAR=$(CURRENT_YEAR)
CFLAGS += -DINITIAL_MONTH=$(CURRENT_MONTH)
CFLAGS += -DINITIAL_DAY=$(CURRENT_DAY)
CFLAGS += -DINITIAL_HOUR=$(CURRENT_HOUR)
CFLAGS += -DINITIAL_MINUTE=$(CURRENT_MINUTE)
$(info Default time set to $(CURRENT_HOUR):$(shell printf "%02d" $(CURRENT_MINUTE)) on $(shell date +"%b") $(CURRENT_DAY) $(shell date +"%Y") $(shell date +%Z))
else
$(error DATE must be YEAR, DAY, or MIN if used.)
endif

endif
endif
8 changes: 8 additions & 0 deletions movement/movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ void app_init(void) {
movement_state.birthdate.bit.year = MOVEMENT_DEFAULT_BIRTHDATE_YEAR;
movement_state.birthdate.bit.month = MOVEMENT_DEFAULT_BIRTHDATE_MONTH;
movement_state.birthdate.bit.day = MOVEMENT_DEFAULT_BIRTHDATE_DAY;
#ifdef INITIAL_TIMEZONE
for (int i = 0, count = sizeof(movement_timezone_offsets) / sizeof(movement_timezone_offsets[0]); i < count; i++) {
if (movement_timezone_offsets[i] == INITIAL_TIMEZONE) {
movement_state.settings.bit.time_zone = i;
break;
}
}
#endif
movement_state.light_ticks = -1;
movement_state.alarm_ticks = -1;
movement_state.next_available_backup_register = 4;
Expand Down
51 changes: 50 additions & 1 deletion watch-library/hardware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,51 @@
#include "watch.h"
#include "tusb.h"

static inline uint32_t get_initial_year(void)
{
#ifdef INITIAL_YEAR
return INITIAL_YEAR;
#else
return 4;
#endif
}

static inline uint32_t get_initial_month(void)
{
#ifdef INITIAL_MONTH
return INITIAL_MONTH;
#else
return 0;
#endif
}

static inline uint32_t get_inital_day(void)
{
#ifdef INITIAL_DAY
return INITIAL_DAY;
#else
return 0;
#endif
}

static inline uint32_t get_inital_hour(void)
{
#ifdef INITIAL_HOUR
return INITIAL_HOUR;
#else
return 0;
#endif
}

static inline uint32_t get_inital_minute(void)
{
#ifdef INITIAL_MINUTE
return INITIAL_MINUTE;
#else
return 0;
#endif
}

int main(void) {
// ASF code. Initialize the MCU with configuration options from Atmel Studio.
init_mcu();
Expand Down Expand Up @@ -69,7 +114,11 @@ int main(void) {
// if date/time register is 0 (power on reset state), default year to 2023.
watch_date_time date_time = watch_rtc_get_date_time();
if (date_time.reg == 0) {
date_time.unit.year = 3;
date_time.unit.year = get_initial_year();
date_time.unit.month = get_initial_month();
date_time.unit.day = get_inital_day();
date_time.unit.hour = get_inital_hour();
date_time.unit.minute = get_inital_minute();
Copy link
Contributor

@mcguirepr89 mcguirepr89 Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is worth much, but it's been helpful for me. For Linux users who only add these 4 lines and don't change any other files, sed can do all the work except setting the timezone offset (though it's kind of hacky).

From movement/make:

sed -Ei "s/(unit.year = ).*(;)/\1$(( $(date +%-g) - 20 ))\2/;s/(unit.month = ).*(;)/\1$(date +%-m)\2/;s/(unit.day = ).*(;)/\1$(date +%-d)\2/;s/(unit.hour = ).*(;)/\1$(date +%-H)\2/;s/(unit.minute = ).*(;)/\1$(( $(date +%-M) + 1))\2/" ../../watch-library/hardware/main.c && make COLOR=YOURCOLOR && make install COLOR=YOURCOLOR

I know it's not pretty, but it is handy. (It also adds 1 minute to the time to allow you to put the watch back together.)

watch_rtc_set_date_time(date_time);
}

Expand Down