SHARE
TWEET

BlizzyB Firmware v2.1

weaknetlabs Sep 23rd, 2014 159 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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[16][2] = { // MF 0,1,2,3,4,5,6,7,8,9,kp,st,2400+2600,kp2,st2,ss4 super
  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.   {2400,2040},
  12. };
  13. int ss4[][4] = {
  14.   {0,1,0,1},{1,1,1,0},{1,1,0,1},
  15.   {1,1,0,0},{1,0,1,1},{1,0,1,0},
  16.   {1,0,0,1},{1,0,0,0},{0,1,1,1},
  17.   {0,1,1,0},
  18. };
  19. uint8_t speedDial[][3] = { // Auto dial hold digits
  20.   {1,2,1},{1,0,1},{1,2,1}, // 0,1,2
  21.   {1,3,1},{1,4,1},{1,0,5}, // 3,4,5
  22.   {6,6,6},{1,0,7},{1,8,1}, // 6,7,8
  23.   {1,0,9}
  24. };
  25. uint8_t bbdur[2] = {60,100}; // 75 ms for MF tones, 120 for KP/ST
  26. int ss4Tone[2] = {2040,2400}; // tones for 0,1 respectively
  27. char keys[4][4] = {
  28.   {'1','2','3','a'},
  29.   {'4','5','6','b'},
  30.   {'7','8','9','c'},
  31.   {'#','0','*','d'}
  32. };
  33. byte rowPins[4] = {5,4,3,2}; //connect to the row pinouts of the keypad
  34. byte colPins[4] = {9,8,7,6}; //connect to the column pinouts of the keypad
  35. // global objects
  36. Tone freq[2]; // array of Tone objects, now we can play as freq[0].play(); etc
  37. Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
  38. boolean rec = 0; // recording on/off
  39. boolean stored = 0; // stored digits?
  40. boolean autoDial = 0; // are we playing stored ANY didgits?
  41. boolean intern = false; // international trunk seizure
  42. boolean ss4Mode = false; // SS4 mode
  43. // the storage of integers MUST be integers (int):
  44. 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};
  45. // call set up function to set up pins:
  46. void setup(void){ // Start up instructions:
  47.   freq[0].begin(11); // Initialize our first tone generator
  48.   freq[1].begin(12); // Initialize our second tone generator
  49.   keypad.setHoldTime(1500); // hold for two seconds to change state to HOLD
  50.   pinMode(10, INPUT); // 2600 button
  51.   pinMode(13, OUTPUT); // LED for recording
  52.   keypad.addEventListener(procButton);
  53.   notify(); // boot successful
  54.   Serial.begin(9600);
  55. }
  56. // our main() function:
  57. void loop(void){ // Here we just get the button, pressed or held, and 2600 switch
  58.   char button = keypad.getKey(); // check for button press
  59.   if(digitalRead(10)==HIGH){ // play 2600Hz if top button pressed
  60.     super(); // supervisory signalling
  61.   }
  62.   return; // end main()
  63. }
  64. // Supervisory (TOP) button
  65. void super(void){
  66.   if(intern){ // international seizure of trunk
  67.     mf(12);
  68.     delay(1337);
  69.     sf(2400,750);
  70.   }else if(ss4Mode){ // SS4 Supervisory Signal
  71.     mf(15);
  72.     delay(150);
  73.     sf(2040,350);
  74.   }else{ // non international
  75.     sf(2600,750);
  76.   }
  77.   return;
  78. }
  79. // Process buttons:
  80. void procButton(KeypadEvent b){
  81.   b -= 48;
  82.   switch (keypad.getState()){
  83.     case RELEASED: // drop right away
  84.       return;
  85.     case PRESSED: // momentary
  86.       if(ss4Mode){ // Signal Switching 4
  87.         if(b<10&&b>=0||b==-13||b==-6){
  88.           ss4Signal(b);
  89.           break;
  90.         }
  91.       }
  92.       if(b<10&&b>=0||b==-13||b==-6){ // MF tone
  93.         mf(b);
  94.       }else if(b==52){ // D
  95.         if (stored) playStored(); // don't copy function onto stack if not needed
  96.       }else if(b<=51&&b>=49){ // A,B,C redbox
  97.         redBox(b); // pass it to RedBox()
  98.       }
  99.       break;
  100.     case HOLD: // HELD (special functions)
  101.       if(b<10&&b>=0||b==-13||b==-6){
  102.         dial(b);
  103.       }else if(b==51){ // C takes care of recording now
  104.         if(rec){ // we are done recording:
  105.           digitalWrite(13, LOW); // turn off LED
  106.           rec = 0;
  107.           stored=1; // we have digits stored
  108.           recNotify();
  109.         }else{ // we start recording
  110.           digitalWrite(13, HIGH); // light up LED
  111.           rec = 1;
  112.           for(int i=0;i<=23;i++){ // reset array
  113.             store[i] = -1;
  114.           }
  115.           recNotify();
  116.         } // END recording code
  117.       }else if(b==49){ // switching any mode "on" changes to mode, all "off" is domestic
  118.         if(intern){ // international to ss4 mode
  119.           intern = false;
  120.           ss4Mode = true;
  121.           Serial.println("SS4 mode");
  122.         }else if(ss4Mode){ // ss4 mode to domestic
  123.           ss4Mode = false;
  124.           Serial.println("domestic mode");
  125.         }else{ // domestic to international mode
  126.           intern = true;
  127.           Serial.println("international mode");
  128.         }
  129.         notify();
  130.       }      
  131.       break;
  132.   }
  133.   return;
  134. }
  135. // play stored tones
  136. void playStored(void){
  137.   if(stored){
  138.     autoDial = 1;
  139.     for(int i=0;i<=23;i++){
  140.       if(store[i]==-1){
  141.         return;
  142.       }else{
  143.         mf(store[i]);  
  144.       }    
  145.     }  
  146.   }else{
  147.     return;
  148.   }
  149.   autoDial = 0; // turn off playing
  150.   return;
  151. }
  152. // Record Notification tone:
  153. void recNotify(void){
  154.   if(rec){
  155.     sf(1700,66);
  156.     delay(66);
  157.     sf(2200,500);
  158.     delay(500);
  159.   }else{
  160.     sf(2200,66);
  161.     delay(66);
  162.     sf(1700,500);
  163.     delay(500);  
  164.   }
  165.   return;
  166. }
  167. // Notification Tone:
  168. void notify(void){
  169.   for(int i=0;i<=2;i++){
  170.     freq[0].play(2600,33);
  171.     delay(66);
  172.   }
  173.   delay(500);
  174.   return;
  175. }
  176. // SS4 signalling:
  177. void ss4Signal(int signal){
  178.  for(int i=0;i<=3;i++){
  179.    (ss4[signal][i]) ? freq[0].play(ss4Tone[1],35) : freq[0].play(ss4Tone[0],35);
  180.    delay(70);
  181.  }
  182.  return;
  183. }
  184. // play an MF tone:
  185. void mf(int digit){ // Store the digit IFF recording:
  186.   if(rec && ((digit>=0&&digit<=9)||digit==-13||digit==-6)){
  187.     for(int i=0;i<=23;i++){ // ONLY record KP,ST,0-9
  188.       if(store[i]==-1){
  189.         store[i]=digit;
  190.         break;
  191.       }
  192.     }
  193.   }
  194.   int duration = bbdur[0];
  195.   if(digit<0){
  196.     duration = bbdur[1];
  197.     if(digit==-13){
  198.       (intern) ? digit = 13 : digit = 10;
  199.     }else if(digit==-6){
  200.       (intern) ? digit = 14 : digit = 11;
  201.     }else{
  202.       return; // -1 in store[]?
  203.     }
  204.   }else if(digit==12){ // 85ms for international trunk seizing
  205.     duration = 200;
  206.   }
  207.   if (ss4Mode) duration = 150;
  208.   freq[0].play(bb[digit][0],duration);
  209.   freq[1].play(bb[digit][1],duration);
  210.   (autoDial) ? delay(duration + 60) : delay(duration); // ? expression? statement?
  211.   if(rec){
  212.     delay(25);
  213.     sf(2600,33);
  214.   }// chirp to signify recording
  215.   return; // Now we can leave.
  216. }
  217. // play SF:
  218. void sf(int frequency,int duration){ // play single frequency
  219.   freq[0].play(frequency,duration);
  220.   return;
  221. }
  222. // play red box tones:
  223. void redBox(int coin){ // pass me a button
  224.   int iter;
  225.   int delayMs = 66;
  226.   int rb[2] = {1700,2200};
  227.   switch(coin){
  228.     case 49:
  229.       iter = 5;
  230.       delayMs = 33;
  231.       break;
  232.     case 50:
  233.       iter = 2;
  234.       break;
  235.     case 51:
  236.       iter = 1;
  237.       break;
  238.   }
  239.   for(int i=0;i<iter;i++){
  240.     freq[0].play(rb[0],delayMs);
  241.     freq[1].play(rb[1],delayMs);
  242.     delay(delayMs * 2); // pause for coin and between
  243.   }
  244. }
  245. // play speed dials
  246. void dial(int sd){ // speed dial
  247.   if(rec) return; // we are recording...
  248.   autoDial = 1; // turn on for pauses
  249.   sf(2600,750); // play 2600 1 sec
  250.   delay(2000); // pause
  251.   mf(-13); // KP
  252.   for(int i=0;i<=2;i++){
  253.       mf(speedDial[sd][i]);
  254.   }
  255.   mf(-6); // ST
  256.   autoDial = 0; // turn off pauses
  257.   return;
  258. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top