@@ -61,6 +61,7 @@ def const(x):
61
61
_EEPROM_BASE = const (0x0D )
62
62
_NEOPIXEL_BASE = const (0x0E )
63
63
_TOUCH_BASE = const (0x0F )
64
+ _ENCODER_BASE = const (0x11 )
64
65
65
66
_GPIO_DIRSET_BULK = const (0x02 )
66
67
_GPIO_DIRCLR_BULK = const (0x03 )
@@ -109,6 +110,12 @@ def const(x):
109
110
_HW_ID_CODE = const (0x55 )
110
111
_EEPROM_I2C_ADDR = const (0x3F )
111
112
113
+ _ENCODER_STATUS = const (0x00 )
114
+ _ENCODER_INTENSET = const (0x10 )
115
+ _ENCODER_INTENCLR = const (0x20 )
116
+ _ENCODER_POSITION = const (0x30 )
117
+ _ENCODER_DELTA = const (0x40 )
118
+
112
119
# TODO: update when we get real PID
113
120
_CRICKIT_PID = const (9999 )
114
121
_ROBOHATMM1_PID = const (9998 )
@@ -362,6 +369,31 @@ def set_pwm_freq(self, pin, freq):
362
369
else :
363
370
raise ValueError ("Invalid PWM pin" )
364
371
372
+ def get_encoder_pos (self , encoder = 0 ):
373
+ """Read the current position of the encoder"""
374
+ buf = bytearray (4 )
375
+ self .read (_ENCODER_BASE , _ENCODER_POSITION + encoder , buf )
376
+ return struct .unpack (">i" , buf )[0 ]
377
+
378
+ def set_encoder_pos (self , pos , encoder = 0 ):
379
+ """Set the current position of the encoder"""
380
+ cmd = struct .pack (">i" , pos )
381
+ self .write (_ENCODER_BASE , _ENCODER_POSITION + encoder , cmd )
382
+
383
+ def get_encoder_delta (self , encoder = 0 ):
384
+ """Read the change in encoder position since it was last read"""
385
+ buf = bytearray (4 )
386
+ self .read (_ENCODER_BASE , _ENCODER_DELTA + encoder , buf )
387
+ return struct .unpack (">i" , buf )[0 ]
388
+
389
+ def enable_encoder_interrupt (self , encoder = 0 ):
390
+ """Enable the interrupt to fire when the encoder changes position"""
391
+ self .write8 (_ENCODER_BASE , _ENCODER_INTENSET + encoder , 0x01 )
392
+
393
+ def disable_encoder_interrupt (self , encoder = 0 ):
394
+ """Disable the interrupt from firing when the encoder changes"""
395
+ self .write8 (_ENCODER_BASE , _ENCODER_INTENCLR + encoder , 0x01 )
396
+
365
397
# def enable_sercom_data_rdy_interrupt(self, sercom):
366
398
#
367
399
# _sercom_inten.DATA_RDY = 1
0 commit comments