// You need to see the main() function at the end of this file // and change the login user name and password. // other changes include setting some values as 'const' in order to // free up some ram for use as the keyboard buffer. // Operation // main first outputs a username, password and 'asterisk -r' to the serial // port, before entering an endless loop. // main() polls scankeys() for keyboard keys and stores them in a buffer // untill an 'F' is detected - this indicates keys are no longer being // typed and the stored string may be sent with the a 'dial ' command. // scankeys() also checks the hook switch and returns 'A' (answer) // when the handset is lifted and 'J' when it is replaced. // whilst scankeys() is polling the keyboard it calls pause(). // in addition to allowing time for the i/o lines to settle, pause() // handles the phone led off, light and bright functions. // #include "init.h" // included by C-Wiz #include #include #include void putch(unsigned char); unsigned char getch(void); unsigned char getche(void); const char pattern[8]= 0b11101110,0b11111101, 0b11111011, 0b11110111, 0b11101111, 0b11011111 , 0b10111111, 0b01111111 ; unsigned char keys[8]; // translation table 0x00 = dead key, any others // the value to transmit const unsigned char kx1[5] = 'B','C','3','2','1' ; const unsigned char kx2[5] = 'D','E','6','5','4' ; const unsigned char kx3[5] = 'F','G','9','8','7' ; const unsigned char kx4[5] = 'H','I','#','0','*' ; static bit blanks; static bit hooksw; // handset up or down? #define LED RB0 // the phone LED is wired to port B0 char lastkey; // last key pressed char buffcount ; // number of keys stored in buffer char numberbuffer[16] ; // where we store numbers to send void serout(char) ; // our tx function char keyscan(void); // returns 0 or the key pressed long keytimer; // timer ticks long tickl ; // the led stuff static bit ledon ; static bit ledhigh ; // some i/o routines void serout(char c) { while (!TRMT) continue; // wait for uart tx ready TXREG = c; // send the char } void pause (void) // short delay - actualy, do leds { // here bump the counters tickl++; if (tickl >= 100) tickl = 0 ; // they keep counting if (ledon ==1) { if (ledhigh ==1) { LED = 0; } if (ledhigh ==0) { if (tickl <= 50) LED = 0 ; else LED = 1; } } if (ledon == 0) LED=1; } char keyscan(void) { // keypad is connected on PORTC[0 - 4] and PORTB[4 - 7] // we put a 0 on the PORTC line we are scanning and // after a short delay, read PORTB // the values for each PORTC line are stored and then // checked for a key press a 0 on one of the PORTB lines // means a key was pressed. We use PORTB's Pull up resistors. // We could use other input lines but would have to add // our own resistors. // PORTB1 is the hook switch int c; int row; int col; blanks = 0; c = 0; while ((c == 0) || (blanks == 0)) { for (row = 0; row < 5 ; row++) { //LED = 0; PORTC = pattern[row] ; pause(); c = PORTB ; PORTC = 0xff ; pause(); c = c & 0xf0 ; //upper nibble keys[row] = c ^ 0xf0; // 1,2,4,8, if pressed //LED = 1; } // scanned the kb into keys[] // here we could process dead keys and multiple key // presses (shift, function, cmd etc // here we will just check for normal keys c=0; for (row = 0; row < 6 ; row++) { if (keys[row] & 0b00010000 ) c = kx1[row] ; if (keys[row] & 0b00100000 ) c = kx2[row]; if (keys[row] & 0b01000000 ) c = kx3[row]; if (keys[row] & 0b10000000 ) c = kx4[row]; } // c contains 0 or the last valid key press we found if (c != 0 ) { ledhigh=1 ; keytimer = 0; } //number keys timeout reset if (c == 0 ) { blanks=1 ; // no key pressed keytimer++; if (keytimer > 8192) { keytimer=0; c='F'; } //flush key buffer - return // with 'F' to indicate that dialing has paused and any keys should now // be sent. // handle hook switch if ((hooksw == 1) && (RB1 == 0)) { hooksw = 0; c = 'J'; ledon = 0; } if ((hooksw == 0) && (RB1 == 1)) { hooksw = 1; c = 'A'; ledon = 1;} } // if c!= 0 it is a valid key and we will continue } return c; // return the key } void putch(unsigned char byte) { unsigned char c; int d; /* output one byte */ while(!TRMT) /* set when register is empty */ continue; TXREG = byte; //for (c = 0; c< 128 ; c++) { d++; d--; d++; d--; } } unsigned char getch() { /* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; return RCREG; } unsigned char getche(void) { unsigned char c; putch(c = getch()); return c; } void main(void) { char c; init(); // Function call inserted by C-Wiz buffcount = 0; // 0 chars in the number buffer keytimer=0; // reset the key timer ledon = 0; ledhigh = 0; //TODO Auto-generated main function // LED = 0; pause(); printf("%s","YourAsteriskLoginName\n"); // this is the LOGIN USER NAME pause(); printf("%s","YourPassword\n"); // this is the USER PASSWORD pause(); printf ("%s","asterisk -r\n"); // Run ASTERISK console on this terminal pause(); // // note. We do not check the above worked, we just carry on and hope it was. // If the phone is un-able to make calls, un-plug/reset and try again. It // should be noted that as the username, password, dial and hangup commands are // sent to a terminal that may already be logged in, you should check that they //do not correspond to any linux program names. // For asterisk console, we have to press a 'dial' button, then // the number and then a return key while (1) { c = keyscan(); // changed this to auto return on time out! if ((c == 'F') && (buffcount > 0)) // timeout && keys in buffer? { ledhigh = 0; //here due to a key buff timeout numberbuffer[buffcount] = 0; // mark end of string (for printf) printf ("%s","Dial "); printf ("%s",numberbuffer); printf ("%c",0x0d); c = 0; buffcount=0; //clear line buff - ready for next time } if (c == 'F') c = 0; // here because time out // handle the number keys if ( (( c == '*' ) || ( c == '#') || (( c >= '0' ) && ( c <= '9')) ) && (ledon = 1)) // numbers,#,* & led on (off hook?) { numberbuffer[buffcount++] = c ; if (buffcount > 15) buffcount=15 ; c = 0; } if (c == 'A') { printf ("%s","answer \n") ; c = 0; } //handset picked up if (c == 'J') { printf ("%s","hangup \n") ; // handset put down c = 0; ledhigh=0; buffcount = 0;} // handset put down // We do not need any other function keys to dial numbers (now we have // the keyboard buffer, you can assign the other buttons for extra functions. //if (c == 'I') { printf ("%s","\n") ; c = 0; } // PAUSE/REDIAL press return key if (c == 'G') { printf ("%s","Dial 9000\n") ; c = 0; } //MEM 'Dial' voicemail //if (c == 'H') { printf ("%c",0x08) ; c = 0; } //STORE = BS/Delete if (c!= 00) putch (c); // send a single char if not handled with the above } //end of main() }