ports/embed: Implement additional time functions.

Optionally adds gmtime, localtime, mktime, time, time_ns to the time
module, implemented using mp_hal_time_ns(). This could also be used by
other ports.

I'm unsure where to put modtime_mphal.h, it could also be in extmod. The
important thing is that for MICROPY_PY_TIME_INCLUDEFILE to work it must be
at the same path in both the port build (original source tree) and the
application build (micropython_embed distribution), therefore not in
ports/embed/port.

It is named .h, mismatching the corresponding ports/*/modtime.c, because it
must not be compiled separately, which naming it .c would make harder for
users of the embed port - they would need to explicitly exclude it, whereas
this way they can continue to just compile all the .c files found in the
micropython_embed distribution except those in lib.

Signed-off-by: Christian Walther <cwalther@gmx.ch>
pull/11430/head
Christian Walther 2023-05-05 19:37:49 +02:00
rodzic 5a1638986f
commit 170ba15311
6 zmienionych plików z 87 dodań i 0 usunięć

Wyświetl plik

@ -35,6 +35,7 @@ static const char *example_2 =
"help(random)\n"
"import time\n"
"help(time)\n"
"print('time.gmtime(736622952) = 2023-05-05T17:29:12Z:', time.gmtime(736622952))\n"
"import math\n"
"help(math)\n"
"import frozenhello\n"

Wyświetl plik

@ -8,6 +8,12 @@ MICROPYTHON_TOP = ../..
# Include modules from extmod in the output.
EMBED_EXTRA = extmod
# Include helper sources for the time module in the output.
EMBED_EXTRA += \
shared/timeutils/timeutils.c \
shared/timeutils/timeutils.h \
shared/timeutils/modtime_mphal.h
# Freeze Python modules.
FROZEN_MANIFEST ?= manifest.py

Wyświetl plik

@ -22,6 +22,12 @@
// Enable floating point numbers and the math module.
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
// Enable more functions in the time module. Requires additions to EMBED_EXTRA,
// see micropython_embed.mk.
#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
#define MICROPY_PY_TIME_INCLUDEFILE "shared/timeutils/modtime_mphal.h"
// Requires shared/readline/readline.h, don't bother as we have no input.
#define MICROPY_PY_BUILTINS_INPUT (0)

Wyświetl plik

@ -52,6 +52,15 @@ mp_uint_t mp_hal_ticks_cpu(void) {
#endif
#if MICROPY_PY_TIME_TIME_TIME_NS
uint64_t mp_hal_time_ns(void) {
// Nanoseconds since the Epoch.
return 0;
}
#endif
// Text-mode standard output
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
// This is a simplistic implementation for demonstration purposes. A real

Wyświetl plik

@ -16,6 +16,12 @@ ifeq ($(filter extmod,$(EMBED_EXTRA)),extmod)
include $(TOP)/extmod/extmod.mk
endif
# The parts of EMBED_EXTRA that name literal files and don't need special handling.
EMBED_EXTRA_FILES = $(filter-out extmod,$(EMBED_EXTRA))
# Extra files need to be searched for QSTRs.
SRC_QSTR += $(addprefix $(TOP)/,$(EMBED_EXTRA_FILES))
# Set the location of the MicroPython embed port.
MICROPYTHON_EMBED_PORT = $(MICROPYTHON_TOP)/ports/embed
@ -57,6 +63,9 @@ endif
ifneq ($(FROZEN_MANIFEST),)
PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,frozen)
endif
ifneq ($(EMBED_EXTRA_FILES),)
PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,$(filter-out ./,$(sort $(dir $(EMBED_EXTRA_FILES)))))
endif
.PHONY: micropython-embed-package
micropython-embed-package: $(GENHDR_OUTPUT) $(FROZEN_OUTPUT)
@ -86,6 +95,10 @@ ifneq ($(FROZEN_MANIFEST),)
endif
$(ECHO) "- port"
$(Q)$(CP) $(MICROPYTHON_EMBED_PORT)/port/*.[ch] $(PACKAGE_DIR)/port
ifneq ($(EMBED_EXTRA_FILES),)
$(ECHO) "- extra"
$(Q)$(foreach FILE,$(EMBED_EXTRA_FILES),$(CP) $(TOP)/$(FILE) $(dir $(PACKAGE_DIR)/$(FILE)) &&) true
endif
# Include remaining core make rules.
include $(TOP)/py/mkrules.mk

Wyświetl plik

@ -0,0 +1,52 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/mphal.h"
#include "shared/timeutils/timeutils.h"
// Return the localtime as an 8-tuple.
static mp_obj_t mp_time_localtime_get(void) {
mp_int_t seconds = mp_hal_time_ns() / 1000000000;
timeutils_struct_time_t tm;
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
}
// Returns the number of seconds, as an integer, since the Epoch.
static mp_obj_t mp_time_time_get(void) {
return mp_obj_new_int(mp_hal_time_ns() / 1000000000);
}