@@ -244,22 +244,77 @@ extern mp_obj_t BreakoutEncoderWheel_show(mp_obj_t self_in) {
244
244
}
245
245
246
246
extern mp_obj_t BreakoutEncoderWheel_gpio_pin_mode (size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
247
- // breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_wheel_BreakoutEncoderWheel_obj_t);
247
+ enum { ARG_self, ARG_gpio, ARG_mode };
248
+ static const mp_arg_t allowed_args[] = {
249
+ { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
250
+ { MP_QSTR_gpio, MP_ARG_REQUIRED | MP_ARG_INT },
251
+ { MP_QSTR_mode, MP_ARG_OBJ, { .u_obj = mp_const_none }},
252
+ };
248
253
249
- return mp_const_none;
254
+ // Parse args.
255
+ mp_arg_val_t args[MP_ARRAY_SIZE (allowed_args)];
256
+ mp_arg_parse_all (n_args, pos_args, kw_args, MP_ARRAY_SIZE (allowed_args), allowed_args, args);
257
+
258
+ breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = MP_OBJ_TO_PTR2 (args[ARG_self].u_obj , breakout_encoder_wheel_BreakoutEncoderWheel_obj_t);
259
+ int gpio = args[ARG_gpio].u_int ;
260
+ if (gpio < 7 || gpio > 9 ) {
261
+ mp_raise_ValueError (" gpio out of range. Expected GP7 (7), GP8 (8), or GP9 (9)" );
262
+ }
263
+
264
+ if (args[ARG_mode].u_obj == mp_const_none) {
265
+ return mp_obj_new_int (self->breakout ->gpio_pin_mode (gpio));
266
+ }
267
+ else {
268
+ int mode = mp_obj_get_int (args[ARG_mode].u_obj );
269
+ self->breakout ->gpio_pin_mode (gpio, mode);
270
+
271
+ return mp_const_none;
272
+ }
250
273
}
251
274
252
275
extern mp_obj_t BreakoutEncoderWheel_gpio_pin_value (size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
253
- // breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = MP_OBJ_TO_PTR2(self_in, breakout_encoder_wheel_BreakoutEncoderWheel_obj_t);
276
+ enum { ARG_self, ARG_gpio, ARG_value, ARG_load, ARG_wait_for_load };
277
+ static const mp_arg_t allowed_args[] = {
278
+ { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
279
+ { MP_QSTR_gpio, MP_ARG_REQUIRED | MP_ARG_INT },
280
+ { MP_QSTR_value, MP_ARG_OBJ, { .u_obj = mp_const_none }},
281
+ { MP_QSTR_load, MP_ARG_BOOL, { .u_bool = true }},
282
+ { MP_QSTR_wait_for_load, MP_ARG_BOOL, { .u_bool = false }},
283
+ };
254
284
255
- return mp_const_none;
285
+ // Parse args.
286
+ mp_arg_val_t args[MP_ARRAY_SIZE (allowed_args)];
287
+ mp_arg_parse_all (n_args, pos_args, kw_args, MP_ARRAY_SIZE (allowed_args), allowed_args, args);
288
+
289
+ breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = MP_OBJ_TO_PTR2 (args[ARG_self].u_obj , breakout_encoder_wheel_BreakoutEncoderWheel_obj_t);
290
+ int gpio = args[ARG_gpio].u_int ;
291
+ if (gpio < 7 || gpio > 9 ) {
292
+ mp_raise_ValueError (" gpio out of range. Expected GP7 (7), GP8 (8), or GP9 (9)" );
293
+ }
294
+
295
+ if (args[ARG_value].u_obj == mp_const_none) {
296
+ if (self->breakout ->gpio_pin_mode (gpio) == IOExpander::PIN_ADC) {
297
+ return mp_obj_new_float (self->breakout ->gpio_pin_value_as_voltage (gpio));
298
+ }
299
+ else {
300
+ return mp_obj_new_int (self->breakout ->gpio_pin_value (gpio));
301
+ }
302
+ }
303
+ else {
304
+ int value = mp_obj_get_int (args[ARG_value].u_obj );
305
+ bool load = args[ARG_load].u_bool ;
306
+ bool wait_for_load = args[ARG_wait_for_load].u_bool ;
307
+ self->breakout ->gpio_pin_value (gpio, value, load, wait_for_load);
308
+
309
+ return mp_const_none;
310
+ }
256
311
}
257
312
258
313
extern mp_obj_t BreakoutEncoderWheel_gpio_pwm_load (size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
259
314
enum { ARG_self, ARG_wait_for_load };
260
315
static const mp_arg_t allowed_args[] = {
261
316
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
262
- { MP_QSTR_wait_for_load, MP_ARG_BOOL, { .u_bool = false }},
317
+ { MP_QSTR_wait_for_load, MP_ARG_BOOL, { .u_bool = true }},
263
318
};
264
319
265
320
// Parse args.
@@ -279,8 +334,8 @@ extern mp_obj_t BreakoutEncoderWheel_gpio_pwm_frequency(size_t n_args, const mp_
279
334
static const mp_arg_t allowed_args[] = {
280
335
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
281
336
{ MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_OBJ },
282
- { MP_QSTR_load, MP_ARG_BOOL, { .u_bool = false }},
283
- { MP_QSTR_wait_for_load, MP_ARG_BOOL, { .u_bool = false }},
337
+ { MP_QSTR_load, MP_ARG_BOOL, { .u_bool = true }},
338
+ { MP_QSTR_wait_for_load, MP_ARG_BOOL, { .u_bool = true }},
284
339
};
285
340
286
341
// Parse args.
@@ -289,10 +344,17 @@ extern mp_obj_t BreakoutEncoderWheel_gpio_pwm_frequency(size_t n_args, const mp_
289
344
290
345
breakout_encoder_wheel_BreakoutEncoderWheel_obj_t *self = MP_OBJ_TO_PTR2 (args[ARG_self].u_obj , breakout_encoder_wheel_BreakoutEncoderWheel_obj_t);
291
346
float frequency = mp_obj_get_float (args[ARG_frequency].u_obj );
347
+ uint32_t period = (uint32_t )(IOExpander::CLOCK_FREQ / frequency);
348
+ if (period / 128 > IOExpander::MAX_PERIOD) {
349
+ mp_raise_ValueError (" The provided frequency is too low" );
350
+ }
351
+ if (period < 2 ) {
352
+ mp_raise_ValueError (" The provided frequency is too high" );
353
+ }
354
+
292
355
bool load = args[ARG_load].u_bool ;
293
356
bool wait_for_load = args[ARG_wait_for_load].u_bool ;
294
-
295
- int period = self->breakout ->gpio_pwm_frequency (frequency, load, wait_for_load);
357
+ period = self->breakout ->gpio_pwm_frequency (frequency, load, wait_for_load);
296
358
297
359
return mp_obj_new_int (period);
298
360
}
0 commit comments