Skip to content

Commit

Permalink
Servo/Motor: Use m_new instead of new.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Mar 13, 2023
1 parent e75c90c commit c529bd6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions micropython/modules/motor/motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ void pimoroni_tuple_or_list(const mp_obj_t &object, mp_obj_t **items, size_t *le
}

uint8_t* pimoroni_motors_from_items(mp_obj_t *items, size_t length, int motor_count) {
uint8_t *motors = new uint8_t[length];
uint8_t *motors = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int motor = mp_obj_get_int(items[i]);
if(motor < 0 || motor >= motor_count) {
delete[] motors;
m_free(motors);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a motor in the list or tuple is out of range. Expected 0 to %d"), motor_count - 1);
}
else {
Expand Down Expand Up @@ -636,7 +636,7 @@ mp_obj_t MotorCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t
else {
// Specific check for is a single 2 pin list/tuple was provided
if(pair_count == 2 && mp_obj_is_int(items[0]) && mp_obj_is_int(items[1])) {
pins = new pin_pair[1];
pins = m_new(pin_pair, 1);
pair_count = 1;

int pos = mp_obj_get_int(items[0]);
Expand All @@ -656,7 +656,7 @@ mp_obj_t MotorCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t
}
else {
// Create and populate a local array of pins
pins = new pin_pair[pair_count];
pins = m_new(pin_pair, pair_count);
for(size_t i = 0; i < pair_count; i++) {
mp_obj_t obj = items[i];
if(!mp_obj_is_type(obj, &mp_type_tuple)) {
Expand Down
72 changes: 36 additions & 36 deletions micropython/modules/servo/servo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,11 +1289,11 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of pins
pins = new uint8_t[pin_count];
pins = m_new(uint8_t, pin_count);
for(size_t i = 0; i < pin_count; i++) {
int pin = mp_obj_get_int(items[i]);
if(pin < 0 || pin >= (int)NUM_BANK0_GPIOS) {
delete[] pins;
m_free(pins);
mp_raise_ValueError("a pin in the list or tuple is out of range. Expected 0 to 29");
}
else {
Expand Down Expand Up @@ -1348,7 +1348,7 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t

// Cleanup the pins array
if(pins != nullptr)
delete[] pins;
m_free(pins);

if(!cluster->init()) {
m_del_class(ServoCluster, cluster);
Expand Down Expand Up @@ -1449,19 +1449,19 @@ extern mp_obj_t ServoCluster_enable(size_t n_args, const mp_obj_t *pos_args, mp_
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
servos[i] = (uint8_t)servo;
}
}
self->cluster->enable(servos, length, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -1533,19 +1533,19 @@ extern mp_obj_t ServoCluster_disable(size_t n_args, const mp_obj_t *pos_args, mp
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
servos[i] = (uint8_t)servo;
}
}
self->cluster->disable(servos, length, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -1668,11 +1668,11 @@ extern mp_obj_t ServoCluster_pulse(size_t n_args, const mp_obj_t *pos_args, mp_m
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -1681,7 +1681,7 @@ extern mp_obj_t ServoCluster_pulse(size_t n_args, const mp_obj_t *pos_args, mp_m
}
float pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
self->cluster->pulse(servos, length, pulse, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -1786,11 +1786,11 @@ extern mp_obj_t ServoCluster_value(size_t n_args, const mp_obj_t *pos_args, mp_m
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -1799,7 +1799,7 @@ extern mp_obj_t ServoCluster_value(size_t n_args, const mp_obj_t *pos_args, mp_m
}
float value = mp_obj_get_float(args[ARG_value].u_obj);
self->cluster->value(servos, length, value, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -1904,11 +1904,11 @@ extern mp_obj_t ServoCluster_phase(size_t n_args, const mp_obj_t *pos_args, mp_m
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -1917,7 +1917,7 @@ extern mp_obj_t ServoCluster_phase(size_t n_args, const mp_obj_t *pos_args, mp_m
}
float phase = mp_obj_get_float(args[ARG_phase].u_obj);
self->cluster->phase(servos, length, phase, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2108,19 +2108,19 @@ extern mp_obj_t ServoCluster_to_min(size_t n_args, const mp_obj_t *pos_args, mp_
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
servos[i] = (uint8_t)servo;
}
}
self->cluster->to_min(servos, length, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2196,19 +2196,19 @@ extern mp_obj_t ServoCluster_to_mid(size_t n_args, const mp_obj_t *pos_args, mp_
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
servos[i] = (uint8_t)servo;
}
}
self->cluster->to_mid(servos, length, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2284,19 +2284,19 @@ extern mp_obj_t ServoCluster_to_max(size_t n_args, const mp_obj_t *pos_args, mp_
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
servos[i] = (uint8_t)servo;
}
}
self->cluster->to_max(servos, length, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2376,11 +2376,11 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -2389,7 +2389,7 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
}
float in = mp_obj_get_float(args[ARG_in].u_obj);
self->cluster->to_percent(servos, length, in, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2448,11 +2448,11 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -2463,7 +2463,7 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
float in_min = mp_obj_get_float(args[ARG_in_min].u_obj);
float in_max = mp_obj_get_float(args[ARG_in_max].u_obj);
self->cluster->to_percent(servos, length, in, in_min, in_max, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down Expand Up @@ -2526,11 +2526,11 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
mp_raise_TypeError("list or tuple must contain at least one integer");
else {
// Create and populate a local array of servo indices
uint8_t *servos = new uint8_t[length];
uint8_t *servos = m_new(uint8_t, length);
for(size_t i = 0; i < length; i++) {
int servo = mp_obj_get_int(items[i]);
if(servo < 0 || servo >= servo_count) {
delete[] servos;
m_free(servos);
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("a servo in the list or tuple is out of range. Expected 0 to %d"), servo_count - 1);
}
else {
Expand All @@ -2543,7 +2543,7 @@ extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args,
float value_min = mp_obj_get_float(args[ARG_value_min].u_obj);
float value_max = mp_obj_get_float(args[ARG_value_max].u_obj);
self->cluster->to_percent(servos, length, in, in_min, in_max, value_min, value_max, args[ARG_load].u_bool);
delete[] servos;
m_free(servos);
}
}
}
Expand Down

0 comments on commit c529bd6

Please sign in to comment.