Added LCD and debug.

master
q3k 2013-04-08 20:18:08 +02:00
parent 7cbe3329fe
commit ec50d50f5c
2 changed files with 88 additions and 6 deletions

54
adsl.py
View File

@ -2,7 +2,10 @@ import os
import time
import sys
import subprocess
import re
import config
import lcd
def _get_process_list():
@ -57,15 +60,54 @@ def _check_connectivity():
return True
def _get_public_ip():
p = subprocess.Popen(["ip", "a", "list", "dev", "ppp0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stderr, stdout = p.communicate()
if p.returncode != 0:
return "UNKNOWN"
else:
for line in stderr.split("\n"):
m = re.match("^inet ([\.0-9]*) peer", line.strip())
if m:
return m.group(1)
return "UNKNOWN"
def loop():
screen = lcd.LCDScreen()
def log(text, text2=None, clear=True):
if clear:
screen.clear()
text = text[:16]
text = ' ' * ((16 - len(text))/2) + text
screen.text_at(0, 0, text)
if text2:
text2 = text2[:16]
text2 = ' ' * ((16 - len(text2))/2) + text2
screen.text_at(0, 1, text2)
while True:
pppd_pid = _get_pppd_pid()
if not pppd_pid:
log("Starting PPPd...")
_start_pppd()
time.sleep(60)
screen.clear()
timeout = 30
while timeout >= 0:
log("PPPd started.", "Testing in %i s. " % timeout, False)
time.sleep(1)
timeout -= 1
log("PPPd started.", "Testing...")
if not _check_connectivity():
sys.stderr.write("Restarting PPPd.\n")
log("ADSL down!", "Restarting PPPd...")
_kill_pppd()
_start_pppd()
log("ADSL down!", "PPPd restarted!")
else:
if not _check_connectivity():
sys.stderr.write("Restarting PPPd.\n")
_kill_pppd()
_start_pppd()
time.sleep(60)
log("ADSL up!", _get_public_ip())
time.sleep(60)

40
lcd.py Normal file
View File

@ -0,0 +1,40 @@
import serial
class LCDScreen(object):
def _command(self, command):
self.serial.write("\xFE" + chr(command))
self.serial.flush()
def clear(self):
self._command(1)
def home(self):
self._command(2)
def blank(self):
self._command(8)
def unblank(self):
self._command(12)
def location(self, x, y):
self._command(128 + 0x40*y + x)
def text(self, text):
sanitized = "".join(c for c in text if c.lower() in "1234567890abcdefghjiklmnopqrstuvwxyz{}[]/+-_. ")
print sanitized
self.serial.write(sanitized)
self.serial.flush()
def text_at(self, x, y, text):
self.location(x, y)
self.text(text)
def __init__(self):
self.serial = serial.Serial("/dev/ttyS1", 2400)
self.clear()
self.home()
def close(self):
self.serial.close()