Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Tone.h>
- #include <Keypad.h>
- // 2014 weaknetlabs@gmail.com BBv2.2 Hardware
- // fill stack with global variables:
- int bb[15][2] = { // MF 0,1,2,3,4,5,6,7,8,9,kp,st,2400+2600,kp2,st2
- {1300,1500},{700,900},{700,1100},
- {900,1100},{700,1300},{900,1300},
- {1100,1300},{700,1500},{900,1500},
- {1100,1500},{1100,1700},{1500,1700},
- {2600,2400},{1300,1700},{900,1700},
- };
- 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
- 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?
- boolean intern = false; // international trunk seizure
- // 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
- }
- // 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
- if(intern){ // international seizure of trunk
- mf(12);
- delay(1337);
- sf(2400,750);
- }else{ // non international
- sf(2600,750);
- }
- }
- return; // end main()
- }
- // Process buttons:
- void procButton(KeypadEvent b){
- b -= 48;
- switch (keypad.getState()){
- case RELEASED: // drop right away
- return;
- case PRESSED: // momentary
- 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
- }else if(b<=51&&b>=49){ // A,B,C redbox
- redBox(b); // pass it to RedBox()
- }
- break;
- case HOLD: // HELD (special functions)
- 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){
- (intern) ? intern = false : intern = true;
- notify();
- }
- break;
- }
- 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;
- }
- // Notification Tone:
- void notify(void){
- for(int i=0;i<=2;i++){
- freq[0].play(2600,33);
- delay(66);
- }
- delay(500);
- 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];
- if(digit<0){
- duration = bbdur[1];
- if(digit==-13){
- (intern) ? digit = 13 : digit = 10;
- }else if(digit==-6){
- (intern) ? digit = 14 : digit = 11;
- }else{
- return; // -1 in store[]?
- }
- }else if(digit==12){ // 85ms for international trunk seizing
- duration = 200;
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement