SHARE
TWEET

BlizzyB Firmware v2.5b

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