for my robot I'd like to have a generic constructor in order to initialize a motor either by passing a hash or by an id like below.
Code: Select all
>>> class Motor():
... def __init__(self,hash=0,id=255):
... self.hash = hash
... self.id = id
... def get_id(self):
... return self.id
... def get_hash(self):
... return self.hash
...
>>> mot1 = Motor(hash=42)
>>> mot2 = Motor(id=23)
>>> mot1.get_id()
255
>>> mot1.get_hash()
42
>>> mot2.get_id()
23
>>> mot2.get_hash()
0
Code: Select all
typedef struct _pb_motor_obj_t
{
mp_obj_base_t base;
mp_int_t motor_id;
mp_int_t motor_hash;
}pb_motor_obj_t;
STATIC mp_obj_t motor_obj_make_new( const mp_obj_type_t *type,
size_t n_args,
size_t n_kw,
const mp_obj_t *args )
{
// check arguments
mp_arg_check_num(n_args, n_kw, 0, 2, false);
//TODO: parse arguments
pb_motor_obj_t *motor = m_new_obj(pb_motor_obj_t);
motor->base.type = &pb_motor_type;
// return battery object
return motor;
}
Thanks for reading
chrizz