Skip to content

Commit 9dd7761

Browse files
committed
esp32: Allow RTC init with only date.
Signed-off-by: Sebastian Romero <s.romero@arduino.cc>
1 parent cf0c014 commit 9dd7761

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

ports/esp32/machine_rtc.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,20 @@ static mp_obj_t machine_rtc_datetime_helper(mp_uint_t n_args, const mp_obj_t *ar
126126
return mp_obj_new_tuple(8, tuple);
127127
} else {
128128
// Set time
129-
130129
mp_obj_t *items;
131-
mp_obj_get_array_fixed_n(args[1], 8, &items);
132130

133131
struct timeval tv = {0};
134132
if (use_init_format) {
135-
tv.tv_sec = timeutils_seconds_since_epoch(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[3]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]));
136-
tv.tv_usec = mp_obj_get_int(items[6]);
137-
} else {
133+
size_t seq_len;
134+
mp_ob_get_array_min_max_n(args[1], 3, 8, &seq_len, &items);
135+
mp_int_t hour = seq_len > 3 ? mp_obj_get_int(items[3]) : 0;
136+
mp_int_t minute = seq_len > 4 ? mp_obj_get_int(items[4]) : 0;
137+
mp_int_t second = seq_len > 5 ? mp_obj_get_int(items[5]) : 0;
138+
mp_int_t subsecond = seq_len > 6 ? mp_obj_get_int(items[6]) : 0;
139+
tv.tv_sec = timeutils_seconds_since_epoch(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), hour, minute, second);
140+
tv.tv_usec = subsecond;
141+
} else {
142+
mp_obj_get_array_fixed_n(args[1], 8, &items);
138143
tv.tv_sec = timeutils_seconds_since_epoch(mp_obj_get_int(items[0]), mp_obj_get_int(items[1]), mp_obj_get_int(items[2]), mp_obj_get_int(items[4]), mp_obj_get_int(items[5]), mp_obj_get_int(items[6]));
139144
tv.tv_usec = mp_obj_get_int(items[7]);
140145
}

py/obj.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,25 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
448448
}
449449
}
450450

451+
void mp_ob_get_array_min_max_n(mp_obj_t o, size_t min_len, size_t max_len, size_t *len, mp_obj_t **items) {
452+
mp_obj_get_array(o, len, items);
453+
if (*len < min_len) {
454+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
455+
mp_raise_ValueError(MP_ERROR_TEXT("tuple/list length too short"));
456+
#else
457+
mp_raise_msg_varg(&mp_type_ValueError,
458+
MP_ERROR_TEXT("requested minimum length %d but object has length %d"), (int)min_len, (int)*len);
459+
#endif
460+
} else if (*len > max_len) {
461+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
462+
mp_raise_ValueError(MP_ERROR_TEXT("tuple/list length too long"));
463+
#else
464+
mp_raise_msg_varg(&mp_type_ValueError,
465+
MP_ERROR_TEXT("requested maximum length %d but object has length %d"), (int)max_len, (int)*len);
466+
#endif
467+
}
468+
}
469+
451470
// is_slice determines whether the index is a slice index
452471
size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice) {
453472
mp_int_t i;

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *im
10451045
#endif
10461046
void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
10471047
void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items); // *items may point inside a GC block
1048+
void mp_ob_get_array_min_max_n(mp_obj_t o, size_t min_len, size_t max_len, size_t *len, mp_obj_t **items); // *items may point inside a GC block
10481049
size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
10491050
mp_obj_t mp_obj_id(mp_obj_t o_in);
10501051
mp_obj_t mp_obj_len(mp_obj_t o_in);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy