-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
pcmRF.cpp
140 lines (102 loc) · 2.62 KB
/
pcmRF.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <pcmRF.h>
#if defined(ENABLE_RF)
#include <Arduino.h>
#include <SD.h>
#include <SdFat.h>
#include <SPI.h>
#include <RF24.h>
boolean rfPlaying = 0;
const uint64_t addresses[14] = { 0xABCDABCD71LL, 0x544d52687CLL, 0x544d526832LL, 0x544d52683CLL,0x544d526846LL, 0x544d526850LL,0x544d52685ALL, 0x544d526820LL, 0x544d52686ELL, 0x544d52684BLL, 0x544d526841LL, 0x544d526855LL,0x544d52685FLL,0x544d526869LL};
#if !defined(SDFAT)
File txFile;
#else
SdFile txFile;
#endif
byte buff[32];
RF24 radi(0,0);
//************** RF SECTION ********************
pcmRF::pcmRF( RF24& _radio ){
radi = _radio;
}
void pcmRF::begin(){
radi.begin();
radi.setChannel(1); // Set RF channel to 1
radi.setAutoAck(0); // Disable ACKnowledgement packets
radi.setDataRate(RF24_1MBPS); // Set data rate as specified in user options
radi.setCRCLength(RF24_CRC_8);
radi.openWritingPipe(addresses[1]);
radi.openReadingPipe(1,addresses[0]);
}
boolean pcmRF::isPlaying(){
return rfPlaying;
}
void pcmRF::stop(){
TIMSK1 &= ~( _BV(OCIE1A) | _BV(OCIE1B) );
#if !defined(SDFAT)
if(txFile){txFile.close();}
#else
if(txFile.isOpen()){txFile.close();}
#endif
rfPlaying = 0;
}
void stop(){
TIMSK1 &= ~( _BV(OCIE1A) | _BV(OCIE1B) );
#if !defined(SDFAT)
if(txFile){txFile.close();}
#else
if(txFile.isOpen()){txFile.close();}
#endif
rfPlaying = 0;
}
void pcmRF::broadcast(byte device){
noInterrupts();
if(device == 255){
radi.openWritingPipe(addresses[1]);
}else{
radi.openWritingPipe(addresses[device+2]);
}
interrupts();
}
void pcmRF::play(const char* filename, byte device){
stop();
rfPlaying = 1;
if(device == 255){
radi.openWritingPipe(addresses[1]);
}else{
radi.openWritingPipe(addresses[device+2]);
}
#if !defined(SDFAT)
if(!txFile){ txFile = SD.open(filename);}
if(txFile){
txFile.seek(24); //skip the header info
#else
if(!txFile.isOpen()){ txFile.open(filename);}
if(txFile.isOpen()){
txFile.seekSet(24); //skip the header info
#endif
unsigned int SAMPLE_RATE = txFile.read();
SAMPLE_RATE |= txFile.read() << 8;
#if !defined(SDFAT)
txFile.seek(44);
#else
txFile.seekSet(44);
#endif
unsigned int res = (10 * (1600000/SAMPLE_RATE)) * 32;
noInterrupts();
ICR1 = res;
TCCR1A = _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
TIMSK1 = ( _BV(OCIE1A) );
interrupts();
}else{
#if defined (debug)
Serial.println("failed to open music file");
#endif
}
}
ISR(TIMER1_COMPA_vect){
radi.writeFast(&buff,32);
txFile.read((byte*)buff,32);
if(txFile.available() < 32){ radi.txStandBy(); stop(); }
}
#endif