This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
PiMiner.py
92 lines (86 loc) · 2.32 KB
/
PiMiner.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
#!/usr/bin/python
import sys, subprocess, time, urllib2, socket
sys.path.append("/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate")
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from PiMinerDisplay import PiMinerDisplay
HOLD_TIME = 3.0 #Time (seconds) to hold select button for shut down
REFRESH_TIME= 3.0 #Time (seconds) between data updates
HALT_ON_EXIT= True
display = PiMinerDisplay()
lcd = display.lcd
prevCol = -1
prev = -1
lastTime = time.time()
def shutdown():
lcd.clear()
if HALT_ON_EXIT:
lcd.message('Wait 30 seconds\nto unplug...')
subprocess.call("sync")
subprocess.call(["shutdown", "-h", "now"])
else:
exit(0)
'''
#WIP - startup on boot
def internetOn():
try:
response=urllib2.urlopen('http://google.com',timeout=3)
return True
except urllib2.URLError as err:
pass
return False
'''
#Check for network connection at startup
t = time.time()
while True:
lcd.clear()
lcd.message('checking network\nconnection ...')
if (time.time() - t) > 120:
# No connection reached after 2 minutes
lcd.clear()
lcd.message('network is\nunavailable')
time.sleep(30)
exit(0)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 0))
lcd.backlight(lcd.ON)
lcd.clear()
lcd.message('IP address:\n' + s.getsockname()[0])
time.sleep(5)
display.initInfo() # Start info gathering/display
break # Success
except:
time.sleep(1) # Pause a moment, keep trying
'''
if internetOn() == True:
time.sleep(5)
break # Success
else:
time.sleep(1) # Pause a moment, keep trying
'''
# Listen for button presses
while True:
b = lcd.buttons()
if b is not prev:
if lcd.buttonPressed(lcd.SELECT):
tt = time.time() # Start time of button press
while lcd.buttonPressed(lcd.SELECT): # Wait for button release
if (time.time() - tt) >= HOLD_TIME: # Extended hold?
shutdown() # We're outta here
display.backlightStep()
elif lcd.buttonPressed(lcd.LEFT):
display.scrollRight()
elif lcd.buttonPressed(lcd.RIGHT):
display.scrollLeft()
elif lcd.buttonPressed(lcd.UP):
display.modeUp()
elif lcd.buttonPressed(lcd.DOWN):
display.modeDown()
prev = b
lastTime = time.time()
else:
now = time.time()
since = now - lastTime
if since > REFRESH_TIME or since < 0.0:
display.update()
lastTime = now