37 private Object m_lock =
new Object();
83 private Device m_device =
null;
84 private int m_encoderNo = -1;
85 private int m_zeroPosition = 0;
86 private int m_i2cAddr = 0;
87 private Processor m_processor;
88 private boolean m_group =
false;
90 private int m_range = 0;
92 private Encoder m_encoder =
null;
94 private void getIntCallback(ByteBuffer buffer, Object
object) {
97 Encoder encoder = (Encoder)
object;
98 int pos = buffer.getInt();
100 synchronized (m_lock) {
101 encoder.m_lastPos = pos;
105 public EncoderType getType() {
110 void groupCallback(ByteBuffer data, Object
object) {
111 Encoder encoder = (Encoder)
object;
112 byte pos = data.get();
117 m_device.getInt(m_i2cAddr, TwoWire.k_getEncoderPos, (
byte) encoder.m_encoderNo,
118 (b, o) -> getIntCallback(b, o), encoder);
120 synchronized (m_lock) {
121 encoder.m_lastPos += pos;
126 private void verifyPin(
int pin, Device.PinType type) {
127 if (!m_device.isPinValid(m_i2cAddr, pin, type)) {
128 throw new TwoWire.HardwareException(
129 String.format(
"Encoder: Invalid %s pin: %d", type.toString(), pin));
135 if (m_device.checkUsedPin(m_i2cAddr, pin)) {
136 throw new TwoWire.HardwareException(String.format(
"Encoder: Pin %d is in use", pin));
140 private void init(
EncoderType type,
int pin1,
int pin2,
int i2cAddr,
boolean group) {
142 throw new TwoWire.HardwareException(String.format(
"Encoder: The two pins must be different"));
145 if (type ==
EncoderType.QuadraturePosition && Device.m_boardSubVersion == 0) {
146 throw new TwoWire.HardwareException(
"The hardware does not support positional quadrature encoders, update firmware");
149 m_device = Device.getInstance();
151 m_processor = m_device.getProcessor(i2cAddr);
161 m_range = m_device.getEncoderRange(m_i2cAddr);
169 m_device.setUsedPin(m_i2cAddr, pin1);
174 m_device.setUsedPin(m_i2cAddr, pin2);
178 m_encoderNo = m_processor.getNextEncoderNo();
180 m_device.sendCommand(m_i2cAddr, TwoWire.k_configureEncoder, (
byte) m_encoderNo, (
byte) type.ordinal(),
181 (
byte) pin1, (
byte) pin2);
184 m_processor.addGroupRequest(RequestType.EncoderRelativePosition, m_encoderNo, (d, c) -> groupCallback(d, c),
186 Device.startGroupRequests();
203 init(type, pin1, pin2, i2cAddr,
false);
220 init(type, pin1, pin2, i2cAddr, group);
231 init(type, pin1, pin2,
Device.k_mainTwoWireId,
false);
247 init(type, pin1, pin2,
Device.k_mainTwoWireId, group);
262 public int getEncoderNo() {
263 return (m_encoderNo);
279 private int getRawPosition() {
280 if (m_encoder !=
null) {
281 return (m_encoder.getRawPosition());
285 m_lastPos = m_device.getInt(m_i2cAddr, TwoWire.k_getEncoderPos, (
byte) m_encoderNo);
288 synchronized (m_lock) {
299 int pos = getRawPosition() - m_zeroPosition;
317 private int getSpeedTW() {
318 if (m_encoder !=
null) {
322 return (m_device.getShort(m_i2cAddr, TwoWire.k_getEncoderSpeed, (
byte) m_encoderNo));
329 m_zeroPosition = getRawPosition();
339 m_zeroPosition = getRawPosition() - position;
349 m_zeroPosition = zero;
352 public int getZero() {
353 return m_zeroPosition;
361 Logger.
log(
"Encoder", 1, String.format(
"setInverted(%b)", invert));
362 m_device.sendCommand(m_i2cAddr,
TwoWire.k_setEncoderInverted, (
byte) m_encoderNo, (
byte) (invert ? 1 : 0));