1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "py/builtin.h"
6 #include "py/mperrno.h"
7 #include "py/obj.h"
8 #include "py/runtime.h"
9 #include "ulog/ulog.h"
10 
11 #define LOG_TAG "DRIVER_KEYPAD"
12 
13 extern const mp_obj_type_t driver_keypad_type;
14 
15 // this is the actual C-structure for our new object
16 typedef struct {
17     // base represents some basic information, like type
18     mp_obj_base_t Base;
19     // a member created by us
20     char *ModuleName;
21 } mp_keypad_obj_t;
22 
keypad_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)23 void keypad_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
24 {
25     LOGD(LOG_TAG, "entern %s;\n", __func__);
26     mp_keypad_obj_t *self = MP_OBJ_TO_PTR(self_in);
27     mp_printf(print, "ModuleName(%s)", self->ModuleName);
28 }
29 
keypad_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)30 STATIC mp_obj_t keypad_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
31 {
32     LOGD(LOG_TAG, "entern  %s;\n", __func__);
33     mp_keypad_obj_t *driver_obj = m_new_obj(mp_keypad_obj_t);
34     if (!driver_obj) {
35         mp_raise_OSError(MP_EINVAL);
36     }
37 
38     driver_obj->Base.type = &driver_keypad_type;
39     driver_obj->ModuleName = "keypad";
40 
41     return MP_OBJ_FROM_PTR(driver_obj);
42 }
43 
obj_open(size_t n_args,const mp_obj_t * args)44 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
45 {
46     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
47     int ret = -1;
48     void *instance = NULL;
49     if (n_args < 5) {
50         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
51         return mp_const_none;
52     }
53     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
54     mp_keypad_obj_t *driver_obj = (mp_keypad_obj_t *)self;
55     if (driver_obj == NULL) {
56         LOGE(LOG_TAG, "driver_obj is NULL\n");
57         return mp_const_none;
58     }
59     LOGD(LOG_TAG, "%s:out\n", __func__);
60 
61     return mp_const_none;
62 }
63 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(keypad_obj_open, 5, obj_open);
64 
obj_close(size_t n_args,const mp_obj_t * args)65 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
66 {
67     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
68     int ret = -1;
69     void *instance = NULL;
70     if (n_args < 5) {
71         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
72         return mp_const_none;
73     }
74     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
75     mp_keypad_obj_t *driver_obj = (mp_keypad_obj_t *)self;
76     if (driver_obj == NULL) {
77         LOGE(LOG_TAG, "driver_obj is NULL\n");
78         return mp_const_none;
79     }
80     LOGD(LOG_TAG, "%s:out\n", __func__);
81 
82     return mp_const_none;
83 }
84 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(keypad_obj_close, 5, obj_close);
85 
obj_on(size_t n_args,const mp_obj_t * args)86 STATIC mp_obj_t obj_on(size_t n_args, const mp_obj_t *args)
87 {
88     LOGD(LOG_TAG, "entern  %s; n_args = %d;\n", __func__, n_args);
89     int ret = -1;
90     void *instance = NULL;
91     if (n_args < 5) {
92         LOGE(LOG_TAG, "%s: args num is illegal :n_args = %d;\n", __func__, n_args);
93         return mp_const_none;
94     }
95     mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
96     mp_keypad_obj_t *driver_obj = (mp_keypad_obj_t *)self;
97     if (driver_obj == NULL) {
98         LOGE(LOG_TAG, "driver_obj is NULL\n");
99         return mp_const_none;
100     }
101     LOGD(LOG_TAG, "%s:out\n", __func__);
102 
103     return mp_const_none;
104 }
105 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(keypad_obj_on, 5, obj_on);
106 
107 STATIC const mp_rom_map_elem_t keypad_locals_dict_table[] = {
108     { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_KeyPad) },
109     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&keypad_obj_open) },
110     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&keypad_obj_close) },
111     { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&keypad_obj_on) },
112 };
113 
114 STATIC MP_DEFINE_CONST_DICT(keypad_locals_dict, keypad_locals_dict_table);
115 
116 const mp_obj_type_t driver_keypad_type = {
117     .base = { &mp_type_type },
118     .name = MP_QSTR_KeyPad,
119     .print = keypad_obj_print,
120     .make_new = keypad_obj_make_new,
121     .locals_dict = (mp_obj_dict_t *)&keypad_locals_dict,
122 };
123