forked from cymplecy/scratch_gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipd03.py
executable file
·138 lines (123 loc) · 3.74 KB
/
ipd03.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
#! /usr/bin/python
# GNU GPL V3
import smbus, time, subprocess, RPi.GPIO as GPIO
# Define list for digits 0..9, space, dash and DP in 7-segment (active High)
digits = [0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111, 0b00000000, 0b01000000, 0b10000000]
if GPIO.RPI_REVISION > 1:
bus = smbus.SMBus(1) # For revision 1 Raspberry Pi, change to bus = smbus.SMBus(1) for revision 2.
else:
bus = smbus.SMBus(0) # For revision 1 Raspberry Pi, change to bus = smbus.SMBus(1) for revision 2.
addr = 0x20 # I2C address of MCP23017
bus.write_byte_data(addr, 0x00, 0x00) # Set all of bank 0 to outputs
bus.write_byte_data(addr, 0x01, 0x00) # Set all of bank 1 to outputs
bus.write_byte_data(addr, 0x13, 0xff) # Set all of bank 1 to High (Off)
def sendDigit(digit, pos):
t = (1<<pos) ^ 255
bus.write_byte_data(addr, 0x13, t) # Set bank 1 Pos to Low
bus.write_byte_data(addr, 0x12, digit) # Set bank 0 to digit
speed = 0.005
def iDisplay(val):
val1 = val/10
val2 = val1/10
count1 = 0
while count1 < 5:
count2 = 0
while count2 < 20:
sendDigit(digits[val - val1*10], 0) # '1'
time.sleep(speed)
sendDigit(0, 0)
sendDigit(digits[val1 - val2*10], 1) # '2'
time.sleep(speed)
sendDigit(0, 1)
sendDigit(digits[val2], 2) # '3'
time.sleep(speed)
sendDigit(0, 2)
sendDigit(digits[0], 3) # '0'
time.sleep(speed)
sendDigit(0, 3)
count2 += 1
time.sleep(0.5)
count1 += 1
def scroll(str1, count):
string = ' ' + str1 + ' '
for j in range(count):
for i in range(len(string)-3):
str2 = string[i:(i+4)]
sDisplay2(str2, count)
def sDisplay2(safeStr, count):
d1 = val(safeStr[3])
d2 = val(safeStr[2])
d3 = val(safeStr[1])
d4 = val(safeStr[0])
count2 = 0
while count2 < 10:
sendDigit(digits[d1], 0) # '1'
time.sleep(speed)
sendDigit(0, 0)
sendDigit(digits[d2], 1) # '2'
time.sleep(speed)
sendDigit(0, 1)
sendDigit(digits[d3], 2) # '3'
time.sleep(speed)
sendDigit(0, 2)
sendDigit(digits[d4], 3) # '0'
time.sleep(speed)
sendDigit(0, 3)
count2 += 1
def sDisplay(string):
safeStr = ' ' + string
l = len(safeStr)
d1 = val(safeStr[l-1])
d2 = val(safeStr[l-2])
d3 = val(safeStr[l-3])
d4 = val(safeStr[l-4])
count1 = 0
while count1 < 5:
count2 = 0
while count2 < 20:
sendDigit(digits[d1], 0) # '1'
time.sleep(speed)
sendDigit(0, 0)
sendDigit(digits[d2], 1) # '2'
time.sleep(speed)
sendDigit(0, 1)
sendDigit(digits[d3], 2) # '3'
time.sleep(speed)
sendDigit(0, 2)
sendDigit(digits[d4], 3) # '0'
time.sleep(speed)
sendDigit(0, 3)
count2 += 1
time.sleep(0.5)
count1 += 1
def val(digit):
if (ord(digit) >= 48 and ord(digit) <= 57):
return ord(digit) - 48
elif ord(digit) == 32: #space
return 10 # 10 is a blank
elif ord(digit) == 46: #period
return 11 # 11 is a dash
arg = 'ip route list'
waiting = True
while waiting:
p = subprocess.Popen(arg, shell = True, stdout = subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
length = len(split_data)
print 'Length', length
if length > 8:
waiting = False
else:
scroll("0.0.0.0", 2)
ipaddr = split_data[split_data.index('src')+1]
print ipaddr
#parts = ipaddr.split('.')
#print parts[0]
#print parts[1]
#print parts[2]
#print parts[3]
#print int(parts[3])
#iDisplay(int(parts[3]))
#sDisplay(parts[3])
scroll(ipaddr, 5)
bus.write_byte_data(addr, 0x13, 0xff) # Set all of bank 1 to High (Off)