Skip to content

Commit 9684e64

Browse files
committed
listen before talk improvements and send compressed timings support
to reduce used stack memory
1 parent 418fd63 commit 9684e64

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

RFControl.cpp

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void RFControl::stopReceiving() {
6262
detachInterrupt(interruptPin);
6363
}
6464
interruptPin = -1;
65+
state = STATUS_WAITING;
6566
}
6667

6768
bool RFControl::hasData() {
@@ -163,9 +164,9 @@ void recording(unsigned int duration, int package) {
163164
//less than 32 timings -> restart the package.
164165
if (data_end[package] - data_start[package] >= 32)
165166
{
166-
if (state == STATUS_RECORDING_3)
167+
if (state == STATUS_RECORDING_3) {
167168
state = STATUS_RECORDING_END;
168-
else
169+
}else
169170
{
170171
state = STATUS_RECORDING_0 + package + 1;
171172
}
@@ -175,7 +176,7 @@ void recording(unsigned int duration, int package) {
175176
#ifdef RF_CONTROL_SIMULATE_ARDUINO
176177
printf(" => restart package");
177178
#endif
178-
data_end[package] = data_start[package];
179+
data_end[package] = data_start[package];
179180
switch (package)
180181
{
181182
case 0:
@@ -487,9 +488,67 @@ bool RFControl::compressTimingsAndSortBuckets(unsigned int buckets[8], unsigned
487488
return true;
488489
}
489490

491+
void listenBeforeTark()
492+
{
493+
// listen before talk
494+
unsigned long waited = 0;
495+
if(interruptPin != -1) {
496+
while(state != STATUS_WAITING && state != STATUS_RECORDING_END) {
497+
//wait till no rf message is in the air
498+
waited += 10;
499+
delayMicroseconds(10000);
500+
// don't wait loner than 5sec
501+
if(waited > 5000) {
502+
break;
503+
}
504+
// leave some time between the message in air and the newly send
505+
if(state == STATUS_WAITING || state == STATUS_RECORDING_END) {
506+
waited += 1000;
507+
delayMicroseconds(1000*1000);
508+
}
509+
}
510+
511+
// stop receiving while sending, this method preservs the recording state
512+
detachInterrupt(interruptPin);
513+
}
514+
// this prevents loosing the data in the receiving buffer, after sending
515+
if(data1_ready || data2_ready) {
516+
state = STATUS_RECORDING_END;
517+
}
518+
}
519+
520+
void afterTalk()
521+
{
522+
// enable reciving again
523+
if(interruptPin != -1) {
524+
attachInterrupt(interruptPin, handleInterrupt, CHANGE);
525+
}
526+
}
527+
528+
529+
void RFControl::sendByCompressedTimings(int transmitterPin,unsigned int* buckets, char* compressTimings, unsigned int repeats) {
530+
listenBeforeTark();
531+
unsigned int timings_size = strlen(compressTimings);
532+
pinMode(transmitterPin, OUTPUT);
533+
for(unsigned int i = 0; i < repeats; i++) {
534+
digitalWrite(transmitterPin, LOW);
535+
int state = LOW;
536+
for(unsigned int j = 0; j < timings_size; j++) {
537+
state = !state;
538+
digitalWrite(transmitterPin, state);
539+
unsigned int index = compressTimings[j] - '0';
540+
delayMicroseconds(buckets[index]);
541+
}
542+
}
543+
digitalWrite(transmitterPin, LOW);
544+
afterTalk();
545+
}
546+
547+
490548
void RFControl::sendByTimings(int transmitterPin, unsigned int *timings, unsigned int timings_size, unsigned int repeats) {
549+
listenBeforeTark();
550+
491551
pinMode(transmitterPin, OUTPUT);
492-
state = STATUS_RECORDING_END; //Stops the receiver
493552
for(unsigned int i = 0; i < repeats; i++) {
494553
digitalWrite(transmitterPin, LOW);
495554
int state = LOW;
@@ -500,5 +559,6 @@ void RFControl::sendByTimings(int transmitterPin, unsigned int *timings, unsigne
500559
}
501560
}
502561
digitalWrite(transmitterPin, LOW);
503-
state = STATUS_WAITING; //starts the receiver
562+
afterTalk();
504563
}
564+

RFControl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RFControl
2222
static bool compressTimings(unsigned int buckets[8], unsigned int *timings, unsigned int timings_size);
2323
static bool compressTimingsAndSortBuckets(unsigned int buckets[8], unsigned int *timings, unsigned int timings_size);
2424
static void sendByTimings(int transmitterPin, unsigned int *timings, unsigned int timings_size, unsigned int repeats = 3);
25+
static void sendByCompressedTimings(int transmitterPin, unsigned int* buckets, char* compressTimings, unsigned int repeats = 3);
2526
private:
2627
RFControl();
2728
};

0 commit comments

Comments
 (0)