|
| 1 | +#include <mpy_m5unified.h> |
| 2 | + |
| 3 | +#define MAKE_METHOD_V(prefix, func, arg_min, arg_max) extern mp_obj_t prefix##_##func(size_t,const mp_obj_t*); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( prefix##_##func##_obj, arg_min, arg_max, prefix##_##func ); |
| 4 | +#define MAKE_METHOD_0(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t ); STATIC MP_DEFINE_CONST_FUN_OBJ_1( prefix##_##func##_obj, prefix##_##func ); |
| 5 | +#define MAKE_METHOD_1(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t,mp_obj_t ); STATIC MP_DEFINE_CONST_FUN_OBJ_2( prefix##_##func##_obj, prefix##_##func ); |
| 6 | +#define MAKE_METHOD_2(prefix, func) extern mp_obj_t prefix##_##func(mp_obj_t,mp_obj_t,mp_obj_t); STATIC MP_DEFINE_CONST_FUN_OBJ_3( prefix##_##func##_obj, prefix##_##func ); |
| 7 | + |
| 8 | +#define MAKE_TABLE( prefix, func ) \ |
| 9 | + { MP_ROM_QSTR(MP_QSTR_##func), MP_ROM_PTR(&prefix##_##func##_obj) } |
| 10 | + |
| 11 | +//-------- GFX common wrapper |
| 12 | +MAKE_METHOD_0( gfx, width ); |
| 13 | +MAKE_METHOD_0( gfx, height ); |
| 14 | +MAKE_METHOD_0( gfx, getRotation ); |
| 15 | +MAKE_METHOD_0( gfx, getColorDepth ); |
| 16 | +MAKE_METHOD_1( gfx, setRotation ); |
| 17 | +MAKE_METHOD_1( gfx, setColorDepth ); |
| 18 | +MAKE_METHOD_1( gfx, print ); |
| 19 | +MAKE_METHOD_2( gfx, setCursor ); |
| 20 | +MAKE_METHOD_V( gfx, fillScreen , 1, 2 ); |
| 21 | +MAKE_METHOD_V( gfx, drawPixel , 3, 4 ); |
| 22 | +MAKE_METHOD_V( gfx, drawCircle , 4, 5 ); |
| 23 | +MAKE_METHOD_V( gfx, fillCircle , 4, 5 ); |
| 24 | +MAKE_METHOD_V( gfx, drawLine , 5, 6 ); |
| 25 | +MAKE_METHOD_V( gfx, drawRect , 5, 6 ); |
| 26 | +MAKE_METHOD_V( gfx, fillRect , 5, 6 ); |
| 27 | +MAKE_METHOD_V( gfx, printf , 2, 32); |
| 28 | +MAKE_METHOD_V( gfx, newCanvas , 3, 5 ); |
| 29 | + |
| 30 | +#define TABLE_PARTS_GFX_BASE \ |
| 31 | + MAKE_TABLE( gfx, drawCircle ), \ |
| 32 | + MAKE_TABLE( gfx, drawLine ), \ |
| 33 | + MAKE_TABLE( gfx, drawPixel ), \ |
| 34 | + MAKE_TABLE( gfx, drawRect ), \ |
| 35 | + MAKE_TABLE( gfx, fillCircle ), \ |
| 36 | + MAKE_TABLE( gfx, fillRect ), \ |
| 37 | + MAKE_TABLE( gfx, fillScreen ), \ |
| 38 | + MAKE_TABLE( gfx, getColorDepth), \ |
| 39 | + MAKE_TABLE( gfx, getRotation ), \ |
| 40 | + MAKE_TABLE( gfx, height ), \ |
| 41 | + MAKE_TABLE( gfx, newCanvas ), \ |
| 42 | + MAKE_TABLE( gfx, print ), \ |
| 43 | + MAKE_TABLE( gfx, printf ), \ |
| 44 | + MAKE_TABLE( gfx, setColorDepth), \ |
| 45 | + MAKE_TABLE( gfx, setCursor ), \ |
| 46 | + MAKE_TABLE( gfx, setRotation ), \ |
| 47 | + MAKE_TABLE( gfx, width ), \ |
| 48 | + { MP_ROM_QSTR(MP_QSTR_BLACK ), MP_ROM_INT( 0x000000 ) }, \ |
| 49 | + { MP_ROM_QSTR(MP_QSTR_NAVY ), MP_ROM_INT( 0x000080 ) }, \ |
| 50 | + { MP_ROM_QSTR(MP_QSTR_DARKGREEN ), MP_ROM_INT( 0x008000 ) }, \ |
| 51 | + { MP_ROM_QSTR(MP_QSTR_DARKCYAN ), MP_ROM_INT( 0x008080 ) }, \ |
| 52 | + { MP_ROM_QSTR(MP_QSTR_MAROON ), MP_ROM_INT( 0x800000 ) }, \ |
| 53 | + { MP_ROM_QSTR(MP_QSTR_PURPLE ), MP_ROM_INT( 0x800080 ) }, \ |
| 54 | + { MP_ROM_QSTR(MP_QSTR_OLIVE ), MP_ROM_INT( 0x808000 ) }, \ |
| 55 | + { MP_ROM_QSTR(MP_QSTR_LIGHTGREY ), MP_ROM_INT( 0xC0C0C0 ) }, \ |
| 56 | + { MP_ROM_QSTR(MP_QSTR_DARKGREY ), MP_ROM_INT( 0x808080 ) }, \ |
| 57 | + { MP_ROM_QSTR(MP_QSTR_BLUE ), MP_ROM_INT( 0x0000FF ) }, \ |
| 58 | + { MP_ROM_QSTR(MP_QSTR_GREEN ), MP_ROM_INT( 0x00FF00 ) }, \ |
| 59 | + { MP_ROM_QSTR(MP_QSTR_CYAN ), MP_ROM_INT( 0x00FFFF ) }, \ |
| 60 | + { MP_ROM_QSTR(MP_QSTR_RED ), MP_ROM_INT( 0xFF0000 ) }, \ |
| 61 | + { MP_ROM_QSTR(MP_QSTR_MAGENTA ), MP_ROM_INT( 0xFF00FF ) }, \ |
| 62 | + { MP_ROM_QSTR(MP_QSTR_YELLOW ), MP_ROM_INT( 0xFFFF00 ) }, \ |
| 63 | + { MP_ROM_QSTR(MP_QSTR_WHITE ), MP_ROM_INT( 0xFFFFFF ) }, \ |
| 64 | + { MP_ROM_QSTR(MP_QSTR_ORANGE ), MP_ROM_INT( 0xFFA500 ) }, \ |
| 65 | + { MP_ROM_QSTR(MP_QSTR_GREENYELLOW), MP_ROM_INT( 0xADFF2F ) }, \ |
| 66 | + { MP_ROM_QSTR(MP_QSTR_PINK ), MP_ROM_INT( 0xFFC0CB ) } |
| 67 | + |
| 68 | +//-------- GFX device wrapper |
| 69 | +MAKE_METHOD_0( gfx, startWrite ); |
| 70 | +MAKE_METHOD_0( gfx, endWrite ); |
| 71 | + |
| 72 | +STATIC const mp_rom_map_elem_t gfxdevice_member_table[] = { |
| 73 | + TABLE_PARTS_GFX_BASE , |
| 74 | + MAKE_TABLE( gfx, startWrite ), |
| 75 | + MAKE_TABLE( gfx, endWrite ), |
| 76 | +}; |
| 77 | +STATIC MP_DEFINE_CONST_DICT(gfxdevice_member, gfxdevice_member_table); |
| 78 | +const mp_obj_type_t gfxdevice_type = { |
| 79 | + { &mp_type_type }, |
| 80 | + .locals_dict = (mp_obj_dict_t *)&gfxdevice_member, |
| 81 | +}; |
| 82 | + |
| 83 | +//-------- GFX canvas wrapper |
| 84 | +MAKE_METHOD_0( gfx, delete ); |
| 85 | +MAKE_METHOD_2( gfx, push ); |
| 86 | + |
| 87 | +STATIC const mp_rom_map_elem_t gfxcanvas_member_table[] = { |
| 88 | + TABLE_PARTS_GFX_BASE , |
| 89 | + MAKE_TABLE( gfx, delete), |
| 90 | + MAKE_TABLE( gfx, push), |
| 91 | +}; |
| 92 | +STATIC MP_DEFINE_CONST_DICT(gfxcanvas_member, gfxcanvas_member_table); |
| 93 | +const mp_obj_type_t gfxcanvas_type = { |
| 94 | + { &mp_type_type }, |
| 95 | + .flags = MP_TYPE_FLAG_IS_SUBCLASSED, |
| 96 | + .locals_dict = (mp_obj_dict_t *)&gfxcanvas_member, |
| 97 | +}; |
| 98 | + |
| 99 | +//-------- Button wrapper |
| 100 | +MAKE_METHOD_0( btn, isHolding ); |
| 101 | +MAKE_METHOD_0( btn, isPressed ); |
| 102 | +MAKE_METHOD_0( btn, isReleased ); |
| 103 | +MAKE_METHOD_0( btn, wasChangePressed ); |
| 104 | +MAKE_METHOD_0( btn, wasClicked ); |
| 105 | +MAKE_METHOD_0( btn, wasHold ); |
| 106 | +MAKE_METHOD_0( btn, wasPressed ); |
| 107 | +MAKE_METHOD_0( btn, wasReleased ); |
| 108 | +MAKE_METHOD_0( btn, lastChange ); |
| 109 | +MAKE_METHOD_1( btn, pressedFor ); |
| 110 | +MAKE_METHOD_1( btn, releasedFor ); |
| 111 | +MAKE_METHOD_1( btn, setDebounceThresh); |
| 112 | +MAKE_METHOD_1( btn, setHoldThresh ); |
| 113 | + |
| 114 | +STATIC const mp_rom_map_elem_t btn_member_table[] = { |
| 115 | + MAKE_TABLE( btn, isHolding ), |
| 116 | + MAKE_TABLE( btn, isPressed ), |
| 117 | + MAKE_TABLE( btn, isReleased ), |
| 118 | + MAKE_TABLE( btn, wasChangePressed ), |
| 119 | + MAKE_TABLE( btn, wasClicked ), |
| 120 | + MAKE_TABLE( btn, wasHold ), |
| 121 | + MAKE_TABLE( btn, wasPressed ), |
| 122 | + MAKE_TABLE( btn, wasReleased ), |
| 123 | + MAKE_TABLE( btn, lastChange ), |
| 124 | + MAKE_TABLE( btn, pressedFor ), |
| 125 | + MAKE_TABLE( btn, releasedFor ), |
| 126 | + MAKE_TABLE( btn, setDebounceThresh), |
| 127 | + MAKE_TABLE( btn, setHoldThresh ), |
| 128 | +}; |
| 129 | + |
| 130 | +STATIC MP_DEFINE_CONST_DICT(btn_member, btn_member_table); |
| 131 | +const mp_obj_type_t btn_type = { |
| 132 | + { &mp_type_type }, |
| 133 | + .locals_dict = (mp_obj_dict_t *)&btn_member, |
| 134 | +}; |
| 135 | + |
| 136 | + |
| 137 | +//-------- M5 wrapper |
| 138 | + |
| 139 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(m5_begin_obj, m5_begin); |
| 140 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(m5_update_obj, m5_update); |
| 141 | + |
| 142 | +STATIC const mp_rom_map_elem_t m5_globals_table[] = { |
| 143 | + { MP_ROM_QSTR(MP_QSTR___name__ ), MP_ROM_QSTR(MP_QSTR_m5) }, |
| 144 | + { MP_ROM_QSTR(MP_QSTR_begin ), MP_ROM_PTR(&m5_begin_obj) }, |
| 145 | + { MP_ROM_QSTR(MP_QSTR_update ), MP_ROM_PTR(&m5_update_obj) }, |
| 146 | + { MP_ROM_QSTR(MP_QSTR_btnA ), MP_OBJ_FROM_PTR(&m5_btnA) }, |
| 147 | + { MP_ROM_QSTR(MP_QSTR_btnB ), MP_OBJ_FROM_PTR(&m5_btnB) }, |
| 148 | + { MP_ROM_QSTR(MP_QSTR_btnC ), MP_OBJ_FROM_PTR(&m5_btnC) }, |
| 149 | + { MP_ROM_QSTR(MP_QSTR_btnPWR ), MP_OBJ_FROM_PTR(&m5_btnPWR) }, |
| 150 | + { MP_ROM_QSTR(MP_QSTR_btnEXT ), MP_OBJ_FROM_PTR(&m5_btnEXT) }, |
| 151 | + { MP_ROM_QSTR(MP_QSTR_display ), MP_OBJ_FROM_PTR(&m5_display) }, |
| 152 | + { MP_ROM_QSTR(MP_QSTR_lcd ), MP_OBJ_FROM_PTR(&m5_display) }, |
| 153 | +}; |
| 154 | + |
| 155 | +STATIC MP_DEFINE_CONST_DICT(mp_module_m5_globals, m5_globals_table); |
| 156 | + |
| 157 | +// Define module object. |
| 158 | +const mp_obj_module_t m5m5unified_user_cmodule = { |
| 159 | + .base = { &mp_type_module }, |
| 160 | + .globals = (mp_obj_dict_t *)&mp_module_m5_globals, |
| 161 | +}; |
| 162 | + |
| 163 | + |
| 164 | +MP_REGISTER_MODULE(MP_QSTR_m5, m5m5unified_user_cmodule, MODULE_M5UNIFIED_ENABLED); |
0 commit comments