From 0bf999f52a50590f4c660df41aa979f39da04c38 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 31 Jul 2018 13:39:12 -0700 Subject: [PATCH] Use more mp_raise_* to save 28 bytes code size. --- shared-bindings/microcontroller/Pin.c | 3 ++- shared-bindings/neopixel_write/__init__.c | 2 +- shared-bindings/pulseio/PulseOut.c | 2 +- shared-module/bitbangio/SPI.c | 20 +++++++------------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index fd5285192c..33bd43347b 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -30,6 +30,7 @@ #include "py/nlr.h" #include "py/obj.h" +#include "py/runtime.h" //| .. currentmodule:: microcontroller //| @@ -74,7 +75,7 @@ const mp_obj_type_t mcu_pin_type = { void assert_pin(mp_obj_t obj, bool none_ok) { if ((obj != mp_const_none || !none_ok) && !MP_OBJ_IS_TYPE(obj, &mcu_pin_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", mcu_pin_type.name)); + mp_raise_TypeError_varg("Expected a %q", mcu_pin_type.name); } } diff --git a/shared-bindings/neopixel_write/__init__.c b/shared-bindings/neopixel_write/__init__.c index d6b578a4a8..c3c475c7cc 100644 --- a/shared-bindings/neopixel_write/__init__.c +++ b/shared-bindings/neopixel_write/__init__.c @@ -64,7 +64,7 @@ //| STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) { if (!MP_OBJ_IS_TYPE(digitalinout_obj, &digitalio_digitalinout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", digitalio_digitalinout_type.name)); + mp_raise_TypeError_varg("Expected a %q", digitalio_digitalinout_type.name); } // Convert parameters into expected types. const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index a44e28d5d9..dc3c025008 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -71,7 +71,7 @@ STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_ar mp_obj_t carrier_obj = args[0]; if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", pulseio_pwmout_type.name)); + mp_raise_TypeError_varg("Expected a %q", pulseio_pwmout_type.name); } // create Pulse object from the given pin diff --git a/shared-module/bitbangio/SPI.c b/shared-module/bitbangio/SPI.c index e572d4aab1..b203fe1b42 100644 --- a/shared-module/bitbangio/SPI.c +++ b/shared-module/bitbangio/SPI.c @@ -26,8 +26,8 @@ #include "mpconfigport.h" -#include "py/nlr.h" #include "py/obj.h" +#include "py/runtime.h" #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" @@ -41,15 +41,13 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, const mcu_pin_obj_t * miso) { digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock); if (result != DIGITALINOUT_OK) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "Clock pin init failed.")); + mp_raise_ValueError("Clock pin init failed."); } if (mosi != mp_const_none) { result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi); if (result != DIGITALINOUT_OK) { common_hal_digitalio_digitalinout_deinit(&self->clock); - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "MOSI pin init failed.")); + mp_raise_ValueError("MOSI pin init failed."); } self->has_mosi = true; } @@ -60,8 +58,7 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self, if (mosi != mp_const_none) { common_hal_digitalio_digitalinout_deinit(&self->mosi); } - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "MISO pin init failed.")); + mp_raise_ValueError("MISO pin init failed."); } self->has_miso = true; } @@ -121,8 +118,7 @@ void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self) { // Writes out the given data. bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len) { if (len > 0 && !self->has_mosi) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "Cannot write without MOSI pin.")); + mp_raise_ValueError("Cannot write without MOSI pin."); } uint32_t delay_half = self->delay_half; @@ -177,8 +173,7 @@ bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t // Reads in len bytes while outputting zeroes. bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len) { if (len > 0 && !self->has_miso) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "Cannot read without MISO pin.")); + mp_raise_ValueError("Cannot read without MISO pin."); } uint32_t delay_half = self->delay_half; @@ -242,8 +237,7 @@ bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, // transfer bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len) { if (len > 0 && (!self->has_mosi || !self->has_miso) ) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, - "Cannot transfer without MOSI and MISO pins.")); + mp_raise_ValueError("Cannot transfer without MOSI and MISO pins."); } uint32_t delay_half = self->delay_half;