This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
/
bb-fw.c
383 lines (382 loc) · 9.84 KB
/
bb-fw.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <Tone.h>
#include <Keypad.h>
// 2014 weaknetlabs@gmail.com BBv2.2 Hardware
// fill stack with global variables:
// firmware version 2.5a
//
int bb[16][2] = { // MF 0,1,2,3,4,5,6,7,8,9,kp,st,2400+2600,kp2,st2,ss4 super
{1300,1500},{700,900},{700,1100}, // 0,1,2
{900,1100},{700,1300},{900,1300}, // 3,4,5
{1100,1300},{700,1500},{900,1500}, // 6,7,8
{1100,1500},{1100,1700},{1500,1700}, // 9,kp,st
{2600,2400},{1300,1700},{900,1700}, // 2400+2600,kp2,st2
{2400,2040}, // ss4 Supervisory
};
int dtmfFreq[8] = { // in Hz
697,770,852,941,1209,1336,1477,1633
};
int dtmf[16][2] = {
{0,4},{0,5},{0,6},{0,7}, // 1,2,3,A
{1,4},{1,5},{1,6},{1,7}, // 4,5,6,B
{2,4},{2,5},{2,6},{2,7}, // 7,8,9,C
{3,4},{3,5},{3,6},{3,7}, // *,0,#,D
};
int ss4[][4] = {
{0,1,0,1},{1,1,1,0},{1,1,0,1}, // 0,1,2
{1,1,0,0},{1,0,1,1},{1,0,1,0}, // 3,4,5
{1,0,0,1},{1,0,0,0},{0,1,1,1}, // 6,7,8
{0,1,1,0},{0,0,0,0},{0,1,0,0}, // 9,KP,OP11
{0,0,1,1}, // OP12
};
uint8_t speedDial[][3] = { // Auto dial hold digits
{1,2,1},{1,0,1},{1,2,1}, // 0,1,2
{1,3,1},{1,4,1},{1,0,5}, // 3,4,5
{6,6,6},{1,0,7},{1,8,1}, // 6,7,8
{1,0,9}
};
uint8_t bbdur[2] = {60,100}; // 75 ms for MF tones, 120 for KP/ST
int ss4Tone[2] = {2040,2400}; // tones for 0,1 respectively
char keys[4][4] = {
{'1','2','3','a'},
{'4','5','6','b'},
{'7','8','9','c'},
{'#','0','*','d'}
};
byte rowPins[4] = {5,4,3,2}; //connect to the row pinouts of the keypad
byte colPins[4] = {9,8,7,6}; //connect to the column pinouts of the keypad
// global objects
Tone freq[2]; // array of Tone objects, now we can play as freq[0].play(); etc
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
boolean rec = 0; // recording on/off
boolean stored = 0; // stored digits?
boolean autoDial = 0; // are we playing stored ANY didgits?
int mode = 0; // 0 for MF, 1 for intern, 2 for SS4, 3 for DP, 4 for DTMF
int mf2 = 0; // MF2 mode
// the storage of integers MUST be integers (int):
int store[24] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
// call set up function to set up pins:
void setup(void){ // Start up instructions:
freq[0].begin(11); // Initialize our first tone generator
freq[1].begin(12); // Initialize our second tone generator
keypad.setHoldTime(1500); // hold for two seconds to change state to HOLD
pinMode(10, INPUT); // 2600 button
pinMode(13, OUTPUT); // LED for recording
keypad.addEventListener(procButton);
notify(); // boot successful
Serial.begin(9600);
}
// our main() function:
void loop(void){ // Here we just get the button, pressed or held, and 2600 switch
char button = keypad.getKey(); // check for button press
if(digitalRead(10)==HIGH){ // play 2600Hz if top button pressed
super(); // supervisory signalling
}
return; // end main()
}
// Supervisory (TOP) button
void super(void){
if(mode==1){ // international seizure of trunk
mf(12);
delay(1337);
sf(2400,750);
}else if(mode==2){ // SS4 Supervisory Signal
mf(15);
delay(150);
sf(2040,350);
}else{ // non international
sf(2600,750);
}
return;
}
// Process buttons:
void procButton(KeypadEvent b){
b -= 48;
switch (keypad.getState()){
case RELEASED: // drop right away
return;
case PRESSED: // momentary
if(mode==2){ // Signal Switching 4
ss4Signal(b);
break;
}else if(mode==3){
pulse(b); // pulse it
return;
}
if(mode==4&&(b<10&&b>=0||b==-13||b==-6||(b>=49&&b<=52))){ // MF tone
mf(b);
}
if(b<10&&b>=0||b==-13||b==-6){ // MF tone
mf(b);
}else if(b==52){ // D
if (stored) playStored(); // don't copy function onto stack if not needed
return;
}else if(mode==1){ // international kp2/st2
if(b>=49&&b<=51){
mf(b);
return;
}
}
else if(mode==0&&(b<=51&&b>=49)){ // A,B,C redbox
redBox(b); // pass it to RedBox()
return;
}
break;
case HOLD: // HELD (special functions)
if(b==50&&mode==3){ // HOLD B for MF2 in PD Mode
(mf2)? mf2 = 0 : mf2 = 1; // turn off if on, or on if off
freq[0].play(440,70);
delay(140);
freq[0].play(440,70);
delay(140);
}
if(b<10&&b>=0||b==-13||b==-6){
dial(b);
}else if(b==51){ // C takes care of recording now
if(rec){ // we are done recording:
digitalWrite(13, LOW); // turn off LED
rec = 0;
stored=1; // we have digits stored
recNotify();
}else{ // we start recording
digitalWrite(13, HIGH); // light up LED
rec = 1;
for(int i=0;i<=23;i++){ // reset array
store[i] = -1;
}
recNotify();
} // END recording code
}else if(b==49){ // ('A' HELD) switching any mode "on" changes to mode, all "off" is domestic
if(mode==0){ // mf to international
mode=1;
}else if(mode==1){ // international to ss4 mode
mode=2;
}else if(mode==2){ // ss4 mode to pulse mode
mode=3;
}else if(mode==3){ // pulse mode to DTMF
mode=4;
}else if(mode==4){ // DTMF to domestic
mode=0;
}
notifyMode();
return;
}
break;
}
return;
}
// pulse mode
void pulse(int signal){
int pulse = 2600;
if(mf2) { pulse = 2280; } // MF2 mode (AC11)
if(signal==49) freq[0].play(300,200);
if(signal>9) { return; }
if(signal==-13||signal==-6){
return;
}else{
if(signal==0){ signal = 10; }
for(int i=0;i<signal;i++){
digitalWrite(13, HIGH); // pulsing LED
freq[0].play(pulse,66);
delay(66);
digitalWrite(13, LOW);
delay(34);
}
delay(500); // no new digit accepted until after this time
}
return;
}
// play stored tones
void playStored(void){
if(stored){
autoDial = 1;
for(int i=0;i<=23;i++){
if(store[i]==-1){
return;
}else{
mf(store[i]);
}
}
}else{
return;
}
autoDial = 0; // turn off playing
return;
}
// Record Notification tone:
void recNotify(void){
if(rec){
sf(1700,66);
delay(66);
sf(2200,500);
delay(500);
}else{
sf(2200,66);
delay(66);
sf(1700,500);
delay(500);
}
return;
}
// Mode notification
void notifyMode(){
int count = 1;
int dur = 70;
int frequency = 440;
if(mode==1){ count = 2; }
if(mode==2){ count = 3; }
if(mode==3){ count = 4; }
if(mode==4){ count = 5; }
for(int i = 0;i<count;i++){
freq[0].play(frequency,dur);
delay(dur*2);
}
}
// Notification Tone:
void notify(void){
Serial.println(mode);
for(int i=0;i<=2;i++){
freq[0].play(2600,33);
delay(66);
}
delay(500);
return;
}
// SS4 signalling:
void ss4Signal(int signal){
if(signal==49){ // A
mf(15);
delay(250);
sf(2400,350);
return;
}
if(signal==50){// B
signal==11;
}
if(signal==51){ // C
signal==12;
}
if(signal==52){ // D
sf(2600,750);
return;
}
if(signal==-13){ signal = 10; }
if(signal==-6){ //
super();
return;
}
for(int i=0;i<=3;i++){
(ss4[signal][i]) ? freq[0].play(ss4Tone[1],35) : freq[0].play(ss4Tone[0],35);
delay(70);
}
return;
}
// play an MF tone:
void mf(int digit){ // Store the digit IFF recording:
if(rec && ((digit>=0&&digit<=9)||digit==-13||digit==-6)){
for(int i=0;i<=23;i++){ // ONLY record KP,ST,0-9
if(store[i]==-1){
store[i]=digit;
break;
}
}
}
int duration = bbdur[0]; // OKAY for DTMF too
if(mode==1){
if(digit==49){ // international mode A
digit=13;
}
if(digit==50){ // international mode B
digit=14;
}
if(digit==51){
sf(2600,1000);
return;
}
}else if(mode==4){ // DTMF
Serial.println(digit);
if((digit>=1&&digit<=3)) { // 1,2,3
digit-=1;
}else if(digit>=7&&digit<=9){
digit+=1;
}else if(digit==49){
digit = 3; // A
}else if(digit==50){
digit = 7; // B
}else if(digit==51){
digit = 11; // C
}else if(digit==52){
digit = 15; // D
}else if(digit==-13){
digit = 12; // *
}else if(digit==-6){
digit = 14; // #
}else if(digit==0){
digit = 13; // 0
}else if(digit==13){ duration=150; }
Serial.println("and: ");
Serial.println(digit);
freq[0].play(dtmfFreq[dtmf[digit][0]],duration);
freq[1].play(dtmfFreq[dtmf[digit][1]],duration);
return;
}
if(digit<0){
duration = bbdur[1];
if(digit==-13){ digit=10; }
if(digit==-6){ digit=11; }
}
if(digit==-1){ return; } // -1 in storage?
if(digit==12){ // 85ms for international trunk seizing
duration = 200;
}
if (mode==3) duration = 150; // SS4 Supervisory MF (150ms)
freq[0].play(bb[digit][0],duration);
freq[1].play(bb[digit][1],duration);
(autoDial) ? delay(duration + 60) : delay(duration); // ? expression? statement?
if(rec){
delay(25);
sf(2600,33);
}// chirp to signify recording
return; // Now we can leave.
}
// play SF:
void sf(int frequency,int duration){ // play single frequency
freq[0].play(frequency,duration);
return;
}
// play red box tones:
void redBox(int coin){ // pass me a button
int iter;
int delayMs = 66;
int rb[2] = {1700,2200};
switch(coin){
case 49:
iter = 5;
delayMs = 33;
break;
case 50:
iter = 2;
break;
case 51:
iter = 1;
break;
}
for(int i=0;i<iter;i++){
freq[0].play(rb[0],delayMs);
freq[1].play(rb[1],delayMs);
delay(delayMs * 2); // pause for coin and between
}
}
// play speed dials
void dial(int sd){ // speed dial
if(rec) return; // we are recording...
autoDial = 1; // turn on for pauses
sf(2600,750); // play 2600 1 sec
delay(2000); // pause
mf(-13); // KP
for(int i=0;i<=2;i++){
mf(speedDial[sd][i]);
}
mf(-6); // ST
autoDial = 0; // turn off pauses
return;
}