@@ -43,7 +43,7 @@ typedef struct _machine_qencd_obj_t {
43
43
ENC_Type * instance ;
44
44
int8_t id ;
45
45
uint8_t mode ;
46
- bool home_trigger_mode ;
46
+ bool is_signed ;
47
47
uint8_t match_pin ;
48
48
uint32_t cpr ;
49
49
uint32_t compare_pos ;
@@ -295,7 +295,7 @@ STATIC uint32_t calc_filter(uint32_t filter_ns, uint16_t *count, uint16_t *perio
295
295
//
296
296
STATIC void mp_machine_qencd_init_helper (machine_qencd_obj_t * self ,
297
297
size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
298
- enum { ARG_index , ARG_home , ARG_match , ARG_filter , ARG_reverse , ARG_cpr , ARG_compare };
298
+ enum { ARG_index , ARG_home , ARG_match , ARG_filter , ARG_reverse , ARG_cpr , ARG_compare , ARG_signed };
299
299
static const mp_arg_t allowed_args [] = {
300
300
{ MP_QSTR_index , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
301
301
{ MP_QSTR_home , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
@@ -304,6 +304,7 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
304
304
{ MP_QSTR_reverse , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
305
305
{ MP_QSTR_cpr , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
306
306
{ MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
307
+ { MP_QSTR_signed , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
307
308
};
308
309
enc_config_t enc_config ;
309
310
@@ -321,6 +322,10 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
321
322
enc_config .enableReverseDirection = true;
322
323
}
323
324
325
+ if (args [ARG_signed ].u_int != 0 ) {
326
+ self -> is_signed = true;
327
+ }
328
+
324
329
if (args [ARG_cpr ].u_obj != mp_const_none ) {
325
330
uint32_t cpr = mp_get_ll_int (args [ARG_cpr ].u_obj );
326
331
self -> cpr = cpr ;
@@ -350,7 +355,6 @@ STATIC void mp_machine_qencd_init_helper(machine_qencd_obj_t *self,
350
355
connect_pin_to_encoder (home , xbar_ouput_table [self -> id ].enc_home , XBAR_IN );
351
356
enc_config .HOMETriggerMode = kENC_HOMETriggerOnRisingEdge ;
352
357
}
353
- self -> home_trigger_mode = enc_config .HOMETriggerMode ;
354
358
355
359
// Check for a Match pin for the compare match signal
356
360
mp_obj_t match = args [ARG_match ].u_obj ;
@@ -405,6 +409,7 @@ STATIC mp_obj_t mp_machine_qencd_make_new(const mp_obj_type_t *type, size_t n_ar
405
409
self -> status = 0 ;
406
410
self -> irq = NULL ;
407
411
self -> match_pin = 0 ;
412
+ self -> is_signed = false;
408
413
self -> mode = MODE_ENCODER ;
409
414
410
415
// Process the remaining parameters
@@ -456,7 +461,11 @@ STATIC mp_obj_t machine_qencd_value(size_t n_args, const mp_obj_t *args) {
456
461
machine_qencd_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
457
462
if (n_args == 1 ) {
458
463
// Get the counter or position as unsigned 32 bit value.
459
- return mp_obj_new_int_from_uint (ENC_GetPositionValue (self -> instance ));
464
+ if (self -> is_signed ) {
465
+ return mp_obj_new_int ((int32_t )ENC_GetPositionValue (self -> instance ));
466
+ } else {
467
+ return mp_obj_new_int_from_uint (ENC_GetPositionValue (self -> instance ));
468
+ }
460
469
} else {
461
470
// Set the encoder position value and clear the rev counter.
462
471
uint64_t value = mp_get_ll_int (args [1 ]);
@@ -574,12 +583,13 @@ const mp_obj_type_t machine_qencd_type = {
574
583
575
584
STATIC void mp_machine_counter_init_helper (machine_qencd_obj_t * self ,
576
585
size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
577
- enum { ARG_direction , ARG_match , ARG_filter , ARG_compare };
586
+ enum { ARG_direction , ARG_match , ARG_filter , ARG_compare , ARG_signed };
578
587
static const mp_arg_t allowed_args [] = {
579
588
{ MP_QSTR_direction , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
580
589
{ MP_QSTR_match , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_INT (-1 )} },
581
590
{ MP_QSTR_filter , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
582
591
{ MP_QSTR_compare , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
592
+ { MP_QSTR_signed , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = false} },
583
593
};
584
594
enc_config_t enc_config ;
585
595
@@ -624,6 +634,10 @@ STATIC void mp_machine_counter_init_helper(machine_qencd_obj_t *self,
624
634
& enc_config .filterCount , & enc_config .filterSamplePeriod );
625
635
}
626
636
637
+ if (args [ARG_signed ].u_int != 0 ) {
638
+ self -> is_signed = true;
639
+ }
640
+
627
641
// Initialize the ENC module.
628
642
ENC_Init (self -> instance , & enc_config );
629
643
clear_encoder_registers (self );
@@ -659,6 +673,7 @@ STATIC mp_obj_t mp_machine_counter_make_new(const mp_obj_type_t *type, size_t n_
659
673
self -> status = 0 ;
660
674
self -> irq = NULL ;
661
675
self -> match_pin = 0 ;
676
+ self -> is_signed = false;
662
677
self -> mode = MODE_COUNTER ;
663
678
664
679
// Process the remaining parameters
0 commit comments