@@ -42,6 +42,8 @@ typedef struct _machine_qencd_obj_t {
42
42
mp_obj_base_t base ;
43
43
ENC_Type * instance ;
44
44
int8_t id ;
45
+ uint8_t input_a ;
46
+ uint8_t input_b ;
45
47
uint8_t mode ;
46
48
bool is_signed ;
47
49
uint8_t match_pin ;
@@ -343,9 +345,12 @@ STATIC void mp_machine_qencd_init_helper_common(machine_qencd_obj_t *self,
343
345
344
346
STATIC void mp_machine_qencd_init_helper (machine_qencd_obj_t * self ,
345
347
size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
346
- enum { ARG_home , ARG_reverse , ARG_match , ARG_filter_ns , ARG_cpc , ARG_compare , ARG_signed , ARG_index };
348
+ enum { ARG_phase_a , ARG_phase_b , ARG_home , ARG_reverse ,
349
+ ARG_match , ARG_filter_ns , ARG_cpc , ARG_compare , ARG_signed , ARG_index };
347
350
348
351
static const mp_arg_t allowed_args [] = {
352
+ { MP_QSTR_phase_a , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
353
+ { MP_QSTR_phase_b , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
349
354
{ MP_QSTR_home , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
350
355
{ MP_QSTR_reverse , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
351
356
{ MP_QSTR_match , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
@@ -360,6 +365,18 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
360
365
MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
361
366
362
367
// Process the Encoder specific keyword arguments
368
+ // Get referred Pin object(s) and connect them to the encoder
369
+ if (args [ARG_phase_a ].u_obj != mp_const_none ) {
370
+ self -> input_a = connect_pin_to_encoder (args [ARG_phase_a ].u_obj , xbar_signal_table [self -> id ].enc_input_a , XBAR_IN );
371
+ }
372
+ if (args [ARG_phase_b ].u_obj != mp_const_none ) {
373
+ self -> input_b = connect_pin_to_encoder (args [ARG_phase_b ].u_obj , xbar_signal_table [self -> id ].enc_input_b , XBAR_IN );
374
+ }
375
+ // Check for valid input pins
376
+ if (self -> input_a == 0 || self -> input_b == 0 || self -> input_a == self -> input_b ) {
377
+ mp_raise_ValueError (MP_ERROR_TEXT ("invalid or missing input pins" ));
378
+ }
379
+
363
380
// Check for a Home pin, resetting the counters
364
381
if (args [ARG_home ].u_obj != MP_ROM_INT (-1 )) {
365
382
if (args [ARG_home ].u_obj != mp_const_none ) {
@@ -383,7 +400,7 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
383
400
// Qencoder(id, input_a, input_b, [args])
384
401
STATIC mp_obj_t mp_machine_qencd_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
385
402
// Check number of arguments
386
- mp_arg_check_num (n_args , n_kw , 3 , MP_OBJ_FUN_ARGS_MAX , true);
403
+ mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
387
404
388
405
CLOCK_EnableClock (kCLOCK_Iomuxc ); // just in case it was not set yet
389
406
XBARA_Init (XBARA1 );
@@ -397,21 +414,15 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
397
414
qenc_deinit_single (qencd_table [id ]);
398
415
}
399
416
400
- // Get referred Pin object(s) and connect them to the encoder
401
- uint8_t input_a = connect_pin_to_encoder (args [1 ], xbar_signal_table [id ].enc_input_a , XBAR_IN );
402
- uint8_t input_b = connect_pin_to_encoder (args [2 ], xbar_signal_table [id ].enc_input_b , XBAR_IN );
403
- // Check for valid input pins
404
- if (input_a == input_b ) {
405
- mp_raise_ValueError (MP_ERROR_TEXT ("input pins must be diffrent" ));
406
- }
407
-
408
417
// Connect the trigger input to low level
409
418
XBARA_SetSignalsConnection (XBARA1 , kXBARA1_InputLogicLow , xbar_signal_table [id ].enc_trigger );
410
419
411
420
// Create and populate the Qencoder object.
412
421
machine_qencd_obj_t * self = m_new_obj (machine_qencd_obj_t );
413
422
qencd_table [id ] = self ;
414
423
self -> id = id ;
424
+ self -> input_a = 0 ;
425
+ self -> input_b = 0 ;
415
426
self -> base .type = & machine_qencd_type ;
416
427
self -> instance = enc_instances [id + 1 ];
417
428
self -> cpc = 0 ;
@@ -429,7 +440,7 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
429
440
// Process the remaining parameters
430
441
mp_map_t kw_args ;
431
442
mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
432
- mp_machine_qencd_init_helper (self , n_args - 3 , args + 3 , & kw_args );
443
+ mp_machine_qencd_init_helper (self , n_args - 1 , args + 1 , & kw_args );
433
444
434
445
return MP_OBJ_FROM_PTR (self );
435
446
}
@@ -592,8 +603,9 @@ const mp_obj_type_t machine_qencd_type = {
592
603
593
604
STATIC void mp_machine_counter_init_helper (machine_qencd_obj_t * self ,
594
605
size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
595
- enum { ARG_direction , ARG_match , ARG_filter_ns , ARG_cpc , ARG_compare , ARG_signed , ARG_index };
606
+ enum { ARG_src , ARG_direction , ARG_match , ARG_filter_ns , ARG_cpc , ARG_compare , ARG_signed , ARG_index };
596
607
static const mp_arg_t allowed_args [] = {
608
+ { MP_QSTR_src , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
597
609
{ MP_QSTR_direction , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
598
610
{ MP_QSTR_match , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
599
611
{ MP_QSTR_filter_ns , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
@@ -607,6 +619,13 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
607
619
mp_arg_parse_all (n_args , pos_args , kw_args ,
608
620
MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
609
621
622
+ if (args [ARG_src ].u_obj != mp_const_none ) {
623
+ self -> input_a = connect_pin_to_encoder (args [ARG_src ].u_obj , xbar_signal_table [self -> id ].enc_input_a , XBAR_IN );
624
+ }
625
+ if (self -> input_a == 0 ) {
626
+ mp_raise_ValueError (MP_ERROR_TEXT ("missing input pin" ));
627
+ }
628
+
610
629
mp_obj_t direction = args [ARG_direction ].u_obj ;
611
630
if (direction != MP_ROM_INT (-1 )) {
612
631
if (direction == MP_ROM_INT (COUNTER_UP )) {
@@ -625,7 +644,7 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
625
644
// Counter(id, input, [args])
626
645
STATIC mp_obj_t mp_machine_counter_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
627
646
// Check number of arguments
628
- mp_arg_check_num (n_args , n_kw , 2 , MP_OBJ_FUN_ARGS_MAX , true);
647
+ mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
629
648
630
649
CLOCK_EnableClock (kCLOCK_Iomuxc ); // just in case it was not set yet
631
650
XBARA_Init (XBARA1 );
@@ -639,9 +658,6 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
639
658
qenc_deinit_single (qencd_table [id ]);
640
659
}
641
660
642
- // Get input Pin object and connect it to the encoder
643
- connect_pin_to_encoder (args [1 ], xbar_signal_table [id ].enc_input_a , XBAR_IN );
644
-
645
661
// Connect input_b and the trigger input to a fixed level.
646
662
XBARA_SetSignalsConnection (XBARA1 , kXBARA1_InputLogicLow , xbar_signal_table [id ].enc_input_b );
647
663
XBARA_SetSignalsConnection (XBARA1 , kXBARA1_InputLogicLow , xbar_signal_table [id ].enc_trigger );
@@ -650,6 +666,8 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
650
666
machine_qencd_obj_t * self = m_new_obj (machine_qencd_obj_t );
651
667
qencd_table [id ] = self ;
652
668
self -> id = id ;
669
+ self -> input_a = 0 ;
670
+ self -> input_b = 0 ;
653
671
self -> base .type = & machine_counter_type ;
654
672
self -> instance = enc_instances [id + 1 ];
655
673
self -> cpc = 0 ;
@@ -670,7 +688,7 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
670
688
// Process the remaining parameters
671
689
mp_map_t kw_args ;
672
690
mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
673
- mp_machine_counter_init_helper (self , n_args - 2 , args + 2 , & kw_args );
691
+ mp_machine_counter_init_helper (self , n_args - 1 , args + 1 , & kw_args );
674
692
675
693
return MP_OBJ_FROM_PTR (self );
676
694
}
0 commit comments