Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PulseIn Implementation #396

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY
#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
#define PULSE_IN 0x74 // send a pulse in command

// pin modes
//#define INPUT 0x00 // defined in Arduino.h
Expand Down
48 changes: 47 additions & 1 deletion examples/StandardFirmata/StandardFirmata.ino
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,53 @@ void sysexCallback(byte command, byte argc, byte *argv)
}
Firmata.write(END_SYSEX);
break;

case PULSE_IN:{
byte pulseDurationArray[4] = {
(argv[2] & 0x7F) | ((argv[3] & 0x7F) << 7)
,(argv[4] & 0x7F) | ((argv[5] & 0x7F) << 7)
,(argv[6] & 0x7F) | ((argv[7] & 0x7F) << 7)
,(argv[8] & 0x7F) | ((argv[9] & 0x7F) << 7)
};
unsigned long pulseDuration = ((unsigned long)pulseDurationArray[0] << 24)
+ ((unsigned long)pulseDurationArray[1] << 16)
+ ((unsigned long)pulseDurationArray[2] << 8)
+ ((unsigned long)pulseDurationArray[3]);
if(argv[1] == HIGH){
pinMode(argv[0],OUTPUT);
digitalWrite(argv[0],LOW);
delayMicroseconds(2);
digitalWrite(argv[0],HIGH);
delayMicroseconds(pulseDuration);
digitalWrite(argv[0],LOW);
} else {
digitalWrite(argv[0],HIGH);
delayMicroseconds(2);
digitalWrite(argv[0],LOW);
delayMicroseconds(pulseDuration);
digitalWrite(argv[0],HIGH);
}
unsigned long duration;
byte responseArray[5];
byte timeoutArray[4] = {
(argv[10] & 0x7F) | ((argv[11] & 0x7F) << 7)
,(argv[12] & 0x7F) | ((argv[13] & 0x7F) << 7)
,(argv[14] & 0x7F) | ((argv[15] & 0x7F) << 7)
,(argv[16] & 0x7F) | ((argv[17] & 0x7F) << 7)
};
unsigned long timeout = ((unsigned long)timeoutArray[0] << 24)
+ ((unsigned long)timeoutArray[1] << 16)
+ ((unsigned long)timeoutArray[2] << 8)
+ ((unsigned long)timeoutArray[3]);
pinMode(argv[0],INPUT);
duration = pulseIn(argv[0], argv[1],timeout);
responseArray[0] = argv[0];
responseArray[1] = (((unsigned long)duration >> 24) & 0xFF) ;
responseArray[2] = (((unsigned long)duration >> 16) & 0xFF) ;
responseArray[3] = (((unsigned long)duration >> 8) & 0xFF);
responseArray[4] = (((unsigned long)duration & 0xFF));
Firmata.sendSysex(PULSE_IN,5,responseArray);
break;
}
case SERIAL_MESSAGE:
#ifdef FIRMATA_SERIAL_FEATURE
serialFeature.handleSysex(command, argc, argv);
Expand Down