Skip to content

Commit

Permalink
Merge pull request #3659 from iot49/msgpack
Browse files Browse the repository at this point in the history
module msgpack
  • Loading branch information
tannewt authored Jan 11, 2021
2 parents 1fb3d24 + 13efbf2 commit 9124529
Show file tree
Hide file tree
Showing 27 changed files with 952 additions and 0 deletions.
24 changes: 24 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ msgstr ""
msgid "3-arg pow() not supported"
msgstr ""

#: shared-module/msgpack/__init__.c
msgid "64 bit types"
msgstr ""

#: ports/atmel-samd/common-hal/countio/Counter.c
#: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c
msgid "A hardware interrupt channel is already in use"
Expand Down Expand Up @@ -2426,6 +2430,10 @@ msgstr ""
msgid "circle can only be registered in one parent"
msgstr ""

#: shared-bindings/msgpack/ExtType.c
msgid "code outside range 0~127"
msgstr ""

#: shared-bindings/displayio/Palette.c
msgid "color buffer must be 3 bytes (RGB) or 4 bytes (RGB + pad byte)"
msgstr ""
Expand Down Expand Up @@ -2510,6 +2518,10 @@ msgstr ""
msgid "default 'except' must be last"
msgstr ""

#: shared-bindings/msgpack/__init__.c
msgid "default is not a function"
msgstr ""

#: shared-bindings/audiobusio/PDMIn.c
msgid ""
"destination buffer must be a bytearray or array of type 'B' for bit_depth = 8"
Expand Down Expand Up @@ -2609,6 +2621,10 @@ msgstr ""
msgid "expecting key:value for dict"
msgstr ""

#: shared-bindings/msgpack/__init__.c
msgid "ext_hook is not a function"
msgstr ""

#: py/argcheck.c
msgid "extra keyword arguments given"
msgstr ""
Expand Down Expand Up @@ -3141,6 +3157,10 @@ msgstr ""
msgid "no binding for nonlocal found"
msgstr ""

#: shared-module/msgpack/__init__.c
msgid "no default packer"
msgstr ""

#: py/builtinimport.c
msgid "no module named '%q'"
msgstr ""
Expand Down Expand Up @@ -3474,6 +3494,10 @@ msgstr ""
msgid "shape must be a tuple"
msgstr ""

#: shared-module/msgpack/__init__.c
msgid "short read"
msgstr ""

#: py/objstr.c
msgid "sign not allowed in string format specifier"
msgstr ""
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/bdmicro_vina_d21/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_PIXELBUF = 1
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_PIXELBUF = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_GAMEPAD = 0
CFLAGS_INLINE_LIMIT = 50
CIRCUITPY_MSGPACK = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
# supersized, not ultra-supersized
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/qtpy_m0_haxpress/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0

SUPEROPT_GC = 0

Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/snekboard/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_BITBANGIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_BUSDEVICE = 0

Expand Down
2 changes: 2 additions & 0 deletions ports/cxd56/boards/spresense/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ USB_PRODUCT = "Spresense"
USB_MANUFACTURER = "Sony"

INTERNAL_FLASH_FILESYSTEM = 1

CIRCUITPY_MSGPACK = 0
1 change: 1 addition & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
CIRCUITPY_PIXELBUF = 0
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/simmel/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BUSIO = 1
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_MSGPACK = 0
CIRCUITPY_GAMEPAD = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
Expand Down
6 changes: 6 additions & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ endif
ifeq ($(CIRCUITPY_PEW),1)
SRC_PATTERNS += _pew/%
endif
ifeq ($(CIRCUITPY_MSGPACK),1)
SRC_PATTERNS += msgpack/%
endif

# All possible sources are listed here, and are filtered by SRC_PATTERNS in SRC_COMMON_HAL
SRC_COMMON_HAL_ALL = \
Expand Down Expand Up @@ -412,6 +415,8 @@ $(filter $(SRC_PATTERNS), \
math/__init__.c \
microcontroller/ResetReason.c \
microcontroller/RunMode.c \
msgpack/__init__.c \
msgpack/ExtType.c \
supervisor/RunReason.c \
)

Expand Down Expand Up @@ -481,6 +486,7 @@ SRC_SHARED_MODULE_ALL = \
memorymonitor/AllocationAlarm.c \
memorymonitor/AllocationSize.c \
network/__init__.c \
msgpack/__init__.c \
os/__init__.c \
random/__init__.c \
rgbmatrix/RGBMatrix.c \
Expand Down
8 changes: 8 additions & 0 deletions py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,13 @@ extern const struct _mp_obj_module_t wifi_module;
#define WIFI_MODULE
#endif

#if CIRCUITPY_MSGPACK
extern const struct _mp_obj_module_t msgpack_module;
#define MSGPACK_MODULE { MP_ROM_QSTR(MP_QSTR_msgpack), MP_ROM_PTR(&msgpack_module) },
#else
#define MSGPACK_MODULE
#endif

// Define certain native modules with weak links so they can be replaced with Python
// implementations. This list may grow over time.
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
Expand Down Expand Up @@ -830,6 +837,7 @@ extern const struct _mp_obj_module_t wifi_module;
_EVE_MODULE \
MEMORYMONITOR_MODULE \
MICROCONTROLLER_MODULE \
MSGPACK_MODULE \
NEOPIXEL_WRITE_MODULE \
NETWORK_MODULE \
SOCKET_MODULE \
Expand Down
3 changes: 3 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,6 @@ CFLAGS += -DCIRCUITPY_WIFI=$(CIRCUITPY_WIFI)
# Enabled micropython.native decorator (experimental)
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)

CIRCUITPY_MSGPACK ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_MSGPACK=$(CIRCUITPY_MSGPACK)
126 changes: 126 additions & 0 deletions shared-bindings/msgpack/ExtType.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Bernhard Boser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "py/runtime.h"
#include "py/smallint.h"
#include "py/objproperty.h"
#include "shared-bindings/msgpack/ExtType.h"

//| class ExtType:
//| """ExtType represents ext type in msgpack."""
//| def __init__(self, code: int, data: bytes) -> None:
//| """Constructor
//| :param int code: type code in range 0~127.
//| :param bytes data: representation."""
//|
STATIC mp_obj_t mod_msgpack_exttype_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mod_msgpack_extype_obj_t *self = m_new_obj(mod_msgpack_extype_obj_t);
self->base.type = &mod_msgpack_exttype_type;
enum { ARG_code, ARG_data };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_code, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

int code = args[ARG_code].u_int;
if (code < 0 || code > 127) {
mp_raise_AttributeError(translate("code outside range 0~127"));
}
self->code = code;

mp_obj_t data = args[ARG_data].u_obj;
self->data = data;
return MP_OBJ_FROM_PTR(self);
}


//| code: int
//| """The type code, in range 0~127."""
//| ...
//|
STATIC mp_obj_t mod_msgpack_exttype_get_code(mp_obj_t self_in) {
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(self->code);
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_code_obj, mod_msgpack_exttype_get_code);

STATIC mp_obj_t mod_msgpack_exttype_set_code(mp_obj_t self_in, mp_obj_t code_in) {
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
int code = mp_obj_get_int(code_in);
if (code < 0 || code > 127) {
mp_raise_AttributeError(translate("code outside range 0~127"));
}
self->code = code;
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_code_obj, mod_msgpack_exttype_set_code);

const mp_obj_property_t mod_msgpack_exttype_code_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&mod_msgpack_exttype_get_code_obj,
(mp_obj_t)&mod_msgpack_exttype_set_code_obj,
(mp_obj_t)&mp_const_none_obj},
};

//| data: bytes
//| """Data."""
//| ...
//|
STATIC mp_obj_t mod_msgpack_exttype_get_data(mp_obj_t self_in) {
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
return self->data;
}
MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_data_obj, mod_msgpack_exttype_get_data);

STATIC mp_obj_t mod_msgpack_exttype_set_data(mp_obj_t self_in, mp_obj_t data_in) {
mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
self->data = data_in;
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_data_obj, mod_msgpack_exttype_set_data);

const mp_obj_property_t mod_msgpack_exttype_data_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&mod_msgpack_exttype_get_data_obj,
(mp_obj_t)&mod_msgpack_exttype_set_data_obj,
(mp_obj_t)&mp_const_none_obj},
};

STATIC mp_rom_map_elem_t mod_msgpack_exttype_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_code), MP_ROM_PTR(&mod_msgpack_exttype_code_obj) },
{ MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&mod_msgpack_exttype_data_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mod_msgpack_exttype_locals_dict, mod_msgpack_exttype_locals_dict_table);

const mp_obj_type_t mod_msgpack_exttype_type = {
{ &mp_type_type },
.name = MP_QSTR_ExtType,
.make_new = mod_msgpack_exttype_make_new,
.locals_dict = (mp_obj_dict_t*)&mod_msgpack_exttype_locals_dict,
};
Loading

0 comments on commit 9124529

Please sign in to comment.