diff --git a/drivers/motor/motor_cluster.cpp b/drivers/motor/motor_cluster.cpp index 2900fe73..d953b7f6 100644 --- a/drivers/motor/motor_cluster.cpp +++ b/drivers/motor/motor_cluster.cpp @@ -9,22 +9,22 @@ namespace motor { MotorCluster::MotorCluster(PIO pio, uint sm, uint pin_base, uint pin_pair_count, Direction direction, float speed_scale, float zeropoint, float deadzone, float freq, DecayMode mode, - bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_base, (pin_pair_count * 2), seq_buffer, dat_buffer, false), pwm_frequency(freq) { + bool auto_phase) + : pwms(pio, sm, pin_base, (pin_pair_count * 2), false), pwm_frequency(freq) { create_motor_states(direction, speed_scale, zeropoint, deadzone, mode, auto_phase); } MotorCluster::MotorCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, Direction direction, float speed_scale, float zeropoint, float deadzone, float freq, DecayMode mode, - bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_pairs, length, seq_buffer, dat_buffer, false), pwm_frequency(freq) { + bool auto_phase) + : pwms(pio, sm, pin_pairs, length, false), pwm_frequency(freq) { create_motor_states(direction, speed_scale, zeropoint, deadzone, mode, auto_phase); } MotorCluster::MotorCluster(PIO pio, uint sm, std::initializer_list pin_pairs, Direction direction, float speed_scale, float zeropoint, float deadzone, float freq, DecayMode mode, - bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_pairs, seq_buffer, dat_buffer, false), pwm_frequency(freq) { + bool auto_phase) + : pwms(pio, sm, pin_pairs, false), pwm_frequency(freq) { create_motor_states(direction, speed_scale, zeropoint, deadzone, mode, auto_phase); } diff --git a/drivers/motor/motor_cluster.hpp b/drivers/motor/motor_cluster.hpp index c1147692..d6d4a2d8 100644 --- a/drivers/motor/motor_cluster.hpp +++ b/drivers/motor/motor_cluster.hpp @@ -36,13 +36,13 @@ namespace motor { public: MotorCluster(PIO pio, uint sm, uint pin_base, uint pin_pair_count, Direction direction = NORMAL_DIR, float speed_scale = MotorState::DEFAULT_SPEED_SCALE, float zeropoint = MotorState::DEFAULT_ZEROPOINT, float deadzone = MotorState::DEFAULT_DEADZONE, float freq = MotorState::DEFAULT_FREQUENCY, DecayMode mode = MotorState::DEFAULT_DECAY_MODE, - bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); + bool auto_phase = true); MotorCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, Direction direction = NORMAL_DIR, float speed_scale = MotorState::DEFAULT_SPEED_SCALE, float zeropoint = MotorState::DEFAULT_ZEROPOINT, float deadzone = MotorState::DEFAULT_DEADZONE, float freq = MotorState::DEFAULT_FREQUENCY, DecayMode mode = MotorState::DEFAULT_DECAY_MODE, - bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); + bool auto_phase = true); MotorCluster(PIO pio, uint sm, std::initializer_list pin_pairs, Direction direction = NORMAL_DIR, float speed_scale = MotorState::DEFAULT_SPEED_SCALE, float zeropoint = MotorState::DEFAULT_ZEROPOINT, float deadzone = MotorState::DEFAULT_DEADZONE, float freq = MotorState::DEFAULT_FREQUENCY, DecayMode mode = MotorState::DEFAULT_DECAY_MODE, - bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); + bool auto_phase = true); ~MotorCluster(); diff --git a/drivers/pwm/pwm_cluster.cpp b/drivers/pwm/pwm_cluster.cpp index 0765b46c..0e6f28a7 100644 --- a/drivers/pwm/pwm_cluster.cpp +++ b/drivers/pwm/pwm_cluster.cpp @@ -22,7 +22,7 @@ uint8_t PWMCluster::claimed_sms[] = { 0x0, 0x0 }; uint PWMCluster::pio_program_offset = 0; -PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_mask, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_mask, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(pin_mask & ((1u << NUM_BANK0_GPIOS) - 1)) @@ -39,11 +39,11 @@ PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_mask, Sequence *seq_buffer, Tr } } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_base, uint pin_count, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_base, uint pin_count, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(0x00000000) @@ -60,10 +60,10 @@ PWMCluster::PWMCluster(PIO pio, uint sm, uint pin_base, uint pin_count, Sequence channel_count++; } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -PWMCluster::PWMCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(0x00000000) @@ -82,10 +82,10 @@ PWMCluster::PWMCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, S } } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pins, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pins, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(0x00000000) @@ -103,10 +103,10 @@ PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pins, Se } } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -PWMCluster::PWMCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(0x00000000) @@ -129,10 +129,10 @@ PWMCluster::PWMCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t len } } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pin_pairs, Sequence *seq_buffer, TransitionData *dat_buffer, bool loading_zone) +PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pin_pairs, bool loading_zone) : pio(pio) , sm(sm) , pin_mask(0x00000000) @@ -154,47 +154,20 @@ PWMCluster::PWMCluster(PIO pio, uint sm, std::initializer_list pin_pai } } - constructor_common(seq_buffer, dat_buffer); + constructor_common(); } -void PWMCluster::constructor_common(Sequence *seq_buffer, TransitionData *dat_buffer) { +void PWMCluster::constructor_common() { // Initialise all the channels this PWM will control if(channel_count > 0) { channels = new ChannelState[channel_count]; } - if(seq_buffer == nullptr) { - sequences = new Sequence[NUM_BUFFERS]; - loop_sequences = new Sequence[NUM_BUFFERS]; - managed_seq_buffer = true; - } - else { - sequences = seq_buffer; - loop_sequences = seq_buffer + NUM_BUFFERS; - managed_seq_buffer = false; - } - - if(dat_buffer == nullptr) { - transitions = new TransitionData[BUFFER_SIZE]; - looping_transitions = new TransitionData[BUFFER_SIZE]; - managed_dat_buffer = true; - } - else { - transitions = dat_buffer; - looping_transitions = dat_buffer + BUFFER_SIZE; - managed_dat_buffer = false; - } - // Set up the transition buffers for(uint i = 0; i < NUM_BUFFERS; i++) { - Sequence& seq = sequences[i]; - Sequence& looping_seq = loop_sequences[i]; - seq = Sequence(); - looping_seq = Sequence(); - // Need to set a delay otherwise a lockup occurs when first changing frequency - seq.data[0].delay = 10; - looping_seq.data[0].delay = 10; + sequences[i].data[0].delay = 10; + loop_sequences[i].data[0].delay = 10; } } @@ -244,16 +217,6 @@ PWMCluster::~PWMCluster() { } } - if(managed_seq_buffer) { - delete[] sequences; - delete[] loop_sequences; - } - - if(managed_dat_buffer) { - delete[] transitions; - delete[] looping_transitions; - } - delete[] channels; } diff --git a/drivers/pwm/pwm_cluster.hpp b/drivers/pwm/pwm_cluster.hpp index 0d0cc403..76392327 100644 --- a/drivers/pwm/pwm_cluster.hpp +++ b/drivers/pwm/pwm_cluster.hpp @@ -108,13 +108,11 @@ namespace pimoroni { uint8_t channel_to_pin_map[NUM_BANK0_GPIOS]; uint wrap_level; - Sequence *sequences; - Sequence *loop_sequences; - bool managed_seq_buffer = false; + Sequence sequences[NUM_BUFFERS]; + Sequence loop_sequences[NUM_BUFFERS]; - TransitionData *transitions; - TransitionData *looping_transitions; - bool managed_dat_buffer = false; + TransitionData transitions[BUFFER_SIZE]; + TransitionData looping_transitions[BUFFER_SIZE]; volatile uint read_index = 0; volatile uint last_written_index = 0; @@ -136,17 +134,17 @@ namespace pimoroni { // Constructors/Destructor //-------------------------------------------------- public: - PWMCluster(PIO pio, uint sm, uint pin_mask, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); - PWMCluster(PIO pio, uint sm, uint pin_base, uint pin_count, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); - PWMCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); - PWMCluster(PIO pio, uint sm, std::initializer_list pins, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, uint pin_mask, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, uint pin_base, uint pin_count, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, std::initializer_list pins, bool loading_zone = DEFAULT_USE_LOADING_ZONE); - PWMCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); - PWMCluster(PIO pio, uint sm, std::initializer_list pin_pairs, Sequence *seq_buffer = nullptr, TransitionData *dat_buffer = nullptr, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, const pin_pair *pin_pairs, uint32_t length, bool loading_zone = DEFAULT_USE_LOADING_ZONE); + PWMCluster(PIO pio, uint sm, std::initializer_list pin_pairs, bool loading_zone = DEFAULT_USE_LOADING_ZONE); ~PWMCluster(); private: - void constructor_common(Sequence *seq_buffer, TransitionData *dat_buffer); + void constructor_common(); //-------------------------------------------------- diff --git a/drivers/servo/servo_cluster.cpp b/drivers/servo/servo_cluster.cpp index e87b9124..cd58d249 100644 --- a/drivers/servo/servo_cluster.cpp +++ b/drivers/servo/servo_cluster.cpp @@ -3,43 +3,43 @@ #include namespace servo { - ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_mask, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type, float freq, bool auto_phase) + : pwms(pio, sm, pin_mask), pwm_frequency(freq) { create_servo_states(default_type, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_base, pin_count, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type, float freq, bool auto_phase) + : pwms(pio, sm, pin_base, pin_count), pwm_frequency(freq) { create_servo_states(default_type, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, CalibrationType default_type, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pins, length, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, CalibrationType default_type, float freq, bool auto_phase) + : pwms(pio, sm, pins, length), pwm_frequency(freq) { create_servo_states(default_type, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, std::initializer_list pins, CalibrationType default_type, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pins, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, std::initializer_list pins, CalibrationType default_type, float freq, bool auto_phase) + : pwms(pio, sm, pins), pwm_frequency(freq) { create_servo_states(default_type, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_mask, const Calibration& calibration, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_mask, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_mask, const Calibration& calibration, float freq, bool auto_phase) + : pwms(pio, sm, pin_mask), pwm_frequency(freq) { create_servo_states(calibration, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, const Calibration& calibration, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pin_base, pin_count, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, const Calibration& calibration, float freq, bool auto_phase) + : pwms(pio, sm, pin_base, pin_count), pwm_frequency(freq) { create_servo_states(calibration, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, const Calibration& calibration, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pins, length, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, const Calibration& calibration, float freq, bool auto_phase) + : pwms(pio, sm, pins, length), pwm_frequency(freq) { create_servo_states(calibration, auto_phase); } - ServoCluster::ServoCluster(PIO pio, uint sm, std::initializer_list pins, const Calibration& calibration, float freq, bool auto_phase, PWMCluster::Sequence *seq_buffer, PWMCluster::TransitionData *dat_buffer) - : pwms(pio, sm, pins, seq_buffer, dat_buffer), pwm_frequency(freq) { + ServoCluster::ServoCluster(PIO pio, uint sm, std::initializer_list pins, const Calibration& calibration, float freq, bool auto_phase) + : pwms(pio, sm, pins), pwm_frequency(freq) { create_servo_states(calibration, auto_phase); } diff --git a/drivers/servo/servo_cluster.hpp b/drivers/servo/servo_cluster.hpp index ecdc79e2..e6ee0b5b 100644 --- a/drivers/servo/servo_cluster.hpp +++ b/drivers/servo/servo_cluster.hpp @@ -24,15 +24,15 @@ namespace servo { // Constructors/Destructor //-------------------------------------------------- public: - ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, std::initializer_list pins, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); + ServoCluster(PIO pio, uint sm, uint pin_mask, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, std::initializer_list pins, CalibrationType default_type = ANGULAR, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); - ServoCluster(PIO pio, uint sm, uint pin_mask, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); - ServoCluster(PIO pio, uint sm, std::initializer_list pins, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true, PWMCluster::Sequence *seq_buffer = nullptr, PWMCluster::TransitionData *dat_buffer = nullptr); + ServoCluster(PIO pio, uint sm, uint pin_mask, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, uint pin_base, uint pin_count, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, const uint8_t *pins, uint32_t length, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); + ServoCluster(PIO pio, uint sm, std::initializer_list pins, const Calibration& calibration, float freq = ServoState::DEFAULT_FREQUENCY, bool auto_phase = true); ~ServoCluster(); diff --git a/micropython/modules/badger2040/badger2040.cpp b/micropython/modules/badger2040/badger2040.cpp index 748e8c3a..56b7fd75 100644 --- a/micropython/modules/badger2040/badger2040.cpp +++ b/micropython/modules/badger2040/badger2040.cpp @@ -2,7 +2,7 @@ #include "hardware/watchdog.h" #include "badger2040.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) +#include "micropython/modules/util.hpp" extern "C" { #include "badger2040.h" diff --git a/micropython/modules/breakout_as7262/breakout_as7262.cpp b/micropython/modules/breakout_as7262/breakout_as7262.cpp index ddd45732..9cf17ca1 100644 --- a/micropython/modules/breakout_as7262/breakout_as7262.cpp +++ b/micropython/modules/breakout_as7262/breakout_as7262.cpp @@ -1,11 +1,6 @@ #include "libraries/breakout_as7262/breakout_as7262.hpp" #include "common/pimoroni_i2c.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -63,7 +58,7 @@ mp_obj_t BreakoutAS7262_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutAS7262((pimoroni::I2C *)(self->i2c->i2c), args[ARG_int].u_int); + self->breakout = m_new_class(BreakoutAS7262, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_int].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutAS7262: breakout not found when initialising"); diff --git a/micropython/modules/breakout_bh1745/breakout_bh1745.cpp b/micropython/modules/breakout_bh1745/breakout_bh1745.cpp index 93aa88a6..0b479b6b 100644 --- a/micropython/modules/breakout_bh1745/breakout_bh1745.cpp +++ b/micropython/modules/breakout_bh1745/breakout_bh1745.cpp @@ -1,13 +1,7 @@ #include "libraries/breakout_bh1745/breakout_bh1745.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - - using namespace pimoroni; extern "C" { @@ -64,7 +58,7 @@ mp_obj_t BreakoutBH1745_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutBH1745((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); + self->breakout = m_new_class(BreakoutBH1745, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutBH1745: breakout not found when initialising"); diff --git a/micropython/modules/breakout_bme280/breakout_bme280.cpp b/micropython/modules/breakout_bme280/breakout_bme280.cpp index cae9930f..15aa0eb7 100644 --- a/micropython/modules/breakout_bme280/breakout_bme280.cpp +++ b/micropython/modules/breakout_bme280/breakout_bme280.cpp @@ -1,11 +1,5 @@ #include "drivers/bme280/bme280.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -62,7 +56,7 @@ mp_obj_t BreakoutBME280_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BME280((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); + self->breakout = m_new_class(BME280, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutBME280: breakout not found when initialising"); diff --git a/micropython/modules/breakout_bme68x/breakout_bme68x.cpp b/micropython/modules/breakout_bme68x/breakout_bme68x.cpp index 70d92b66..967b0ce0 100644 --- a/micropython/modules/breakout_bme68x/breakout_bme68x.cpp +++ b/micropython/modules/breakout_bme68x/breakout_bme68x.cpp @@ -1,10 +1,5 @@ #include "drivers/bme68x/bme68x.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -63,7 +58,7 @@ mp_obj_t BreakoutBME68X_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BME68X((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); + self->breakout = m_new_class(BME68X, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutBME68X: breakout not found when initialising"); diff --git a/micropython/modules/breakout_bmp280/breakout_bmp280.cpp b/micropython/modules/breakout_bmp280/breakout_bmp280.cpp index 83052ec3..9ae83c2b 100644 --- a/micropython/modules/breakout_bmp280/breakout_bmp280.cpp +++ b/micropython/modules/breakout_bmp280/breakout_bmp280.cpp @@ -1,10 +1,5 @@ #include "drivers/bmp280/bmp280.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -62,7 +57,7 @@ mp_obj_t BreakoutBMP280_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BMP280((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); + self->breakout = m_new_class(BMP280, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_int].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutBMP280: breakout not found when initialising"); diff --git a/micropython/modules/breakout_colourlcd160x80/breakout_colourlcd160x80.cpp b/micropython/modules/breakout_colourlcd160x80/breakout_colourlcd160x80.cpp index 03ef5175..f598b636 100644 --- a/micropython/modules/breakout_colourlcd160x80/breakout_colourlcd160x80.cpp +++ b/micropython/modules/breakout_colourlcd160x80/breakout_colourlcd160x80.cpp @@ -1,11 +1,5 @@ #include "libraries/breakout_colourlcd160x80/breakout_colourlcd160x80.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) -#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -70,7 +64,7 @@ mp_obj_t BreakoutColourLCD160x80_make_new(const mp_obj_type_t *type, size_t n_ar mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW); - self->breakout = new BreakoutColourLCD160x80((uint16_t *)bufinfo.buf, (BG_SPI_SLOT)slot); + self->breakout = m_new_class(BreakoutColourLCD160x80, (uint16_t *)bufinfo.buf, (BG_SPI_SLOT)slot); } else { mp_raise_ValueError("slot not a valid value. Expected 0 to 1"); @@ -119,7 +113,7 @@ mp_obj_t BreakoutColourLCD160x80_make_new(const mp_obj_type_t *type, size_t n_ar self->base.type = &breakout_colourlcd160x80_BreakoutColourLCD160x80_type; spi_inst_t *spi = (spi_id == 0) ? spi0 : spi1; - self->breakout = new BreakoutColourLCD160x80((uint16_t *)bufinfo.buf, spi, + self->breakout = m_new_class(BreakoutColourLCD160x80, (uint16_t *)bufinfo.buf, spi, args[ARG_cs].u_int, args[ARG_dc].u_int, sck, mosi, PIN_UNUSED, args[ARG_bl].u_int); } diff --git a/micropython/modules/breakout_colourlcd240x240/breakout_colourlcd240x240.cpp b/micropython/modules/breakout_colourlcd240x240/breakout_colourlcd240x240.cpp index 72c2c56c..f08e3723 100644 --- a/micropython/modules/breakout_colourlcd240x240/breakout_colourlcd240x240.cpp +++ b/micropython/modules/breakout_colourlcd240x240/breakout_colourlcd240x240.cpp @@ -1,11 +1,7 @@ #include "libraries/breakout_colourlcd240x240/breakout_colourlcd240x240.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) +#include "micropython/modules/util.hpp" -#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) -#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) using namespace pimoroni; diff --git a/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp b/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp index b15a1edb..29bd9ee3 100644 --- a/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp +++ b/micropython/modules/breakout_dotmatrix/breakout_dotmatrix.cpp @@ -1,11 +1,7 @@ #include "libraries/breakout_dotmatrix/breakout_dotmatrix.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) using namespace pimoroni; @@ -64,7 +60,7 @@ mp_obj_t BreakoutDotMatrix_make_new(const mp_obj_type_t *type, size_t n_args, si self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutDotMatrix((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); + self->breakout = m_new_class(BreakoutDotMatrix, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "DotMatrix breakout not found when initialising"); diff --git a/micropython/modules/breakout_encoder/breakout_encoder.cpp b/micropython/modules/breakout_encoder/breakout_encoder.cpp index 8dbaf21e..360917d4 100644 --- a/micropython/modules/breakout_encoder/breakout_encoder.cpp +++ b/micropython/modules/breakout_encoder/breakout_encoder.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_encoder/breakout_encoder.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -68,7 +63,7 @@ mp_obj_t BreakoutEncoder_make_new(const mp_obj_type_t *type, size_t n_args, size self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutEncoder((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutEncoder, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutEncoder: breakout not found when initialising"); diff --git a/micropython/modules/breakout_icp10125/breakout_icp10125.cpp b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp index 83c84c72..64ce6418 100644 --- a/micropython/modules/breakout_icp10125/breakout_icp10125.cpp +++ b/micropython/modules/breakout_icp10125/breakout_icp10125.cpp @@ -1,6 +1,6 @@ #include "drivers/icp10125/icp10125.hpp" +#include "micropython/modules/util.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) using namespace pimoroni; @@ -43,7 +43,7 @@ mp_obj_t BreakoutICP10125_make_new(const mp_obj_type_t *type, size_t n_args, siz self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new ICP10125((pimoroni::I2C *)(self->i2c->i2c)); + self->breakout = m_new_class(ICP10125, (pimoroni::I2C *)(self->i2c->i2c)); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutICP10125: breakout not found when initialising"); diff --git a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp index 2e44ea82..a9a6d2c0 100644 --- a/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp +++ b/micropython/modules/breakout_ioexpander/breakout_ioexpander.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_ioexpander/breakout_ioexpander.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -68,7 +63,7 @@ mp_obj_t BreakoutIOExpander_make_new(const mp_obj_type_t *type, size_t n_args, s self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutIOExpander((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutIOExpander, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutIOExpander: breakout not found when initialising"); diff --git a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp index 53d04645..51f4995c 100644 --- a/micropython/modules/breakout_ltr559/breakout_ltr559.cpp +++ b/micropython/modules/breakout_ltr559/breakout_ltr559.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_ltr559/breakout_ltr559.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -67,7 +62,7 @@ mp_obj_t BreakoutLTR559_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutLTR559((pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutLTR559, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutLTR559: breakout not found when initialising"); diff --git a/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp b/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp index 072e91b7..25792514 100644 --- a/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp +++ b/micropython/modules/breakout_matrix11x7/breakout_matrix11x7.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_matrix11x7/breakout_matrix11x7.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -64,7 +59,7 @@ mp_obj_t BreakoutMatrix11x7_make_new(const mp_obj_type_t *type, size_t n_args, s self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutMatrix11x7((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); + self->breakout = m_new_class(BreakoutMatrix11x7, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutMatrix11x7: breakout not found when initialising"); diff --git a/micropython/modules/breakout_mics6814/breakout_mics6814.cpp b/micropython/modules/breakout_mics6814/breakout_mics6814.cpp index b7802c18..2c9532f7 100644 --- a/micropython/modules/breakout_mics6814/breakout_mics6814.cpp +++ b/micropython/modules/breakout_mics6814/breakout_mics6814.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_mics6814/breakout_mics6814.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -68,7 +63,7 @@ mp_obj_t BreakoutMICS6814_make_new(const mp_obj_type_t *type, size_t n_args, siz self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutMICS6814((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutMICS6814, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutMICS6814: breakout not found when initialising"); diff --git a/micropython/modules/breakout_msa301/breakout_msa301.cpp b/micropython/modules/breakout_msa301/breakout_msa301.cpp index 46c9eb57..0addf89e 100644 --- a/micropython/modules/breakout_msa301/breakout_msa301.cpp +++ b/micropython/modules/breakout_msa301/breakout_msa301.cpp @@ -1,10 +1,5 @@ #include "libraries/breakout_msa301/breakout_msa301.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -61,7 +56,7 @@ mp_obj_t BreakoutMSA301_make_new(const mp_obj_type_t *type, size_t n_args, size_ self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutMSA301((pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutMSA301, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutMSA301: breakout not found when initialising"); diff --git a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp index 9b5ff27b..0bc86bc8 100644 --- a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp +++ b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp @@ -1,12 +1,6 @@ #include "libraries/breakout_pmw3901/breakout_pmw3901.hpp" #include "libraries/breakout_paa5100/breakout_paa5100.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) -#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -84,16 +78,16 @@ mp_obj_t make_new(enum ChipType chip, const mp_obj_type_t *type, size_t n_args, self->base.type = &breakout_pmw3901_BreakoutPMW3901_type; if(chip == ChipType::PMW3901) { - BreakoutPMW3901 *breakout = new BreakoutPMW3901((BG_SPI_SLOT)slot); + BreakoutPMW3901 *breakout = m_new_class(BreakoutPMW3901, (BG_SPI_SLOT)slot); if (!breakout->init()) { - delete breakout; + m_del_class(BreakoutPMW3901, breakout); mp_raise_msg(&mp_type_RuntimeError, "BreakoutPMW3901: Init failed"); } self->breakout = breakout; } else { - BreakoutPAA5100 *breakout = new BreakoutPAA5100((BG_SPI_SLOT)slot); + BreakoutPAA5100 *breakout = m_new_class(BreakoutPAA5100, (BG_SPI_SLOT)slot); if (!breakout->init()) { - delete breakout; + m_del_class(BreakoutPAA5100, breakout); mp_raise_msg(&mp_type_RuntimeError, "BreakoutPAA5100: Init failed"); } self->breakout = breakout; @@ -148,16 +142,16 @@ mp_obj_t make_new(enum ChipType chip, const mp_obj_type_t *type, size_t n_args, spi_inst_t *spi = (spi_id == 0) ? spi0 : spi1; if(chip == ChipType::PMW3901) { - BreakoutPMW3901 *breakout = new BreakoutPMW3901(spi, args[ARG_cs].u_int, sck, mosi, miso, args[ARG_interrupt].u_int); + BreakoutPMW3901 *breakout = m_new_class(BreakoutPMW3901, spi, args[ARG_cs].u_int, sck, mosi, miso, args[ARG_interrupt].u_int); if (!breakout->init()) { - delete breakout; + m_del_class(BreakoutPMW3901, breakout); mp_raise_msg(&mp_type_RuntimeError, "BreakoutPMW3901: Init failed"); } self->breakout = breakout; } else { - BreakoutPAA5100 *breakout = new BreakoutPAA5100(spi, args[ARG_cs].u_int, sck, mosi, miso, args[ARG_interrupt].u_int); + BreakoutPAA5100 *breakout = m_new_class(BreakoutPAA5100, spi, args[ARG_cs].u_int, sck, mosi, miso, args[ARG_interrupt].u_int); if (!breakout->init()) { - delete breakout; + m_del_class(BreakoutPAA5100, breakout); mp_raise_msg(&mp_type_RuntimeError, "BreakoutPAA5100: Init failed"); } self->breakout = breakout; @@ -172,7 +166,7 @@ mp_obj_t make_new(enum ChipType chip, const mp_obj_type_t *type, size_t n_args, /***** Destructor ******/ mp_obj_t BreakoutPMW3901___del__(mp_obj_t self_in) { breakout_pmw3901_BreakoutPMW3901_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_pmw3901_BreakoutPMW3901_obj_t); - delete self->breakout; + m_del_class(BreakoutPMW3901, self->breakout); return mp_const_none; } diff --git a/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp b/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp index a838e330..1fcd6780 100644 --- a/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp +++ b/micropython/modules/breakout_potentiometer/breakout_potentiometer.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_potentiometer/breakout_potentiometer.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -68,7 +63,7 @@ mp_obj_t BreakoutPotentiometer_make_new(const mp_obj_type_t *type, size_t n_args self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutPotentiometer((pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutPotentiometer, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutPotentiometer: breakout not found when initialising"); diff --git a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp index b7d763be..253f47fb 100644 --- a/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp +++ b/micropython/modules/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_rgbmatrix5x5/breakout_rgbmatrix5x5.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -64,7 +59,7 @@ mp_obj_t BreakoutRGBMatrix5x5_make_new(const mp_obj_type_t *type, size_t n_args, self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutRGBMatrix5x5((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); + self->breakout = m_new_class(BreakoutRGBMatrix5x5, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutRGBMatrix5x5: breakout not found when initialising"); diff --git a/micropython/modules/breakout_roundlcd/breakout_roundlcd.cpp b/micropython/modules/breakout_roundlcd/breakout_roundlcd.cpp index b0bb4e6c..282c68f5 100644 --- a/micropython/modules/breakout_roundlcd/breakout_roundlcd.cpp +++ b/micropython/modules/breakout_roundlcd/breakout_roundlcd.cpp @@ -1,11 +1,7 @@ #include "libraries/breakout_roundlcd/breakout_roundlcd.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) +#include "micropython/modules/util.hpp" -#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) -#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) using namespace pimoroni; diff --git a/micropython/modules/breakout_rtc/breakout_rtc.cpp b/micropython/modules/breakout_rtc/breakout_rtc.cpp index e91bdf69..0906ad83 100644 --- a/micropython/modules/breakout_rtc/breakout_rtc.cpp +++ b/micropython/modules/breakout_rtc/breakout_rtc.cpp @@ -1,13 +1,8 @@ -#include "../../../libraries/breakout_rtc/breakout_rtc.hpp" +#include "libraries/breakout_rtc/breakout_rtc.hpp" +#include "micropython/modules/util.hpp" #include #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -64,7 +59,7 @@ mp_obj_t BreakoutRTC_make_new(const mp_obj_type_t *type, size_t n_args, size_t n self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutRTC((pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutRTC, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutRTC: breakout not found when initialising"); diff --git a/micropython/modules/breakout_sgp30/breakout_sgp30.cpp b/micropython/modules/breakout_sgp30/breakout_sgp30.cpp index 21f59739..e027a2d5 100644 --- a/micropython/modules/breakout_sgp30/breakout_sgp30.cpp +++ b/micropython/modules/breakout_sgp30/breakout_sgp30.cpp @@ -1,10 +1,5 @@ #include "libraries/breakout_sgp30/breakout_sgp30.hpp" - -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) +#include "micropython/modules/util.hpp" using namespace pimoroni; @@ -57,7 +52,7 @@ mp_obj_t BreakoutSGP30_make_new(const mp_obj_type_t *type, size_t n_args, size_t self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutSGP30((pimoroni::I2C *)(self->i2c->i2c)); + self->breakout = m_new_class(BreakoutSGP30, (pimoroni::I2C *)(self->i2c->i2c)); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "BreakoutSGP30: breakout not found when initialising"); diff --git a/micropython/modules/breakout_trackball/breakout_trackball.cpp b/micropython/modules/breakout_trackball/breakout_trackball.cpp index 54f3da82..fc4f8f36 100644 --- a/micropython/modules/breakout_trackball/breakout_trackball.cpp +++ b/micropython/modules/breakout_trackball/breakout_trackball.cpp @@ -1,12 +1,7 @@ #include "libraries/breakout_trackball/breakout_trackball.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -68,7 +63,7 @@ mp_obj_t BreakoutTrackball_make_new(const mp_obj_type_t *type, size_t n_args, si self->i2c = PimoroniI2C_from_machine_i2c_or_native(args[ARG_i2c].u_obj); - self->breakout = new BreakoutTrackball((pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); + self->breakout = m_new_class(BreakoutTrackball, (pimoroni::I2C *)(self->i2c->i2c), args[ARG_address].u_int, args[ARG_interrupt].u_int); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "Trackball breakout not found when initialising"); diff --git a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp index 67b3a1f8..db398cfe 100644 --- a/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp +++ b/micropython/modules/breakout_vl53l5cx/vl53l5cx.cpp @@ -1,8 +1,8 @@ #include #include "vl53l5cx.hpp" #include "pico/multicore.h" +#include "micropython/modules/util.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) extern "C" { @@ -47,7 +47,8 @@ void VL53L5CX_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t k mp_obj_t VL53L5CX___del__(mp_obj_t self_in) { _VL53L5CX_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VL53L5CX_obj_t); //self->breakout->stop_ranging(); // i2c object might have been deleted already? - delete self->breakout; + // TODO: Since we're now holding a pointer to the I2C object it *should* still be available here? + m_del_class(VL53L5CX, self->breakout); return mp_const_none; } @@ -100,7 +101,7 @@ mp_obj_t VL53L5CX_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw mp_raise_ValueError("Firmware must be 84k bytes!"); } - self->breakout = new pimoroni::VL53L5CX((pimoroni::I2C*)self->i2c->i2c, (uint8_t *)bufinfo.buf, addr); + self->breakout = m_new_class(pimoroni::VL53L5CX, (pimoroni::I2C*)self->i2c->i2c, (uint8_t *)bufinfo.buf, addr); if(!self->breakout->init()) { mp_raise_msg(&mp_type_RuntimeError, "VL53L5CX: init error"); diff --git a/micropython/modules/encoder/encoder.cpp b/micropython/modules/encoder/encoder.cpp index e6f289e2..f00af3f8 100644 --- a/micropython/modules/encoder/encoder.cpp +++ b/micropython/modules/encoder/encoder.cpp @@ -1,8 +1,8 @@ #include "drivers/encoder/encoder.hpp" +#include "micropython/modules/util.hpp" #include #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) using namespace pimoroni; using namespace encoder; @@ -138,9 +138,9 @@ mp_obj_t Encoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, freq_divider = mp_obj_get_float(args[ARG_freq_divider].u_obj); } - Encoder *encoder = new Encoder(pio, sm, pins, args[ARG_common_pin].u_int, (Direction)direction, counts_per_rev, count_microsteps, freq_divider); + Encoder *encoder = m_new_class(Encoder, pio, sm, pins, args[ARG_common_pin].u_int, (Direction)direction, counts_per_rev, count_microsteps, freq_divider); if(!encoder->init()) { - delete encoder; + m_del_class(Encoder, encoder); 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"); } @@ -155,7 +155,7 @@ mp_obj_t Encoder_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, /***** Destructor ******/ mp_obj_t Encoder___del__(mp_obj_t self_in) { _Encoder_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Encoder_obj_t); - delete self->encoder; + m_del_class(Encoder, self->encoder); return mp_const_none; } diff --git a/micropython/modules/hub75/hub75.cpp b/micropython/modules/hub75/hub75.cpp index 423d3074..13f7ce58 100644 --- a/micropython/modules/hub75/hub75.cpp +++ b/micropython/modules/hub75/hub75.cpp @@ -2,7 +2,7 @@ #include "hub75.hpp" #include "pico/multicore.h" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) +#include "micropython/modules/util.hpp" extern "C" { @@ -65,7 +65,7 @@ void Hub75_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind mp_obj_t Hub75___del__(mp_obj_t self_in) { _Hub75_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Hub75_obj_t); self->hub75->stop(dma_complete); - delete self->hub75; + m_del_class(Hub75, self->hub75); return mp_const_none; } @@ -111,7 +111,7 @@ mp_obj_t Hub75_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c hub75_obj = m_new_obj_with_finaliser(_Hub75_obj_t); hub75_obj->base.type = &Hub75_type; hub75_obj->buf = buffer; - hub75_obj->hub75 = new Hub75(width, height, buffer, paneltype, stb_invert); + hub75_obj->hub75 = m_new_class(Hub75, width, height, buffer, paneltype, stb_invert); return MP_OBJ_FROM_PTR(hub75_obj); } diff --git a/micropython/modules/motor/motor.cpp b/micropython/modules/motor/motor.cpp index 490f334d..4fb88b38 100644 --- a/micropython/modules/motor/motor.cpp +++ b/micropython/modules/motor/motor.cpp @@ -1,9 +1,9 @@ #include "drivers/motor/motor.hpp" #include "drivers/motor/motor_cluster.hpp" #include "common/pimoroni_common.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) using namespace pimoroni; using namespace motor; @@ -192,7 +192,7 @@ mp_obj_t Motor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c self = m_new_obj_with_finaliser(_Motor_obj_t); self->base.type = &Motor_type; - self->motor = new Motor(pins, (Direction)direction, speed_scale, zeropoint, deadzone, freq, (DecayMode)mode, args[ARG_ph_en_driver].u_bool); + 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(); return MP_OBJ_FROM_PTR(self); @@ -202,7 +202,7 @@ mp_obj_t Motor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c /***** Destructor ******/ mp_obj_t Motor___del__(mp_obj_t self_in) { _Motor_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Motor_obj_t); - delete self->motor; + m_del_class(Motor, self->motor); return mp_const_none; } @@ -539,8 +539,6 @@ extern mp_obj_t Motor_decay_mode(size_t n_args, const mp_obj_t *pos_args, mp_map typedef struct _MotorCluster_obj_t { mp_obj_base_t base; MotorCluster* cluster; - PWMCluster::Sequence *seq_buf; - PWMCluster::TransitionData *dat_buf; } _MotorCluster_obj_t; @@ -731,28 +729,21 @@ mp_obj_t MotorCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t bool auto_phase = args[ARG_auto_phase].u_bool; - MotorCluster *cluster; - PWMCluster::Sequence *seq_buffer = m_new(PWMCluster::Sequence, PWMCluster::NUM_BUFFERS * 2); - PWMCluster::TransitionData *dat_buffer = m_new(PWMCluster::TransitionData, PWMCluster::BUFFER_SIZE * 2); - cluster = new MotorCluster(pio, sm, pins, pair_count, (Direction)direction, speed_scale, zeropoint, deadzone, - freq, (DecayMode)mode, auto_phase, seq_buffer, dat_buffer); + MotorCluster *cluster = m_new_class(MotorCluster, pio, sm, pins, pair_count, (Direction)direction, speed_scale, zeropoint, deadzone, + freq, (DecayMode)mode, auto_phase); // Cleanup the pins array if(pins != nullptr) delete[] pins; if(!cluster->init()) { - delete cluster; - m_del(PWMCluster::Sequence, seq_buffer, PWMCluster::NUM_BUFFERS * 2); - m_del(PWMCluster::TransitionData, dat_buffer, PWMCluster::BUFFER_SIZE * 2); + m_del_class(MotorCluster, cluster); 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->cluster = cluster; - self->seq_buf = seq_buffer; - self->dat_buf = dat_buffer; return MP_OBJ_FROM_PTR(self); } @@ -761,7 +752,7 @@ mp_obj_t MotorCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t /***** Destructor ******/ mp_obj_t MotorCluster___del__(mp_obj_t self_in) { _MotorCluster_obj_t *self = MP_OBJ_TO_PTR2(self_in, _MotorCluster_obj_t); - delete self->cluster; + m_del_class(MotorCluster, self->cluster); return mp_const_none; } diff --git a/micropython/modules/pico_display/pico_display.cpp b/micropython/modules/pico_display/pico_display.cpp index 9b14e2d2..c6b7650d 100644 --- a/micropython/modules/pico_display/pico_display.cpp +++ b/micropython/modules/pico_display/pico_display.cpp @@ -2,7 +2,7 @@ #include "hardware/sync.h" #include "pico/binary_info.h" -#include "libraries/pico_display/pico_display.hpp" +#include "libraries/pico_display/pico_display.hpp"zzz using namespace pimoroni; diff --git a/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp b/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp index 6167cdc3..5bb4900c 100644 --- a/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp +++ b/micropython/modules/pimoroni_i2c/pimoroni_i2c.cpp @@ -1,12 +1,7 @@ #include "common/pimoroni_i2c.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace pimoroni; @@ -24,7 +19,8 @@ _PimoroniI2C_obj_t* PimoroniI2C_from_machine_i2c_or_native(mp_obj_t i2c_obj) { machine_i2c_obj_t *machine_i2c = (machine_i2c_obj_t *)MP_OBJ_TO_PTR(i2c_obj); pimoroni_i2c = m_new_obj(_PimoroniI2C_obj_t); pimoroni_i2c->base.type = &PimoroniI2C_type; - pimoroni_i2c->i2c = new I2C(machine_i2c->sda, machine_i2c->scl, machine_i2c->freq); + + pimoroni_i2c->i2c = m_new_class(I2C, machine_i2c->sda, machine_i2c->scl, machine_i2c->freq); return pimoroni_i2c; } else { mp_raise_ValueError(MP_ERROR_TEXT("Bad I2C object")); @@ -81,7 +77,7 @@ mp_obj_t PimoroniI2C_make_new(const mp_obj_type_t *type, size_t n_args, size_t n self = m_new_obj(_PimoroniI2C_obj_t); self->base.type = &PimoroniI2C_type; - self->i2c = new I2C(sda, scl, baud); + self->i2c = m_new_class(I2C, sda, scl, baud); return MP_OBJ_FROM_PTR(self); } diff --git a/micropython/modules/plasma/plasma.cpp b/micropython/modules/plasma/plasma.cpp index e4b83392..f7ac60f8 100644 --- a/micropython/modules/plasma/plasma.cpp +++ b/micropython/modules/plasma/plasma.cpp @@ -1,13 +1,8 @@ #include "drivers/plasma/ws2812.hpp" #include "drivers/plasma/apa102.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) - -// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. -#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) -#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) - using namespace plasma; @@ -47,7 +42,7 @@ void PlasmaWS2812_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind /***** Destructor ******/ mp_obj_t PlasmaWS2812___del__(mp_obj_t self_in) { _PlasmaWS2812_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PlasmaWS2812_obj_t); - delete self->ws2812; + m_del_class(WS2812, self->ws2812); return mp_const_none; } @@ -103,7 +98,7 @@ mp_obj_t PlasmaWS2812_make_new(const mp_obj_type_t *type, size_t n_args, size_t self->base.type = &PlasmaWS2812_type; self->buf = buffer; - self->ws2812 = new WS2812(num_leds, pio, sm, dat, freq, rgbw, color_order, (WS2812::RGB *)buffer); + self->ws2812 = m_new_class(WS2812, num_leds, pio, sm, dat, freq, rgbw, color_order, (WS2812::RGB *)buffer); return MP_OBJ_FROM_PTR(self); } @@ -252,7 +247,7 @@ void PlasmaAPA102_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind /***** Destructor ******/ mp_obj_t PlasmaAPA102___del__(mp_obj_t self_in) { _PlasmaAPA102_obj_t *self = MP_OBJ_TO_PTR2(self_in, _PlasmaAPA102_obj_t); - delete self->apa102; + m_del_class(APA102, self->apa102); return mp_const_none; } @@ -312,7 +307,7 @@ mp_obj_t PlasmaAPA102_make_new(const mp_obj_type_t *type, size_t n_args, size_t self->base.type = &PlasmaAPA102_type; self->buf = buffer; - self->apa102 = new APA102(num_leds, pio, sm, dat, clk, freq, buffer); + self->apa102 = m_new_class(APA102, num_leds, pio, sm, dat, clk, freq, buffer); return MP_OBJ_FROM_PTR(self); } diff --git a/micropython/modules/servo/servo.cpp b/micropython/modules/servo/servo.cpp index 47a07dbb..35dd0779 100644 --- a/micropython/modules/servo/servo.cpp +++ b/micropython/modules/servo/servo.cpp @@ -1,8 +1,8 @@ #include "drivers/servo/servo.hpp" #include "drivers/servo/servo_cluster.hpp" +#include "micropython/modules/util.hpp" #include -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) using namespace pimoroni; using namespace servo; @@ -75,7 +75,7 @@ mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n self = m_new_obj_with_finaliser(_Calibtration_obj_t); self->base.type = &Calibration_type; - self->calibration = new Calibration(calibration_type); + self->calibration = m_new_class(Calibration, calibration_type); } else { mp_raise_TypeError("cannot convert object to an integer"); @@ -84,7 +84,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->calibration = new Calibration(); + self->calibration = m_new_class(Calibration); } return MP_OBJ_FROM_PTR(self); @@ -94,7 +94,7 @@ mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n /***** Destructor ******/ mp_obj_t Calibration___del__(mp_obj_t self_in) { _Calibtration_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Calibtration_obj_t); - delete self->calibration; + m_del_class(Calibration, self->calibration); return mp_const_none; } @@ -895,10 +895,13 @@ mp_obj_t Servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c self = m_new_obj_with_finaliser(_Servo_obj_t); self->base.type = &Servo_type; + void *servo_target = m_new(Servo, 1); + if(calib != nullptr) - self->servo = new Servo(pin, *calib, freq); + self->servo = new(servo_target) Servo(pin, *calib, freq); else - self->servo = new Servo(pin, calibration_type, freq); + self->servo = new(servo_target) Servo(pin, calibration_type, freq); + self->servo->init(); return MP_OBJ_FROM_PTR(self); @@ -908,7 +911,7 @@ mp_obj_t Servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c /***** Destructor ******/ mp_obj_t Servo___del__(mp_obj_t self_in) { _Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t); - delete self->servo; + m_del_class(Servo, self->servo); return mp_const_none; } @@ -1161,7 +1164,7 @@ extern mp_obj_t Servo_calibration(size_t n_args, const mp_obj_t *pos_args, mp_ma _Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t); calib->base.type = &Calibration_type; - calib->calibration = new Calibration(self->servo->calibration()); + calib->calibration = m_new_class(Calibration, self->servo->calibration()); return MP_OBJ_FROM_PTR(calib); } else { @@ -1199,8 +1202,6 @@ extern mp_obj_t Servo_calibration(size_t n_args, const mp_obj_t *pos_args, mp_ma typedef struct _ServoCluster_obj_t { mp_obj_base_t base; ServoCluster* cluster; - PWMCluster::Sequence *seq_buf; - PWMCluster::TransitionData *dat_buf; } _ServoCluster_obj_t; @@ -1330,19 +1331,19 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t bool auto_phase = args[ARG_auto_phase].u_bool; ServoCluster *cluster; - PWMCluster::Sequence *seq_buffer = m_new(PWMCluster::Sequence, PWMCluster::NUM_BUFFERS * 2); - PWMCluster::TransitionData *dat_buffer = m_new(PWMCluster::TransitionData, PWMCluster::BUFFER_SIZE * 2); + void *cluster_target = m_new(ServoCluster, 1); + if(mask_provided) { if(calib != nullptr) - cluster = new ServoCluster(pio, sm, pin_mask, *calib, freq, auto_phase, seq_buffer, dat_buffer); + cluster = new(cluster_target) ServoCluster(pio, sm, pin_mask, *calib, freq, auto_phase); else - cluster = new ServoCluster(pio, sm, pin_mask, calibration_type, freq, auto_phase, seq_buffer, dat_buffer); + cluster = new(cluster_target) ServoCluster(pio, sm, pin_mask, calibration_type, freq, auto_phase); } else { if(calib != nullptr) - cluster = new ServoCluster(pio, sm, pins, pin_count, *calib, freq, auto_phase, seq_buffer, dat_buffer); + cluster = new(cluster_target) ServoCluster(pio, sm, pins, pin_count, *calib, freq, auto_phase); else - cluster = new ServoCluster(pio, sm, pins, pin_count, calibration_type, freq, auto_phase, seq_buffer, dat_buffer); + cluster = new(cluster_target) ServoCluster(pio, sm, pins, pin_count, calibration_type, freq, auto_phase); } // Cleanup the pins array @@ -1350,17 +1351,13 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t delete[] pins; if(!cluster->init()) { - delete cluster; - m_del(PWMCluster::Sequence, seq_buffer, PWMCluster::NUM_BUFFERS * 2); - m_del(PWMCluster::TransitionData, dat_buffer, PWMCluster::BUFFER_SIZE * 2); + m_del_class(ServoCluster, cluster); 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->cluster = cluster; - self->seq_buf = seq_buffer; - self->dat_buf = dat_buffer; return MP_OBJ_FROM_PTR(self); } @@ -1369,7 +1366,7 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t /***** Destructor ******/ mp_obj_t ServoCluster___del__(mp_obj_t self_in) { _ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(self_in, _ServoCluster_obj_t); - delete self->cluster; + m_del_class(ServoCluster, self->cluster); return mp_const_none; } @@ -2661,7 +2658,7 @@ extern mp_obj_t ServoCluster_calibration(size_t n_args, const mp_obj_t *pos_args _Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t); calib->base.type = &Calibration_type; - calib->calibration = new Calibration(self->cluster->calibration((uint)servo)); + calib->calibration = m_new_class(Calibration, self->cluster->calibration((uint)servo)); return MP_OBJ_FROM_PTR(calib); } } diff --git a/micropython/modules/st7789/st7789.cpp b/micropython/modules/st7789/st7789.cpp index e32c22c9..7d6f0a27 100644 --- a/micropython/modules/st7789/st7789.cpp +++ b/micropython/modules/st7789/st7789.cpp @@ -1,11 +1,7 @@ #include "libraries/generic_st7789/generic_st7789.hpp" -#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) +#include "micropython/modules/util.hpp" -#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) -#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) -#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) using namespace pimoroni; @@ -93,7 +89,7 @@ mp_obj_t GenericST7789_make_new(const mp_obj_type_t *type, size_t n_args, size_t if(args[ARG_slot].u_int != -1) { BG_SPI_SLOT slot = (BG_SPI_SLOT)args[ARG_slot].u_int; - self->st7789 = new ST7789Generic(width, height, round, self->buffer, slot); + self->st7789 = m_new_class(ST7789Generic, width, height, round, self->buffer, slot); if (rotate180) { self->st7789->configure_display(true); } @@ -121,7 +117,7 @@ mp_obj_t GenericST7789_make_new(const mp_obj_type_t *type, size_t n_args, size_t mp_raise_ValueError(MP_ERROR_TEXT("bad MOSI pin")); } spi_inst_t *spi = (spi_id == 0) ? spi0 : spi1; - self->st7789 = new ST7789Generic(width, height, round, self->buffer, + self->st7789 = m_new_class(ST7789Generic, width, height, round, self->buffer, spi, cs, dc, sck, mosi, bl); if (rotate180) { self->st7789->configure_display(true); diff --git a/micropython/modules/util.hpp b/micropython/modules/util.hpp new file mode 100644 index 00000000..6765de9f --- /dev/null +++ b/micropython/modules/util.hpp @@ -0,0 +1,16 @@ +#include + +#define MP_OBJ_TO_PTR2(o, t) ((t *)(uintptr_t)(o)) + +// SDA/SCL on even/odd pins, I2C0/I2C1 on even/odd pairs of pins. +#define IS_VALID_SCL(i2c, pin) (((pin) & 1) == 1 && (((pin) & 2) >> 1) == (i2c)) +#define IS_VALID_SDA(i2c, pin) (((pin) & 1) == 0 && (((pin) & 2) >> 1) == (i2c)) + +#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) +#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) +#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) +#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) + + +#define m_new_class(cls, ...) new(m_new(cls, 1)) cls(__VA_ARGS__) +#define m_del_class(cls, ptr) ptr->~cls();m_del(cls, ptr, 1) \ No newline at end of file