tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/encoders/quadrature_device.go (about) 1 package encoders 2 3 type QuadratureDevice struct { 4 cfg QuadratureConfig 5 impl quadratureImpl 6 } 7 8 type QuadratureConfig struct { 9 Precision int 10 } 11 12 type quadratureImpl interface { 13 configure(cfg QuadratureConfig) error 14 readValue() int 15 writeValue(int) 16 } 17 18 func (enc *QuadratureDevice) Configure(cfg QuadratureConfig) error { 19 if cfg.Precision < 1 { 20 cfg.Precision = 4 21 } 22 enc.cfg = cfg 23 return enc.impl.configure(cfg) 24 } 25 26 // Position returns the stored int value for the encoder 27 func (enc *QuadratureDevice) Position() int { 28 return enc.impl.readValue() / enc.cfg.Precision 29 } 30 31 // SetPosition overwrites the currently stored value with the specified int value 32 func (enc *QuadratureDevice) SetPosition(v int) { 33 enc.impl.writeValue(v * enc.cfg.Precision) 34 }