Advertisement
weaknetlabs

BlizzyB Firmware v2.2a

Sep 24th, 2014
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 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[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){ // ('A' HELD) 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.         }else if(ss4Mode){ // ss4 mode to domestic
  122.           ss4Mode = false;
  123.         }else{ // domestic to international mode
  124.           intern = true;
  125.         }
  126.         notifyMode();
  127.       }      
  128.       break;
  129.   }
  130.   return;
  131. }
  132. // play stored tones
  133. void playStored(void){
  134.   if(stored){
  135.     autoDial = 1;
  136.     for(int i=0;i<=23;i++){
  137.       if(store[i]==-1){
  138.         return;
  139.       }else{
  140.         mf(store[i]);  
  141.       }    
  142.     }  
  143.   }else{
  144.     return;
  145.   }
  146.   autoDial = 0; // turn off playing
  147.   return;
  148. }
  149. // Record Notification tone:
  150. void recNotify(void){
  151.   if(rec){
  152.     sf(1700,66);
  153.     delay(66);
  154.     sf(2200,500);
  155.     delay(500);
  156.   }else{
  157.     sf(2200,66);
  158.     delay(66);
  159.     sf(1700,500);
  160.     delay(500);  
  161.   }
  162.   return;
  163. }
  164. // Mode notification
  165. void notifyMode(){
  166.   int count = 1;
  167.   int dur = 70;
  168.   int frequency = 440;
  169.   if(intern){ count = 2; }
  170.   if(ss4Mode){ count = 3; }
  171.  Serial.println(count);
  172.   for(int i = 0;i<count;i++){
  173.     freq[0].play(frequency,dur);
  174.     delay(dur*2);
  175.   }
  176. }
  177.  
  178. // Notification Tone:
  179. void notify(void){
  180.   for(int i=0;i<=2;i++){
  181.     freq[0].play(2600,33);
  182.     delay(66);
  183.   }
  184.   delay(500);
  185.   return;
  186. }
  187. // SS4 signalling:
  188. void ss4Signal(int signal){
  189.  for(int i=0;i<=3;i++){
  190.    (ss4[signal][i]) ? freq[0].play(ss4Tone[1],35) : freq[0].play(ss4Tone[0],35);
  191.    delay(70);
  192.  }
  193.  return;
  194. }
  195. // play an MF tone:
  196. void mf(int digit){ // Store the digit IFF recording:
  197.   if(rec && ((digit>=0&&digit<=9)||digit==-13||digit==-6)){
  198.     for(int i=0;i<=23;i++){ // ONLY record KP,ST,0-9
  199.       if(store[i]==-1){
  200.         store[i]=digit;
  201.         break;
  202.       }
  203.     }
  204.   }
  205.   int duration = bbdur[0];
  206.   if(digit<0){
  207.     duration = bbdur[1];
  208.     if(digit==-13){
  209.       (intern) ? digit = 13 : digit = 10;
  210.     }else if(digit==-6){
  211.       (intern) ? digit = 14 : digit = 11;
  212.     }else{
  213.       return; // -1 in store[]?
  214.     }
  215.   }else if(digit==12){ // 85ms for international trunk seizing
  216.     duration = 200;
  217.   }
  218.   if (ss4Mode) duration = 150;
  219.   freq[0].play(bb[digit][0],duration);
  220.   freq[1].play(bb[digit][1],duration);
  221.   (autoDial) ? delay(duration + 60) : delay(duration); // ? expression? statement?
  222.   if(rec){
  223.     delay(25);
  224.     sf(2600,33);
  225.   }// chirp to signify recording
  226.   return; // Now we can leave.
  227. }
  228. // play SF:
  229. void sf(int frequency,int duration){ // play single frequency
  230.   freq[0].play(frequency,duration);
  231.   return;
  232. }
  233. // play red box tones:
  234. void redBox(int coin){ // pass me a button
  235.   int iter;
  236.   int delayMs = 66;
  237.   int rb[2] = {1700,2200};
  238.   switch(coin){
  239.     case 49:
  240.       iter = 5;
  241.       delayMs = 33;
  242.       break;
  243.     case 50:
  244.       iter = 2;
  245.       break;
  246.     case 51:
  247.       iter = 1;
  248.       break;
  249.   }
  250.   for(int i=0;i<iter;i++){
  251.     freq[0].play(rb[0],delayMs);
  252.     freq[1].play(rb[1],delayMs);
  253.     delay(delayMs * 2); // pause for coin and between
  254.   }
  255. }
  256. // play speed dials
  257. void dial(int sd){ // speed dial
  258.   if(rec) return; // we are recording...
  259.   autoDial = 1; // turn on for pauses
  260.   sf(2600,750); // play 2600 1 sec
  261.   delay(2000); // pause
  262.   mf(-13); // KP
  263.   for(int i=0;i<=2;i++){
  264.       mf(speedDial[sd][i]);
  265.   }
  266.   mf(-6); // ST
  267.   autoDial = 0; // turn off pauses
  268.   return;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement