/
pipodcorder2.py
executable file
·265 lines (216 loc) · 6.02 KB
/
pipodcorder2.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/python
# PiPodCorder main script
# for Raspberry Pi
# by Michael Horne (www.recantha.co.uk/blog)
# Date: 27/10/2012
# Contributions taken from
# HD44780 LCD Test Script
# Author : Matt Hawkins
# Site : http://www.raspberrypi-spy.co.uk
# Adafruit I2C Library
#import
import RPi.GPIO as GPIO
import time
import os
import socket
import commands
import subprocess as sub
import serial
import pyfirmata
###################
# LCD CONFIGURATION
###################
# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
# Define some device constants
LCD_WIDTH = 16 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005
################################
# Switch on/off various parts of the 'corder
################################
ENABLE_IP=False
ENABLE_TEMPERATURE=False
ENABLE_DISTANCE=False
ENABLE_SERIAL=False
ENABLE_FIRMATA=True
if ENABLE_SERIAL:
SERIAL = serial.Serial('/dev/ttyACM0', 9600)
# Define Firmata-enabled Arduino
if ENABLE_FIRMATA:
FIRMATA_BOARD = pyfirmata.Arduino('/dev/ttyACM0')
it = pyfirmata.util.Iterator(FIRMATA_BOARD)
it.start()
# temperature
FIRMATA_PIN_TEMPERATURE=FIRMATA_BOARD.get_pin('a:0:i')
# magnetism
FIRMATA_PIN_MAGNET=FIRMATA_BOARD.get_pin('a:1:i')
# light
FIRMATA_PIN_LDR=FIRMATA_BOARD.get_pin('a:2:i')
# red LED
FIRMATA_PIN_RED_LED=FIRMATA_BOARD.get_pin('d:10:p')
# green LED
FIRMATA_PIN_GREEN_LED=FIRMATA_BOARD.get_pin('d:9:p')
# switches
FIRMATA_PIN_MENU_STEP=FIRMATA_BOARD.get_pin('d:6:i')
FIRMATA_PIN_MENU_SELECT=FIRMATA_BOARD.get_pin('d:5:i')
def get_ip_addresses():
ips = commands.getoutput("/sbin/ifconfig | grep -i \"inet\" | grep -iv \"inet6\" | " + "awk {'print $2'} | sed -ne 's/addr\:/ /p'")
addrs = ips.split('\n')
return addrs
def read_temperature():
gettemp = sub.Popen(['gpio/tmp102/temperature_read.sh'], stdout=sub.PIPE, stderr=sub.PIPE)
temp = gettemp.communicate()
return temp[0]
def read_sonar():
getdata = sub.Popen(['gpio/ultrasonic/sonar_ping.py'], stdout=sub.PIPE, stderr=sub.PIPE)
temp = getdata.communicate()
return temp[0]
def read_arduino():
SERIAL.flushInput()
line = SERIAL.readline()
line = line.rstrip("\n")
line = line.rstrip("\r")
return line
def main():
# Main program block
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
# Initialise display
send_to_lcd(" ", " ")
time.sleep(2)
send_to_lcd("-=====v==v====-", "-=PiPodCorder=-")
time.sleep(0.5)
if ENABLE_FIRMATA:
while FIRMATA_PIN_TEMPERATURE.read() is None:
pass
while True:
lcd_init()
if ENABLE_IP:
local_hostname=socket.gethostname()
for addr in get_ip_addresses():
send_to_lcd(local_hostname, addr)
time.sleep(0.5)
if ENABLE_SERIAL:
number_of_serial_reads = 3
for i in range(number_of_serial_reads):
send_to_lcd('Arduino', read_arduino())
if ENABLE_TEMPERATURE:
temperature=read_temperature()
send_to_lcd('On-plate temp', temperature)
time.sleep(0.2)
send_to_lcd('On-plate temp', temperature+".........")
if ENABLE_DISTANCE:
for t in range(0,2):
distance=read_sonar()
send_to_lcd('Target distance', distance)
time.sleep(0.2)
send_to_lcd('Target distance', distance+".....")
if ENABLE_FIRMATA:
for i in range(1):
FIRMATA_PIN_RED_LED.write(0)
FIRMATA_PIN_GREEN_LED.write(1)
# temp_pin_read = FIRMATA_PIN_TEMPERATURE.read()
# tempCalc = ((temp_pin_read*4.88) - 500) / 10
# tempCalc = ((temp_pin_read*5.43) / 0.1)
# print tempCalc
# temp="%.1f oC" % (FIRMATA_PIN_TEMPERATURE.read() * 5 * 100)
# send_to_lcd('Arduino temp', temp)
# magnt="%.1f g" % (FIRMATA_PIN_MAGNET.read() * 5 * 100)
# send_to_lcd('Magnetism', magnt)
# llevel="%.1f lux" % (FIRMATA_PIN_LDR.read() * 5 * 100)
# send_to_lcd('Light level', llevel)
print "Menu step"
print FIRMATA_PIN_MENU_STEP.read()
print "Menu select"
print FIRMATA_PIN_MENU_SELECT.read()
FIRMATA_PIN_RED_LED.write(1)
FIRMATA_PIN_GREEN_LED.write(0)
time.sleep(0.1)
if ENABLE_FIRMATA:
FIRMATA_BOARD.exit()
# LCD FUNCTIONS
def send_to_lcd(_line_1, _line_2):
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string(_line_1)
print _line_1
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(_line_2)
print _line_2
time.sleep(1) # 3 second delay
def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD)
lcd_byte(0x32,LCD_CMD)
lcd_byte(0x28,LCD_CMD)
lcd_byte(0x0C,LCD_CMD)
lcd_byte(0x06,LCD_CMD)
lcd_byte(0x01,LCD_CMD)
def lcd_string(message):
# Send string to display
message = message.ljust(LCD_WIDTH," ")
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
time.sleep(0.02)
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = data
# mode = True for character
# False for command
GPIO.output(LCD_RS, mode) # RS
# High bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
# Low bits
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)
# Toggle 'Enable' pin
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
if __name__ == '__main__':
main()