@@ -62,6 +62,7 @@ void RFControl::stopReceiving() {
62
62
detachInterrupt (interruptPin);
63
63
}
64
64
interruptPin = -1 ;
65
+ state = STATUS_WAITING;
65
66
}
66
67
67
68
bool RFControl::hasData () {
@@ -163,9 +164,9 @@ void recording(unsigned int duration, int package) {
163
164
// less than 32 timings -> restart the package.
164
165
if (data_end[package] - data_start[package] >= 32 )
165
166
{
166
- if (state == STATUS_RECORDING_3)
167
+ if (state == STATUS_RECORDING_3) {
167
168
state = STATUS_RECORDING_END;
168
- else
169
+ } else
169
170
{
170
171
state = STATUS_RECORDING_0 + package + 1 ;
171
172
}
@@ -175,7 +176,7 @@ void recording(unsigned int duration, int package) {
175
176
#ifdef RF_CONTROL_SIMULATE_ARDUINO
176
177
printf (" => restart package" );
177
178
#endif
178
- data_end[package] = data_start[package];
179
+ data_end[package] = data_start[package];
179
180
switch (package)
180
181
{
181
182
case 0 :
@@ -487,9 +488,67 @@ bool RFControl::compressTimingsAndSortBuckets(unsigned int buckets[8], unsigned
487
488
return true ;
488
489
}
489
490
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
+
490
548
void RFControl::sendByTimings (int transmitterPin, unsigned int *timings, unsigned int timings_size, unsigned int repeats) {
549
+ listenBeforeTark ();
550
+
491
551
pinMode (transmitterPin, OUTPUT);
492
- state = STATUS_RECORDING_END; // Stops the receiver
493
552
for (unsigned int i = 0 ; i < repeats; i++) {
494
553
digitalWrite (transmitterPin, LOW);
495
554
int state = LOW;
@@ -500,5 +559,6 @@ void RFControl::sendByTimings(int transmitterPin, unsigned int *timings, unsigne
500
559
}
501
560
}
502
561
digitalWrite (transmitterPin, LOW);
503
- state = STATUS_WAITING; // starts the receiver
562
+ afterTalk ();
504
563
}
564
+
0 commit comments