SHARE
TWEET

BlizzyB Firmware v2.5

weaknetlabs Sep 25th, 2014 (edited) 240 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. // firmware version 2.5
  6. //
  7. int bb[16][2] = { // MF 0,1,2,3,4,5,6,7,8,9,kp,st,2400+2600,kp2,st2,ss4 super
  8.   {1300,1500},{700,900},{700,1100},      // 0,1,2
  9.   {900,1100},{700,1300},{900,1300},      // 3,4,5
  10.   {1100,1300},{700,1500},{900,1500},     // 6,7,8
  11.   {1100,1500},{1100,1700},{1500,1700},   // 9,kp,st
  12.   {2600,2400},{1300,1700},{900,1700},    // 2400+2600,kp2,st2
  13.   {2400,2040},                           // ss4 Supervisory
  14. };
  15. int dtmfFreq[8] = { // in Hz
  16.   697,770,852,941,1209,1336,1477,1633  
  17. };
  18. int dtmf[16][2] = {
  19.   {0,4},{0,5},{0,6},{0,7}, // 1,2,3,A
  20.   {1,4},{1,5},{1,6},{1,7}, // 4,5,6,B
  21.   {2,4},{2,5},{2,6},{2,7}, // 7,8,9,C
  22.   {3,4},{3,5},{3,6},{3,7}, // *,0,#,D
  23. };
  24. int ss4[][4] = {
  25.   {0,1,0,1},{1,1,1,0},{1,1,0,1}, // 0,1,2
  26.   {1,1,0,0},{1,0,1,1},{1,0,1,0}, // 3,4,5
  27.   {1,0,0,1},{1,0,0,0},{0,1,1,1}, // 6,7,8
  28.   {0,1,1,0},{0,0,0,0},{0,1,0,0}, // 9,KP,OP11
  29.   {0,0,1,1},                     // OP12
  30. };
  31. uint8_t speedDial[][3] = { // Auto dial hold digits
  32.   {1,2,1},{1,0,1},{1,2,1}, // 0,1,2
  33.   {1,3,1},{1,4,1},{1,0,5}, // 3,4,5
  34.   {6,6,6},{1,0,7},{1,8,1}, // 6,7,8
  35.   {1,0,9}
  36. };
  37. uint8_t bbdur[2] = {60,100}; // 75 ms for MF tones, 120 for KP/ST
  38. int ss4Tone[2] = {2040,2400}; // tones for 0,1 respectively
  39. char keys[4][4] = {
  40.   {'1','2','3','a'},
  41.   {'4','5','6','b'},
  42.   {'7','8','9','c'},
  43.   {'#','0','*','d'}
  44. };
  45. byte rowPins[4] = {5,4,3,2}; //connect to the row pinouts of the keypad
  46. byte colPins[4] = {9,8,7,6}; //connect to the column pinouts of the keypad
  47. // global objects
  48. Tone freq[2]; // array of Tone objects, now we can play as freq[0].play(); etc
  49. Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
  50. boolean rec = 0; // recording on/off
  51. boolean stored = 0; // stored digits?
  52. boolean autoDial = 0; // are we playing stored ANY didgits?
  53. int mode = 0; // 0 for MF, 1 for intern, 2 for SS4, 3 for DP, 4 for DTMF
  54. // the storage of integers MUST be integers (int):
  55. 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};
  56. // call set up function to set up pins:
  57. void setup(void){ // Start up instructions:
  58.   freq[0].begin(11); // Initialize our first tone generator
  59.   freq[1].begin(12); // Initialize our second tone generator
  60.   keypad.setHoldTime(1500); // hold for two seconds to change state to HOLD
  61.   pinMode(10, INPUT); // 2600 button
  62.   pinMode(13, OUTPUT); // LED for recording
  63.   keypad.addEventListener(procButton);
  64.   notify(); // boot successful
  65.   Serial.begin(9600);
  66. }
  67. // our main() function:
  68. void loop(void){ // Here we just get the button, pressed or held, and 2600 switch
  69.   char button = keypad.getKey(); // check for button press
  70.   if(digitalRead(10)==HIGH){ // play 2600Hz if top button pressed
  71.     super(); // supervisory signalling
  72.   }
  73.   return; // end main()
  74. }
  75. // Supervisory (TOP) button
  76. void super(void){
  77.   if(mode==1){ // international seizure of trunk
  78.     mf(12);
  79.     delay(1337);
  80.     sf(2400,750);
  81.   }else if(mode==2){ // SS4 Supervisory Signal
  82.     mf(15);
  83.     delay(150);
  84.     sf(2040,350);
  85.   }else{ // non international
  86.     sf(2600,750);
  87.   }
  88.   return;
  89. }
  90. // Process buttons:
  91. void procButton(KeypadEvent b){
  92.   b -= 48;
  93.   switch (keypad.getState()){
  94.     case RELEASED: // drop right away
  95.       return;
  96.     case PRESSED: // momentary
  97.       if(mode==2){ // Signal Switching 4
  98.         ss4Signal(b);
  99.         break;
  100.       }else if(mode==3){
  101.         pulse(b); // pulse it
  102.         return;
  103.       }
  104.       if(mode==4&&(b<10&&b>=0||b==-13||b==-6||(b>=49&&b<=52))){ // MF tone
  105.         mf(b);
  106.       }
  107.       if(b<10&&b>=0||b==-13||b==-6){ // MF tone
  108.         mf(b);
  109.       }else if(b==52){ // D
  110.         if (stored) playStored(); // don't copy function onto stack if not needed
  111.         return;
  112.       }else if(mode==1){ // international kp2/st2
  113.         if(b>=49&&b<=51){
  114.           mf(b);
  115.           return;
  116.         }
  117.       }
  118.       else if(mode==0&&(b<=51&&b>=49)){ // A,B,C redbox
  119.         redBox(b); // pass it to RedBox()
  120.         return;
  121.       }
  122.       break;
  123.     case HOLD: // HELD (special functions)
  124.       if(b<10&&b>=0||b==-13||b==-6){
  125.         dial(b);
  126.       }else if(b==51){ // C takes care of recording now
  127.         if(rec){ // we are done recording:
  128.           digitalWrite(13, LOW); // turn off LED
  129.           rec = 0;
  130.           stored=1; // we have digits stored
  131.           recNotify();
  132.         }else{ // we start recording
  133.           digitalWrite(13, HIGH); // light up LED
  134.           rec = 1;
  135.           for(int i=0;i<=23;i++){ // reset array
  136.             store[i] = -1;
  137.           }
  138.           recNotify();
  139.         } // END recording code
  140.       }else if(b==49){ // ('A' HELD) switching any mode "on" changes to mode, all "off" is domestic
  141.         if(mode==0){ // mf to international
  142.           mode=1;
  143.         }else if(mode==1){ // international to ss4 mode
  144.           mode=2;
  145.         }else if(mode==2){ // ss4 mode to pulse mode
  146.           mode=3;
  147.         }else if(mode==3){ // pulse mode to DTMF
  148.           mode=4;
  149.         }else if(mode==4){ // DTMF to domestic
  150.           mode=0;
  151.         }
  152.         notifyMode();
  153.         return;
  154.       }      
  155.       break;
  156.   }
  157.   return;
  158. }
  159. // pulse mode
  160. void pulse(int signal){
  161.   if(signal==49) freq[0].play(300,200);
  162.   if(signal>9) { return; }
  163.   if(signal==-13||signal==-6){
  164.     return;
  165.   }else{
  166.     if(signal==0){ signal = 10; }
  167.     for(int i=0;i<signal;i++){
  168.       digitalWrite(13, HIGH); // pulsing LED
  169.       freq[0].play(2600,66);
  170.       delay(66);
  171.       digitalWrite(13, LOW);
  172.       delay(34);
  173.     }
  174.     delay(500); // no new digit accepted until after this time
  175.   }
  176.   return;
  177. }
  178. // play stored tones
  179. void playStored(void){
  180.   if(stored){
  181.     autoDial = 1;
  182.     for(int i=0;i<=23;i++){
  183.       if(store[i]==-1){
  184.         return;
  185.       }else{
  186.         mf(store[i]);  
  187.       }    
  188.     }  
  189.   }else{
  190.     return;
  191.   }
  192.   autoDial = 0; // turn off playing
  193.   return;
  194. }
  195. // Record Notification tone:
  196. void recNotify(void){
  197.   if(rec){
  198.     sf(1700,66);
  199.     delay(66);
  200.     sf(2200,500);
  201.     delay(500);
  202.   }else{
  203.     sf(2200,66);
  204.     delay(66);
  205.     sf(1700,500);
  206.     delay(500);  
  207.   }
  208.   return;
  209. }
  210. // Mode notification
  211. void notifyMode(){
  212.   int count = 1;
  213.   int dur = 70;
  214.   int frequency = 440;
  215.   if(mode==1){ count = 2; }
  216.   if(mode==2){ count = 3; }
  217.   if(mode==3){ count = 4; }
  218.   if(mode==4){ count = 5; }
  219.   for(int i = 0;i<count;i++){
  220.     freq[0].play(frequency,dur);
  221.     delay(dur*2);
  222.   }
  223. }
  224.  
  225. // Notification Tone:
  226. void notify(void){
  227.   Serial.println(mode);
  228.   for(int i=0;i<=2;i++){
  229.     freq[0].play(2600,33);
  230.     delay(66);
  231.   }
  232.   delay(500);
  233.   return;
  234. }
  235. // SS4 signalling:
  236. void ss4Signal(int signal){
  237.   if(signal==49){ // A  
  238.     mf(15);
  239.     delay(250);
  240.     sf(2400,350);
  241.     return;
  242.   }
  243.   if(signal==50){// B
  244.     signal==11;
  245.   }
  246.   if(signal==51){ // C
  247.     signal==12;
  248.   }
  249.   if(signal==52){ // D
  250.     sf(2600,750);
  251.     return;
  252.   }
  253.  if(signal==-13){ signal = 10; }
  254.  if(signal==-6){ //
  255.   super();
  256.   return;
  257.  }
  258.  for(int i=0;i<=3;i++){
  259.    (ss4[signal][i]) ? freq[0].play(ss4Tone[1],35) : freq[0].play(ss4Tone[0],35);
  260.    delay(70);
  261.  }
  262.  return;
  263. }
  264. // play an MF tone:
  265. void mf(int digit){ // Store the digit IFF recording:
  266.   if(rec && ((digit>=0&&digit<=9)||digit==-13||digit==-6)){
  267.     for(int i=0;i<=23;i++){ // ONLY record KP,ST,0-9
  268.       if(store[i]==-1){
  269.         store[i]=digit;
  270.         break;
  271.       }
  272.     }
  273.   }
  274.   int duration = bbdur[0]; // OKAY for DTMF too
  275.   if(mode==1){
  276.     if(digit==49){ // international mode A
  277.       digit=13;
  278.     }
  279.     if(digit==50){ // international mode B
  280.       digit=14;
  281.     }
  282.     if(digit==51){
  283.      sf(2600,1000);
  284.      return;
  285.     }
  286.   }else if(mode==4){ // DTMF
  287.     Serial.println(digit);
  288.     if((digit>=1&&digit<=3)) { // 1,2,3
  289.       digit-=1;
  290.     }else if(digit>=7&&digit<=9){
  291.       digit+=1;
  292.     }else if(digit==49){
  293.       digit = 3; // A
  294.     }else if(digit==50){
  295.       digit = 7; // B
  296.     }else if(digit==51){
  297.       digit = 11;  // C
  298.     }else if(digit==52){
  299.       digit = 15;  // D
  300.     }else if(digit==-13){
  301.       digit = 12;  // *
  302.     }else if(digit==-6){
  303.       digit = 14;  // #
  304.     }else if(digit==0){
  305.       digit = 13; // 0
  306.     }else if(digit==13){ duration=150; }
  307.     Serial.println("and: ");
  308.     Serial.println(digit);
  309.     freq[0].play(dtmfFreq[dtmf[digit][0]],duration);
  310.     freq[1].play(dtmfFreq[dtmf[digit][1]],duration);
  311.     return;
  312.   }
  313.   if(digit<0){
  314.     duration = bbdur[1];
  315.     if(digit==-13){ digit=10; }
  316.     if(digit==-6){ digit=11; }
  317.   }
  318.   if(digit==-1){ return; } // -1 in storage?
  319.   if(digit==12){ // 85ms for international trunk seizing
  320.     duration = 200;
  321.   }
  322.   if (mode==3) duration = 150; // SS4 Supervisory MF (150ms)
  323.   freq[0].play(bb[digit][0],duration);
  324.   freq[1].play(bb[digit][1],duration);
  325.   (autoDial) ? delay(duration + 60) : delay(duration); // ? expression? statement?
  326.   if(rec){
  327.     delay(25);
  328.     sf(2600,33);
  329.   }// chirp to signify recording
  330.   return; // Now we can leave.
  331. }
  332. // play SF:
  333. void sf(int frequency,int duration){ // play single frequency
  334.   freq[0].play(frequency,duration);
  335.   return;
  336. }
  337. // play red box tones:
  338. void redBox(int coin){ // pass me a button
  339.   int iter;
  340.   int delayMs = 66;
  341.   int rb[2] = {1700,2200};
  342.   switch(coin){
  343.     case 49:
  344.       iter = 5;
  345.       delayMs = 33;
  346.       break;
  347.     case 50:
  348.       iter = 2;
  349.       break;
  350.     case 51:
  351.       iter = 1;
  352.       break;
  353.   }
  354.   for(int i=0;i<iter;i++){
  355.     freq[0].play(rb[0],delayMs);
  356.     freq[1].play(rb[1],delayMs);
  357.     delay(delayMs * 2); // pause for coin and between
  358.   }
  359. }
  360. // play speed dials
  361. void dial(int sd){ // speed dial
  362.   if(rec) return; // we are recording...
  363.   autoDial = 1; // turn on for pauses
  364.   sf(2600,750); // play 2600 1 sec
  365.   delay(2000); // pause
  366.   mf(-13); // KP
  367.   for(int i=0;i<=2;i++){
  368.       mf(speedDial[sd][i]);
  369.   }
  370.   mf(-6); // ST
  371.   autoDial = 0; // turn off pauses
  372.   return;
  373. }
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