diff --git a/Smol Watch Project/My Project/.atmelstart/atmel_start_config.atstart b/Smol Watch Project/My Project/.atmelstart/atmel_start_config.atstart index 0c082b9..741d407 100644 --- a/Smol Watch Project/My Project/.atmelstart/atmel_start_config.atstart +++ b/Smol Watch Project/My Project/.atmelstart/atmel_start_config.atstart @@ -517,10 +517,10 @@ drivers: functionality: System api: HAL:HPL:GCLK configuration: - $input: 400000 - $input_id: External Crystal Oscillator 0.4-32MHz (XOSC) - RESERVED_InputFreq: 400000 - RESERVED_InputFreq_id: External Crystal Oscillator 0.4-32MHz (XOSC) + $input: 32768 + $input_id: 32kHz External Crystal Oscillator (XOSC32K) + RESERVED_InputFreq: 32768 + RESERVED_InputFreq_id: 32kHz External Crystal Oscillator (XOSC32K) _$freq_output_Generic clock generator 0: 4000000 _$freq_output_Generic clock generator 1: 400000 _$freq_output_Generic clock generator 2: 400000 @@ -762,7 +762,7 @@ drivers: functionality: Calendar api: HAL:Driver:Calendar configuration: - rtc_arch_init_reset: true + rtc_arch_init_reset: false rtc_arch_prescaler: Peripheral clock divided by 1024 rtc_cmpeo0: false rtc_event_control: false diff --git a/Smol Watch Project/My Project/Config/hpl_rtc_config.h b/Smol Watch Project/My Project/Config/hpl_rtc_config.h index 174f77a..9085ca3 100644 --- a/Smol Watch Project/My Project/Config/hpl_rtc_config.h +++ b/Smol Watch Project/My Project/Config/hpl_rtc_config.h @@ -15,7 +15,7 @@ // Note that the previous power down data in RTC is lost if it's enabled. // rtc_arch_init_reset #ifndef CONF_RTC_INIT_RESET -#define CONF_RTC_INIT_RESET 1 +#define CONF_RTC_INIT_RESET 0 #endif // Prescaler configuration diff --git a/Smol Watch Project/My Project/My Project.cproj b/Smol Watch Project/My Project/My Project.cproj index ac2e0cd..af621b7 100644 --- a/Smol Watch Project/My Project/My Project.cproj +++ b/Smol Watch Project/My Project/My Project.cproj @@ -200,7 +200,7 @@ - + @@ -409,7 +409,6 @@ %24(PackRepoDir)\atmel\SAML22_DFP\1.2.77\include - Optimize debugging experience (-Og) True Maximum (-g3) True diff --git a/Smol Watch Project/My Project/main.c b/Smol Watch Project/My Project/main.c index 3af1ed0..b803fb6 100644 --- a/Smol Watch Project/My Project/main.c +++ b/Smol Watch Project/My Project/main.c @@ -4,38 +4,38 @@ #include "mars_clock.h" Watch watch; -bool locked = true; +bool local = true; void calendar_callback(struct calendar_descriptor *const calendar) { } static void mode_callback() { - locked = !locked; - gpio_set_pin_level(GREEN, !locked); + local = !local; + struct calendar_date_time date_time; + calendar_get_date_time(&CALENDAR_0, &date_time); + update_display(&watch, date_time, local); } static void light_callback() { - if (locked) return; struct calendar_date_time date_time; calendar_get_date_time(&CALENDAR_0, &date_time); date_time.time.min = (date_time.time.min + 1) % 60; watch_set_date_time(date_time); - update_display(&watch, date_time); + update_display(&watch, date_time, local); } static void alarm_callback() { - if (locked) return; struct calendar_date_time date_time; calendar_get_date_time(&CALENDAR_0, &date_time); date_time.time.sec = 0; watch_set_date_time(date_time); - update_display(&watch, date_time); + update_display(&watch, date_time, local); } static void tick_callback() { struct calendar_date_time date_time; calendar_get_date_time(&CALENDAR_0, &date_time); - update_display(&watch, date_time); + update_display(&watch, date_time, local); } int main(void) @@ -61,11 +61,11 @@ int main(void) watch_enable_date_time(&watch); struct calendar_date_time date_time; - date_time.date.year = 1; // reference year is 2020, add this to that. + date_time.date.year = 2021; date_time.date.month = 5; - date_time.date.day = 2; - date_time.time.hour = 7; - date_time.time.min = 15; + date_time.date.day = 6; + date_time.time.hour = 23; + date_time.time.min = 30; date_time.time.sec = 0; watch_set_date_time(date_time); /* struct calendar_alarm alarm; @@ -76,7 +76,7 @@ int main(void) alarm.callback = calendar_callback; calendar_set_alarm(&CALENDAR_0, &alarm, &calendar_callback); */ - update_display(&watch, date_time); + update_display(&watch, date_time, local); watch_enable_tick(tick_callback); while (1) { diff --git a/Smol Watch Project/My Project/mars_clock.c b/Smol Watch Project/My Project/mars_clock.c index 82a47d0..53ce336 100644 --- a/Smol Watch Project/My Project/mars_clock.c +++ b/Smol Watch Project/My Project/mars_clock.c @@ -8,10 +8,79 @@ #include #include "mars_clock.h" -// note: mars time not working, committing just the earth clock. +static unsigned short days[4][12] = +{ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}, + { 366, 397, 425, 456, 486, 517, 547, 578, 609, 639, 670, 700}, + { 731, 762, 790, 821, 851, 882, 912, 943, 974,1004,1035,1065}, + {1096,1127,1155,1186,1216,1247,1277,1308,1339,1369,1400,1430}, +}; -void update_display(Watch *watch, struct calendar_date_time date_time) { +unsigned int date_time_to_epoch(struct calendar_date_time date_time) +{ + unsigned int second = date_time.time.sec; + unsigned int minute = date_time.time.min; + unsigned int hour = date_time.time.hour; + unsigned int day = date_time.date.day-1; // 0-30 + unsigned int month = date_time.date.month-1; // 0-11 + unsigned int year = date_time.date.year - 1970; // 0-99 + return (((year/4*(365*4+1)+days[year%4][month]+day)*24+hour)*60+minute)*60+second; +} + +void epoch_to_date_time(struct calendar_date_time date_time, unsigned int epoch) +{ + date_time.time.sec = epoch % 60; + epoch /= 60; + date_time.time.min = epoch % 60; + epoch /= 60; + date_time.time.hour = epoch % 24; + epoch /= 24; + + unsigned int years = epoch/(365*4+1)*4; + epoch %= 365*4+1; + + unsigned int year; + for (year=3; year>0; year--) + { + if (epoch >= days[year][0]) + break; + } + + unsigned int month; + for (month=11; month>0; month--) + { + if (epoch >= days[year][month]) + break; + } + + date_time.date.year = years+year; + date_time.date.month = month+1; + date_time.date.day = epoch-days[year][month]+1; +} + +void h_to_hms(struct calendar_date_time *date_time, double h) { + unsigned int seconds = (unsigned int)(h * 3600.0); + date_time->time.hour = seconds / 3600; + seconds = seconds % 3600; + date_time->time.min = floor(seconds / 60); + date_time->time.sec = round(seconds % 60); +} + + +void update_display(Watch *watch, struct calendar_date_time date_time, bool local) { char buf[6]; - sprintf(&buf[0], "TE %02d%02d%02d", date_time.time.hour, date_time.time.min, date_time.time.sec); + if (local) { + sprintf(&buf[0], "TE %02d%02d%02d", date_time.time.hour, date_time.time.min, date_time.time.sec); + } else { + unsigned int now = date_time_to_epoch(date_time); + double jdut = 2440587.5 + ((double)now / 86400.0); + double jdtt = jdut + ((37.0 + 32.184) / 86400.0); + double jd2k = jdtt - 2451545.0; + double msd = ((jd2k - 4.5) / 1.0274912517) + 44796.0 - 0.0009626; + double mtc = fmod(24 * msd, 24); + struct calendar_date_time mars_time; + h_to_hms(&mars_time, mtc); + sprintf(&buf[0], "MA %02d%02d%02d", mars_time.time.hour, mars_time.time.min, mars_time.time.sec); + } watch_display_string(watch, buf, 0); } diff --git a/Smol Watch Project/My Project/mars_clock.h b/Smol Watch Project/My Project/mars_clock.h index dd0150e..71b45f3 100644 --- a/Smol Watch Project/My Project/mars_clock.h +++ b/Smol Watch Project/My Project/mars_clock.h @@ -11,7 +11,7 @@ #include "hpl_calendar.h" #include "watch-library/watch.h" -void update_display(Watch *watch, struct calendar_date_time date_time); +void update_display(Watch *watch, struct calendar_date_time date_time, bool local); #endif /* MARS_CLOCK_H_ */ \ No newline at end of file