move PikaStdDevice.Time to time

This commit is contained in:
lyon 2023-01-09 20:35:22 +08:00
parent dd2dce1e35
commit 79df1cf1cc
13 changed files with 528 additions and 432 deletions

View File

@ -98,49 +98,8 @@ class GPIO(BaseDev):
def platformRead(self): ...
class Time(BaseDev):
def __init__(self): ...
def sleep(self, s: float):
"""Sleep for s seconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time(self) -> float:
"""Get the current time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time_ns(self) -> int:
"""Get the current time in nanoseconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def gmtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def localtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def mktime(self) -> int:
"""Convert struct_time to unix time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def asctime(self):
"""Convert struct_time to string."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def ctime(self, unix_time: float):
"""Convert unix time to string."""
@abstractmethod
def sleep_s(self, s: int): ...
@abstractmethod
def sleep_ms(self, ms: int): ...
@abstractmethod
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def platformGetTick(): ...
# class Time(BaseDev):
# use time module instead
class ADC(BaseDev):

View File

@ -1,5 +1,5 @@
#include "PikaStdDevice_Time.h"
#include "PikaStdDevice_common.h"
#include "_time.h"
#include "PikaVM.h"
#if defined(__linux)
#include <unistd.h>
#endif
@ -7,7 +7,7 @@
#include <windows.h>
#endif
void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
void _time_sleep_ms(PikaObj* self, int ms) {
#if defined(__linux)
usleep(ms * 1000);
#elif defined(_WIN32)
@ -16,7 +16,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
__platform_sleep_ms(ms);
#endif
}
void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
void _time_sleep_s(PikaObj* self, int s) {
#if defined(__linux)
sleep(s);
#elif defined(_WIN32)
@ -26,7 +26,7 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
#endif
}
void PikaStdDevice_Time_platformGetTick(PikaObj* self) {
void _time_platformGetTick(PikaObj* self) {
obj_setInt(self, "tick", __platform_getTick());
}
@ -559,7 +559,7 @@ void time_asctime(const _tm* this_tm) {
time_printf("%s\n", str);
}
pika_float PikaStdDevice_Time_time(PikaObj* self) {
pika_float _time_time(PikaObj* self) {
/* run platformGetTick() */
PIKA_PYTHON_BEGIN
/* clang-format off */
@ -579,7 +579,7 @@ pika_float PikaStdDevice_Time_time(PikaObj* self) {
return time_time(self);
}
int PikaStdDevice_Time_time_ns(PikaObj* self) {
int _time_time_ns(PikaObj* self) {
return time_time_ns(self);
}
@ -603,7 +603,7 @@ void time_set_tm_value(PikaObj* self, const _tm* this_tm) {
#endif
}
void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) {
void _time_gmtime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -620,7 +620,7 @@ void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) {
#endif
}
void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) {
void _time_localtime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -656,7 +656,7 @@ void time_get_tm_value(PikaObj* self, _tm* this_tm) {
#endif
}
int PikaStdDevice_Time_mktime(PikaObj* self) {
int _time_mktime(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -670,7 +670,7 @@ int PikaStdDevice_Time_mktime(PikaObj* self) {
#endif
}
void PikaStdDevice_Time_asctime(PikaObj* self) {
void _time_asctime(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -681,7 +681,7 @@ void PikaStdDevice_Time_asctime(PikaObj* self) {
time_asctime(&this_tm);
#endif
}
void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) {
void _time_ctime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -694,7 +694,7 @@ void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) {
#endif
}
void PikaStdDevice_Time___init__(PikaObj* self) {
void _time___init__(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
#else
_tm this_tm;
@ -704,7 +704,7 @@ void PikaStdDevice_Time___init__(PikaObj* self) {
#endif
}
void PikaStdDevice_Time_sleep(PikaObj* self, pika_float s) {
void _time_sleep(PikaObj* self, pika_float s) {
Args* args = New_args(NULL);
args_setInt(args, "ms", s * 1000);
obj_runNativeMethod(self, "sleep_ms", args);

56
package/time/_time.pyi Normal file
View File

@ -0,0 +1,56 @@
from PikaObj import *
def __init__(self): ...
def sleep(self, s: float):
"""Sleep for s seconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time(self) -> float:
"""Get the current time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time_ns(self) -> int:
"""Get the current time in nanoseconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def gmtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def localtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def mktime(self) -> int:
"""Convert struct_time to unix time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def asctime(self):
"""Convert struct_time to string."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def ctime(self, unix_time: float):
"""Convert unix time to string."""
@abstractmethod
def sleep_s(self, s: int): ...
@abstractmethod
def sleep_ms(self, ms: int): ...
@abstractmethod
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def platformGetTick(): ...

41
package/time/time.py Normal file
View File

@ -0,0 +1,41 @@
import _time
def sleep(s: float):
return _time.sleep(s)
def sleep_s(s: int):
return _time.sleep_s(s)
def sleep_ms(ms: int):
return _time.sleep_ms(ms)
def time() -> float:
return _time.time()
def time_ns() -> int:
return _time.time_ns()
def gmtime(unix_time: float):
return _time.gmtime(unix_time)
def localtime(unix_time: float):
return _time.localtime(unix_time)
def mktime() -> int:
return _time.mktime()
def asctime() -> str:
return _time.asctime()
def ctime(unix_time: float) -> str:
return _time.ctime(unix_time)

View File

@ -98,49 +98,8 @@ class GPIO(BaseDev):
def platformRead(self): ...
class Time(BaseDev):
def __init__(self): ...
def sleep(self, s: float):
"""Sleep for s seconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time(self) -> float:
"""Get the current time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time_ns(self) -> int:
"""Get the current time in nanoseconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def gmtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def localtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def mktime(self) -> int:
"""Convert struct_time to unix time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def asctime(self):
"""Convert struct_time to string."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def ctime(self, unix_time: float):
"""Convert unix time to string."""
@abstractmethod
def sleep_s(self, s: int): ...
@abstractmethod
def sleep_ms(self, ms: int): ...
@abstractmethod
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def platformGetTick(): ...
# class Time(BaseDev):
# use time module instead
class ADC(BaseDev):

View File

@ -7,14 +7,6 @@ class GPIO(PikaStdDevice.GPIO):
def eventTest(self): ...
class Time(PikaStdDevice.Time):
# override
def sleep_s(self, s: int): ...
def sleep_ms(self, ms: int): ...
def platformGetTick(): ...
def platformGetEventId(self): ...
class ADC(PikaStdDevice.ADC):
# override
def platformEnable(self): ...

View File

@ -0,0 +1,56 @@
from PikaObj import *
def __init__(self): ...
def sleep(self, s: float):
"""Sleep for s seconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time(self) -> float:
"""Get the current time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def time_ns(self) -> int:
"""Get the current time in nanoseconds."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def gmtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def localtime(self, unix_time: float):
"""Convert unix time to struct_time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def mktime(self) -> int:
"""Convert struct_time to unix time."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def asctime(self):
"""Convert struct_time to string."""
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def ctime(self, unix_time: float):
"""Convert unix time to string."""
@abstractmethod
def sleep_s(self, s: int): ...
@abstractmethod
def sleep_ms(self, ms: int): ...
@abstractmethod
@PIKA_C_MACRO_IF("PIKA_STD_DEVICE_UNIX_TIME_ENABLE")
def platformGetTick(): ...

View File

@ -5,7 +5,7 @@ import GTestTask, TempDevTest
import cb_test
import configparser
import test_module1, test_cmodule, test_module4, import_test
import hashlib, hmac, aes, base64
import hashlib, hmac, aes, base64, time
mem = PikaStdLib.MemChecker()
print('hello pikascript!')

View File

@ -1,18 +0,0 @@
#include "TemplateDevice_Time.h"
void TemplateDevice_Time_sleep_ms(PikaObj *self, int ms){
}
void TemplateDevice_Time_sleep_s(PikaObj *self, int s){
}
static volatile uint64_t tick_ms = 0;
void TemplateDevice_Time_platformGetTick(PikaObj *self){
tick_ms += 50;
obj_setInt(self, "tick", tick_ms);
}
void TemplateDevice_Time_platformGetEventId(PikaObj *self){
}

View File

@ -1,5 +1,5 @@
#include "PikaStdDevice_Time.h"
#include "PikaStdDevice_common.h"
#include "_time.h"
#include "PikaVM.h"
#if defined(__linux)
#include <unistd.h>
#endif
@ -7,7 +7,7 @@
#include <windows.h>
#endif
void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
void _time_sleep_ms(PikaObj* self, int ms) {
#if defined(__linux)
usleep(ms * 1000);
#elif defined(_WIN32)
@ -16,7 +16,7 @@ void PikaStdDevice_Time_sleep_ms(PikaObj* self, int ms) {
__platform_sleep_ms(ms);
#endif
}
void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
void _time_sleep_s(PikaObj* self, int s) {
#if defined(__linux)
sleep(s);
#elif defined(_WIN32)
@ -26,7 +26,7 @@ void PikaStdDevice_Time_sleep_s(PikaObj* self, int s) {
#endif
}
void PikaStdDevice_Time_platformGetTick(PikaObj* self) {
void _time_platformGetTick(PikaObj* self) {
obj_setInt(self, "tick", __platform_getTick());
}
@ -559,7 +559,7 @@ void time_asctime(const _tm* this_tm) {
time_printf("%s\n", str);
}
pika_float PikaStdDevice_Time_time(PikaObj* self) {
pika_float _time_time(PikaObj* self) {
/* run platformGetTick() */
PIKA_PYTHON_BEGIN
/* clang-format off */
@ -579,7 +579,7 @@ pika_float PikaStdDevice_Time_time(PikaObj* self) {
return time_time(self);
}
int PikaStdDevice_Time_time_ns(PikaObj* self) {
int _time_time_ns(PikaObj* self) {
return time_time_ns(self);
}
@ -603,7 +603,7 @@ void time_set_tm_value(PikaObj* self, const _tm* this_tm) {
#endif
}
void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) {
void _time_gmtime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -620,7 +620,7 @@ void PikaStdDevice_Time_gmtime(PikaObj* self, pika_float unix_time) {
#endif
}
void PikaStdDevice_Time_localtime(PikaObj* self, pika_float unix_time) {
void _time_localtime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -656,7 +656,7 @@ void time_get_tm_value(PikaObj* self, _tm* this_tm) {
#endif
}
int PikaStdDevice_Time_mktime(PikaObj* self) {
int _time_mktime(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -670,7 +670,7 @@ int PikaStdDevice_Time_mktime(PikaObj* self) {
#endif
}
void PikaStdDevice_Time_asctime(PikaObj* self) {
void _time_asctime(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -681,7 +681,7 @@ void PikaStdDevice_Time_asctime(PikaObj* self) {
time_asctime(&this_tm);
#endif
}
void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) {
void _time_ctime(PikaObj* self, pika_float unix_time) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
obj_setErrorCode(self, 1);
obj_setSysOut(
@ -694,7 +694,7 @@ void PikaStdDevice_Time_ctime(PikaObj* self, pika_float unix_time) {
#endif
}
void PikaStdDevice_Time___init__(PikaObj* self) {
void _time___init__(PikaObj* self) {
#if !PIKA_STD_DEVICE_UNIX_TIME_ENABLE
#else
_tm this_tm;
@ -704,7 +704,7 @@ void PikaStdDevice_Time___init__(PikaObj* self) {
#endif
}
void PikaStdDevice_Time_sleep(PikaObj* self, pika_float s) {
void _time_sleep(PikaObj* self, pika_float s) {
Args* args = New_args(NULL);
args_setInt(args, "ms", s * 1000);
obj_runNativeMethod(self, "sleep_ms", args);

View File

@ -0,0 +1,41 @@
import _time
def sleep(s: float):
return _time.sleep(s)
def sleep_s(s: int):
return _time.sleep_s(s)
def sleep_ms(ms: int):
return _time.sleep_ms(ms)
def time() -> float:
return _time.time()
def time_ns() -> int:
return _time.time_ns()
def gmtime(unix_time: float):
return _time.gmtime(unix_time)
def localtime(unix_time: float):
return _time.localtime(unix_time)
def mktime() -> int:
return _time.mktime()
def asctime() -> str:
return _time.asctime()
def ctime(unix_time: float) -> str:
return _time.ctime(unix_time)

View File

@ -21,6 +21,12 @@ void pika_platform_printf(char* fmt, ...) {
}
}
static volatile uint64_t tick_ms = 0;
int64_t pika_platform_getTick(void) {
tick_ms += 50;
return tick_ms;
}
/* quick_malloc is always open */
uint8_t __is_quick_malloc(void) {
// return 1;

View File

@ -20,10 +20,12 @@ TEST(unix_time, time) {
pikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
extern unsigned char pikaModules_py_a[];
obj_linkLibrary(self, pikaModules_py_a);
obj_run(self,
"mytime = TemplateDevice.Time()\n"
"t1= mytime.time()\n"
"t2= mytime.time()\n");
"import time\n"
"t1= time.time()\n"
"t2= time.time()\n");
/* 获取数据比对 */
float t1 = obj_getFloat(self, "t1");
float t2 = obj_getFloat(self, "t2");
@ -42,19 +44,21 @@ TEST(unix_time, unix_time) {
pikaMemInfo.heapUsedMax = 0;
/* run */
PikaObj* self = newRootObj("pikaMain", New_PikaMain);
extern unsigned char pikaModules_py_a[];
obj_linkLibrary(self, pikaModules_py_a);
obj_run(self,
"mytime = PikaStdDevice.Time()\n"
"mytime.localtime(0.0)\n");
"import time\n"
"time.localtime(0.0)\n");
/* 获取数据比对 */
int tm_sec = obj_getInt(self, "mytime.tm_sec");
int tm_min = obj_getInt(self, "mytime.tm_min");
int tm_hour = obj_getInt(self, "mytime.tm_hour");
int tm_mday = obj_getInt(self, "mytime.tm_mday");
int tm_mon = obj_getInt(self, "mytime.tm_mon");
int tm_year = obj_getInt(self, "mytime.tm_year");
int tm_wday = obj_getInt(self, "mytime.tm_wday");
int tm_yday = obj_getInt(self, "mytime.tm_yday");
int tm_isdst = obj_getInt(self, "mytime.tm_isdst");
int tm_sec = obj_getInt(self, "time._time.tm_sec");
int tm_min = obj_getInt(self, "time._time.tm_min");
int tm_hour = obj_getInt(self, "time._time.tm_hour");
int tm_mday = obj_getInt(self, "time._time.tm_mday");
int tm_mon = obj_getInt(self, "time._time.tm_mon");
int tm_year = obj_getInt(self, "time._time.tm_year");
int tm_wday = obj_getInt(self, "time._time.tm_wday");
int tm_yday = obj_getInt(self, "time._time.tm_yday");
int tm_isdst = obj_getInt(self, "time._time.tm_isdst");
/* assert */
EXPECT_EQ(tm_sec, 0);
EXPECT_EQ(tm_min, 0);