Merge pull request #1075 from tannewt/more_mp_raise

Use more mp_raise_* to save 28 bytes code size.
This commit is contained in:
Dan Halbert 2018-07-31 19:09:10 -04:00 committed by GitHub
commit 761f9a938e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 16 deletions

View File

@ -30,6 +30,7 @@
#include "py/nlr.h" #include "py/nlr.h"
#include "py/obj.h" #include "py/obj.h"
#include "py/runtime.h"
//| .. currentmodule:: microcontroller //| .. currentmodule:: microcontroller
//| //|
@ -74,7 +75,7 @@ const mp_obj_type_t mcu_pin_type = {
void assert_pin(mp_obj_t obj, bool none_ok) { 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)) { 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);
} }
} }

View File

@ -64,7 +64,7 @@
//| //|
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) { 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)) { 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. // Convert parameters into expected types.
const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj); const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj);

View File

@ -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]; mp_obj_t carrier_obj = args[0];
if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { 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 // create Pulse object from the given pin

View File

@ -26,8 +26,8 @@
#include "mpconfigport.h" #include "mpconfigport.h"
#include "py/nlr.h"
#include "py/obj.h" #include "py/obj.h"
#include "py/runtime.h"
#include "common-hal/microcontroller/Pin.h" #include "common-hal/microcontroller/Pin.h"
#include "shared-bindings/microcontroller/__init__.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) { const mcu_pin_obj_t * miso) {
digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock); digitalinout_result_t result = common_hal_digitalio_digitalinout_construct(&self->clock, clock);
if (result != DIGITALINOUT_OK) { if (result != DIGITALINOUT_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("Clock pin init failed.");
"Clock pin init failed."));
} }
if (mosi != mp_const_none) { if (mosi != mp_const_none) {
result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi); result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi);
if (result != DIGITALINOUT_OK) { if (result != DIGITALINOUT_OK) {
common_hal_digitalio_digitalinout_deinit(&self->clock); common_hal_digitalio_digitalinout_deinit(&self->clock);
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("MOSI pin init failed.");
"MOSI pin init failed."));
} }
self->has_mosi = true; self->has_mosi = true;
} }
@ -60,8 +58,7 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
if (mosi != mp_const_none) { if (mosi != mp_const_none) {
common_hal_digitalio_digitalinout_deinit(&self->mosi); common_hal_digitalio_digitalinout_deinit(&self->mosi);
} }
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("MISO pin init failed.");
"MISO pin init failed."));
} }
self->has_miso = true; self->has_miso = true;
} }
@ -121,8 +118,7 @@ void shared_module_bitbangio_spi_unlock(bitbangio_spi_obj_t *self) {
// Writes out the given data. // Writes out the given data.
bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len) { bool shared_module_bitbangio_spi_write(bitbangio_spi_obj_t *self, const uint8_t *data, size_t len) {
if (len > 0 && !self->has_mosi) { if (len > 0 && !self->has_mosi) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("Cannot write without MOSI pin.");
"Cannot write without MOSI pin."));
} }
uint32_t delay_half = self->delay_half; 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. // Reads in len bytes while outputting zeroes.
bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len) { bool shared_module_bitbangio_spi_read(bitbangio_spi_obj_t *self, uint8_t *data, size_t len) {
if (len > 0 && !self->has_miso) { if (len > 0 && !self->has_miso) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("Cannot read without MISO pin.");
"Cannot read without MISO pin."));
} }
uint32_t delay_half = self->delay_half; 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 // transfer
bool shared_module_bitbangio_spi_transfer(bitbangio_spi_obj_t *self, const uint8_t *dout, uint8_t *din, size_t len) { 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) ) { if (len > 0 && (!self->has_mosi || !self->has_miso) ) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mp_raise_ValueError("Cannot transfer without MOSI and MISO pins.");
"Cannot transfer without MOSI and MISO pins."));
} }
uint32_t delay_half = self->delay_half; uint32_t delay_half = self->delay_half;