@@ -49,6 +49,7 @@ typedef struct _machine_encoder_obj_t {
49
49
uint8_t mode ;
50
50
bool is_signed ;
51
51
uint8_t match_pin ;
52
+ uint8_t phases_inv ;
52
53
uint32_t cpc ;
53
54
uint32_t filter ;
54
55
uint16_t status ;
@@ -403,7 +404,7 @@ static void mp_machine_encoder_init_helper_common(machine_encoder_obj_t *self,
403
404
static void mp_machine_encoder_init_helper (machine_encoder_obj_t * self ,
404
405
size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
405
406
enum { ARG_phase_a , ARG_phase_b , ARG_home ,
406
- ARG_match_pin , ARG_filter_ns , ARG_cpc , ARG_signed , ARG_index };
407
+ ARG_match_pin , ARG_filter_ns , ARG_cpc , ARG_signed , ARG_index , ARG_phases };
407
408
408
409
static const mp_arg_t allowed_args [] = {
409
410
{ MP_QSTR_phase_a , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
@@ -414,6 +415,7 @@ static void mp_machine_encoder_init_helper(machine_encoder_obj_t *self,
414
415
{ MP_QSTR_cpc , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
415
416
{ MP_QSTR_signed , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
416
417
{ MP_QSTR_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
418
+ { MP_QSTR_phases , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 4 } },
417
419
};
418
420
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
419
421
mp_arg_parse_all (n_args , pos_args , kw_args ,
@@ -442,6 +444,12 @@ static void mp_machine_encoder_init_helper(machine_encoder_obj_t *self,
442
444
}
443
445
}
444
446
447
+ // Get the Phases argument
448
+ if ((args [ARG_phases ].u_int != 1 ) && (args [ARG_phases ].u_int != 2 ) && (args [ARG_phases ].u_int != 4 )) {
449
+ mp_raise_ValueError (MP_ERROR_TEXT ("invalid value for phases" ));
450
+ }
451
+ self -> phases_inv = 4 / args [ARG_phases ].u_int ;
452
+
445
453
// Set the common options
446
454
mp_machine_encoder_init_helper_common (self , args + ARG_match_pin , & self -> enc_config );
447
455
@@ -552,7 +560,7 @@ static mp_obj_t machine_encoder_value(size_t n_args, const mp_obj_t *args) {
552
560
uint32_t actual_value = ENC_GetPositionValue (self -> instance );
553
561
if (n_args > 1 ) {
554
562
// Set the encoder position value and clear the rev counter.
555
- uint32_t value = mp_obj_int_get_truncated (args [1 ]);
563
+ uint32_t value = mp_obj_int_get_truncated (args [1 ]) * self -> phases_inv ;
556
564
clear_encoder_registers (self );
557
565
// Set the position and rev register
558
566
ENC_SetInitialPositionValue (self -> instance , value );
@@ -562,9 +570,13 @@ static mp_obj_t machine_encoder_value(size_t n_args, const mp_obj_t *args) {
562
570
}
563
571
// Get the position as signed or unsigned 32 bit value.
564
572
if (self -> is_signed ) {
565
- return mp_obj_new_int ((int32_t )actual_value );
573
+ int32_t value = (int32_t )actual_value ;
574
+ if (value < 0 ) {
575
+ value -= (self -> phases_inv - 1 );
576
+ }
577
+ return mp_obj_new_int (value / self -> phases_inv );
566
578
} else {
567
- return mp_obj_new_int_from_uint (actual_value );
579
+ return mp_obj_new_int_from_uint (actual_value / self -> phases_inv );
568
580
}
569
581
}
570
582
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_encoder_value_obj , 1 , 2 , machine_encoder_value ) ;
@@ -751,6 +763,7 @@ static mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
751
763
self -> irq = NULL ;
752
764
self -> match_pin = 0 ;
753
765
self -> is_signed = true;
766
+ self -> phases_inv = 1 ;
754
767
self -> mode = MODE_COUNTER ;
755
768
756
769
// Set defaults for ENC Config
0 commit comments