Skip to content

Commit cadf300

Browse files
kwagyemanclaude
andcommitted
ports/unix: Add py_clock to standard variant for doc generation.
OpenMV's extmod/modtime.c unconditionally includes "py_clock.h" and registers py_clock_type in the time module. Standalone ports (the unix port used by tools/gen-cpydiff.py to produce the CPython-differences docs) have no py_clock and fail to build. Ship the OpenMV clock module in the unix standard variant so the port builds and genrst can be generated in CI. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent d532f6c commit cadf300

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
*
4+
* Copyright (C) 2013-2024 OpenMV, LLC.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* Clock Python module.
25+
*/
26+
#include "py/obj.h"
27+
#include "py/mphal.h"
28+
#include "py_clock.h"
29+
30+
/* Clock Type */
31+
typedef struct _py_clock_obj_t {
32+
mp_obj_base_t base;
33+
uint32_t t_start;
34+
uint32_t t_ticks;
35+
uint32_t t_frame;
36+
} py_clock_obj_t;
37+
38+
mp_obj_t py_clock_tick(mp_obj_t clock_obj) {
39+
py_clock_obj_t *clock = (py_clock_obj_t *) clock_obj;
40+
clock->t_start = mp_hal_ticks_ms();
41+
return mp_const_none;
42+
}
43+
static MP_DEFINE_CONST_FUN_OBJ_1(py_clock_tick_obj, py_clock_tick);
44+
45+
mp_obj_t py_clock_fps(mp_obj_t clock_obj) {
46+
py_clock_obj_t *clock = (py_clock_obj_t *) clock_obj;
47+
clock->t_frame++;
48+
clock->t_ticks += (mp_hal_ticks_ms() - clock->t_start);
49+
float fps = 1000.0f / (clock->t_ticks / (float) clock->t_frame);
50+
return mp_obj_new_float(fps);
51+
}
52+
static MP_DEFINE_CONST_FUN_OBJ_1(py_clock_fps_obj, py_clock_fps);
53+
54+
mp_obj_t py_clock_avg(mp_obj_t clock_obj) {
55+
py_clock_obj_t *clock = (py_clock_obj_t *) clock_obj;
56+
clock->t_frame++;
57+
clock->t_ticks += (mp_hal_ticks_ms() - clock->t_start);
58+
return mp_obj_new_float(clock->t_ticks / (float) clock->t_frame);
59+
}
60+
static MP_DEFINE_CONST_FUN_OBJ_1(py_clock_avg_obj, py_clock_avg);
61+
62+
mp_obj_t py_clock_reset(mp_obj_t clock_obj) {
63+
py_clock_obj_t *clock = (py_clock_obj_t *) clock_obj;
64+
clock->t_start = 0;
65+
clock->t_ticks = 0;
66+
clock->t_frame = 0;
67+
return mp_const_none;
68+
}
69+
static MP_DEFINE_CONST_FUN_OBJ_1(py_clock_reset_obj, py_clock_reset);
70+
71+
static void py_clock_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
72+
py_clock_obj_t *self = self_in;
73+
mp_printf(print, "t_start:%d t_ticks:%d t_frame:%d\n", self->t_start, self->t_ticks, self->t_frame);
74+
}
75+
76+
mp_obj_t py_clock_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
77+
py_clock_obj_t *clock = NULL;
78+
clock = m_new_obj(py_clock_obj_t);
79+
clock->base.type = &py_clock_type;
80+
clock->t_start = 0;
81+
clock->t_ticks = 0;
82+
clock->t_frame = 0;
83+
return MP_OBJ_FROM_PTR(clock);
84+
}
85+
86+
static const mp_rom_map_elem_t py_clock_locals_dict_table[] = {
87+
{ MP_OBJ_NEW_QSTR(MP_QSTR_tick), MP_ROM_PTR(&py_clock_tick_obj)},
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_fps), MP_ROM_PTR(&py_clock_fps_obj)},
89+
{ MP_OBJ_NEW_QSTR(MP_QSTR_avg), MP_ROM_PTR(&py_clock_avg_obj)},
90+
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), MP_ROM_PTR(&py_clock_reset_obj)},
91+
{ NULL, NULL },
92+
};
93+
static MP_DEFINE_CONST_DICT(py_clock_locals_dict, py_clock_locals_dict_table);
94+
95+
MP_DEFINE_CONST_OBJ_TYPE(
96+
py_clock_type,
97+
MP_QSTR_Clock,
98+
MP_TYPE_FLAG_NONE,
99+
print, py_clock_print,
100+
make_new, py_clock_make_new,
101+
locals_dict, &py_clock_locals_dict
102+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
*
4+
* Copyright (C) 2013-2024 OpenMV, LLC.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
* Clock Python module.
25+
*/
26+
#ifndef __PY_CLOCK_H__
27+
#define __PY_CLOCK_H__
28+
extern const mp_obj_type_t py_clock_type;
29+
#endif // __PY_CLOCK_H__

0 commit comments

Comments
 (0)