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

Send a free sequence of pulses with custom timings #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
69 changes: 69 additions & 0 deletions RCSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,34 @@ void RCSwitch::send(const char* sCodeWord) {
* then the bit at position length-2, and so on, till finally the bit at position 0.
*/
void RCSwitch::send(unsigned long code, unsigned int length) {

// if there is a pointer to a transmit timings array, write the timings to
// the array instead of sending them.
// allways start with low signal. Do not send (return).
// the host program is completely responsible for the array memory management
if (transmittimings){
int invertedoffset = 0;
if (this->protocol.invertedSignal) invertedoffset = 1;
for (int i = length-1; i >= 0; i--) {
if (code & (1L << i)) {
transmittimings[2*(length-i-1)+1+invertedoffset] = this->protocol.pulseLength * this->protocol.one.high;
transmittimings[2*(length-i-1)+1+invertedoffset+1] = this->protocol.pulseLength * this->protocol.one.low;
} else {
transmittimings[2*(length-i-1)+1+invertedoffset] = this->protocol.pulseLength * this->protocol.zero.high;
transmittimings[2*(length-i-1)+1+invertedoffset+1] = this->protocol.pulseLength * this->protocol.zero.low;
}
}
if (this->protocol.invertedSignal) {
transmittimings[0] = this->protocol.pulseLength * this->protocol.syncFactor.high;
transmittimings[1] = this->protocol.pulseLength * this->protocol.syncFactor.low;
} else {
transmittimings[2*length+1] = this->protocol.pulseLength * this->protocol.syncFactor.high;
transmittimings[0] = this->protocol.pulseLength * this->protocol.syncFactor.low;
}
transmittimings[2*length+2] = 0; // zero-terminate the array
return;
}

if (this->nTransmitterPin == -1)
return;

Expand Down Expand Up @@ -513,6 +541,47 @@ void RCSwitch::send(unsigned long code, unsigned int length) {
#endif
}

/**
* Transmit signal with the timings stored in transmittimings zero-terminated integrer array
* until a 0 timing is found or the RCSWITCH_MAX_CHANGES is reached
* first timing is always low. A final low is always sent in case the number of timings is not even
* the host program is completely responsible for the array memory management
*/
void RCSwitch::send(unsigned int * ptrtransmittimings) {
if (this->nTransmitterPin == -1)
return;

if (!ptrtransmittimings)
return;

#if not defined( RCSwitchDisableReceiving )
// make sure the receiver is disabled while we transmit
int nReceiverInterrupt_backup = nReceiverInterrupt;
if (nReceiverInterrupt_backup != -1) {
this->disableReceive();
}
#endif

for (int nRepeat = 0; nRepeat < nRepeatTransmit; nRepeat++) {
unsigned int currenttiming = 0;
bool currentlogiclevel = false;
while( ptrtransmittimings[currenttiming] && currenttiming < RCSWITCH_MAX_CHANGES ) {
digitalWrite(this->nTransmitterPin, currentlogiclevel ? HIGH : LOW);
delayMicroseconds( ptrtransmittimings[currenttiming] );
currenttiming++;
currentlogiclevel = !currentlogiclevel;
}
digitalWrite(this->nTransmitterPin, LOW);
}

#if not defined( RCSwitchDisableReceiving )
// enable receiver again if we just disabled it
if (nReceiverInterrupt_backup != -1) {
this->enableReceive(nReceiverInterrupt_backup);
}
#endif
}

/**
* Transmit a single high-low pulse.
*/
Expand Down
2 changes: 2 additions & 0 deletions RCSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class RCSwitch {
void sendTriState(const char* sCodeWord);
void send(unsigned long code, unsigned int length);
void send(const char* sCodeWord);
void send(unsigned int * ptrtransmittimings);
unsigned int * transmittimings = NULL; // this should have a get/set functions interface

#if not defined( RCSwitchDisableReceiving )
void enableReceive(int interrupt);
Expand Down