#!/usr/bin/python
#****************************************
#   phone_dialer.py
#   by Michael Wright, Y2kLeader.com
#   To execute type: 
#   pythone3 phone_dialer.py 5155551234
#****************************************
import sys
import os
import time
import serial
import io
phone_number=sys.argv[1]
#open the usb port for the modem
ser=serial.Serial('/dev/ttyUSB3',115200,timeout=.5)
#type arecord -l and aplay -l to get the correct device numbers then assign them below as strings
modem="1"
mic="2"
speaker="0"

def send(command):
   #This function sends the command to the modem
   command=command+"\r"
   ser.write(command.encode())
   time.sleep(.1)  

def kill_aplay():
   #kill any aplay or arecord processes that might be playing in the background
   os.system("kill $(ps | grep aplay | awk {'print $1'})")
   os.system("kill $(ps | grep arecord | awk {'print $1'})")

kill_aplay()


voice_out="arecord -q -f S16_LE -D plughw:"+mic+" | aplay -q -D plughw:"+modem+" -c 1 -f S16_LE -r 44100 &"
voice_in="arecord -q -D plughw:"+modem+" -c 1 -f S16_LE | aplay -q -f S16_LE -D plughw:"+speaker+" &"
os.system(voice_out)
time.sleep(1)
os.system(voice_in)
time.sleep(1)
send('AT+QGPSCFG=\"outport\",\"none\"')
send("AT+qcfg=\"USBCFG\",0x2c7c,0x0125,1,1,1,1,1,1,1")
send("AT+qpcmv=1,2")
send("ATD"+phone_number+";")
os.system('clear') #clears the screen
choice=""
while choice!="x":
   choice = input('Press x then <Enter> to hang up ') 
   if(choice=="x"):
      break

send("ATH")
kill_aplay()
ser.close()


