Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import Image import ImageDraw import ImageFont import os import serial import re

ID: 3835241 • Letter: I

Question

import Image

import ImageDraw

import ImageFont

import os

import serial

import re

from lib_tft24T import TFT24T

import RPi.GPIO as GPIO

import spidev

import time

from datetime import datetime

import speech_recognition as sr

spi = spidev.SpiDev()

spi.open(0,1)

spi.max_speed_hz = 1000000

spi.mode = 0b00

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.5)

ok = "OK"

admin_num = ""

RTO_num = ""

number = ""

rcv = None

admin_config = False

STMPE_ADDR = 0x41

STMPE_SYS_CTRL1 = 0x03

STMPE_SYS_CTRL1_RESET = 0x02

STMPE_SYS_CTRL2 = 0x04

STMPE_TSC_CTRL = 0x40

STMPE_TSC_CTRL_EN = 0x01

STMPE_TSC_CTRL_XYZ = 0x00

STMPE_TSC_CTRL_XY = 0x02

STMPE_INT_CTRL = 0x09

STMPE_INT_CTRL_POL_HIGH = 0x04

STMPE_INT_CTRL_POL_LOW = 0x00

STMPE_INT_CTRL_EDGE = 0x02

STMPE_INT_CTRL_LEVEL = 0x00

STMPE_INT_CTRL_ENABLE = 0x01

STMPE_INT_CTRL_DISABLE = 0x00

STMPE_INT_EN = 0x0A

STMPE_INT_EN_TOUCHDET = 0x01

STMPE_INT_EN_FIFOTH = 0x02

STMPE_INT_EN_FIFOOF = 0x04

STMPE_INT_EN_FIFOFULL = 0x08

STMPE_INT_EN_FIFOEMPTY = 0x10

STMPE_INT_EN_ADC = 0x40

STMPE_INT_EN_GPIO = 0x80

STMPE_INT_STA = 0x0B

STMPE_INT_STA_TOUCHDET = 0x01

STMPE_ADC_CTRL1 = 0x20

STMPE_ADC_CTRL1_12BIT = 0x08

STMPE_ADC_CTRL1_10BIT = 0x00

STMPE_ADC_CTRL2 = 0x21

STMPE_ADC_CTRL2_1_625MHZ = 0x00

STMPE_ADC_CTRL2_3_25MHZ = 0x01

STMPE_ADC_CTRL2_6_5MHZ = 0x02

STMPE_TSC_CFG = 0x41

STMPE_TSC_CFG_1SAMPLE = 0x00

STMPE_TSC_CFG_2SAMPLE = 0x40

STMPE_TSC_CFG_4SAMPLE = 0x80

STMPE_TSC_CFG_8SAMPLE = 0xC0

STMPE_TSC_CFG_DELAY_10US = 0x00

STMPE_TSC_CFG_DELAY_50US = 0x08

STMPE_TSC_CFG_DELAY_100US = 0x10

STMPE_TSC_CFG_DELAY_500US = 0x18

STMPE_TSC_CFG_DELAY_1MS = 0x20

STMPE_TSC_CFG_DELAY_5MS = 0x28

STMPE_TSC_CFG_DELAY_10MS = 0x30

STMPE_TSC_CFG_DELAY_50MS = 0x38

STMPE_TSC_CFG_SETTLE_10US = 0x00

STMPE_TSC_CFG_SETTLE_100US = 0x01

STMPE_TSC_CFG_SETTLE_500US = 0x02

STMPE_TSC_CFG_SETTLE_1MS = 0x03

STMPE_TSC_CFG_SETTLE_5MS = 0x04

STMPE_TSC_CFG_SETTLE_10MS = 0x05

STMPE_TSC_CFG_SETTLE_50MS = 0x06

STMPE_TSC_CFG_SETTLE_100MS = 0x07

STMPE_FIFO_TH = 0x4A

STMPE_FIFO_SIZE = 0x4C

STMPE_FIFO_STA = 0x4B

STMPE_FIFO_STA_RESET = 0x01

STMPE_FIFO_STA_OFLOW = 0x80

STMPE_FIFO_STA_FULL = 0x40

STMPE_FIFO_STA_EMPTY = 0x20

STMPE_FIFO_STA_THTRIG = 0x10

STMPE_TSC_I_DRIVE = 0x58

STMPE_TSC_I_DRIVE_20MA = 0x00

STMPE_TSC_I_DRIVE_50MA = 0x01

STMPE_TSC_DATA_X = 0x4D

STMPE_TSC_DATA_Y = 0x4F

STMPE_TSC_FRACTION_Z = 0x56

STMPE_GPIO_SET_PIN = 0x10

STMPE_GPIO_CLR_PIN = 0x11

STMPE_GPIO_DIR = 0x13

STMPE_GPIO_ALT_FUNCT = 0x17

def display_clr(img = False):

if img:

font = ImageFont.truetype('FreeMonoBold.ttf', 25)

TFT.load_wallpaper("pad.jpg") # Image of pad!!!

TFT.display()

else:

TFT.clear()

  

def send_cmd(cmd,response=None,t=0.5):

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=t)

cmd = cmd + " "

port.write(cmd)

rcv = port.readall().strip()

if response:

return rcv.endswith(response)

else:

return rcv

def send_sms(text,num,img = False):

display_clr(img)

if img:

fill = "black"

else:

fill = "lightgreen"

draw.textrotated((0, 0), 'Sending sms to ', 0, font, fill)

draw.textrotated((0, 30), num , 0, font, fill)

TFT.display() # Let's take a look

send_cmd("AT+CMGF=1",ok)

if send_cmd("AT+CMGS=""+num+'"','>'):

if send_cmd(text+"",ok,5):

display_clr(img)

draw.textrotated((0, 0), 'SMS sent ', 0, font, fill)

TFT.display() # Let's take a look

time.sleep(1)

  

def get_data():

rcv = ""

rcv = port.readall()

check_data(rcv)

port.flushInput()

def check_data(data):

global admin_num

global admin_config

if data.find("+CLIP") > 0:

index1 = data.find('"') + 1

index2 = data.find(',') - 1

number = data[index1:index2]

TFT.clear()

draw.textrotated((0, 30), 'receiving call ', 0, font, fill="lightgreen")

draw.textrotated((0, 60), number, 0, font, fill="lightgreen")

TFT.display() # Let's take a look

if not admin_config:

admin_num = number

admin_config = True

time.sleep(1)

send_cmd("ATH",ok)

  

elif admin_config:

time.sleep(1)

send_cmd("ATH",ok)

  

def gps_track():

display_clr(True)

draw.textrotated((0, 0), 'Tracking ', 0, font, fill="black")

TFT.display() # Let's take a look

time.sleep(2)

gps_data = send_cmd("AT+CGPSINF=0")

print "gps_data =", gps_data

if gps_data.find("+CGPSINF:") >= 0:

index1 = gps_data.find(',') + 1

raw_data = gps_data[index1:]

index1 = raw_data.find(',')

_lat = str(raw_data[ : index1])

index1 = raw_data.find(',') + 1

raw_data = raw_data[index1:]

index1 = raw_data.find(',')

_lon = str(raw_data[ : index1])

return _lat,_lon

else:

return -1,-1

def check_MIC():

try:

with sr.Microphone(device_index = 2,sample_rate = 44100, chunk_size = 512) as source: # use the default microphone as the audio source

None

return True

except:

return False

def get_cmd():

display_clr(True)

draw.textrotated((0, 0), 'listning... ', 0, font, fill="black")

TFT.display() # Let's take a look

with sr.Microphone(device_index = 2,sample_rate = 44100, chunk_size = 512) as source: # use the default microphone as the audio source

audio = r.record(source,duration=1) # listen for the first phrase and extract it into audio data

try:

speech = r.recognize_google(audio)

if speech == "help":

return True

  

except: # speech is unintelligible

print("Could not understand audio")

display_clr(True)

draw.textrotated((0, 0), 'Could not understand audio', 0, font, fill="black")

TFT.display() # Let's take a look

return False

  

def writeRegister8(reg , val):

spi.open(0,1)

spi.max_speed_hz = 1000000

spi.xfer([reg , val])

spi.close()

def readRegister8(reg):

spi.open(0,1)

spi.max_speed_hz = 1000000

reg = 0x80 | reg

spidata = 0b0000

spidata = spi.xfer([reg ,0])

spi.close()

return spidata[1]

def bufferEmpty():

return (readRegister8(STMPE_FIFO_STA) & STMPE_FIFO_STA_EMPTY)

def bufferSize():

return readRegister8(STMPE_FIFO_SIZE)

  

def touched():

return (readRegister8(STMPE_TSC_CTRL) & 0x80)

def readData():

  

data = [0b0000,0b0000,0b0000,0b0000]

for i in range(0,4):

data[i] = readRegister8(0xD7) #SPI.transfer(0x00);

x = data[0]

y = data[1]

z = data[3]

if bufferEmpty():

writeRegister8(STMPE_INT_STA, 0xFF) # reset all ints

return x,y,z

def send_distress():

lat,lon = gps_track()

if lat > 0 and lon > 0:

display_clr(True)

draw.textrotated((0, 0), 'lat=' + str(lat), 0, font, fill="black")

draw.textrotated((0, 30), 'lon=' + str(lon), 0, font, fill="black")

TFT.display() # Let's take a look

time.sleep(2)

map_site = "Need help! " + "http://maps.google.com/maps?f=q&q=" + str(lat) + "," + str(lon) + "&z=16"

send_sms(map_site,admin_num,True)

else:

map_site = "Need help! " + "gps not working"

display_clr(True)

draw.textrotated((0, 0), 'gps not working', 0, font, fill="black")

TFT.display() # Let's take a look

time.sleep(2)

send_sms(map_site,admin_num,True)

  

DC = 25

RST = 19

LED = 26

restart = 2

d0 = 12

d1 = 16

d2 = 20

d3 = 21

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

GPIO.setup(restart,GPIO.IN)

GPIO.setup(d0,GPIO.OUT)

GPIO.setup(d1,GPIO.OUT)

GPIO.setup(d2,GPIO.OUT)

GPIO.setup(d3,GPIO.OUT)

GPIO.output(d0, False)

GPIO.output(d1, False)

GPIO.output(d2, False)

GPIO.output(d3, False)

  

TFT = TFT24T(spidev.SpiDev(), GPIO, landscape=True)

# Initialize display.

TFT.initLCD(DC, RST, LED)

##TFT.initTOUCH(PEN)

draw = TFT.draw()

spi.open(0,1)

spi.max_speed_hz = 1000000

  

hotspots = [

(80,40,150,120, "help"),   

(150,145,220,255, "up"),

(280,150,350,250, "down"),

(210,255,280,350, "left"),

(200,60,290,150, "right"),]

reset = True

distress = False

while reset:

  

try:

main = True

font = ImageFont.truetype('FreeMonoBold.ttf', 25)

TFT.clear()

draw.textrotated((0, 50), ' Raspberry pi Based ', 0,font, fill="lightgreen")

draw.textrotated((0, 80), ' Wheelchair operation', 0,font, fill="lightgreen")

draw.textrotated((0, 110), ' & ', 0,font, fill="lightgreen")

draw.textrotated((0, 140), ' Safety System ', 0,font, fill="lightgreen")

TFT.display()

time.sleep(3)

TFT.clear()

draw.textrotated((0, 30), 'checking MIC', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

time.sleep(3)

r = sr.Recognizer()

t1 = datetime.now()

while not check_MIC():

t2 = datetime.now()

delta = t2 - t1

time_elapse = delta.total_seconds()

if time_elapse > 5:

main = False

TFT.clear()

draw.textrotated((0, 30), 'MIC not found', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

time.sleep(3)

TFT.clear()

draw.textrotated((0, 30), 'Connect MIC and',0, font, fill="lightgreen")

draw.textrotated((0, 60), 'Restart the system',0, font, fill="lightgreen")

TFT.display() # Let's take a look

while GPIO.input(restart) == True:

None

  

if main:

TFT.clear()

draw.textrotated((0, 30), 'MIC found', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

time.sleep(3)

font = ImageFont.truetype('FreeMonoBold.ttf', 30)

TFT.clear()

draw.textrotated((0, 30), 'Connecting GSM ', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

while True:

if send_cmd("AT",ok):

send_cmd("ATE0",ok)

send_cmd("AT+CNMI=2,2,0,0",ok)

send_cmd("AT+CGPSPWR=1",ok)

send_cmd("AT+CLIP=1",ok)

TFT.clear()

draw.textrotated((0, 30), 'Connected', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

time.sleep(1)

break

else:

TFT.clear()

draw.textrotated((0, 30), 'GSM not Connected', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

main = False

time.sleep(3)

TFT.clear()

draw.textrotated((0, 30), 'Connect GSM and',0, font, fill="lightgreen")

draw.textrotated((0, 60), 'Press reset',0, font, fill="lightgreen")

TFT.display() # Let's take a look

while GPIO.input(restart) == True:

None

break

  

if main:

TFT.clear()

draw.textrotated((0, 30), 'Waiting for Admin', 0,font, fill="lightgreen")

draw.textrotated((0, 60), 'to call ', 0,font, fill="lightgreen")

TFT.display() # Let's take a look

while not admin_config:

time.sleep(0.5)

if port.inWaiting() > 0:

get_data()

  

send_sms("This number is configure as ADMIN..",admin_num)

TFT.clear()

draw.textrotated((0, 30), 'ADMIN number is ',0,font, fill="lightgreen")

draw.textrotated((0, 60), 'Cofigured ',0, font, fill="lightgreen")

TFT.display() # Let's take a look

time.sleep(2)

TFT.load_wallpaper("pad.jpg") # Image of calculator !!!

TFT.display()

font = ImageFont.truetype('FreeMonoBold.ttf', 20)

t1 = datetime.now()

while main:

t_help = 0

if touched():

while not bufferEmpty():

xt,yt,zt = readData()

print xt , yt

pos = [xt , yt]

writeRegister8(STMPE_INT_STA, 0xFF) # reset all ints

spot = TFT.penOnHotspot(hotspots, pos)

if spot == "help":

t_help = t_help+1

print "help"

if t_help > 20:   

GPIO.output(d0, False)

GPIO.output(d1, False)

GPIO.output(d2, False)

GPIO.output(d3, False)

send_distress()

while not bufferEmpty():

xt,yt,zt = readData()

print xt , yt

pos = [xt , yt]

writeRegister8(STMPE_INT_STA, 0xFF) # reset all ints

TFT.load_wallpaper("pad.jpg") # Image of calculator !!!

TFT.display()

  

if spot == "up":

GPIO.output(d0, True)

GPIO.output(d1, False)

GPIO.output(d2, False)

GPIO.output(d3, True)

  

if spot == "down":

GPIO.output(d0, False)

GPIO.output(d1, True)

GPIO.output(d2, True)

GPIO.output(d3, False)

  

if spot == "left":

GPIO.output(d0, True)

GPIO.output(d1, False)

GPIO.output(d2, True)

GPIO.output(d3, False)

  

if spot == "right":

GPIO.output(d0, False)

GPIO.output(d1, True)

GPIO.output(d2, False)

GPIO.output(d3, True)

t1 = datetime.now()

  

GPIO.output(d0, False)

GPIO.output(d1, False)

GPIO.output(d2, False)

GPIO.output(d3, False)

t2 = datetime.now()

delta = t2 - t1

time_elapse = delta.total_seconds()

if time_elapse > 5:

if get_cmd() == True:

print "got help"

global distress

GPIO.output(d0, False)

GPIO.output(d1, False)

GPIO.output(d2, False)

GPIO.output(d3, False)

distress = False

send_distress()

TFT.load_wallpaper("pad.jpg") # Image of calculator !!!

TFT.display()

TFT.load_wallpaper("pad.jpg") # Image of calculator !!!

TFT.display()

t1 = datetime.now()

  

if GPIO.input(restart) == False:

break

  

except Exception as e :

font = ImageFont.truetype('FreeMonoBold.ttf', 30)

TFT.clear()

draw.textrotated((50, 100), '!!!!ERROR!!!!',0,font, fill="lightgreen")

draw.textrotated((50, 130), ' Press reset',0, font, fill="lightgreen")

TFT.display() # Let's take a look

  

while GPIO.input(restart):

None

python TM

Explanation / Answer


# Package import Image, ImageDraw and so on and it has raspberry module RPi.GPIO as GPIO
import Image
import ImageDraw
import ImageFont
import os
import serial
import re
from lib_tft24T import TFT24T
import RPi.GPIO as GPIO
import spidev
import time
from datetime import datetime
import speech_recognition as sr

# Creating object by calling spidev
spi = spidev.SpiDev()
# Function open called by passing param
spi.open(0,1)
# Max Speed set in HZ
spi.max_speed_hz = 1000000
# Mode set
spi.mode = 0b00
# Raspberry Serial port set to ttyAMA0 and baudrate is 9600 and timeout being 0.5
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.5)

# String declaration
ok = "OK"
admin_num = ""
RTO_num = ""
number = ""
# Receing set None
rcv = None
# No admin config
admin_config = False
# Values Setup in hexadecimal
STMPE_ADDR = 0x41
STMPE_SYS_CTRL1 = 0x03
STMPE_SYS_CTRL1_RESET = 0x02
STMPE_SYS_CTRL2 = 0x04
STMPE_TSC_CTRL = 0x40
STMPE_TSC_CTRL_EN = 0x01
STMPE_TSC_CTRL_XYZ = 0x00
STMPE_TSC_CTRL_XY = 0x02
STMPE_INT_CTRL = 0x09
STMPE_INT_CTRL_POL_HIGH = 0x04
STMPE_INT_CTRL_POL_LOW = 0x00
STMPE_INT_CTRL_EDGE = 0x02
STMPE_INT_CTRL_LEVEL = 0x00
STMPE_INT_CTRL_ENABLE = 0x01
STMPE_INT_CTRL_DISABLE = 0x00
STMPE_INT_EN = 0x0A
STMPE_INT_EN_TOUCHDET = 0x01
STMPE_INT_EN_FIFOTH = 0x02
STMPE_INT_EN_FIFOOF = 0x04
STMPE_INT_EN_FIFOFULL = 0x08
STMPE_INT_EN_FIFOEMPTY = 0x10
STMPE_INT_EN_ADC = 0x40
STMPE_INT_EN_GPIO = 0x80
STMPE_INT_STA = 0x0B
STMPE_INT_STA_TOUCHDET = 0x01
STMPE_ADC_CTRL1 = 0x20
STMPE_ADC_CTRL1_12BIT = 0x08
STMPE_ADC_CTRL1_10BIT = 0x00
STMPE_ADC_CTRL2 = 0x21
STMPE_ADC_CTRL2_1_625MHZ = 0x00
STMPE_ADC_CTRL2_3_25MHZ = 0x01
STMPE_ADC_CTRL2_6_5MHZ = 0x02
STMPE_TSC_CFG = 0x41
STMPE_TSC_CFG_1SAMPLE = 0x00
STMPE_TSC_CFG_2SAMPLE = 0x40
STMPE_TSC_CFG_4SAMPLE = 0x80
STMPE_TSC_CFG_8SAMPLE = 0xC0
STMPE_TSC_CFG_DELAY_10US = 0x00
STMPE_TSC_CFG_DELAY_50US = 0x08
STMPE_TSC_CFG_DELAY_100US = 0x10
STMPE_TSC_CFG_DELAY_500US = 0x18
STMPE_TSC_CFG_DELAY_1MS = 0x20
STMPE_TSC_CFG_DELAY_5MS = 0x28
STMPE_TSC_CFG_DELAY_10MS = 0x30
STMPE_TSC_CFG_DELAY_50MS = 0x38
STMPE_TSC_CFG_SETTLE_10US = 0x00
STMPE_TSC_CFG_SETTLE_100US = 0x01
STMPE_TSC_CFG_SETTLE_500US = 0x02
STMPE_TSC_CFG_SETTLE_1MS = 0x03
STMPE_TSC_CFG_SETTLE_5MS = 0x04
STMPE_TSC_CFG_SETTLE_10MS = 0x05
STMPE_TSC_CFG_SETTLE_50MS = 0x06
STMPE_TSC_CFG_SETTLE_100MS = 0x07
STMPE_FIFO_TH = 0x4A
STMPE_FIFO_SIZE = 0x4C
STMPE_FIFO_STA = 0x4B
STMPE_FIFO_STA_RESET = 0x01
STMPE_FIFO_STA_OFLOW = 0x80
STMPE_FIFO_STA_FULL = 0x40
STMPE_FIFO_STA_EMPTY = 0x20
STMPE_FIFO_STA_THTRIG = 0x10
STMPE_TSC_I_DRIVE = 0x58
STMPE_TSC_I_DRIVE_20MA = 0x00
STMPE_TSC_I_DRIVE_50MA = 0x01
STMPE_TSC_DATA_X = 0x4D
STMPE_TSC_DATA_Y = 0x4F
STMPE_TSC_FRACTION_Z = 0x56
STMPE_GPIO_SET_PIN = 0x10
STMPE_GPIO_CLR_PIN = 0x11
STMPE_GPIO_DIR = 0x13
STMPE_GPIO_ALT_FUNCT = 0x17

# def to display image or clear image if not there
def display_clr(img = False):
if img:
font = ImageFont.truetype('FreeMonoBold.ttf', 25)
TFT.load_wallpaper("pad.jpg") # Image of pad!!!
TFT.display()
else:
TFT.clear()

# to send command and timeout set 0.5
def send_cmd(cmd,response=None,t=0.5):
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=t)
cmd = cmd + " "
# Sending command
port.write(cmd)
rcv = port.readall().strip()
# If response received return it
if response:
return rcv.endswith(response)
else:
return rcv

# sending sms
# calling dislay image
# setting up design and text to send and send sms and wait for sms to send
def send_sms(text,num,img = False):
display_clr(img)
if img:
fill = "black"
else:
fill = "lightgreen"
draw.textrotated((0, 0), 'Sending sms to ', 0, font, fill)
draw.textrotated((0, 30), num , 0, font, fill)
TFT.display() # Let's take a look
send_cmd("AT+CMGF=1",ok)
if send_cmd("AT+CMGS=""+num+'"','>'):
if send_cmd(text+"",ok,5):
display_clr(img)
draw.textrotated((0, 0), 'SMS sent ', 0, font, fill)
TFT.display() # Let's take a look
time.sleep(1)
  
# get all info by reading receiving option and displayingit
def get_data():
rcv = ""
rcv = port.readall()
check_data(rcv)
port.flushInput()

# Checking the data
#It is processing of data
def check_data(data):
global admin_num
global admin_config
#If CLIP is present
if data.find("+CLIP") > 0:
index1 = data.find('"') + 1
index2 = data.find(',') - 1
number = data[index1:index2]
TFT.clear()
# to show text as receing call and background as lightgreen
draw.textrotated((0, 30), 'receiving call ', 0, font, fill="lightgreen")
draw.textrotated((0, 60), number, 0, font, fill="lightgreen")
TFT.display() # Let's take a look
# To check admin by defult is false
if not admin_config:
admin_num = number
admin_config = True
time.sleep(1)
# Send ATh as OK cmd
send_cmd("ATH",ok)
  
elif admin_config:
time.sleep(1)
send_cmd("ATH",ok)
# GPS track module
def gps_track():
display_clr(True)
# Setting text and background
draw.textrotated((0, 0), 'Tracking ', 0, font, fill="black")
TFT.display() # Let's take a look
time.sleep(2)
# sending gps command
gps_data = send_cmd("AT+CGPSINF=0")
print "gps_data =", gps_data
# if gps data has GPSINF it will process the data
if gps_data.find("+CGPSINF:") >= 0:
index1 = gps_data.find(',') + 1
raw_data = gps_data[index1:]
index1 = raw_data.find(',')
_lat = str(raw_data[ : index1])
index1 = raw_data.find(',') + 1
raw_data = raw_data[index1:]
index1 = raw_data.find(',')
_lon = str(raw_data[ : index1])
return _lat,_lon
else:
return -1,-1

# to check the mic is working as default one
def check_MIC():
try:
with sr.Microphone(device_index = 2,sample_rate = 44100, chunk_size = 512) as source:
# use the default microphone as the audio source
None
return True
except:
return False

# get the command send
def get_cmd():
display_clr(True)
draw.textrotated((0, 0), 'listning... ', 0, font, fill="black")
TFT.display()
# Let's take a look
with sr.Microphone(device_index = 2,sample_rate = 44100, chunk_size = 512) as source:
# use the default microphone as the audio source
audio = r.record(source,duration=1) # listen for the first phrase and extract it into audio data

# using google speech recognition software
try:
speech = r.recognize_google(audio)
if speech == "help":
return True
  
except:
# speech is unintelligible
print("Could not understand audio")
display_clr(True)
draw.textrotated((0, 0), 'Could not understand audio', 0, font, fill="black")
TFT.display() # Let's take a look
return False

# Writing to Regsitery
def writeRegister8(reg , val):
spi.open(0,1)
spi.max_speed_hz = 1000000
spi.xfer([reg , val])
spi.close()

# Reading registery
def readRegister8(reg):
spi.open(0,1)
spi.max_speed_hz = 1000000
reg = 0x80 | reg
spidata = 0b0000
spidata = spi.xfer([reg ,0])
spi.close()
return spidata[1]

# read registery to check buffer status
def bufferEmpty():
return (readRegister8(STMPE_FIFO_STA) & STMPE_FIFO_STA_EMPTY)
# read registery to check buffer size
def bufferSize():
return readRegister8(STMPE_FIFO_SIZE)
# read registery to check screen touched
def touched():
return (readRegister8(STMPE_TSC_CTRL) & 0x80)
# reading data from registery
def readData():
# data values
data = [0b0000,0b0000,0b0000,0b0000]
for i in range(0,4):
data[i] = readRegister8(0xD7)
#SPI.transfer(0x00);

x = data[0]
y = data[1]
z = data[3]
if bufferEmpty():
writeRegister8(STMPE_INT_STA, 0xFF)
# reset all ints
return x,y,z

# sending location
def send_distress():
# getting longitude and latitude
lat,lon = gps_track()
if lat > 0 and lon > 0:
display_clr(True)
draw.textrotated((0, 0), 'lat=' + str(lat), 0, font, fill="black")
draw.textrotated((0, 30), 'lon=' + str(lon), 0, font, fill="black")
TFT.display() # Let's take a look
time.sleep(2)
# communicated through google maps
map_site = "Need help! " + "http://maps.google.com/maps?f=q&q=" + str(lat) + "," + str(lon) + "&z=16"
send_sms(map_site,admin_num,True)
else:
# Send gps not working msg
map_site = "Need help! " + "gps not working"
display_clr(True)
draw.textrotated((0, 0), 'gps not working', 0, font, fill="black")
TFT.display() # Let's take a look
time.sleep(2)
send_sms(map_site,admin_num,True)

# constant declaration
DC = 25
RST = 19
LED = 26
restart = 2
d0 = 12
d1 = 16
d2 = 20
d3 = 21

# setting all pins
# set mode and warnings
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#setup pins more
GPIO.setup(restart,GPIO.IN)
GPIO.setup(d0,GPIO.OUT)
GPIO.setup(d1,GPIO.OUT)
GPIO.setup(d2,GPIO.OUT)
GPIO.setup(d3,GPIO.OUT)

GPIO.output(d0, False)
GPIO.output(d1, False)
GPIO.output(d2, False)
GPIO.output(d3, False)
#TFT setup
TFT = TFT24T(spidev.SpiDev(), GPIO, landscape=True)

# Initialize display.
TFT.initLCD(DC, RST, LED)
##TFT.initTOUCH(PEN)
draw = TFT.draw()

spi.open(0,1)
spi.max_speed_hz = 1000000
# Menu Setup on TFT
hotspots = [
(80,40,150,120, "help"),   
(150,145,220,255, "up"),
(280,150,350,250, "down"),
(210,255,280,350, "left"),
(200,60,290,150, "right"),]

reset = True
distress = False

while reset:
  
try:
main = True
font = ImageFont.truetype('FreeMonoBold.ttf', 25)
TFT.clear()
# Setup style
draw.textrotated((0, 50), ' Raspberry pi Based ', 0,font, fill="lightgreen")
draw.textrotated((0, 80), ' Wheelchair operation', 0,font, fill="lightgreen")
draw.textrotated((0, 110), ' & ', 0,font, fill="lightgreen")
draw.textrotated((0, 140), ' Safety System ', 0,font, fill="lightgreen")
TFT.display()
time.sleep(3)

TFT.clear()
# Checking Mic
draw.textrotated((0, 30), 'checking MIC', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
time.sleep(3)
r = sr.Recognizer()
t1 = datetime.now()
while not check_MIC():
t2 = datetime.now()
delta = t2 - t1
time_elapse = delta.total_seconds()
if time_elapse > 5:
main = False
TFT.clear()
#Mic issue reporting
draw.textrotated((0, 30), 'MIC not found', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
time.sleep(3)
TFT.clear()
# help info display
draw.textrotated((0, 30), 'Connect MIC and',0, font, fill="lightgreen")
draw.textrotated((0, 60), 'Restart the system',0, font, fill="lightgreen")
TFT.display() # Let's take a look
while GPIO.input(restart) == True:
None
  
if main:
TFT.clear()
# Mic is there
# checking GSM for connection
draw.textrotated((0, 30), 'MIC found', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
time.sleep(3)
font = ImageFont.truetype('FreeMonoBold.ttf', 30)
TFT.clear()
draw.textrotated((0, 30), 'Connecting GSM ', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
# starting to send cmd
while True:
if send_cmd("AT",ok):
send_cmd("ATE0",ok)
send_cmd("AT+CNMI=2,2,0,0",ok)
send_cmd("AT+CGPSPWR=1",ok)
send_cmd("AT+CLIP=1",ok)
TFT.clear()
# it is connected now
draw.textrotated((0, 30), 'Connected', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
time.sleep(1)
break
else:
TFT.clear()
# if failure to report
draw.textrotated((0, 30), 'GSM not Connected', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
main = False
time.sleep(3)
TFT.clear()
# GSM connection success
draw.textrotated((0, 30), 'Connect GSM and',0, font, fill="lightgreen")
draw.textrotated((0, 60), 'Press reset',0, font, fill="lightgreen")
TFT.display() # Let's take a look
while GPIO.input(restart) == True:
None
break
  
if main:
TFT.clear()
# wait for admin to configuring
draw.textrotated((0, 30), 'Waiting for Admin', 0,font, fill="lightgreen")
draw.textrotated((0, 60), 'to call ', 0,font, fill="lightgreen")
TFT.display() # Let's take a look
while not admin_config:
time.sleep(0.5)
if port.inWaiting() > 0:
get_data()
# showing number of admin
send_sms("This number is configure as ADMIN..",admin_num)
TFT.clear()
draw.textrotated((0, 30), 'ADMIN number is ',0,font, fill="lightgreen")
draw.textrotated((0, 60), 'Cofigured ',0, font, fill="lightgreen")
TFT.display() # Let's take a look
time.sleep(2)
# show wallpaper
TFT.load_wallpaper("pad.jpg") # Image of calculator !!!
TFT.display()
font = ImageFont.truetype('FreeMonoBold.ttf', 20)
t1 = datetime.now()
while main:
t_help = 0
if touched():
while not bufferEmpty():
xt,yt,zt = readData()
print xt , yt
pos = [xt , yt]
writeRegister8(STMPE_INT_STA, 0xFF) # reset all ints
spot = TFT.penOnHotspot(hotspots, pos)
# help option used
if spot == "help":
t_help = t_help+1
print "help"
if t_help > 20:   
GPIO.output(d0, False)
GPIO.output(d1, False)
GPIO.output(d2, False)
GPIO.output(d3, False)
send_distress()
while not bufferEmpty():
xt,yt,zt = readData()
print xt , yt
pos = [xt , yt]
writeRegister8(STMPE_INT_STA, 0xFF) # reset all ints
TFT.load_wallpaper("pad.jpg") # Image of calculator !!!
TFT.display()
# if up option is used to setup different pins
if spot == "up":
GPIO.output(d0, True)
GPIO.output(d1, False)
GPIO.output(d2, False)
GPIO.output(d3, True)
# if down option is used to setup different pins

if spot == "down":
GPIO.output(d0, False)
GPIO.output(d1, True)
GPIO.output(d2, True)
GPIO.output(d3, False)
# if left option is used to setup different pins
if spot == "left":
GPIO.output(d0, True)
GPIO.output(d1, False)
GPIO.output(d2, True)
GPIO.output(d3, False)
# if right option is used to setup different pins
if spot == "right":
GPIO.output(d0, False)
GPIO.output(d1, True)
GPIO.output(d2, False)
GPIO.output(d3, True)

t1 = datetime.now()
  
GPIO.output(d0, False)
GPIO.output(d1, False)
GPIO.output(d2, False)
GPIO.output(d3, False)

t2 = datetime.now()
delta = t2 - t1
time_elapse = delta.total_seconds()
if time_elapse > 5:
if get_cmd() == True:
print "got help"
global distress
GPIO.output(d0, False)
GPIO.output(d1, False)
GPIO.output(d2, False)
GPIO.output(d3, False)
distress = False
send_distress()
TFT.load_wallpaper("pad.jpg") # Image of calculator !!!
TFT.display()
TFT.load_wallpaper("pad.jpg") # Image of calculator !!!
TFT.display()
t1 = datetime.now()
# if fail to restart break it
if GPIO.input(restart) == False:
break
  
except Exception as e :
font = ImageFont.truetype('FreeMonoBold.ttf', 30)
TFT.clear()
draw.textrotated((50, 100), '!!!!ERROR!!!!',0,font, fill="lightgreen")
draw.textrotated((50, 130), ' Press reset',0, font, fill="lightgreen")
TFT.display() # Let's take a look
  
while GPIO.input(restart):
None