From cf62eae4cec84ca27620a6b30978e6a2cf4e5798 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 27 Feb 2024 10:59:29 +0000 Subject: [PATCH] MicroPython: Switch to mp_obj_malloc_with_finaliser. --- micropython/modules/adcfft/adcfft.cpp | 3 +-- micropython/modules/badger2040/badger2040.cpp | 3 +-- .../modules/breakout_mlx90640/mlx90640.cpp | 3 +-- .../breakout_pmw3901/breakout_pmw3901.cpp | 6 ++---- .../modules/breakout_vl53l5cx/vl53l5cx.cpp | 3 +-- .../modules/cosmic_unicorn/cosmic_unicorn.cpp | 6 ++---- micropython/modules/encoder/encoder.cpp | 3 +-- .../galactic_unicorn/galactic_unicorn.cpp | 6 ++---- micropython/modules/hub75/hub75.cpp | 3 +-- micropython/modules/jpegdec/jpegdec.cpp | 3 +-- micropython/modules/motor/motor.cpp | 6 ++---- .../pico_rgb_keypad/pico_rgb_keypad.cpp | 3 +-- .../modules/pico_scroll/pico_scroll.cpp | 3 +-- .../modules/pico_unicorn/pico_unicorn.cpp | 3 +-- .../modules/picographics/picographics.cpp | 3 +-- micropython/modules/picovector/picovector.cpp | 9 +++------ micropython/modules/plasma/plasma.cpp | 6 ++---- micropython/modules/pngdec/pngdec.cpp | 3 +-- micropython/modules/servo/servo.cpp | 18 ++++++------------ .../stellar_unicorn/stellar_unicorn.cpp | 6 ++---- 20 files changed, 33 insertions(+), 66 deletions(-) diff --git a/micropython/modules/adcfft/adcfft.cpp b/micropython/modules/adcfft/adcfft.cpp index e1e28a9b6..4211e8d8f 100644 --- a/micropython/modules/adcfft/adcfft.cpp +++ b/micropython/modules/adcfft/adcfft.cpp @@ -21,8 +21,7 @@ mp_obj_t adcfft_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - adcfft_obj_t *self = m_new_obj_with_finaliser(adcfft_obj_t); - self->base.type = &adcfft_type; + adcfft_obj_t *self = mp_obj_malloc_with_finaliser(adcfft_obj_t, &adcfft_type); unsigned int adc_channel = args[ARG_adc_channel].u_int; unsigned int adc_gpio = args[ARG_adc_gpio].u_int; diff --git a/micropython/modules/badger2040/badger2040.cpp b/micropython/modules/badger2040/badger2040.cpp index f2a38a5d0..f0e2c25a5 100644 --- a/micropython/modules/badger2040/badger2040.cpp +++ b/micropython/modules/badger2040/badger2040.cpp @@ -98,8 +98,7 @@ mp_obj_t Badger2040_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ buffer = m_new(uint8_t, width * height / 8); } - badger2040_obj = m_new_obj_with_finaliser(_Badger2040_obj_t); - badger2040_obj->base.type = &Badger2040_type; + badger2040_obj = mp_obj_malloc_with_finaliser(_Badger2040_obj_t, &Badger2040_type); badger2040_obj->buf = buffer; badger2040_obj->badger2040 = m_new_class(pimoroni::Badger2040, buffer); badger2040_obj->badger2040->init(); diff --git a/micropython/modules/breakout_mlx90640/mlx90640.cpp b/micropython/modules/breakout_mlx90640/mlx90640.cpp index 369377024..b48ba88ae 100644 --- a/micropython/modules/breakout_mlx90640/mlx90640.cpp +++ b/micropython/modules/breakout_mlx90640/mlx90640.cpp @@ -51,8 +51,7 @@ mp_obj_t MLX90640_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - _ModMLX90640_obj_t *self = m_new_obj_with_finaliser(_ModMLX90640_obj_t); - self->base.type = &MLX90640_type; + _ModMLX90640_obj_t *self = mp_obj_malloc_with_finaliser(_ModMLX90640_obj_t, &MLX90640_type); self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); self->address = args[ARG_address].u_int; diff --git a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp index 0bc86bc89..f970ec55b 100644 --- a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp +++ b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp @@ -74,8 +74,7 @@ mp_obj_t make_new(enum ChipType chip, const mp_obj_type_t *type, size_t n_args, int slot = args[ARG_slot].u_int; if(slot == BG_SPI_FRONT || slot == BG_SPI_BACK) { - self = m_new_obj_with_finaliser(breakout_pmw3901_BreakoutPMW3901_obj_t); - self->base.type = &breakout_pmw3901_BreakoutPMW3901_type; + self = mp_obj_malloc_with_finaliser(breakout_pmw3901_BreakoutPMW3901_obj_t, &breakout_pmw3901_BreakoutPMW3901_type); if(chip == ChipType::PMW3901) { BreakoutPMW3901 *breakout = m_new_class(BreakoutPMW3901, (BG_SPI_SLOT)slot); @@ -137,8 +136,7 @@ mp_obj_t make_new(enum ChipType chip, const mp_obj_type_t *type, size_t n_args, mp_raise_ValueError(MP_ERROR_TEXT("bad MISO pin")); } - self = m_new_obj_with_finaliser(breakout_pmw3901_BreakoutPMW3901_obj_t); - self->base.type = &breakout_pmw3901_BreakoutPMW3901_type; + self = mp_obj_malloc_with_finaliser(breakout_pmw3901_BreakoutPMW3901_obj_t, &breakout_pmw3901_BreakoutPMW3901_type); spi_inst_t *spi = (spi_id == 0) ? spi0 : spi1; if(chip == ChipType::PMW3901) { diff --git a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp index 1dabb1047..c9233a69d 100644 --- a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp +++ b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp @@ -60,8 +60,7 @@ mp_obj_t VL53L5CX_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw int addr = args[ARG_addr].u_int; - self = m_new_obj_with_finaliser(_VL53L5CX_obj_t); - self->base.type = &VL53L5CX_type; + self = mp_obj_malloc_with_finaliser(_VL53L5CX_obj_t, &VL53L5CX_type); self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); diff --git a/micropython/modules/cosmic_unicorn/cosmic_unicorn.cpp b/micropython/modules/cosmic_unicorn/cosmic_unicorn.cpp index 5fffb7bea..9f06738fa 100644 --- a/micropython/modules/cosmic_unicorn/cosmic_unicorn.cpp +++ b/micropython/modules/cosmic_unicorn/cosmic_unicorn.cpp @@ -391,8 +391,7 @@ mp_obj_t CosmicUnicorn_make_new(const mp_obj_type_t *type, size_t n_args, size_t CosmicUnicorn *cosmic = m_new_class(CosmicUnicorn); cosmic->init(); - self = m_new_obj_with_finaliser(_CosmicUnicorn_obj_t); - self->base.type = &CosmicUnicorn_type; + self = mp_obj_malloc_with_finaliser(_CosmicUnicorn_obj_t, &CosmicUnicorn_type); self->cosmic = cosmic; return MP_OBJ_FROM_PTR(self); @@ -507,8 +506,7 @@ extern mp_obj_t CosmicUnicorn_synth_channel(mp_obj_t self_in, mp_obj_t channel_i // NOTE This seems to work, in that it give MP access to the calibration object // Could very easily mess up in weird ways once object deletion is considered? - _Channel_obj_t *channel_obj = m_new_obj_with_finaliser(_Channel_obj_t); - channel_obj->base.type = &Channel_type; + _Channel_obj_t *channel_obj = mp_obj_malloc_with_finaliser(_Channel_obj_t, &Channel_type); channel_obj->channel = &self->cosmic->synth_channel(channel); return MP_OBJ_FROM_PTR(channel_obj); diff --git a/micropython/modules/encoder/encoder.cpp b/micropython/modules/encoder/encoder.cpp index f00af3f84..ae74c3002 100644 --- a/micropython/modules/encoder/encoder.cpp +++ b/micropython/modules/encoder/encoder.cpp @@ -144,8 +144,7 @@ mp_obj_t Encoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, mp_raise_msg(&mp_type_RuntimeError, "unable to allocate the hardware resources needed to initialise this Encoder. Try running `import gc` followed by `gc.collect()` before creating it"); } - self = m_new_obj_with_finaliser(_Encoder_obj_t); - self->base.type = &Encoder_type; + self = mp_obj_malloc_with_finaliser(_Encoder_obj_t, &Encoder_type); self->encoder = encoder; return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/galactic_unicorn/galactic_unicorn.cpp b/micropython/modules/galactic_unicorn/galactic_unicorn.cpp index 64790e163..85860dcf1 100644 --- a/micropython/modules/galactic_unicorn/galactic_unicorn.cpp +++ b/micropython/modules/galactic_unicorn/galactic_unicorn.cpp @@ -391,8 +391,7 @@ mp_obj_t GalacticUnicorn_make_new(const mp_obj_type_t *type, size_t n_args, size GalacticUnicorn *galactic = m_new_class(GalacticUnicorn); galactic->init(); - self = m_new_obj_with_finaliser(_GalacticUnicorn_obj_t); - self->base.type = &GalacticUnicorn_type; + self = mp_obj_malloc_with_finaliser(_GalacticUnicorn_obj_t, &GalacticUnicorn_type); self->galactic = galactic; return MP_OBJ_FROM_PTR(self); @@ -507,8 +506,7 @@ extern mp_obj_t GalacticUnicorn_synth_channel(mp_obj_t self_in, mp_obj_t channel // NOTE This seems to work, in that it give MP access to the calibration object // Could very easily mess up in weird ways once object deletion is considered? - _Channel_obj_t *channel_obj = m_new_obj_with_finaliser(_Channel_obj_t); - channel_obj->base.type = &Channel_type; + _Channel_obj_t *channel_obj = mp_obj_malloc_with_finaliser(_Channel_obj_t, &Channel_type); channel_obj->channel = &self->galactic->synth_channel(channel); return MP_OBJ_FROM_PTR(channel_obj); diff --git a/micropython/modules/hub75/hub75.cpp b/micropython/modules/hub75/hub75.cpp index 8a8c76248..6e586c7fe 100644 --- a/micropython/modules/hub75/hub75.cpp +++ b/micropython/modules/hub75/hub75.cpp @@ -120,8 +120,7 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c buffer = m_new(Pixel, width * height); } - hub75_obj = m_new_obj_with_finaliser(_Hub75_obj_t); - hub75_obj->base.type = &Hub75_type; + hub75_obj = mp_obj_malloc_with_finaliser(_Hub75_obj_t, &Hub75_type); hub75_obj->buf = buffer; hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert, color_order); diff --git a/micropython/modules/jpegdec/jpegdec.cpp b/micropython/modules/jpegdec/jpegdec.cpp index 5c8e34058..5ca66ad89 100644 --- a/micropython/modules/jpegdec/jpegdec.cpp +++ b/micropython/modules/jpegdec/jpegdec.cpp @@ -186,8 +186,7 @@ mp_obj_t _JPEG_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c if(!MP_OBJ_IS_TYPE(args[ARG_picographics].u_obj, &ModPicoGraphics_type)) mp_raise_ValueError(MP_ERROR_TEXT("PicoGraphics Object Required")); - _JPEG_obj_t *self = m_new_obj_with_finaliser(_JPEG_obj_t); - self->base.type = &JPEG_type; + _JPEG_obj_t *self = mp_obj_malloc_with_finaliser(_JPEG_obj_t, &JPEG_type); self->jpeg = m_new_class(JPEGDEC); self->graphics = (ModPicoGraphics_obj_t *)MP_OBJ_TO_PTR(args[ARG_picographics].u_obj); diff --git a/micropython/modules/motor/motor.cpp b/micropython/modules/motor/motor.cpp index bef55d856..5e83178e8 100644 --- a/micropython/modules/motor/motor.cpp +++ b/micropython/modules/motor/motor.cpp @@ -189,8 +189,7 @@ mp_obj_t Motor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c mp_raise_ValueError("mode out of range. Expected FAST_DECAY (0) or SLOW_DECAY (1)"); } - self = m_new_obj_with_finaliser(_Motor_obj_t); - self->base.type = &Motor_type; + self = mp_obj_malloc_with_finaliser(_Motor_obj_t, &Motor_type); self->motor = m_new_class(Motor, pins, (Direction)direction, speed_scale, zeropoint, deadzone, freq, (DecayMode)mode, args[ARG_ph_en_driver].u_bool); self->motor->init(); @@ -741,8 +740,7 @@ mp_obj_t MotorCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t mp_raise_msg(&mp_type_RuntimeError, "unable to allocate the hardware resources needed to initialise this MotorCluster. Try running `import gc` followed by `gc.collect()` before creating it"); } - self = m_new_obj_with_finaliser(_MotorCluster_obj_t); - self->base.type = &MotorCluster_type; + self = mp_obj_malloc_with_finaliser(_MotorCluster_obj_t, &MotorCluster_type); self->cluster = cluster; return MP_OBJ_FROM_PTR(self); diff --git a/micropython/modules/pico_rgb_keypad/pico_rgb_keypad.cpp b/micropython/modules/pico_rgb_keypad/pico_rgb_keypad.cpp index 7e95916fb..a06cb8123 100644 --- a/micropython/modules/pico_rgb_keypad/pico_rgb_keypad.cpp +++ b/micropython/modules/pico_rgb_keypad/pico_rgb_keypad.cpp @@ -18,8 +18,7 @@ typedef struct _PicoKeypad_obj_t { mp_obj_t picokeypad_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { _PicoKeypad_obj_t *self = nullptr; - self = m_new_obj_with_finaliser(PicoKeypad_obj_t); - self->base.type = &PicoKeypad_type; + self = mp_obj_malloc_with_finaliser(PicoKeypad_obj_t, &PicoKeypad_type); self->keypad = m_new_class(PicoRGBKeypad); self->keypad->init(); diff --git a/micropython/modules/pico_scroll/pico_scroll.cpp b/micropython/modules/pico_scroll/pico_scroll.cpp index e382788ac..7f221ccd9 100644 --- a/micropython/modules/pico_scroll/pico_scroll.cpp +++ b/micropython/modules/pico_scroll/pico_scroll.cpp @@ -40,8 +40,7 @@ typedef struct _ModPicoGraphics_obj_t { mp_obj_t picoscroll_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { _PicoScroll_obj_t *self = nullptr; - self = m_new_obj_with_finaliser(PicoScroll_obj_t); - self->base.type = &PicoScroll_type; + self = mp_obj_malloc_with_finaliser(PicoScroll_obj_t, &PicoScroll_type); self->scroll = m_new_class(PicoScroll); self->scroll->init(); diff --git a/micropython/modules/pico_unicorn/pico_unicorn.cpp b/micropython/modules/pico_unicorn/pico_unicorn.cpp index b53123442..cd1ce602b 100644 --- a/micropython/modules/pico_unicorn/pico_unicorn.cpp +++ b/micropython/modules/pico_unicorn/pico_unicorn.cpp @@ -31,8 +31,7 @@ typedef struct _ModPicoGraphics_obj_t { } ModPicoGraphics_obj_t; mp_obj_t picounicorn_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - picounicorn_obj_t *self = m_new_obj_with_finaliser(picounicorn_obj_t); - self->base.type = &picounicorn_type; + picounicorn_obj_t *self = mp_obj_malloc_with_finaliser(picounicorn_obj_t, &picounicorn_type); self->unicorn = m_new_class(PicoUnicorn); return MP_OBJ_FROM_PTR(self); } diff --git a/micropython/modules/picographics/picographics.cpp b/micropython/modules/picographics/picographics.cpp index 2a8553744..b56f078b6 100644 --- a/micropython/modules/picographics/picographics.cpp +++ b/micropython/modules/picographics/picographics.cpp @@ -280,8 +280,7 @@ mp_obj_t ModPicoGraphics_make_new(const mp_obj_type_t *type, size_t n_args, size mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - self = m_new_obj_with_finaliser(ModPicoGraphics_obj_t); - self->base.type = &ModPicoGraphics_type; + self = mp_obj_malloc_with_finaliser(ModPicoGraphics_obj_t, &ModPicoGraphics_type); PicoGraphicsDisplay display = (PicoGraphicsDisplay)args[ARG_display].u_int; diff --git a/micropython/modules/picovector/picovector.cpp b/micropython/modules/picovector/picovector.cpp index ebe074313..f7bde2279 100644 --- a/micropython/modules/picovector/picovector.cpp +++ b/micropython/modules/picovector/picovector.cpp @@ -122,8 +122,7 @@ mp_obj_t RECTANGLE_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_k mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - _POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t); - self->base.type = &POLYGON_type; + _POLYGON_obj_t *self = mp_obj_malloc_with_finaliser(_POLYGON_obj_t, &POLYGON_type); int x = args[ARG_x].u_int; int y = args[ARG_y].u_int; @@ -154,8 +153,7 @@ mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - _POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t); - self->base.type = &POLYGON_type; + _POLYGON_obj_t *self = mp_obj_malloc_with_finaliser(_POLYGON_obj_t, &POLYGON_type); Point origin(args[ARG_x].u_int, args[ARG_y].u_int); unsigned int sides = args[ARG_sides].u_int; @@ -185,8 +183,7 @@ mp_obj_t REGULAR_POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size } mp_obj_t POLYGON_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - _POLYGON_obj_t *self = m_new_obj_with_finaliser(_POLYGON_obj_t); - self->base.type = &POLYGON_type; + _POLYGON_obj_t *self = mp_obj_malloc_with_finaliser(_POLYGON_obj_t, &POLYGON_type); size_t num_points = n_args; const mp_obj_t *points = all_args; diff --git a/micropython/modules/plasma/plasma.cpp b/micropython/modules/plasma/plasma.cpp index 26c35c147..349a67554 100644 --- a/micropython/modules/plasma/plasma.cpp +++ b/micropython/modules/plasma/plasma.cpp @@ -96,8 +96,7 @@ mp_obj_t PlasmaWS2812_make_new(const mp_obj_type_t *type, size_t n_args, size_t buffer = m_new(WS2812::RGB, num_leds); } - self = m_new_obj_with_finaliser(_PlasmaWS2812_obj_t); - self->base.type = &PlasmaWS2812_type; + self = mp_obj_malloc_with_finaliser(_PlasmaWS2812_obj_t, &PlasmaWS2812_type); self->buf = buffer; self->ws2812 = m_new_class(WS2812, num_leds, pio, sm, dat, freq, rgbw, color_order, (WS2812::RGB *)buffer); @@ -307,8 +306,7 @@ mp_obj_t PlasmaAPA102_make_new(const mp_obj_type_t *type, size_t n_args, size_t buffer[i].brightness(15); } - self = m_new_obj_with_finaliser(_PlasmaAPA102_obj_t); - self->base.type = &PlasmaAPA102_type; + self = mp_obj_malloc_with_finaliser(_PlasmaAPA102_obj_t, &PlasmaAPA102_type); self->buf = buffer; self->apa102 = m_new_class(APA102, num_leds, pio, sm, dat, clk, freq, buffer); diff --git a/micropython/modules/pngdec/pngdec.cpp b/micropython/modules/pngdec/pngdec.cpp index 039032197..4abd0564e 100644 --- a/micropython/modules/pngdec/pngdec.cpp +++ b/micropython/modules/pngdec/pngdec.cpp @@ -266,8 +266,7 @@ mp_obj_t _PNG_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, co if(!MP_OBJ_IS_TYPE(args[ARG_picographics].u_obj, &ModPicoGraphics_type)) mp_raise_ValueError(MP_ERROR_TEXT("PicoGraphics Object Required")); - _PNG_obj_t *self = m_new_obj_with_finaliser(_PNG_obj_t); - self->base.type = &PNG_type; + _PNG_obj_t *self = mp_obj_malloc_with_finaliser(_PNG_obj_t, &PNG_type); self->png = m_new_class(PNG); //mp_printf(&mp_plat_print, "PNG RAM %fK\n", sizeof(PNG) / 1024.0f); diff --git a/micropython/modules/servo/servo.cpp b/micropython/modules/servo/servo.cpp index b02e6eb70..584d53f8d 100644 --- a/micropython/modules/servo/servo.cpp +++ b/micropython/modules/servo/servo.cpp @@ -73,8 +73,7 @@ mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } servo::CalibrationType calibration_type = (servo::CalibrationType)type; - self = m_new_obj_with_finaliser(_Calibtration_obj_t); - self->base.type = &Calibration_type; + self = mp_obj_malloc_with_finaliser(_Calibtration_obj_t, &Calibration_type); self->calibration = m_new_class(Calibration, calibration_type); } else { @@ -82,8 +81,7 @@ mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } } else { - self = m_new_obj_with_finaliser(_Calibtration_obj_t); - self->base.type = &Calibration_type; + self = mp_obj_malloc_with_finaliser(_Calibtration_obj_t, &Calibration_type); self->calibration = m_new_class(Calibration); } @@ -892,8 +890,7 @@ mp_obj_t Servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c freq = mp_obj_get_float(args[ARG_freq].u_obj); } - self = m_new_obj_with_finaliser(_Servo_obj_t); - self->base.type = &Servo_type; + self = mp_obj_malloc_with_finaliser(_Servo_obj_t, &Servo_type); void *servo_target = m_new(Servo, 1); @@ -1161,8 +1158,7 @@ extern mp_obj_t Servo_calibration(size_t n_args, const mp_obj_t *pos_args, mp_ma _Servo_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Servo_obj_t); // Create a new MP Calibration instance and assign a copy of the servo's calibration to it - _Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t); - calib->base.type = &Calibration_type; + _Calibration_obj_t *calib = mp_obj_malloc_with_finaliser(_Calibration_obj_t, &Calibration_type); calib->calibration = m_new_class(Calibration, self->servo->calibration()); return MP_OBJ_FROM_PTR(calib); @@ -1355,8 +1351,7 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t mp_raise_msg(&mp_type_RuntimeError, "unable to allocate the hardware resources needed to initialise this ServoCluster. Try running `import gc` followed by `gc.collect()` before creating it"); } - self = m_new_obj_with_finaliser(_ServoCluster_obj_t); - self->base.type = &ServoCluster_type; + self = mp_obj_malloc_with_finaliser(_ServoCluster_obj_t, &ServoCluster_type); self->cluster = cluster; return MP_OBJ_FROM_PTR(self); @@ -2655,8 +2650,7 @@ extern mp_obj_t ServoCluster_calibration(size_t n_args, const mp_obj_t *pos_args mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("servo out of range. Expected 0 to %d"), servo_count - 1); else { // Create a new MP Calibration instance and assign a copy of the servo's calibration to it - _Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t); - calib->base.type = &Calibration_type; + _Calibration_obj_t *calib = mp_obj_malloc_with_finaliser(_Calibration_obj_t, &Calibration_type); calib->calibration = m_new_class(Calibration, self->cluster->calibration((uint)servo)); return MP_OBJ_FROM_PTR(calib); diff --git a/micropython/modules/stellar_unicorn/stellar_unicorn.cpp b/micropython/modules/stellar_unicorn/stellar_unicorn.cpp index a6b19daa1..52df9cdb6 100644 --- a/micropython/modules/stellar_unicorn/stellar_unicorn.cpp +++ b/micropython/modules/stellar_unicorn/stellar_unicorn.cpp @@ -391,8 +391,7 @@ mp_obj_t StellarUnicorn_make_new(const mp_obj_type_t *type, size_t n_args, size_ StellarUnicorn *stellar = m_new_class(StellarUnicorn); stellar->init(); - self = m_new_obj_with_finaliser(_StellarUnicorn_obj_t); - self->base.type = &StellarUnicorn_type; + self = mp_obj_malloc_with_finaliser(_StellarUnicorn_obj_t, &StellarUnicorn_type); self->stellar = stellar; return MP_OBJ_FROM_PTR(self); @@ -507,8 +506,7 @@ extern mp_obj_t StellarUnicorn_synth_channel(mp_obj_t self_in, mp_obj_t channel_ // NOTE This seems to work, in that it give MP access to the calibration object // Could very easily mess up in weird ways once object deletion is considered? - _Channel_obj_t *channel_obj = m_new_obj_with_finaliser(_Channel_obj_t); - channel_obj->base.type = &Channel_type; + _Channel_obj_t *channel_obj = mp_obj_malloc_with_finaliser(_Channel_obj_t, &Channel_type); channel_obj->channel = &self->stellar->synth_channel(channel); return MP_OBJ_FROM_PTR(channel_obj);