File Coverage

blib/lib/Device/Firmata/Constants.pm
Criterion Covered Total %
statement 26 26 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 34 34 100.0


line stmt bran cond sub pod time code
1             package Device::Firmata::Constants;
2              
3             =head1 NAME
4              
5             Device::Firmata::Constants - constants used in the Device::Firmata system
6              
7             =cut
8              
9 1     1   7 use strict;
  1         8  
  1         29  
10 1     1   6 use warnings;
  1         6  
  1         23  
11 1     1   5 use Exporter;
  1         6  
  1         59  
12 1         164 use vars qw/
13             @ISA @EXPORT_OK %EXPORT_TAGS
14             $BASE
15             $COMMANDS $COMMAND_NAMES
16             $COMMAND_LOOKUP
17 1     1   8 /;
  1         1  
18            
19             @ISA = 'Exporter';
20              
21             # Basic commands and constants
22             use constant (
23 1         685 $BASE = {
24             PIN_INPUT => 0,
25             PIN_OUTPUT => 1,
26             PIN_ANALOG => 2,
27             PIN_PWM => 3,
28             PIN_SERVO => 4,
29             PIN_SHIFT => 5,
30             PIN_I2C => 6,
31             PIN_ONEWIRE => 7,
32             PIN_STEPPER => 8,
33             PIN_ENCODER => 9,
34             PIN_SERIAL => 10,
35             PIN_PULLUP => 11,
36             PIN_LOW => 0,
37             PIN_HIGH => 1,
38             }
39 1     1   8 );
  1         2  
40              
41             # We need to apply all the available protocols
42             use constant (
43 1         387 $COMMANDS = {
44              
45             V_2_01 => {
46              
47             MAX_DATA_BYTES => 32, # max number of data bytes in non-Sysex messages
48              
49             # message command bytes (128-255/0x80-0xFF)
50             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
51             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
52             REPORT_ANALOG => 0xC0, # enable analog input by pin #
53             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
54             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
55             REPORT_VERSION => 0xF9, # report protocol version
56             SYSTEM_RESET => 0xFF, # reset from MIDI
57             START_SYSEX => 0xF0, # start a MIDI Sysex message
58             END_SYSEX => 0xF7, # end a MIDI Sysex message
59              
60             # extended command set using sysex (0-127/0x00-0x7F)
61             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
62             STRING_DATA => 0x71, # a string message with 14-bits per char
63             SHIFT_DATA => 0x75, # a bitstream to/from a shift register
64             I2C_REQUEST => 0x76, # send an I2C read/write request
65             I2C_REPLY => 0x77, # a reply to an I2C read request
66             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
67             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
68             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
69             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
70             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
71              
72             # these are DEPRECATED to make the naming more consistent
73             FIRMATA_STRING => 0x71, # same as STRING_DATA
74             SYSEX_I2C_REQUEST => 0x76, # same as I2C_REQUEST
75             SYSEX_I2C_REPLY => 0x77, # same as I2C_REPLY
76             SYSEX_SAMPLING_INTERVAL => 0x7A, # same as SAMPLING_INTERVAL
77              
78             # pin modes
79             INPUT => 0x00, # digital pin in digitalOut mode
80             OUTPUT => 0x01, # digital pin in digitalInput mode
81             ANALOG => 0x02, # analog pin in analogInput mode
82             PWM => 0x03, # digital pin in PWM output mode
83             SERVO => 0x04, # digital pin in Servo output mode
84             SHIFT => 0x05, # shiftIn/shiftOut mode
85             I2C => 0x06, # pin included in I2C setup
86              
87             # Deprecated entries
88             deprecated => [
89             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
90             ],
91             }, # /Constants for Version 2.1
92              
93             V_2_02 => {
94              
95             MAX_DATA_BYTES => 32, # max number of data bytes in non-Sysex messages
96              
97             # message command bytes (128-255/0x80-0xFF)
98             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
99             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
100             REPORT_ANALOG => 0xC0, # enable analog input by pin #
101             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
102             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
103             REPORT_VERSION => 0xF9, # report protocol version
104             SYSTEM_RESET => 0xFF, # reset from MIDI
105             START_SYSEX => 0xF0, # start a MIDI Sysex message
106             END_SYSEX => 0xF7, # end a MIDI Sysex message
107              
108             # extended command set using sysex (0-127/0x00-0x7F)
109             RESERVED_COMMAND => 0x00, # 2nd SysEx data byte is a chip-specific command (AVR, PIC, TI, etc).
110             ANALOG_MAPPING_QUERY => 0x69, # ask for mapping of analog to pin numbers
111             ANALOG_MAPPING_RESPONSE => 0x6A, # reply with mapping info
112             CAPABILITY_QUERY => 0x6B, # ask for supported modes and resolution of all pins
113             CAPABILITY_RESPONSE => 0x6C, # reply with supported modes and resolution
114             PIN_STATE_QUERY => 0x6D, # ask for a pin's current mode and value
115             PIN_STATE_RESPONSE => 0x6E, # reply with a pin's current mode and value
116             EXTENDED_ANALOG => 0x6F, # analog write (PWM, Servo, etc) to any pin
117             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
118             STRING_DATA => 0x71, # a string message with 14-bits per char
119             SHIFT_DATA => 0x75, # shiftOut config/data message (34 bits)
120             I2C_REQUEST => 0x76, # send an I2C read/write request
121             I2C_REPLY => 0x77, # a reply to an I2C read request
122             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
123             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
124             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
125             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
126             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
127              
128             # pin modes
129             INPUT => 0x00, # digital pin in digitalOut mode
130             OUTPUT => 0x01, # digital pin in digitalInput mode
131             ANALOG => 0x02, # analog pin in analogInput mode
132             PWM => 0x03, # digital pin in PWM output mode
133             SERVO => 0x04, # digital pin in Servo output mode
134             SHIFT => 0x05, # shiftIn/shiftOut mode
135             I2C => 0x06, # pin included in I2C setup
136              
137             # Deprecated entries
138             deprecated => [
139             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
140             ],
141              
142             }, # /Constants for Version 2.2
143              
144             V_2_03 => {
145              
146             MAX_DATA_BYTES => 32, # max number of data bytes in non-Sysex messages
147              
148             # message command bytes (128-255/0x80-0xFF)
149             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
150             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
151             REPORT_ANALOG => 0xC0, # enable analog input by pin #
152             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
153             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
154             REPORT_VERSION => 0xF9, # report protocol version
155             SYSTEM_RESET => 0xFF, # reset from MIDI
156             START_SYSEX => 0xF0, # start a MIDI Sysex message
157             END_SYSEX => 0xF7, # end a MIDI Sysex message
158              
159             # extended command set using sysex (0-127/0x00-0x7F)
160             RESERVED_COMMAND => 0x00, # 2nd SysEx data byte is a chip-specific command (AVR, PIC, TI, etc).
161             ANALOG_MAPPING_QUERY => 0x69, # ask for mapping of analog to pin numbers
162             ANALOG_MAPPING_RESPONSE => 0x6A, # reply with mapping info
163             CAPABILITY_QUERY => 0x6B, # ask for supported modes and resolution of all pins
164             CAPABILITY_RESPONSE => 0x6C, # reply with supported modes and resolution
165             PIN_STATE_QUERY => 0x6D, # ask for a pin's current mode and value
166             PIN_STATE_RESPONSE => 0x6E, # reply with a pin's current mode and value
167             EXTENDED_ANALOG => 0x6F, # analog write (PWM, Servo, etc) to any pin
168             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
169             STRING_DATA => 0x71, # a string message with 14-bits per char
170             SHIFT_DATA => 0x75, # shiftOut config/data message (34 bits)
171             I2C_REQUEST => 0x76, # send an I2C read/write request
172             I2C_REPLY => 0x77, # a reply to an I2C read request
173             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
174             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
175             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
176             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
177             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
178              
179             # pin modes
180             INPUT => 0x00, # digital pin in digitalOut mode
181             OUTPUT => 0x01, # digital pin in digitalInput mode
182             ANALOG => 0x02, # analog pin in analogInput mode
183             PWM => 0x03, # digital pin in PWM output mode
184             SERVO => 0x04, # digital pin in Servo output mode
185             SHIFT => 0x05, # shiftIn/shiftOut mode
186             I2C => 0x06, # pin included in I2C setup
187              
188             # Deprecated entries
189             deprecated => [
190             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
191             ],
192              
193             }, # /Constants for Version 2.3 (same as V_2_02)
194              
195             V_2_04 => {
196              
197             MAX_DATA_BYTES => 64, # max number of data bytes in non-Sysex messages
198              
199             # message command bytes (128-255/0x80-0xFF)
200             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
201             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
202             REPORT_ANALOG => 0xC0, # enable analog input by pin #
203             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
204             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
205             REPORT_VERSION => 0xF9, # report protocol version
206             SYSTEM_RESET => 0xFF, # reset from MIDI
207             START_SYSEX => 0xF0, # start a MIDI Sysex message
208             END_SYSEX => 0xF7, # end a MIDI Sysex message
209              
210             # extended command set using sysex (0-127/0x00-0x7F)
211             RESERVED_COMMAND => 0x00, # 2nd SysEx data byte is a chip-specific command (AVR, PIC, TI, etc).
212             ANALOG_MAPPING_QUERY => 0x69, # ask for mapping of analog to pin numbers
213             ANALOG_MAPPING_RESPONSE => 0x6A, # reply with mapping info
214             CAPABILITY_QUERY => 0x6B, # ask for supported modes and resolution of all pins
215             CAPABILITY_RESPONSE => 0x6C, # reply with supported modes and resolution
216             PIN_STATE_QUERY => 0x6D, # ask for a pin's current mode and value
217             PIN_STATE_RESPONSE => 0x6E, # reply with a pin's current mode and value
218             EXTENDED_ANALOG => 0x6F, # analog write (PWM, Servo, etc) to any pin
219             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
220             STRING_DATA => 0x71, # a string message with 14-bits per char
221             STEPPER_DATA => 0x72, # control a stepper motor
222             ONEWIRE_DATA => 0x73, # OneWire read/write/reset/select/skip/search request + read/search reply
223             SHIFT_DATA => 0x75, # shiftOut config/data message (34 bits)
224             I2C_REQUEST => 0x76, # send an I2C read/write request
225             I2C_REPLY => 0x77, # a reply to an I2C read request
226             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
227             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
228             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
229             SCHEDULER_DATA => 0x7B, # createtask/deletetask/addtotask/schedule/querytasks/querytask request and querytasks/querytask reply
230             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
231             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
232              
233             # pin modes
234             INPUT => 0x00, # digital pin in digitalOut mode
235             OUTPUT => 0x01, # digital pin in digitalInput mode
236             ANALOG => 0x02, # analog pin in analogInput mode
237             PWM => 0x03, # digital pin in PWM output mode
238             SERVO => 0x04, # digital pin in Servo output mode
239             SHIFT => 0x05, # shiftIn/shiftOut mode
240             I2C => 0x06, # pin included in I2C setup
241             ONEWIRE => 0x07, # pin configured for 1-Wire commuication
242             STEPPER => 0x08, # pin configured for stepper motor
243              
244             # Deprecated entries
245             deprecated => [
246             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
247             ],
248             }, # /Constants for Version 2.4
249              
250             V_2_05 => {
251              
252             MAX_DATA_BYTES => 64, # max number of data bytes in non-Sysex messages
253              
254             # message command bytes (128-255/0x80-0xFF)
255             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
256             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
257             REPORT_ANALOG => 0xC0, # enable analog input by pin #
258             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
259             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
260             REPORT_VERSION => 0xF9, # report protocol version
261             SYSTEM_RESET => 0xFF, # reset from MIDI
262             START_SYSEX => 0xF0, # start a MIDI Sysex message
263             END_SYSEX => 0xF7, # end a MIDI Sysex message
264              
265             # extended command set using sysex (0-127/0x00-0x7F)
266             RESERVED_COMMAND => 0x00, # 2nd SysEx data byte is a chip-specific command (AVR, PIC, TI, etc).
267             SERIAL_DATA => 0x60, # serial port config/write/read/close/flush/listen request and read reply
268             ENCODER_DATA => 0x61, # receive rotary-encoders current positions
269             ANALOG_MAPPING_QUERY => 0x69, # ask for mapping of analog to pin numbers
270             ANALOG_MAPPING_RESPONSE => 0x6A, # reply with mapping info
271             CAPABILITY_QUERY => 0x6B, # ask for supported modes and resolution of all pins
272             CAPABILITY_RESPONSE => 0x6C, # reply with supported modes and resolution
273             PIN_STATE_QUERY => 0x6D, # ask for a pin's current mode and value
274             PIN_STATE_RESPONSE => 0x6E, # reply with a pin's current mode and value
275             EXTENDED_ANALOG => 0x6F, # analog write (PWM, Servo, etc) to any pin
276             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
277             STRING_DATA => 0x71, # a string message with 14-bits per char
278             STEPPER_DATA => 0x72, # control a stepper motor
279             ONEWIRE_DATA => 0x73, # OneWire read/write/reset/select/skip/search request + read/search reply
280             SHIFT_DATA => 0x75, # shiftOut config/data message (34 bits)
281             I2C_REQUEST => 0x76, # send an I2C read/write request
282             I2C_REPLY => 0x77, # a reply to an I2C read request
283             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
284             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
285             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
286             SCHEDULER_DATA => 0x7B, # createtask/deletetask/addtotask/schedule/querytasks/querytask request and querytasks/querytask reply
287             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
288             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
289              
290             # pin modes
291             INPUT => 0x00, # digital pin in digitalOut mode
292             OUTPUT => 0x01, # digital pin in digitalInput mode
293             ANALOG => 0x02, # analog pin in analogInput mode
294             PWM => 0x03, # digital pin in PWM output mode
295             SERVO => 0x04, # digital pin in Servo output mode
296             SHIFT => 0x05, # shiftIn/shiftOut mode
297             I2C => 0x06, # pin included in I2C setup
298             ONEWIRE => 0x07, # pin configured for 1-Wire commuication
299             STEPPER => 0x08, # pin configured for stepper motor
300             SERIAL => 0x0A, # pin configured for serial port
301             PULLUP => 0x0B, # digital pin in digitalInput mode with pullup
302              
303             # Deprecated entries
304             deprecated => [
305             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
306             ],
307              
308             }, # /Constants for Version 2.5
309              
310             V_2_06 => {
311              
312             MAX_DATA_BYTES => 64, # max number of data bytes in non-Sysex messages
313              
314             # message command bytes (128-255/0x80-0xFF)
315             DIGITAL_MESSAGE => 0x90, # send data for a digital pin
316             ANALOG_MESSAGE => 0xE0, # send data for an analog pin (or PWM)
317             REPORT_ANALOG => 0xC0, # enable analog input by pin #
318             REPORT_DIGITAL => 0xD0, # enable digital input by port pair
319             SET_PIN_MODE => 0xF4, # set a pin to INPUT/OUTPUT/PWM/etc
320             REPORT_VERSION => 0xF9, # report protocol version
321             SYSTEM_RESET => 0xFF, # reset from MIDI
322             START_SYSEX => 0xF0, # start a MIDI Sysex message
323             END_SYSEX => 0xF7, # end a MIDI Sysex message
324              
325             # extended command set using sysex (0-127/0x00-0x7F)
326             RESERVED_COMMAND => 0x00, # 2nd SysEx data byte is a chip-specific command (AVR, PIC, TI, etc).
327             SERIAL_DATA => 0x60, # serial port config/write/read/close/flush/listen request and read reply
328             ENCODER_DATA => 0x61, # receive rotary-encoders current positions
329             ANALOG_MAPPING_QUERY => 0x69, # ask for mapping of analog to pin numbers
330             ANALOG_MAPPING_RESPONSE => 0x6A, # reply with mapping info
331             CAPABILITY_QUERY => 0x6B, # ask for supported modes and resolution of all pins
332             CAPABILITY_RESPONSE => 0x6C, # reply with supported modes and resolution
333             PIN_STATE_QUERY => 0x6D, # ask for a pin's current mode and value
334             PIN_STATE_RESPONSE => 0x6E, # reply with a pin's current mode and value
335             EXTENDED_ANALOG => 0x6F, # analog write (PWM, Servo, etc) to any pin
336             SERVO_CONFIG => 0x70, # set max angle, minPulse, maxPulse, freq
337             STRING_DATA => 0x71, # a string message with 14-bits per char
338             STEPPER_DATA => 0x72, # control a stepper motor
339             ONEWIRE_DATA => 0x73, # OneWire read/write/reset/select/skip/search request + read/search reply
340             SHIFT_DATA => 0x75, # shiftOut config/data message (34 bits)
341             I2C_REQUEST => 0x76, # send an I2C read/write request
342             I2C_REPLY => 0x77, # a reply to an I2C read request
343             I2C_CONFIG => 0x78, # config I2C settings such as delay times and power pins
344             REPORT_FIRMWARE => 0x79, # report name and version of the firmware
345             SAMPLING_INTERVAL => 0x7A, # set the poll rate of the main loop
346             SCHEDULER_DATA => 0x7B, # createtask/deletetask/addtotask/schedule/querytasks/querytask request and querytasks/querytask reply
347             SYSEX_NON_REALTIME => 0x7E, # MIDI Reserved for non-realtime messages
348             SYSEX_REALTIME => 0x7F, # MIDI Reserved for realtime messages
349              
350             # pin modes
351             INPUT => 0x00, # digital pin in digitalOut mode
352             OUTPUT => 0x01, # digital pin in digitalInput mode
353             ANALOG => 0x02, # analog pin in analogInput mode
354             PWM => 0x03, # digital pin in PWM output mode
355             SERVO => 0x04, # digital pin in Servo output mode
356             SHIFT => 0x05, # shiftIn/shiftOut mode
357             I2C => 0x06, # pin included in I2C setup
358             ONEWIRE => 0x07, # pin configured for 1-Wire commuication
359             STEPPER => 0x08, # pin configured for stepper motor
360             ENCODER => 0x09, # pin configured for rotary-encoders
361             SERIAL => 0x0A, # pin configured for serial port
362             PULLUP => 0x0B, # digital pin in digitalInput mode with pullup
363              
364             # Deprecated entries
365             deprecated => [
366             qw( FIRMATA_STRING SYSEX_I2C_REQUEST SYSEX_I2C_REPLY SYSEX_SAMPLING_INTERVAL )
367             ],
368              
369             }, # /Constants for Version 2.6
370             }
371 1     1   9 );
  1         2  
372              
373             # Handle the reverse lookups of the protocol
374             $COMMAND_LOOKUP = {};
375             while ( my ( $protocol_version, $protocol_commands ) = each %$COMMANDS ) {
376             my $protocol_lookup = $COMMAND_LOOKUP->{$protocol_version} = {};
377             my $deprecated = $protocol_lookup->{deprecated} || [];
378             my $deprecated_lookup = { map { ( $_ => 1 ) } @$deprecated };
379             while ( my ( $protocol_command, $command_value ) = each %$protocol_commands ) {
380             next if $protocol_command eq 'deprecated';
381             next if $deprecated_lookup->{$protocol_command};
382             $protocol_lookup->{$command_value} = $protocol_command;
383             }
384             }
385              
386             # Now we consolidate all the string keynames into a single master list.
387 1     1   8 use constant ( $COMMAND_NAMES = { map { map { ( $_ => $_ ) } keys %$_ } values %$COMMANDS } );
  1         2  
  1         30  
  6         33  
  236         880  
388 1     1   8 use constant { COMMAND_NAMES => [ $COMMAND_NAMES = [ keys %$COMMAND_NAMES ] ] };
  1         2  
  1         133  
389              
390             @EXPORT_OK = (
391             @$COMMAND_NAMES, keys %$BASE,
392             keys %$COMMANDS,
393             qw( $COMMANDS $COMMAND_NAMES $COMMAND_LOOKUP ),
394             );
395              
396             %EXPORT_TAGS = ( all => \@EXPORT_OK );
397              
398             1;