Advertisement
weaknetlabs

BlizzyB Firmware v2.0

Sep 23rd, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. #include <Tone.h>
  2. #include <Keypad.h>
  3. // 2014 weaknetlabs@gmail.com BBv2.2 Hardware
  4. // fill stack with global variables:
  5. int bb[15][2] = { // MF 0,1,2,3,4,5,6,7,8,9,kp,st,2400+2600,kp2,st2
  6.   {1300,1500},{700,900},{700,1100},
  7.   {900,1100},{700,1300},{900,1300},
  8.   {1100,1300},{700,1500},{900,1500},
  9.   {1100,1500},{1100,1700},{1500,1700},
  10.   {2600,2400},{1300,1700},{900,1700},
  11. };
  12. uint8_t speedDial[][3] = { // Auto dial hold digits
  13.   {1,2,1},{1,0,1},{1,2,1}, // 0,1,2
  14.   {1,3,1},{1,4,1},{1,0,5}, // 3,4,5
  15.   {6,6,6},{1,0,7},{1,8,1}, // 6,7,8
  16.   {1,0,9}
  17. };
  18. uint8_t bbdur[2] = {60,100}; // 75 ms for MF tones, 120 for KP/ST
  19. char keys[4][4] = {
  20.   {'1','2','3','a'},
  21.   {'4','5','6','b'},
  22.   {'7','8','9','c'},
  23.   {'#','0','*','d'}
  24. };
  25. byte rowPins[4] = {5,4,3,2}; //connect to the row pinouts of the keypad
  26. byte colPins[4] = {9,8,7,6}; //connect to the column pinouts of the keypad
  27. // global objects
  28. Tone freq[2]; // array of Tone objects, now we can play as freq[0].play(); etc
  29. Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
  30. boolean rec = 0; // recording on/off
  31. boolean stored = 0; // stored digits?
  32. boolean autoDial = 0; // are we playing stored ANY didgits?
  33. boolean intern = false; // international trunk seizure
  34. // the storage of integers MUST be integers (int):
  35. 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};
  36. // call set up function to set up pins:
  37. void setup(void){ // Start up instructions:
  38.   freq[0].begin(11); // Initialize our first tone generator
  39.   freq[1].begin(12); // Initialize our second tone generator
  40.   keypad.setHoldTime(1500); // hold for two seconds to change state to HOLD
  41.   pinMode(10, INPUT); // 2600 button
  42.   pinMode(13, OUTPUT); // LED for recording
  43.   keypad.addEventListener(procButton);
  44.   notify(); // boot successful
  45. }
  46. // our main() function:
  47. void loop(void){ // Here we just get the button, pressed or held, and 2600 switch
  48.   char button = keypad.getKey(); // check for button press
  49.   if(digitalRead(10)==HIGH){ // play 2600Hz if top button pressed
  50.     if(intern){ // international seizure of trunk
  51.       mf(12);
  52.       delay(1337);
  53.       sf(2400,750);
  54.     }else{ // non international
  55.       sf(2600,750);
  56.     }
  57.   }
  58.   return; // end main()
  59. }
  60. // Process buttons:
  61. void procButton(KeypadEvent b){
  62.   b -= 48;
  63.   switch (keypad.getState()){
  64.     case RELEASED: // drop right away
  65.       return;
  66.     case PRESSED: // momentary
  67.       if(b<10&&b>=0||b==-13||b==-6){ // MF tone
  68.         mf(b);
  69.       }else if(b==52){ // D
  70.         if (stored) playStored(); // don't copy function onto stack if not needed
  71.       }else if(b<=51&&b>=49){ // A,B,C redbox
  72.         redBox(b); // pass it to RedBox()
  73.       }
  74.       break;
  75.     case HOLD: // HELD (special functions)
  76.       if(b<10&&b>=0||b==-13||b==-6){
  77.         dial(b);
  78.       }else if(b==51){ // C takes care of recording now
  79.         if(rec){ // we are done recording:
  80.           digitalWrite(13, LOW); // turn off LED
  81.           rec = 0;
  82.           stored=1; // we have digits stored
  83.           recNotify();
  84.         }else{ // we start recording
  85.           digitalWrite(13, HIGH); // light up LED
  86.           rec = 1;
  87.           for(int i=0;i<=23;i++){ // reset array
  88.             store[i] = -1;
  89.           }
  90.           recNotify();
  91.         } // END recording code
  92.       }else if(b==49){
  93.         (intern) ? intern = false : intern = true;
  94.         notify();
  95.       }      
  96.       break;
  97.   }
  98.   return;
  99. }
  100. // play stored tones
  101. void playStored(void){
  102.   if(stored){
  103.     autoDial = 1;
  104.     for(int i=0;i<=23;i++){
  105.       if(store[i]==-1){
  106.         return;
  107.       }else{
  108.         mf(store[i]);  
  109.       }    
  110.     }  
  111.   }else{
  112.     return;
  113.   }
  114.   autoDial = 0; // turn off playing
  115.   return;
  116. }
  117. // Record Notification tone:
  118. void recNotify(void){
  119.   if(rec){
  120.     sf(1700,66);
  121.     delay(66);
  122.     sf(2200,500);
  123.     delay(500);
  124.   }else{
  125.     sf(2200,66);
  126.     delay(66);
  127.     sf(1700,500);
  128.     delay(500);  
  129.   }
  130.   return;
  131. }
  132. // Notification Tone:
  133. void notify(void){
  134.   for(int i=0;i<=2;i++){
  135.     freq[0].play(2600,33);
  136.     delay(66);
  137.   }
  138.   delay(500);
  139.   return;
  140. }
  141. // play an MF tone:
  142. void mf(int digit){ // Store the digit IFF recording:
  143.   if(rec && ((digit>=0&&digit<=9)||digit==-13||digit==-6)){
  144.     for(int i=0;i<=23;i++){ // ONLY record KP,ST,0-9
  145.       if(store[i]==-1){
  146.         store[i]=digit;
  147.         break;
  148.       }
  149.     }
  150.   }
  151.   int duration = bbdur[0];
  152.   if(digit<0){
  153.     duration = bbdur[1];
  154.     if(digit==-13){
  155.       (intern) ? digit = 13 : digit = 10;
  156.     }else if(digit==-6){
  157.       (intern) ? digit = 14 : digit = 11;
  158.     }else{
  159.       return; // -1 in store[]?
  160.     }
  161.   }else if(digit==12){ // 85ms for international trunk seizing
  162.     duration = 200;
  163.   }
  164.   freq[0].play(bb[digit][0],duration);
  165.   freq[1].play(bb[digit][1],duration);
  166.   (autoDial) ? delay(duration + 60) : delay(duration); // ? expression? statement?
  167.   if(rec){ delay(25); sf(2600,33); }// chirp to signify recording
  168.   return; // Now we can leave.
  169. }
  170. // play SF:
  171. void sf(int frequency,int duration){ // play single frequency
  172.   freq[0].play(frequency,duration);
  173.   return;
  174. }
  175. // play red box tones:
  176. void redBox(int coin){ // pass me a button
  177.   int iter;
  178.   int delayMs = 66;
  179.   int rb[2] = {1700,2200};
  180.   switch(coin){
  181.     case 49:
  182.       iter = 5;
  183.       delayMs = 33;
  184.       break;
  185.     case 50:
  186.       iter = 2;
  187.       break;
  188.     case 51:
  189.       iter = 1;
  190.       break;
  191.   }
  192.   for(int i=0;i<iter;i++){
  193.     freq[0].play(rb[0],delayMs);
  194.     freq[1].play(rb[1],delayMs);
  195.     delay(delayMs * 2); // pause for coin and between
  196.   }
  197. }
  198. // play speed dials
  199. void dial(int sd){ // speed dial
  200.   if(rec) return; // we are recording...
  201.   autoDial = 1; // turn on for pauses
  202.   sf(2600,750); // play 2600 1 sec
  203.   delay(2000); // pause
  204.   mf(-13); // KP
  205.   for(int i=0;i<=2;i++){
  206.       mf(speedDial[sd][i]);
  207.   }
  208.   mf(-6); // ST
  209.   autoDial = 0; // turn off pauses
  210.   return;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement