kopia lustrzana https://github.com/pimoroni/pimoroni-pico
All current servo functionality now micropython bindings
rodzic
196a5c6111
commit
93eafc4694
|
@ -54,13 +54,13 @@ namespace servo {
|
|||
calibration[2] = Point(max_pulse, max_value);
|
||||
}
|
||||
|
||||
void Calibration::create_uniform_calibration(uint size, float min_pulse, float min_value, float max_pulse, float max_value) {
|
||||
void Calibration::create_uniform_calibration(uint size, float min_pulse, float max_pulse, float min_value, float max_value) {
|
||||
create_blank_calibration(size);
|
||||
if(size > 0) {
|
||||
float size_minus_one = (float)(size - 1);
|
||||
for(uint i = 0; i < size; i++) {
|
||||
float pulse = ((max_pulse - min_pulse) * (float)i) / size_minus_one;
|
||||
float value = ((max_value - min_value) * (float)i) / size_minus_one;
|
||||
float pulse = Calibration::map_float((float)i, 0.0f, size_minus_one, min_pulse, max_pulse);
|
||||
float value = Calibration::map_float((float)i, 0.0f, size_minus_one, min_value, max_value);
|
||||
calibration[i] = Point(pulse, value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ namespace servo {
|
|||
void create_blank_calibration(uint size);
|
||||
void create_two_point_calibration(float min_pulse, float max_pulse, float min_value, float max_value);
|
||||
void create_three_point_calibration(float min_pulse, float mid_pulse, float max_pulse, float min_value, float mid_value, float max_value);
|
||||
void create_uniform_calibration(uint size, float min_pulse, float min_value, float max_pulse, float max_value);
|
||||
void create_uniform_calibration(uint size, float min_pulse, float max_pulse, float min_value, float max_value);
|
||||
void create_default_calibration(CalibrationType default_type);
|
||||
|
||||
uint size() const;
|
||||
|
|
|
@ -2,55 +2,118 @@
|
|||
|
||||
|
||||
/***** Methods *****/
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Calibration___del___obj, Calibration___del__);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_create_blank_calibration_obj, 2, Calibration_create_blank_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_create_two_point_calibration_obj, 5, Calibration_create_two_point_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_create_three_point_calibration_obj, 7, Calibration_create_three_point_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_create_uniform_calibration_obj, 6, Calibration_create_uniform_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_create_default_calibration_obj, 2, Calibration_create_default_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Calibration_size_obj, Calibration_size);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_point_at_obj, 2, Calibration_point_at);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_first_point_obj, 1, Calibration_first_point);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_last_point_obj, 1, Calibration_last_point);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_limit_to_calibration_obj, 3, Calibration_limit_to_calibration);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_value_to_pulse_obj, 2, Calibration_value_to_pulse);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Calibration_value_from_pulse_obj, 2, Calibration_value_from_pulse);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo___del___obj, Servo___del__);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_pin_obj, Servo_pin);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_enable_obj, Servo_enable);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_disable_obj, Servo_disable);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_is_enabled_obj, Servo_is_enabled);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Servo_value_obj, 1, Servo_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Servo_pulse_obj, 1, Servo_pulse);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_min_value_obj, Servo_min_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_mid_value_obj, Servo_mid_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_max_value_obj, Servo_max_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_to_min_obj, Servo_to_min);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_to_mid_obj, Servo_to_mid);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_to_max_obj, Servo_to_max);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Servo_to_percent_obj, 1, Servo_to_percent);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(Servo_to_percent_obj, 2, Servo_to_percent);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(Servo_calibration_obj, Servo_calibration);
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ServoCluster___del___obj, ServoCluster___del__);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_set_rgb_obj, 5, ServoCluster_set_rgb);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_set_hsv_obj, 3, ServoCluster_set_hsv);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_set_brightness_obj, 2, ServoCluster_set_brightness);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_start_obj, 1, ServoCluster_start);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_get_obj, 2, ServoCluster_get);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_1(ServoCluster_clear_obj, ServoCluster_clear);
|
||||
// MP_DEFINE_CONST_FUN_OBJ_1(ServoCluster_update_obj, ServoCluster_update);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(ServoCluster_pin_mask_obj, ServoCluster_pin_mask);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_enable_obj, 2, ServoCluster_enable);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_disable_obj, 2, ServoCluster_disable);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_is_enabled_obj, 2, ServoCluster_is_enabled);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_value_obj, 2, ServoCluster_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_pulse_obj, 2, ServoCluster_pulse);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_min_value_obj, 2, ServoCluster_min_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_mid_value_obj, 2, ServoCluster_mid_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_max_value_obj, 2, ServoCluster_max_value);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_to_min_obj, 2, ServoCluster_to_min);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_to_mid_obj, 2, ServoCluster_to_mid);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_to_max_obj, 2, ServoCluster_to_max);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_to_percent_obj, 3, ServoCluster_to_percent);
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(ServoCluster_calibration_obj, 2, ServoCluster_calibration);
|
||||
|
||||
/***** Binding of Methods *****/
|
||||
STATIC const mp_rom_map_elem_t Calibration_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&Calibration___del___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_blank_calibration), MP_ROM_PTR(&Calibration_create_blank_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_two_point_calibration), MP_ROM_PTR(&Calibration_create_two_point_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_three_point_calibration), MP_ROM_PTR(&Calibration_create_three_point_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_uniform_calibration), MP_ROM_PTR(&Calibration_create_uniform_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_default_calibration), MP_ROM_PTR(&Calibration_create_default_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&Calibration_size_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_point_at), MP_ROM_PTR(&Calibration_point_at_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_first_point), MP_ROM_PTR(&Calibration_first_point_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_last_point), MP_ROM_PTR(&Calibration_last_point_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_limit_to_calibration), MP_ROM_PTR(&Calibration_limit_to_calibration_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_value_to_pulse), MP_ROM_PTR(&Calibration_value_to_pulse_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_value_from_pulse), MP_ROM_PTR(&Calibration_value_from_pulse_obj) },
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t Servo_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&Servo___del___obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&Servo_pin_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&Servo_enable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&Servo_disable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_is_enabled), MP_ROM_PTR(&Servo_is_enabled_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&Servo_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pulse), MP_ROM_PTR(&Servo_pulse_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_min_value), MP_ROM_PTR(&Servo_min_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_mid_value), MP_ROM_PTR(&Servo_mid_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_max_value), MP_ROM_PTR(&Servo_max_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_min), MP_ROM_PTR(&Servo_to_min_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_mid), MP_ROM_PTR(&Servo_to_mid_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_max), MP_ROM_PTR(&Servo_to_max_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_percent), MP_ROM_PTR(&Servo_to_percent_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&Servo_calibration_obj) },
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t ServoCluster_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ServoCluster___del___obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_set_rgb), MP_ROM_PTR(&ServoCluster_set_rgb_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_set_hsv), MP_ROM_PTR(&ServoCluster_set_hsv_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_set_brightness), MP_ROM_PTR(&ServoCluster_set_brightness_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&ServoCluster_start_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&ServoCluster_get_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&ServoCluster_clear_obj) },
|
||||
// { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&ServoCluster_update_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pin_mask), MP_ROM_PTR(&ServoCluster_pin_mask_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&ServoCluster_enable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&ServoCluster_disable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_is_enabled), MP_ROM_PTR(&ServoCluster_is_enabled_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&ServoCluster_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pulse), MP_ROM_PTR(&ServoCluster_pulse_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_min_value), MP_ROM_PTR(&ServoCluster_min_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_mid_value), MP_ROM_PTR(&ServoCluster_mid_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_max_value), MP_ROM_PTR(&ServoCluster_max_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_min), MP_ROM_PTR(&ServoCluster_to_min_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_mid), MP_ROM_PTR(&ServoCluster_to_mid_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_max), MP_ROM_PTR(&ServoCluster_to_max_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_to_percent), MP_ROM_PTR(&ServoCluster_to_percent_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&ServoCluster_calibration_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(Calibration_locals_dict, Calibration_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(Servo_locals_dict, Servo_locals_dict_table);
|
||||
STATIC MP_DEFINE_CONST_DICT(ServoCluster_locals_dict, ServoCluster_locals_dict_table);
|
||||
|
||||
/***** Class Definition *****/
|
||||
const mp_obj_type_t Calibration_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_calibration,
|
||||
.print = Calibration_print,
|
||||
.make_new = Calibration_make_new,
|
||||
.locals_dict = (mp_obj_dict_t*)&Calibration_locals_dict,
|
||||
};
|
||||
|
||||
const mp_obj_type_t Servo_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_servo,
|
||||
|
@ -67,6 +130,7 @@ const mp_obj_type_t ServoCluster_type = {
|
|||
.locals_dict = (mp_obj_dict_t*)&ServoCluster_locals_dict,
|
||||
};
|
||||
|
||||
// TODO What is this? Should remove?
|
||||
typedef struct _mp_obj_float_t {
|
||||
mp_obj_base_t base;
|
||||
mp_float_t value;
|
||||
|
@ -100,6 +164,7 @@ const mp_obj_module_t servo2040_user_cmodule = {
|
|||
|
||||
STATIC const mp_map_elem_t servo_globals_table[] = {
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_servo) },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Calibration), (mp_obj_t)&Calibration_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_Servo), (mp_obj_t)&Servo_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_ServoCluster), (mp_obj_t)&ServoCluster_type },
|
||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_servo2040), (mp_obj_t)&servo2040_user_cmodule },
|
||||
|
@ -107,6 +172,7 @@ STATIC const mp_map_elem_t servo_globals_table[] = {
|
|||
{ MP_ROM_QSTR(MP_QSTR_ANGULAR), MP_ROM_INT(0x00) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LINEAR), MP_ROM_INT(0x01) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_CONTINUOUS), MP_ROM_INT(0x02) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_EMPTY), MP_ROM_INT(0x03) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_servo_globals, servo_globals_table);
|
||||
|
||||
|
|
|
@ -4,9 +4,7 @@
|
|||
|
||||
#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_SERVO_INVALID(servo) (((servo) < 0) || ((servo) >= (int)NUM_BANK0_GPIOS))
|
||||
|
||||
|
||||
using namespace servo;
|
||||
|
@ -22,6 +20,464 @@ typedef struct _mp_obj_float_t {
|
|||
|
||||
const mp_obj_float_t const_float_1 = {{&mp_type_float}, 1.0f};
|
||||
|
||||
/********** Calibration **********/
|
||||
|
||||
/***** Variables Struct *****/
|
||||
typedef struct _Calibration_obj_t {
|
||||
mp_obj_base_t base;
|
||||
Calibration *calibration;
|
||||
bool owner;
|
||||
} _Calibtration_obj_t;
|
||||
|
||||
|
||||
/***** Print *****/
|
||||
void Calibration_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
(void)kind; //Unused input parameter
|
||||
_Calibtration_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Calibtration_obj_t);
|
||||
Calibration* calib = self->calibration;
|
||||
mp_print_str(print, "Calibration(");
|
||||
|
||||
uint size = calib->size();
|
||||
mp_print_str(print, "size = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(size), PRINT_REPR);
|
||||
|
||||
mp_print_str(print, ", points = {");
|
||||
for(uint i = 0; i < size; i++) {
|
||||
Calibration::Point *point = calib->point_at(i);
|
||||
mp_print_str(print, "{");
|
||||
mp_obj_print_helper(print, mp_obj_new_float(point->pulse), PRINT_REPR);
|
||||
mp_print_str(print, ", ");
|
||||
mp_obj_print_helper(print, mp_obj_new_float(point->value), PRINT_REPR);
|
||||
mp_print_str(print, "}");
|
||||
if(i < size - 1)
|
||||
mp_print_str(print, ", ");
|
||||
}
|
||||
mp_print_str(print, "})");
|
||||
}
|
||||
|
||||
/***** Destructor ******/
|
||||
mp_obj_t Calibration___del__(mp_obj_t self_in) {
|
||||
_Calibtration_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Calibtration_obj_t);
|
||||
if(self->owner)
|
||||
delete self->calibration;
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
/***** Constructor *****/
|
||||
mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
||||
_Calibtration_obj_t *self = nullptr;
|
||||
|
||||
enum { ARG_type };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_type, MP_ARG_INT, {.u_int = (uint8_t)servo::CalibrationType::ANGULAR} },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
servo::CalibrationType calibration_type = (servo::CalibrationType)args[ARG_type].u_int;
|
||||
|
||||
self = m_new_obj_with_finaliser(_Calibtration_obj_t);
|
||||
self->base.type = &Calibration_type;
|
||||
|
||||
self->calibration = new Calibration(calibration_type);
|
||||
self->owner = true;
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
mp_obj_t Calibration_create_blank_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_size };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
int size = args[ARG_size].u_int;
|
||||
if(size < 0)
|
||||
mp_raise_ValueError("size out of range. Expected 0 or greater");
|
||||
else
|
||||
self->calibration->create_blank_calibration((uint)size);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_create_two_point_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_min_pulse, ARG_max_pulse, ARG_min_value, ARG_max_value };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_min_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_min_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
float min_pulse = mp_obj_get_float(args[ARG_min_pulse].u_obj);
|
||||
float max_pulse = mp_obj_get_float(args[ARG_max_pulse].u_obj);
|
||||
float min_value = mp_obj_get_float(args[ARG_min_value].u_obj);
|
||||
float max_value = mp_obj_get_float(args[ARG_max_value].u_obj);
|
||||
self->calibration->create_two_point_calibration(min_pulse, max_pulse, min_value, max_value);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_create_three_point_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_min_pulse, ARG_mid_pulse, ARG_max_pulse, ARG_min_value, ARG_mid_value, ARG_max_value };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_min_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_mid_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_min_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_mid_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
float min_pulse = mp_obj_get_float(args[ARG_min_pulse].u_obj);
|
||||
float mid_pulse = mp_obj_get_float(args[ARG_mid_pulse].u_obj);
|
||||
float max_pulse = mp_obj_get_float(args[ARG_max_pulse].u_obj);
|
||||
float min_value = mp_obj_get_float(args[ARG_min_value].u_obj);
|
||||
float mid_value = mp_obj_get_float(args[ARG_mid_value].u_obj);
|
||||
float max_value = mp_obj_get_float(args[ARG_max_value].u_obj);
|
||||
self->calibration->create_three_point_calibration(min_pulse, mid_pulse, max_pulse, min_value, mid_value, max_value);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_create_uniform_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_size, ARG_min_pulse, ARG_max_pulse, ARG_min_value, ARG_max_value };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_min_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_min_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_max_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
int size = args[ARG_size].u_int;
|
||||
if(size < 0)
|
||||
mp_raise_ValueError("size out of range. Expected 0 or greater");
|
||||
else {
|
||||
float min_pulse = mp_obj_get_float(args[ARG_min_pulse].u_obj);
|
||||
float max_pulse = mp_obj_get_float(args[ARG_max_pulse].u_obj);
|
||||
float min_value = mp_obj_get_float(args[ARG_min_value].u_obj);
|
||||
float max_value = mp_obj_get_float(args[ARG_max_value].u_obj);
|
||||
self->calibration->create_uniform_calibration((uint)size, min_pulse, max_pulse, min_value, max_value);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_create_default_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_type };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_type, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
servo::CalibrationType calibration_type = (servo::CalibrationType)args[ARG_type].u_int;
|
||||
self->calibration->create_default_calibration(calibration_type);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_size(mp_obj_t self_in) {
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Calibration_obj_t);
|
||||
return mp_obj_new_int(self->calibration->size());
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_point_at(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 2) {
|
||||
enum { ARG_self, ARG_index };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
int index = args[ARG_index].u_int;
|
||||
if(index < 0 || index >= (int)self->calibration->size())
|
||||
mp_raise_ValueError("index out of range. Expected 0 to size()-1");
|
||||
else {
|
||||
Calibration::Point *point = self->calibration->point_at((uint)index);
|
||||
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_float(point->pulse);
|
||||
tuple[1] = mp_obj_new_float(point->value);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_index, ARG_point };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_point, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
int index = args[ARG_index].u_int;
|
||||
if(index < 0 || index >= (int)self->calibration->size())
|
||||
mp_raise_ValueError("index out of range. Expected 0 to size()-1");
|
||||
else {
|
||||
Calibration::Point *point = self->calibration->point_at((uint)index);
|
||||
|
||||
const mp_obj_t object = args[ARG_point].u_obj;
|
||||
if(mp_obj_is_type(object, &mp_type_list)) {
|
||||
mp_obj_list_t *list = MP_OBJ_TO_PTR2(object, mp_obj_list_t);
|
||||
if(list->len == 2) {
|
||||
point->pulse = mp_obj_get_float(list->items[0]);
|
||||
point->value = mp_obj_get_float(list->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("list must contain two numbers");
|
||||
}
|
||||
}
|
||||
else if(!mp_obj_is_type(object, &mp_type_tuple)) {
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(object, mp_obj_tuple_t);
|
||||
if(tuple->len == 2) {
|
||||
point->pulse = mp_obj_get_float(tuple->items[0]);
|
||||
point->value = mp_obj_get_float(tuple->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("tuple must contain two numbers");
|
||||
}
|
||||
}
|
||||
else {
|
||||
mp_raise_TypeError("can't convert object to list or tuple");
|
||||
}
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_first_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 1) {
|
||||
enum { ARG_self };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
Calibration::Point *point = self->calibration->first_point();
|
||||
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_float(point->pulse);
|
||||
tuple[1] = mp_obj_new_float(point->value);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_point };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_point, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
Calibration::Point *point = self->calibration->first_point();
|
||||
|
||||
const mp_obj_t object = args[ARG_point].u_obj;
|
||||
if(mp_obj_is_type(object, &mp_type_list)) {
|
||||
mp_obj_list_t *list = MP_OBJ_TO_PTR2(object, mp_obj_list_t);
|
||||
if(list->len == 2) {
|
||||
point->pulse = mp_obj_get_float(list->items[0]);
|
||||
point->value = mp_obj_get_float(list->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("list must contain two numbers");
|
||||
}
|
||||
}
|
||||
else if(!mp_obj_is_type(object, &mp_type_tuple)) {
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(object, mp_obj_tuple_t);
|
||||
if(tuple->len == 2) {
|
||||
point->pulse = mp_obj_get_float(tuple->items[0]);
|
||||
point->value = mp_obj_get_float(tuple->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("tuple must contain two numbers");
|
||||
}
|
||||
}
|
||||
else {
|
||||
mp_raise_TypeError("can't convert object to list or tuple");
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_last_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 1) {
|
||||
enum { ARG_self };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
Calibration::Point *point = self->calibration->last_point();
|
||||
|
||||
mp_obj_t tuple[2];
|
||||
tuple[0] = mp_obj_new_float(point->pulse);
|
||||
tuple[1] = mp_obj_new_float(point->value);
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_point };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_point, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
Calibration::Point *point = self->calibration->last_point();
|
||||
|
||||
const mp_obj_t object = args[ARG_point].u_obj;
|
||||
if(mp_obj_is_type(object, &mp_type_list)) {
|
||||
mp_obj_list_t *list = MP_OBJ_TO_PTR2(object, mp_obj_list_t);
|
||||
if(list->len == 2) {
|
||||
point->pulse = mp_obj_get_float(list->items[0]);
|
||||
point->value = mp_obj_get_float(list->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("list must contain two numbers");
|
||||
}
|
||||
}
|
||||
else if(!mp_obj_is_type(object, &mp_type_tuple)) {
|
||||
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR2(object, mp_obj_tuple_t);
|
||||
if(tuple->len == 2) {
|
||||
point->pulse = mp_obj_get_float(tuple->items[0]);
|
||||
point->value = mp_obj_get_float(tuple->items[1]);
|
||||
}
|
||||
else {
|
||||
mp_raise_ValueError("tuple must contain two numbers");
|
||||
}
|
||||
}
|
||||
else {
|
||||
mp_raise_TypeError("can't convert object to list or tuple");
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_limit_to_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_lower, ARG_upper };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_lower, MP_ARG_REQUIRED | MP_ARG_BOOL },
|
||||
{ MP_QSTR_upper, MP_ARG_REQUIRED | MP_ARG_BOOL },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
bool lower = args[ARG_lower].u_bool;
|
||||
bool upper = args[ARG_upper].u_bool;
|
||||
self->calibration->limit_to_calibration(lower, upper);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_value_to_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_value };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
float value = mp_obj_get_float(args[ARG_value].u_obj);
|
||||
return mp_obj_new_float(self->calibration->value_to_pulse(value));
|
||||
}
|
||||
|
||||
mp_obj_t Calibration_value_from_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_pulse };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_Calibration_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _Calibration_obj_t);
|
||||
|
||||
float pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
|
||||
return mp_obj_new_float(self->calibration->value_from_pulse(pulse));
|
||||
}
|
||||
|
||||
|
||||
/********** Servo **********/
|
||||
|
||||
/***** Variables Struct *****/
|
||||
|
@ -39,10 +495,10 @@ void Servo_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind
|
|||
|
||||
mp_print_str(print, "pin = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_int(self->servo->get_pin()), PRINT_REPR);
|
||||
mp_print_str(print, ", value = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_float(self->servo->get_value()), PRINT_REPR);
|
||||
mp_print_str(print, ", pulse = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_float(self->servo->get_pulse()), PRINT_REPR);
|
||||
mp_print_str(print, ", value = ");
|
||||
mp_obj_print_helper(print, mp_obj_new_float(self->servo->get_value()), PRINT_REPR);
|
||||
mp_print_str(print, ", enabled = ");
|
||||
mp_obj_print_helper(print, self->servo->is_enabled() ? mp_const_true : mp_const_false, PRINT_REPR);
|
||||
|
||||
|
@ -82,6 +538,12 @@ mp_obj_t Servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, c
|
|||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
/***** Methods *****/
|
||||
extern mp_obj_t Servo_pin(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
return mp_obj_new_int(self->servo->get_pin());
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_enable(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
self->servo->enable();
|
||||
|
@ -169,6 +631,21 @@ extern mp_obj_t Servo_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||
}
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_min_value(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
return mp_obj_new_float(self->servo->get_min_value());
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_mid_value(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
return mp_obj_new_float(self->servo->get_mid_value());
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_max_value(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
return mp_obj_new_float(self->servo->get_max_value());
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_to_min(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
self->servo->to_min();
|
||||
|
@ -254,6 +731,20 @@ extern mp_obj_t Servo_to_percent(size_t n_args, const mp_obj_t *pos_args, mp_map
|
|||
return mp_const_none;
|
||||
}
|
||||
|
||||
extern mp_obj_t Servo_calibration(mp_obj_t self_in) {
|
||||
_Servo_obj_t *self = MP_OBJ_TO_PTR2(self_in, _Servo_obj_t);
|
||||
|
||||
// NOTE This seems to work, in that it give MP access to the calibration object
|
||||
// Could very easily mess up in weird ways once object deletion is considered
|
||||
_Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t);
|
||||
calib->base.type = &Calibration_type;
|
||||
|
||||
calib->calibration = &self->servo->calibration();
|
||||
calib->owner = false;
|
||||
|
||||
return MP_OBJ_FROM_PTR(calib);
|
||||
}
|
||||
|
||||
|
||||
/********** ServoCluster **********/
|
||||
|
||||
|
@ -345,24 +836,17 @@ mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t
|
|||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
/*
|
||||
mp_obj_t ServoCluster_clear(mp_obj_t self_in) {
|
||||
/***** Methods *****/
|
||||
extern mp_obj_t ServoCluster_pin_mask(mp_obj_t self_in) {
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(self_in, _ServoCluster_obj_t);
|
||||
self->apa102->clear();
|
||||
return mp_const_none;
|
||||
return mp_obj_new_int(self->cluster->get_pin_mask());
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_update(mp_obj_t self_in) {
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(self_in, _ServoCluster_obj_t);
|
||||
self->apa102->update(true);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_fps };
|
||||
extern mp_obj_t ServoCluster_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_fps, MP_ARG_INT, {.u_int = 60} }
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
|
@ -370,101 +854,382 @@ mp_obj_t ServoCluster_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
self->apa102->start(args[ARG_fps].u_int);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
self->cluster->enable((uint)servo);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_set_brightness(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_brightness };
|
||||
extern mp_obj_t ServoCluster_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_brightness, MP_ARG_REQUIRED | MP_ARG_INT }
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
int brightness = args[ARG_brightness].u_int;
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
self->apa102->set_brightness(brightness);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
self->cluster->disable((uint)servo);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_set_rgb(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_index, ARG_r, ARG_g, ARG_b };
|
||||
extern mp_obj_t ServoCluster_is_enabled(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_r, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_g, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_b, MP_ARG_REQUIRED | MP_ARG_INT }
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
int index = args[ARG_index].u_int;
|
||||
int r = args[ARG_r].u_int;
|
||||
int g = args[ARG_g].u_int;
|
||||
int b = args[ARG_b].u_int;
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
self->apa102->set_rgb(index, r, g, b);
|
||||
|
||||
return mp_const_none;
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return self->cluster->is_enabled((uint)servo) ? mp_const_true : mp_const_false;
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_set_hsv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_index, ARG_h, ARG_s, ARG_v };
|
||||
extern mp_obj_t ServoCluster_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 2) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return mp_obj_new_float(self->cluster->get_value((uint)servo));
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_servo, ARG_value };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
float value = mp_obj_get_float(args[ARG_value].u_obj);
|
||||
self->cluster->set_value((uint)servo, value);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 2) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return mp_obj_new_float(self->cluster->get_pulse((uint)servo));
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_servo, ARG_pulse };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_pulse, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
float pulse = mp_obj_get_float(args[ARG_pulse].u_obj);
|
||||
self->cluster->set_pulse((uint)servo, pulse);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_min_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_hue, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_sat, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&const_float_1)} },
|
||||
{ MP_QSTR_val, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&const_float_1)} }
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
int index = args[ARG_index].u_int;
|
||||
float h = mp_obj_get_float(args[ARG_h].u_obj);
|
||||
float s = mp_obj_get_float(args[ARG_s].u_obj);
|
||||
float v = mp_obj_get_float(args[ARG_v].u_obj);
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return mp_obj_new_float(self->cluster->get_min_value((uint)servo));
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_mid_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
self->apa102->set_hsv(index, h, s, v);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return mp_obj_new_float(self->cluster->get_mid_value((uint)servo));
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_max_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
return mp_obj_new_float(self->cluster->get_max_value((uint)servo));
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_to_min(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
self->cluster->to_min((uint)servo);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t ServoCluster_get(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_index };
|
||||
extern mp_obj_t ServoCluster_to_mid(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
int index = args[ARG_index].u_int;
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
self->cluster->to_mid((uint)servo);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_to_max(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
APA102::RGB rgb = self->apa102->get(index);
|
||||
|
||||
mp_obj_t tuple[4];
|
||||
tuple[0] = mp_obj_new_int(rgb.r);
|
||||
tuple[1] = mp_obj_new_int(rgb.g);
|
||||
tuple[2] = mp_obj_new_int(rgb.b);
|
||||
tuple[3] = mp_obj_new_int(rgb.sof);
|
||||
return mp_obj_new_tuple(4, tuple);
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else
|
||||
self->cluster->to_max((uint)servo);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
if(n_args <= 2) {
|
||||
enum { ARG_self, ARG_servo, ARG_in };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_in, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
float in = mp_obj_get_float(args[ARG_in].u_obj);
|
||||
self->cluster->to_percent((uint)servo, in);
|
||||
}
|
||||
}
|
||||
else if(n_args <= 4) {
|
||||
enum { ARG_self, ARG_servo, ARG_in, ARG_in_min, ARG_in_max };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_in, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_in_min, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_in_max, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
float in = mp_obj_get_float(args[ARG_in].u_obj);
|
||||
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((uint)servo, in, in_min, in_max);
|
||||
}
|
||||
}
|
||||
else {
|
||||
enum { ARG_self, ARG_servo, ARG_in, ARG_in_min, ARG_in_max, ARG_value_min, ARG_value_max };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
{ MP_QSTR_in, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_in_min, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_in_max, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_value_min, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_value_max, MP_ARG_REQUIRED | MP_ARG_OBJ }
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
float in = mp_obj_get_float(args[ARG_in].u_obj);
|
||||
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);
|
||||
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((uint)servo, in, in_min, in_max, value_min, value_max);
|
||||
}
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
extern mp_obj_t ServoCluster_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_self, ARG_servo };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_servo, MP_ARG_REQUIRED | MP_ARG_INT },
|
||||
};
|
||||
|
||||
// Parse args.
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
_ServoCluster_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _ServoCluster_obj_t);
|
||||
|
||||
int servo = args[ARG_servo].u_int;
|
||||
if(IS_SERVO_INVALID(servo))
|
||||
mp_raise_ValueError("servo out of range. Expected 0 to 29");
|
||||
else {
|
||||
// NOTE This seems to work, in that it give MP access to the calibration object
|
||||
// Could very easily mess up in weird ways once object deletion is considered
|
||||
_Calibration_obj_t *calib = m_new_obj_with_finaliser(_Calibration_obj_t);
|
||||
calib->base.type = &Calibration_type;
|
||||
|
||||
calib->calibration = self->cluster->calibration((uint)servo);
|
||||
calib->owner = false;
|
||||
|
||||
return MP_OBJ_FROM_PTR(calib);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
|
@ -2,32 +2,62 @@
|
|||
#include "py/runtime.h"
|
||||
|
||||
/***** Extern of Class Definition *****/
|
||||
extern const mp_obj_type_t Calibration_type;
|
||||
extern const mp_obj_type_t Servo_type;
|
||||
extern const mp_obj_type_t ServoCluster_type;
|
||||
|
||||
/***** Extern of Class Methods *****/
|
||||
extern void Calibration_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t Calibration_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
|
||||
extern mp_obj_t Calibration___del__(mp_obj_t self_in);
|
||||
extern mp_obj_t Calibration_create_blank_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_create_two_point_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_create_three_point_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_create_uniform_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_create_default_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_size(mp_obj_t self_in);
|
||||
extern mp_obj_t Calibration_point_at(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_first_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_last_point(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_limit_to_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_value_to_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Calibration_value_from_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
|
||||
extern void Servo_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t Servo_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
|
||||
extern mp_obj_t Servo___del__(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_pin(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_enable(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_disable(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_is_enabled(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Servo_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Servo_min_value(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_mid_value(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_max_value(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_to_min(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_to_mid(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_to_max(mp_obj_t self_in);
|
||||
extern mp_obj_t Servo_to_percent(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t Servo_calibration(mp_obj_t self_in);
|
||||
|
||||
extern void ServoCluster_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
extern mp_obj_t ServoCluster_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
|
||||
extern mp_obj_t ServoCluster___del__(mp_obj_t self_in);
|
||||
// extern mp_obj_t ServoCluster_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
// extern mp_obj_t ServoCluster_set_rgb(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
// extern mp_obj_t ServoCluster_set_hsv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
// extern mp_obj_t ServoCluster_set_brightness(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
// extern mp_obj_t ServoCluster_get(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
// extern mp_obj_t ServoCluster_clear(mp_obj_t self_in);
|
||||
// extern mp_obj_t ServoCluster_update(mp_obj_t self_in);
|
||||
extern mp_obj_t ServoCluster_pin_mask(mp_obj_t self_in);
|
||||
extern mp_obj_t ServoCluster_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_is_enabled(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_pulse(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_min_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_mid_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_max_value(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_to_min(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_to_mid(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_to_max(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_to_percent(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
extern mp_obj_t ServoCluster_calibration(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
|
||||
extern bool Pimoroni_mp_obj_to_i2c(mp_obj_t in, void *out);
|
||||
|
||||
extern bool Pimoroni_mp_obj_to_calibration(mp_obj_t in, void *out);
|
Ładowanie…
Reference in New Issue