import stamp.core.*; /* This program receives and decodes DTMF tones via an external DTMF decoder. When the menu item has been choosen, the transceiver asks for password. When the password has been entered and is correct, it performs actions. Menu *1## Switch port 1 On #1## Switch port 1 Off ### Switch all ports Off **PIN# Enter password C Cancel menu code ABD Quit main loop (not working yet) */ public class Medina{ // CONSTANTS final static int serialPin = CPU.pin2; // MT3201 DTMF chip serial receive final static int ackPin = CPU.pin1; // MT3201 DTMF Ack send final static int estPin = CPU.pin0; // MT3201 DTMF Est/Dstd receive final static int ledPin = CPU.pin6; // LED diode final static int relPin = CPU.pin8; // pin5 LBA110 relay control (Radio TX) final static int portRelPin1 = CPU.pin9; // LBA110 Relay control for port 1 static boolean doMainLoop = true; static boolean bit1 = false; // Binary code from MT3210 static boolean bit2 = false; // Binary code from MT3210 static boolean bit3 = false; // Binary code from MT3210 static boolean bit4 = false; // Binary code from MT3210 static String binBuf = new String(); // Binary 4 bit buffer static String menBuf = new String(); // Menu buffer static boolean autOK = false; // User has entered valid password static int menuItem = 0; // The menu item that has been choosen static boolean menuItemStored = false; // User has selected menu item static boolean reqAut=false; // Password request has been sendt static boolean mLock=false; // Menu lock, false by default // MAIN public static void main(){ init(); while (doMainLoop == true){ if (CPU.readPin(estPin)==true){ readSerial(); // Read binary code from serial convertBuf(); // Convert binary code to ASCII menuSystem(); // Manage menu system } else{} } } // INITIALIZATION static void init(){ portRel1Off(); txOff(); CPU.writePin(ledPin, false); CPU.delay(4000); CPU.writePin(ledPin, true); System.out.println("init: Ready"); } // READ BINARY CODE FROM SERIAL CONNECTION static void readSerial(){ int maxX = 4; for (int x = 0; x < maxX; x++){ // SHIFT IN CPU.writePin(ackPin, true); // start Ack pulse CPU.delay(10); // Read delay if (CPU.readPin(serialPin)==true){ // Read serial binBuf=binBuf+"1"; // Append char } else { binBuf=binBuf+"0"; // Append char } CPU.writePin(ackPin, false); // End Ack pulse CPU.delay(10); } System.out.println("readSerial: binBuf: "+binBuf); CPU.delay(6000); // Delay between reception of each DTMF tone //sendReceipt(); } // SEND RECEIPT OVER THE RADIO static void sendReceipt(){ CPU.delay(2000); txOn(); CPU.delay(8000); txOff(); System.out.println("sendReceipt: Sendt"); } // CONVERT BINARY BUFFER INTO ASCI BUFFER: MENBUFFER static void convertBuf(){ flashLED(); if (binBuf.equals("1000")){ // digit 1 menBuf=menBuf+"1"; System.out.println("DTMF digit 1"); }else if (binBuf.equals("0100")){ // digit 2 menBuf=menBuf+"2"; System.out.println("DTMF digit 2"); }else if (binBuf.equals("1100")){ // digit 3 menBuf=menBuf+"3"; System.out.println("DTMF digit 3"); }else if (binBuf.equals("0010")){ // digit 4 menBuf=menBuf+"4"; System.out.println("DTMF digit 4"); }else if (binBuf.equals("1010")){ // digit 5 menBuf=menBuf+"5"; System.out.println("DTMF digit 5"); }else if (binBuf.equals("0110")){ // digit 6 menBuf=menBuf+"6"; System.out.println("DTMF digit 6"); }else if (binBuf.equals("1110")){ // digit 7 menBuf=menBuf+"7"; System.out.println("DTMF digit 7"); }else if (binBuf.equals("0001")){ // digit 8 menBuf=menBuf+"8"; System.out.println("DTMF digit 8"); }else if (binBuf.equals("1001")){ // digit 9 menBuf=menBuf+"9"; System.out.println("DTMF digit 9"); }else if (binBuf.equals("0101")){ // digit 0 menBuf=menBuf+"0"; System.out.println("DTMF digit 0"); }else if (binBuf.equals("1101")){ // digit * menBuf=menBuf+"*"; System.out.println("DTMF digit *"); }else if (binBuf.equals("0011")){ // digit # menBuf=menBuf+"#"; System.out.println("DTMF digit #"); }else if (binBuf.equals("1011")){ // digit A menBuf=menBuf+"A"; System.out.println("DTMF digit A"); }else if (binBuf.equals("0111")){ // digit B menBuf=menBuf+"B"; System.out.println("DTMF digit B"); }else if (binBuf.equals("1111")){ // digit C menBuf=menBuf+"C"; System.out.println("DTMF digit C"); System.out.println("convertBuf: Clear menuBuf"); softReset(); }else if (binBuf.equals("0000")){ // digit D menBuf=menBuf+"D"; System.out.println("DTMF digit D"); }else{System.out.println("convertBuf: Error 1 - mismatch");} binBuf=""; // Clear buffer to make ready for reception of new DTMF tones } // CHOOSE MENU ITEM FROM MENBUFFER MULTI DIGIT BUFFER static void menuSystem(){ System.out.println("menuSystem: menBuf: "+menBuf); if ( autOK == true ){ if ( menuItemStored == true ){ // aut ok, menu stored, perform! sendReceipt(); System.out.println("menuSystem: Perform!"); } else { // NULL aut ok, menu not stored (not possible) } } else { if (menuItemStored == true ){ // aut not ok, menu stored, save password! if (menBuf.equals("**1314#")){ autOK=true; doMenu(); } else { autOK=false; } } else { // aut not ok, menu not stored, store menu! if (menBuf.equals("*1##")){ System.out.println("menuSystem: Menu item 1: *1##"); menuItem=1; menuItemStored=true; menBuf=""; sendReceipt(); } else if (menBuf.equals("#1##")){ System.out.println("menuSystem: Menu item 2: #1##"); menuItem=2; menuItemStored=true; menBuf=""; sendReceipt(); } else if (menBuf.equals("###")){ System.out.println("menuSystem: Menu item 3: ###"); menuItem=3; menuItemStored=true; menBuf=""; sendReceipt(); } else if (menBuf.equals("*99#99#")){ System.out.println("menuSystem: Menu item 4: *99#99#"); menuItem=4; menuItemStored=true; menBuf=""; sendReceipt(); } else if (menBuf.equals("#99#99#")){ System.out.println("menuSystem: Menu item 5: #99#99#"); menuItem=5; menuItemStored=true; menBuf=""; sendReceipt(); } else if (menBuf.equals("*ABD#")){ System.out.println("menuSystem: Menu item 6: *ABD#"); menuItem=6; menuItemStored=true; menBuf=""; sendReceipt(); }else {} } } } // MENU ITEM HAS BEEN CHOOSEN, AUT IS OK, PERFORM THE ACTIONS static void doMenu(){ if (menuItem==1){ sendReceipt(); portRel1On(); System.out.println("doMenu: portRel1 On"); } else if (menuItem==2){ sendReceipt(); portRel1Off(); System.out.println("doMenu: portRel1 Off"); } else if (menuItem==3){ sendReceipt(); System.out.println("doMenu: all portRel's Off"); portRel1Off(); } else if (menuItem==4){ sendReceipt(); System.out.println("doMenu: lock menus"); mLock=true; } else if (menuItem==5){ sendReceipt(); System.out.println("doMenu: unlock menus"); mLock=false; } else if (menuItem==6){ sendReceipt(); System.out.println("doMenu: Quit main loop"); doMainLoop=false; } softReset(); } // CLEAR VARIABLES, RESET SYSTEM AND RELAUNCH static void resetSystem(){ autOK=false; menuItem=0; menuItemStored=false; reqAut=false; binBuf=""; menBuf=""; init(); } // CLEAR VARIABLES AND RELAUNCH, BUT SKIP INIT static void softReset(){ autOK=false; menuItem=0; menuItemStored=false; reqAut=false; binBuf=""; menBuf=""; System.out.println("softReset: Done"); } // ACTIVATE RADIO TRANSMITTER static void txOn(){ CPU.writePin(ledPin, false); CPU.writePin(relPin, false); } // DEACTIVATE RADIO TRANSMITTER static void txOff(){ CPU.writePin(relPin, true); CPU.delay(4500); CPU.writePin(ledPin, true); } //ACTIVATE PORTREL 1 static void portRel1On(){ CPU.writePin(portRelPin1, true); } //DEACTIVATE PORTREL 1 static void portRel1Off(){ CPU.writePin(portRelPin1, false); } // Flash LED static void flashLED(){ CPU.writePin(ledPin, false); CPU.delay(600); CPU.writePin(ledPin, true); } }