-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7segment.py
More file actions
157 lines (130 loc) · 3.61 KB
/
7segment.py
File metadata and controls
157 lines (130 loc) · 3.61 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
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import weather
SDI = 11
RCLK = 12
SRCLK = 13
SDI2 = 15
RCLK2 = 16
SRCLK2 = 18
weatherPin = 29
buttonPin = 7
# Number of seconds allowed between refreshes
delta = 5
lastRefresh = 0
repeat = False
# 0x3f = 0
# 0x06 = 1
# 0x5b = 2
# 0x4f = 3
# 0x66 = 4
# 0x6d = 5
# 0x7d = 6
# 0x07 = 7
# 0x7f = 8
# 0x6f = 9
# 0x77 = A
# 0x7c = B
# 0x39 = C
# 0x5e = D
# 0x79 = E
# 0x71 = F
# 0x76 = H
# 0x80 = .
segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x80]
numCode = {"0":'0x3f',"1":'0x06',"2":'0x5b',"3":'0x4f',"4":'0x66',"5":'0x6d',"6":'0x7d',"7":'0x07',"8":'0x7f',"9":'0x6f'}
numCode2 = {"0":0x3f,"1":0x06,"2":0x5b,"3":0x4f,"4":0x66,"5":0x6d,"6":0x7d,"7":0x07,"8":0x7f,"9":0x6f}
letCode = {"A":0x77,"B":0x7c,"C":0x39,"D":0x5e,"E":0x79,"F":0x71}
def print_msg():
print 'Please press Ctrl+C to end the program...'
def button_callback(channel):
global lastRefresh
global repeat
print "button Pressed!"
print int(time.time() - lastRefresh)
if (int(time.time() - lastRefresh) > delta):
repeat = False
repeat = True
lastRefresh = time.time()
getTemp()
#time.sleep(0.2)
def setup():
GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location
GPIO.setwarnings(False)
# Shift Register 1 for LED 1
GPIO.setup(SDI, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
GPIO.output(SDI, GPIO.LOW)
GPIO.output(RCLK, GPIO.LOW)
GPIO.output(SRCLK, GPIO.LOW)
# Shift Register 2 for LED 2
GPIO.setup(SDI2, GPIO.OUT)
GPIO.setup(RCLK2, GPIO.OUT)
GPIO.setup(SRCLK2, GPIO.OUT)
GPIO.output(SDI2, GPIO.LOW)
GPIO.output(RCLK2, GPIO.LOW)
GPIO.output(SRCLK2, GPIO.LOW)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=button_callback, bouncetime=200)
def led_1(dat):
for bit in range(0, 8):
GPIO.output(SDI, 0x80 & (dat << bit))
GPIO.output(SRCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(SRCLK, GPIO.LOW)
GPIO.output(RCLK, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(RCLK, GPIO.LOW)
def led_2(dat):
for bit in range(0, 8):
GPIO.output(SDI2, 0x80 & (dat << bit))
GPIO.output(SRCLK2, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(SRCLK2, GPIO.LOW)
GPIO.output(RCLK2, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(RCLK2, GPIO.LOW)
def printTemp(temperature,humidity):
temp = int(temperature)
temp = str(temp).zfill(2)
hum = int(humidity)
hum = str(hum).zfill(2)
while repeat is True:
if (repeat is False):
break
led_1(segCode[int(temp[0])])
led_2(segCode[int(temp[1])])
time.sleep(2)
led_1(int(0x00))
led_2(int(0x00))
led_1(segCode[int(hum[0])])
led_2(segCode[int(hum[1])])
time.sleep(2)
led_1(int(0x00))
led_2(int(0x00))
def getTemp():
temp, hum = weather.get_weather()
printTemp(temp,hum)
def destroy(): #When program ending, the function is executed.
led_1(int(0x00))
led_2(int(0x00))
GPIO.cleanup()
if __name__ == '__main__': #Program starting from here
print_msg()
setup()
try:
GPIO.wait_for_edge(buttonPin,GPIO.FALLING)
print "\nFalling Edge Detected"
except KeyboardInterrupt:
GPIO.cleanup()
repeat = True
lastRefresh = time.time()
getTemp()
#try:
# while True:
# time.sleep(1)
#
#except KeyboardInterrupt:
# destroy()