Skip to content

Commit 5837c2e

Browse files
committed
[add] expose userfunc module to micropython
1 parent 7ae6622 commit 5837c2e

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

port/genhdr/qstrdefs.generated.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,4 @@ QDEF(MP_QSTR_IRQ, (const byte*)"\xaf\x03" "IRQ")
769769
QDEF(MP_QSTR_IRQ_HIGH_LEVEL, (const byte*)"\x57\x0e" "IRQ_HIGH_LEVEL")
770770
QDEF(MP_QSTR_IRQ_LOW_LEVEL, (const byte*)"\x8d\x0d" "IRQ_LOW_LEVEL")
771771
QDEF(MP_QSTR_show_bmp, (const byte*)"\xe6\x08" "show_bmp")
772-
// This file was automatically generated by makeqstrdata.py
772+
QDEF(MP_QSTR_userfunc, (const byte*)"\x4a\x08" "userfunc")

port/modules/moduserfunc.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 SummerGift <[email protected]>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
29+
STATIC mp_obj_t add(
30+
mp_obj_t arg_1_obj,
31+
mp_obj_t arg_2_obj) {
32+
mp_int_t arg_1 = mp_obj_get_int(arg_1_obj);
33+
mp_int_t arg_2 = mp_obj_get_int(arg_2_obj);
34+
mp_int_t ret_val;
35+
36+
/* Your code start! */
37+
38+
ret_val = arg_1 + arg_2;
39+
40+
/* Your code end! */
41+
42+
return mp_obj_new_int(ret_val);
43+
}
44+
MP_DEFINE_CONST_FUN_OBJ_2(add_obj, add);
45+
46+
STATIC const mp_rom_map_elem_t mp_module_userfunc_globals_table[] = {
47+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_userfunc) },
48+
{ MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&add_obj) },
49+
};
50+
51+
STATIC MP_DEFINE_CONST_DICT(mp_module_userfunc_globals, mp_module_userfunc_globals_table);
52+
53+
const mp_obj_module_t mp_module_userfunc = {
54+
.base = { &mp_type_module },
55+
.globals = (mp_obj_dict_t*)&mp_module_userfunc_globals,
56+
};
57+

port/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ extern const struct _mp_obj_module_t mp_module_io;
354354
extern const struct _mp_obj_fun_builtin_fixed_t machine_soft_reset_obj;
355355
extern const struct _mp_obj_module_t mp_module_ffi;
356356
extern const struct _mp_obj_module_t mp_module_network;
357+
extern const struct _mp_obj_module_t mp_module_userfunc;
357358

358359
#if MICROPY_PY_RTTHREAD
359360
#define RTTHREAD_PORT_BUILTIN_MODULES { MP_ROM_QSTR(MP_QSTR_rtthread), MP_ROM_PTR(&mp_module_rtthread) },
@@ -478,6 +479,8 @@ extern const struct _mp_obj_module_t mp_module_network;
478479
#define MODNETWORK_PORT_BUILTIN_MODULES
479480
#endif
480481

482+
#define USERFUNC_PORT_BUILTIN_MODULES { MP_ROM_QSTR(MP_QSTR_userfunc), MP_ROM_PTR(&mp_module_userfunc) },
483+
481484
// extra built in names to add to the global namespace
482485
#define MICROPY_PORT_BUILTINS \
483486
{ MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&machine_soft_reset_obj) }, \
@@ -493,6 +496,7 @@ extern const struct _mp_obj_module_t mp_module_network;
493496
MODUTIME_PORT_BUILTIN_MODULES \
494497
MODFFI_PORT_BUILTIN_MODULES \
495498
MODNETWORK_PORT_BUILTIN_MODULES \
499+
USERFUNC_PORT_BUILTIN_MODULES \
496500

497501
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
498502
MODUTIME_PORT_BUILTIN_MODULE_WEAK_LINKS \

0 commit comments

Comments
 (0)