@@ -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 ;
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 = 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 ]) * ( 4 / self -> phases ) ;
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 - 1 );
576
+ }
577
+ return mp_obj_new_int (value / self -> phases );
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 );
568
580
}
569
581
}
570
582
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_encoder_value_obj , 1 , 2 , machine_encoder_value ) ;
@@ -776,9 +788,36 @@ static mp_obj_t machine_counter_init(size_t n_args, const mp_obj_t *args, mp_map
776
788
}
777
789
MP_DEFINE_CONST_FUN_OBJ_KW (machine_counter_init_obj , 1 , machine_counter_init );
778
790
791
+
792
+ // counter.value([value])
793
+ static mp_obj_t machine_counter_value (size_t n_args , const mp_obj_t * args ) {
794
+ machine_encoder_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
795
+ if (!self -> active ) {
796
+ mp_raise_ValueError (MP_ERROR_TEXT ("device stopped" ));
797
+ }
798
+ uint32_t actual_value = ENC_GetPositionValue (self -> instance );
799
+ if (n_args > 1 ) {
800
+ // Set the encoder position value and clear the rev counter.
801
+ uint32_t value = mp_obj_int_get_truncated (args [1 ]);
802
+ clear_encoder_registers (self );
803
+ // Set the position and rev register
804
+ ENC_SetInitialPositionValue (self -> instance , value );
805
+ ENC_DoSoftwareLoadInitialPositionValue (self -> instance );
806
+ // Reset the INIT Value
807
+ ENC_SetInitialPositionValue (self -> instance , 0 );
808
+ }
809
+ // Get the position as signed or unsigned 32 bit value.
810
+ if (self -> is_signed ) {
811
+ return mp_obj_new_int ((int32_t )actual_value );
812
+ } else {
813
+ return mp_obj_new_int_from_uint (actual_value );
814
+ }
815
+ }
816
+ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_counter_value_obj , 1 , 2 , machine_counter_value ) ;
817
+
779
818
static const mp_rom_map_elem_t machine_counter_locals_dict_table [] = {
780
819
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& machine_encoder_deinit_obj ) },
781
- { MP_ROM_QSTR (MP_QSTR_value ), MP_ROM_PTR (& machine_encoder_value_obj ) },
820
+ { MP_ROM_QSTR (MP_QSTR_value ), MP_ROM_PTR (& machine_counter_value_obj ) },
782
821
{ MP_ROM_QSTR (MP_QSTR_cycles ), MP_ROM_PTR (& machine_encoder_cycles_obj ) },
783
822
{ MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_counter_init_obj ) },
784
823
{ MP_ROM_QSTR (MP_QSTR_irq ), MP_ROM_PTR (& machine_encoder_irq_obj ) },
0 commit comments