Skip to content

Commit d1e7eb5

Browse files
committed
Use sysex responses instead of MIDI
1 parent e63bc82 commit d1e7eb5

File tree

4 files changed

+43
-111
lines changed

4 files changed

+43
-111
lines changed

examples/SimpleEncoderFirmata/SimpleEncoderFirmata.ino

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -26,90 +26,6 @@
2626
2727
formatted using the GNU C formatting and indenting
2828
29-
* =====================================================
30-
* Encoder PROTOCOL
31-
* =====================================================
32-
*
33-
* Attach encoder query
34-
* -----------------------------------------------------
35-
* 0 START_SYSEX (0xF0)
36-
* 1 ENCODER_DATA (0x61)
37-
* 2 ENCODER_ATTACH (0x00)
38-
* 3 encoder # ([0 - MAX_ENCODERS-1])
39-
* 4 pin A # (first pin)
40-
* 5 pin B # (second pin)
41-
* 6 END_SYSEX (0xF7)
42-
* -----------------------------------------------------
43-
*
44-
* Report encoder's position
45-
* -----------------------------------------------------
46-
* Sysex Query
47-
* ------------------
48-
* 0 START_SYSEX (0xF0)
49-
* 1 ENCODER_DATA (0x61)
50-
* 2 ENCODER_REPORT_POSITION (0x01)
51-
* 3 encoder # ([0 - MAX_ENCODERS-1])
52-
* 4 END_SYSEX (0xF7)
53-
*--------------------
54-
* MIDI Response
55-
* ------------------
56-
* 0 ENCODER_MESSAGE (0x80) | Channel (encoder #, 0-4)
57-
* 1 direction (positive = 0, negative = 1)
58-
* 2 current position, bits 0-6
59-
* 3 current position, bits 7-13
60-
* 4 current position, bits 14-20
61-
* 5 current position, bits 21-27
62-
* -----------------------------------------------------
63-
*
64-
* Report all encoders positions
65-
* -----------------------------------------------------
66-
* Sysex Query
67-
* ------------------
68-
* 0 START_SYSEX (0xF0)
69-
* 1 ENCODER_DATA (0x61)
70-
* 2 ENCODER_REPORT_POSITIONS (0x02)
71-
* 3 END_SYSEX (0xF7)
72-
*--------------------
73-
* Sysex Response
74-
* ------------------
75-
* 0 START_SYSEX (0xF0)
76-
* 1 ENCODER_DATA (0x61)
77-
* 2 first encoder # ([0 - MAX_ENCODERS-1])
78-
* 3 first enc. dir. (positive = 0, negative = 1)
79-
* 4 first enc. position, bits 0-6
80-
* 5 first enc. position, bits 7-13
81-
* 6 first enc. position, bits 14-20
82-
* 7 first enc. position, bits 21-27
83-
* ...
84-
* N END_SYSEX (0xF7)
85-
* -----------------------------------------------------
86-
*
87-
* Reset encoder position to zero (Sysex Query)
88-
* -----------------------------------------------------
89-
* 0 START_SYSEX (0xF0)
90-
* 1 ENCODER_DATA (0x61)
91-
* 2 ENCODER_RESET_POSITION (0x03)
92-
* 3 encoder # ([0 - MAX_ENCODERS-1])
93-
* 4 END_SYSEX (0xF7)
94-
* -----------------------------------------------------
95-
*
96-
* Enable/disable reporting (Sysex Query)
97-
* -----------------------------------------------------
98-
* 0 START_SYSEX (0xF0)
99-
* 1 ENCODER_DATA (0x61)
100-
* 2 ENCODER_REPORT_AUTO (0x04)
101-
* 3 enable (0x00 => false, true otherwise)
102-
* 4 END_SYSEX (0xF7)
103-
* -----------------------------------------------------
104-
*
105-
* Detach encoder (Sysex Query)
106-
* -----------------------------------------------------
107-
* 0 START_SYSEX (0xF0)
108-
* 1 ENCODER_DATA (0x61)
109-
* 2 ENCODER_DETACH (0x05)
110-
* 3 encoder # ([0 - MAX_ENCODERS-1])
111-
* 4 END_SYSEX (0xF7)
112-
*--------------------
11329
*/
11430

11531
//* Due to arduino issue, Wire and Servo libraries need to be included. Should be fixed in later versions

test/encoder_test/encoder_test.ino

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,20 @@ test(reportEncoderPosition)
101101
assertTestPass(attachEncoder);
102102

103103
EncoderFirmata encoder;
104-
byte encoderNum = 0, pin1 = 2, pin2 = 3;
104+
byte encoderNum = 1, pin1 = 2, pin2 = 3;
105105
encoder.attachEncoder(encoderNum, pin1, pin2);
106106

107107
stream.flush();
108108
encoder.reportPosition(encoderNum);
109-
assertEqual(stream.bytesWritten().length(), 6);
109+
assertEqual(stream.bytesWritten().length(), 8);
110+
assertEqual(stream.bytesWritten()[0], 0xF0);
111+
assertEqual(stream.bytesWritten()[1], 0x61);
112+
assertEqual(stream.bytesWritten()[2], 0x01); // dir = 0, channel=1
113+
assertEqual(stream.bytesWritten()[3], 0x00); // position = 0
114+
assertEqual(stream.bytesWritten()[4], 0x00); // position = 0
115+
assertEqual(stream.bytesWritten()[5], 0x00); // position = 0
116+
assertEqual(stream.bytesWritten()[6], 0x00); // position = 0
117+
assertEqual(stream.bytesWritten()[7], 0xF7);
110118
}
111119

112120
test(handeReportEncoderPositionMessage)
@@ -190,12 +198,18 @@ test(fullReport)
190198
assertEqual(stream.bytesWritten().length(), 0); // no encoder attached
191199

192200

193-
byte encoderNum = 0, pin1 = 2, pin2 = 3;
194-
encoder.attachEncoder(encoderNum, pin1, pin2);
201+
byte pin1 = 2, pin2 = 3;
202+
encoder.attachEncoder(0, pin1, pin2);
203+
204+
stream.flush();
205+
encoder.report();
206+
assertEqual(stream.bytesWritten().length(), 8); // 1 encoder attached
207+
208+
encoder.attachEncoder(1, pin1, pin2);
195209

196210
stream.flush();
197211
encoder.report();
198-
assertEqual(stream.bytesWritten().length(), 6); // 1 encoder attached
212+
assertEqual(stream.bytesWritten().length(), 13); // 2 encoder attached
199213
}
200214

201215
test(resetEncoder)

utility/EncoderFirmata.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,16 @@ void EncoderFirmata::resetPosition(byte encoderNum)
192192
}
193193

194194
// Report specify encoder postion using midi protocol
195-
void EncoderFirmata::reportPosition(byte encoderNum)
195+
void EncoderFirmata::reportPosition(byte encoder)
196196
{
197-
if (isEncoderAttached(encoderNum))
197+
if (isEncoderAttached(encoder))
198198
{
199-
signed long position = encoders[encoderNum]->read();
200-
long absValue = abs(position);
201-
Firmata.write(ENCODER_MESSAGE | (encoderNum & 0xF));
202-
Firmata.write(position >= 0 ? 0x00 : 0x01);
203-
Firmata.write((byte)absValue & 0x7F);
204-
Firmata.write((byte)(absValue >> 7) & 0x7F);
205-
Firmata.write((byte)(absValue >> 14) & 0x7F);
206-
Firmata.write((byte)(absValue >> 21) & 0x7F);
199+
Firmata.write(START_SYSEX);
200+
Firmata.write(ENCODER_DATA);
201+
202+
_reportEncoderPosition(encoder);
203+
204+
Firmata.write(END_SYSEX);
207205
}
208206
}
209207
// Report all attached encoders positions (one message for all encoders)
@@ -214,20 +212,24 @@ void EncoderFirmata::reportPositions()
214212
byte encoder;
215213
for(encoder=0; encoder<MAX_ENCODERS; encoder++)
216214
{
217-
if (isEncoderAttached(encoder))
218-
{
219-
signed long position = encoders[encoder]->read();
220-
long absValue = abs(position);
221-
Firmata.write(encoder);
222-
Firmata.write(position >= 0 ? 0x00 : 0x01);
223-
Firmata.write((byte)absValue & 0x7F);
224-
Firmata.write((byte)(absValue >> 7) & 0x7F);
225-
Firmata.write((byte)(absValue >> 14) & 0x7F);
226-
Firmata.write((byte)(absValue >> 21) & 0x7F);
227-
}
215+
_reportEncoderPosition(encoder);
228216
}
229217
Firmata.write(END_SYSEX);
230218
}
219+
void EncoderFirmata::_reportEncoderPosition(byte encoder)
220+
{
221+
if (isEncoderAttached(encoder))
222+
{
223+
signed long position = encoders[encoder]->read();
224+
long absValue = abs(position);
225+
byte direction = position >= 0 ? 0x00 : 0x01;
226+
Firmata.write((direction << 7) | (encoder));
227+
Firmata.write((byte)absValue & 0x7F);
228+
Firmata.write((byte)(absValue >> 7) & 0x7F);
229+
Firmata.write((byte)(absValue >> 14) & 0x7F);
230+
Firmata.write((byte)(absValue >> 21) & 0x7F);
231+
}
232+
}
231233

232234

233235
void EncoderFirmata::toggleAutoReport(bool report)

utility/EncoderFirmata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#define ENCODER_REPORT_AUTO (0x04)
4242
#define ENCODER_DETACH (0x05)
4343
#define ENCODER_DATA (0x61) // TODO : Move to Firmata.h
44-
#define ENCODER_MESSAGE (0x80) // TODO : Move to Firmata.h
4544

4645
class EncoderFirmata:public FirmataFeature
4746
{
@@ -68,6 +67,7 @@ class EncoderFirmata:public FirmataFeature
6867

6968
private:
7069
Encoder *encoders[MAX_ENCODERS];
70+
void _reportEncoderPosition(byte encoder);
7171
volatile bool autoReport;
7272
};
7373

0 commit comments

Comments
 (0)