-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSBfileTransfer.py
More file actions
217 lines (184 loc) · 8.18 KB
/
USBfileTransfer.py
File metadata and controls
217 lines (184 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import RPi.GPIO as GPIO
import time
import os
import subprocess
import glob
class HD44780 :
"""initializing the lcd"""
def __init__(self, pin_rs = 22,pin_e = 27, pins_db = [17,4,3,2]) :
"""initialization"""
self.pin_rs = pin_rs
self.pin_e = pin_e
self.pins_db = pins_db
"""setting the pins for lcd"""
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin_e,GPIO.OUT) #enable pin
GPIO.setup(self.pin_rs,GPIO.OUT) #rs pin
for pin in self.pins_db:
GPIO.setup(pin,GPIO.OUT) #data pins
"""clearing the lcd"""
self.clear()
"""function for clearing the lcd"""
def clear(self):
""" lcd blank code """
self.cmd(0x33)
self.cmd(0x32)
self.cmd(0x28)
self.cmd(0x0C)
self.cmd(0x06)
self.cmd(0x01)
def cmd(self, bits, char_mode = False) :
"""sending command to lcd"""
time.sleep(0.01)
bits = bin(bits)[2:].zfill(8)
GPIO.output(self.pin_rs,char_mode)
for pin in self.pins_db:
GPIO.output(pin, False)
for i in range(4):
if bits[i] == "1" :
GPIO.output(self.pins_db[::-1][i], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
for pin in self.pins_db:
GPIO.output(pin,False)
for i in range(4,8):
if bits[i]=="1":
GPIO.output(self.pins_db[::-1][i-4], True)
GPIO.output(self.pin_e,True)
GPIO.output(self.pin_e, False)
def message(self,text) :
"""send string to lcd. New line wraps to second line"""
for char in text:
if char == '\n':
self.cmd(0xC0) #next line
else:
self.cmd(ord(char), True)
"""here the main program starts"""
if __name__ == '__main__' :
lcd = HD44780()
lcd.message("HELLO") #printing hello
time.sleep(3) #time delay is in seconds
loopi=0 #this variable controls how many times our program will run
"""if you want it to run infinitely you just keep the while loop below as true"""
while(loopi<10):
loopi=loopi+1
lcd.clear()
lcd.message("Welcome")
time.sleep(2)
"""setting up pins for buttons"""
GPIO.setmode(GPIO.BCM)
one = 9
two = 11
three = 5
four = 10
GPIO.setup(one,GPIO.IN,pull_up_down=GPIO.PUD_UP) #PUD_UP will return false when button is pressed
GPIO.setup(two,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(three,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(four,GPIO.IN,pull_up_down=GPIO.PUD_UP)
cont = True
pdList = os.listdir("/media/.") #variable pdList for listing all the folder names present in the media folder.
"""This command will only return folder names without any path"""
lcd.clear()
time.sleep(1.5) #adding delay after every print operation just to avoid any error
if len(pdList)>= 2 : #if two pen drives present then only proceed
lcd.clear()
lcd.message("select source \n Press 1 or 2")
time.sleep(2)
while cont :
selectedDrive = ""
if(GPIO.input(one) == False):
lcd.clear()
lcd.message("Pressed 1")
time.sleep(2)
selectedDrive = pdList[0]
fileList = glob.glob("/media/"+pdList[0]+"/*") #this function returns all the file names present in the folder along with their paths
"""eg. /media/KETAN/abc.mp3"""
cont = False
elif(GPIO.input(two)== False):
lcd.clear()
lcd.message("Pressed 2")
time.sleep(2)
selectedDrive = pdList[1]
fileList = glob.glob("/media/"+pdList[1]+"/*")
cont = False
time.sleep(0.5)
if len(fileList)>=1 :
#there are files to copy - start with the procedure
pointer = 0
displayList = list(fileList)
cutLength = 8 + (len(selectedDrive)) #this is just for cutting down only some part of the file name for displaying
i = 0
#while loop formatting diplay text
while i< len(displayList):
endLength = 13 -(len(displayList[i])-cutLength)
if endLength >= 0 :
endLength = len(displayList[i])
displayList[i] = displayList[i][cutLength : endLength]
while len(displayList[i]) < 13:
displayList[i] = displayList[i]+" "
i=i+1
#selectedList
cont = True
lcd.clear()
lcd.message(displayList[pointer]+"\n"+displayList[(pointer+1)%len(displayList)])
time.sleep(2)
while cont :
if(GPIO.input(one)==False):
if len(displayList[pointer])==13:
displayList[pointer] = displayList[pointer] + "-"
time.sleep(0.5)
elif len(displayList[pointer])==14:
displayList[pointer] = displayList[pointer][:12]
time.sleep(0.5)
lcd.clear()
lcd.message(displayList[pointer]+"\n"+displayList[(pointer+1)%len(displayList)])
elif(GPIO.input(two)==False):
pointer = (pointer - 1)% len(displayList)
lcd.clear()
lcd.message(displayList[pointer]+"\n"+displayList[(pointer+1)%len(displayList)])
time.sleep(0.5)
elif(GPIO.input(three)==False):
pointer = (pointer + 1)% len(displayList)
lcd.clear()
lcd.message(displayList[pointer]+"\n"+displayList[(pointer+1)%len(displayList)])
time.sleep(0.5)
elif(GPIO.input(four)==False):
#code for actually copying the files to destination now
#subprocess.call("cp -R '"+filelist[0]+"' /media/KETAN",shell=True)
lcd.clear()
lcd.message("select \nDestination")
time.sleep(2)
cont1 = True
destination = ""
while(cont1):
if(GPIO.input(one)==False):
destination = "/media/"+pdList[0]
lcd.clear()
lcd.message("Selected 1")
cont1=False
time.sleep(0.5)
elif(GPIO.input(two)==False):
destination = "/media/"+pdList[1]
lcd.clear()
lcd.message("Selected 2")
cont1=False
time.sleep(0.5)
i=0
while i < len(displayList):
if len((displayList[i]))==14:
if(fileList[i] == "System Volume Information"):
print ""
else :
lcd.clear()
lcd.message("..Copying..")
subprocess.call("sudo cp -R '"+fileList[i]+"' '"+destination+"'",shell=True) #this is the linux command that will copy
i=i+1
lcd.clear()
lcd.message("Everything copied.")
cont = False
time.sleep(0.3)
else :
#no file to copy
print ""
else:
pass