Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you explain each line in this code .. this code to control the movement of car by LCD and send the location from tow

image text in transcribedCan you explain each line in this code .. this code to control the movement of car by LCD and send the location from tow way 1. By button in the LCD 2. From voise microphone when he says help .
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+"\x1a",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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

Students also viewed these Databases questions

Question

In order for the note to be negotiable, what must it look like?

Answered: 1 week ago