Skip to content

Commit

Permalink
made new time calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
garobcsi committed Dec 22, 2023
1 parent ede9891 commit 57005ac
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
Binary file modified Firmware/ATC_Paper.bin
Binary file not shown.
6 changes: 0 additions & 6 deletions Firmware/src/epd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ RAM uint8_t epd_update_state = 0;
RAM uint8_t epd_scene = 2;
RAM uint8_t epd_wait_update = 0;

RAM uint8_t hour_refresh = 100;
RAM uint8_t minute_refresh = 100;

const char *BLE_conn_string[] = {"BLE F", "BLE T"};
Expand Down Expand Up @@ -358,11 +357,6 @@ void update_time_scene(struct date_time _time, uint16_t battery_mv, int16_t temp
{
minute_refresh = _time.tm_min;

if (_time.tm_hour != hour_refresh)
{
hour_refresh = _time.tm_hour;
}

scene(_time, battery_mv, temperature, _time.tm_hour == 0 && _time.tm_min == 0);
}
}
Expand Down
77 changes: 54 additions & 23 deletions Firmware/src/etime.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,66 @@ RAM uint32_t last_clock_increase;
RAM uint32_t last_reached_period[10] = {0};
RAM uint8_t has_ever_reached[10] = {0};

uint8_t map[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

_attribute_ram_code_ void init_time(void)
{
one_second_trimmed += time_trime;
current_unix_time = 0;
}

_attribute_ram_code_ void unixTimeToDateTime(uint32_t t) // not calculated week
{
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint32_t f;

// Retrieve hours, minutes and seconds
current_date.tm_sec = t % 60;
t /= 60;
current_date.tm_min = t % 60;
t /= 60;
current_date.tm_hour = t % 24;
t /= 24;

// Convert Unix time to date
a = (uint32_t)((4 * t + 102032) / 146097 + 15);
b = (uint32_t)(t + 2442113 + a - (a / 4));
c = (20 * b - 2442) / 7305;
d = b - 365 * c - (c / 4);
e = d * 1000 / 30601;
f = d - e * 30 - e * 601 / 1000;

// January and February are counted as months 13 and 14 of the previous year
if (e <= 13)
{
c -= 4716;
e -= 1;
}
else
{
c -= 4715;
e -= 13;
}

// Retrieve year, month and day
current_date.tm_year = c;
current_date.tm_month = e;
current_date.tm_day = f;

// week not calculated
current_date.tm_week = 0;
}

_attribute_ram_code_ void handler_time(void)
{
if (clock_time() - last_clock_increase >= one_second_trimmed)
{
last_clock_increase += one_second_trimmed;
current_unix_time++;

current_date.tm_min = (current_unix_time / 60) % 60;
current_date.tm_hour = ((current_unix_time / 60) / 60) % 24;
current_date.tm_sec = current_unix_time % 60;

if (current_unix_time % 86400 == 0) {
current_date.tm_month = current_date.tm_month % 12;
if (current_date.tm_day + 1 > map[current_date.tm_month - 1]) {
current_date.tm_day = 1;
if (current_date.tm_month + 1 > 12) {
current_date.tm_month = 1;
current_date.tm_year += 1;
} else {
current_date.tm_month += 1;
}
} else {
current_date.tm_day = current_date.tm_day + 1;
}

current_date.tm_week = (current_date.tm_week + 1) % 7;
}
unixTimeToDateTime(current_unix_time);
}
}

Expand Down Expand Up @@ -77,6 +102,12 @@ _attribute_ram_code_ void set_time(uint32_t time_now, uint16_t time_year, uint8_
current_date.tm_week = time_week;
}

_attribute_ram_code_ struct date_time get_time(void) {
_attribute_ram_code_ struct date_time get_time(void)
{
return current_date;
}

_attribute_ram_code_ uint32_t get_unix_time(void)
{
return current_unix_time;
}
3 changes: 2 additions & 1 deletion Firmware/src/etime.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ void init_time(void);
void handler_time(void);
uint8_t time_reached_period(timer_channel ch, uint32_t seconds);
void set_time(uint32_t time_now, uint16_t time_year, uint8_t time_month, uint8_t time_day, uint8_t time_week);
struct date_time get_time(void);
struct date_time get_time(void);
uint32_t get_unix_time(void);

0 comments on commit 57005ac

Please sign in to comment.