Различия
Здесь показаны различия между двумя версиями данной страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
dev:pi:main [2016/10/06 09:30] denis [shutdown] |
dev:pi:main [2016/12/09 10:15] (текущий) denis [shutdown] |
||
---|---|---|---|
Строка 96: | Строка 96: | ||
<code bash> | <code bash> | ||
sudo apt-get install python-smbus i2c-tools | sudo apt-get install python-smbus i2c-tools | ||
- | sudo apt-get purge fake-hwclock | + | sudo apt-get purge fake-hwclock ntp |
- | sudo apt-get purge ntp | + | |
sudo apt-get install ntpdate # TODO: not found | sudo apt-get install ntpdate # TODO: not found | ||
Строка 157: | Строка 156: | ||
import RPi.GPIO as gpio | import RPi.GPIO as gpio | ||
- | ledDelayNormal = 1 | + | pinBt=19 |
- | ledDelayFast = 0.1 | + | |
- | acShutdownDelay = 5 | + | |
- | + | ||
- | stateAC = True | + | |
- | timeACfailed = 0 | + | |
- | ledDelay = ledDelayNormal | + | |
- | shutdownCompleted = False | + | |
- | + | ||
- | pinButton=19 | + | |
pinAC=21 | pinAC=21 | ||
pinLed=23 | pinLed=23 | ||
+ | |||
+ | dlyShutdownBt = 5 | ||
+ | dlyShutdownAC = 10 | ||
+ | |||
+ | dlyLedOk = 1 | ||
+ | dlyLedFast = 0.1 | ||
+ | |||
def loop(): | def loop(): | ||
- | global ledDelay | + | hasBt = False |
- | global stateAC | + | hasAC = False |
- | global timeACfailed | + | timeBt = 0 |
+ | timeAC = 0 | ||
+ | dlyLed = dlyLedOk | ||
+ | terminated = False | ||
+ | |||
+ | while gpio.input(pinBt)>0 or gpio.input(pinAC)>0: | ||
+ | for i in range(0,3): | ||
+ | gpio.output(pinLed, True) | ||
+ | time.sleep(dlyLedFast) | ||
+ | gpio.output(pinLed, False) | ||
+ | time.sleep(dlyLedFast) | ||
+ | time.sleep(dlyLedOk) | ||
+ | |||
+ | while not terminated: | ||
+ | |||
+ | if gpio.input(pinBt)==0: | ||
+ | if timeBt > 0: | ||
+ | print "Bt OK" | ||
+ | timeBt = 0 | ||
+ | else: | ||
+ | if timeBt == 0: | ||
+ | print "Bt failed" | ||
+ | timeBt = time.time() | ||
+ | else: | ||
+ | if time.time() - timeBt > dlyShutdownBt: | ||
+ | print "Bt shutdown" | ||
+ | doShutdown() | ||
+ | terminated = True | ||
- | while not shutdownCompleted: | ||
- | if gpio.input(pinAC)==0 and stateAC==False: | ||
- | print "AC OK" | ||
- | stateAC = 1 | ||
- | ledDelay = ledDelayNormal | ||
- | if gpio.input(pinAC)>0: | + | if gpio.input(pinAC)==0: |
- | if stateAC==True: | + | if timeAC > 0: |
+ | print "AC OK" | ||
+ | timeAC = 0 | ||
+ | else: | ||
+ | if timeAC == 0: | ||
print "AC failed" | print "AC failed" | ||
- | stateAC = 0 | + | timeAC = time.time() |
- | ledDelay = ledDelayFast | + | |
- | timeACfailed = time.time() | + | |
else: | else: | ||
- | if time.time() - timeACfailed > acShutdownDelay: | + | if time.time() - timeAC > dlyShutdownAC: |
print "AC shutdown" | print "AC shutdown" | ||
doShutdown() | doShutdown() | ||
- | break | + | terminated = True |
+ | |||
+ | |||
+ | |||
+ | if (timeAC > 0 or timeBt > 0) and dlyLed == dlyLedOk: | ||
+ | dlyLed = dlyLedFast | ||
+ | elif timeAC == 0 and timeBt == 0 and dlyLed != dlyLedOk: | ||
+ | dlyLed = dlyLedOk | ||
gpio.output(pinLed, True) | gpio.output(pinLed, True) | ||
- | time.sleep(ledDelay) | + | time.sleep(dlyLed) |
gpio.output(pinLed, False) | gpio.output(pinLed, False) | ||
- | time.sleep(ledDelay) | + | time.sleep(dlyLed) |
+ | |||
def eventButton(pin): | def eventButton(pin): | ||
Строка 203: | Строка 233: | ||
def doShutdown(): | def doShutdown(): | ||
- | call('halt', shell=False) | + | # call('halt', shell=False) |
- | global shutdownCompleted | + | print "SHUTDOWN!!!" |
- | shutdownCompleted = True | + | |
gpio.setmode(gpio.BOARD) | gpio.setmode(gpio.BOARD) | ||
- | gpio.setup(pinButton, gpio.IN) | + | gpio.setup(pinBt, gpio.IN) |
gpio.setup(pinAC, gpio.IN) | gpio.setup(pinAC, gpio.IN) | ||
- | gpio.add_event_detect(pinButton, gpio.RISING, callback=eventButton, bouncetime=200) | ||
gpio.setwarnings(False) | gpio.setwarnings(False) | ||
gpio.setup(pinLed, gpio.OUT) | gpio.setup(pinLed, gpio.OUT) | ||
loop() | loop() | ||
gpio.cleanup() | gpio.cleanup() | ||
- | |||
</code> | </code> | ||
+ | ''/etc/rc.local'': | ||
+ | <code bash> | ||
+ | # add this line before "exit 0" | ||
+ | python /home/pi/softshut/softshut.py & | ||
+ | </code> | ||