Skip to content

Instantly share code, notes, and snippets.

@thomasmacpherson
Created March 18, 2014 13:41
Show Gist options
  • Save thomasmacpherson/9620246 to your computer and use it in GitHub Desktop.
Save thomasmacpherson/9620246 to your computer and use it in GitHub Desktop.
Controls PiFace Control And Display through a web browser. Returns the status of the switches and the LCD text in a JSON string. Set the text with GET variables and send various commands.
"""
simplewebcontrolcad.py
Controls PiFace Control And Display through a web browser. Returns the status of the switches and the LCD text in a JSON string. Set the text with GET
variables and send various commands.
Copyright (C) 2013 Thomas Preston <thomas.preston@openlx.org.uk> simplewebcontrol.py
Copyright (C) 2014 Thomas Macpherson-Pope <thomas.macpherson-pope@openlx.org.uk> simplewebcontrolcad.py
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import subprocess
import http.server
import urllib.parse
import pifacecad
JSON_FORMAT = "{{'input_port': {input}, 'text_value': {text_value}, 'cursor_position': {cursor_position}}}"
DEFAULT_PORT = 8000
LCD_TEXT_GET_STRING = "lcd_text"
COMMAND_GET_STRING = "command"
GET_IP_CMD = "hostname -I"
class PiFaceWebHandler(http.server.BaseHTTPRequestHandler):
"""Handles PiFace web control requests"""
def do_GET(self):
output_value = "NULL"
switches_value = self.pifacecad.switch_port.value
# parse the query string
qs = urllib.parse.urlparse(self.path).query
query_components = urllib.parse.parse_qs(qs)
# set the output
if LCD_TEXT_GET_STRING in query_components:
new_text_value = query_components[LCD_TEXT_GET_STRING][0]
output_value = self.set_text_value(new_text_value)
elif COMMAND_GET_STRING in query_components:
command = query_components[COMMAND_GET_STRING][0]
self.action_command(command)
cursor_position = self.pifacecad.lcd.get_cursor()
# reply with JSON
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes(JSON_FORMAT.format(
input=switches_value,
text_value=output_value,
cursor_position=cursor_position
), 'UTF-8'))
def set_text_value(self, new_text_value):
"""Sets the text value to new_value."""
print("Setting text port to {}.".format(new_text_value))
port_value = new_text_value
self.pifacecad.lcd.write(port_value)
return port_value
def action_command(self, command):
print("Actioning command")
if command == "backlight_on":
self.pifacecad.lcd.backlight_on()
elif command == "backlight_off":
self.pifacecad.lcd.backlight_off()
elif command == "blink_off":
self.pifacecad.lcd.blink_off()
elif command == "blink_on":
self.pifacecad.lcd.blink_on()
elif command == "clear":
self.pifacecad.lcd.clear()
elif command == "cursor_off":
self.pifacecad.lcd.cursor_off()
elif command == "cursor_on":
self.pifacecad.lcd.cursor_on()
elif command == "display_off":
self.pifacecad.lcd.display_off()
elif command == "display_on":
self.pifacecad.lcd.display_on()
elif command == "home":
self.pifacecad.lcd.home()
else:
print(command + " is not an implemented command")
def get_my_ip():
"""Returns this computers IP address as a string."""
ip = subprocess.check_output(GET_IP_CMD, shell=True).decode('utf-8')[:-1]
return ip.strip()
if __name__ == "__main__":
# get the port
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = DEFAULT_PORT
# set up PiFace Digital
PiFaceWebHandler.pifacecad = pifacecad.PiFaceCAD()
print("Starting simple PiFace web control at:\n\n"
"\thttp://{addr}:{port}\n\n"
"Set text with:\n\n"
"\thttp://{addr}:{port}/?lcd_text=Hello World\n"
"Send commands with:\n\n"
"\thttp://{addr}:{port}/?command=your_command\n\n"
"Commands include:\n"
"\tbacklight_off\n"
"\tbacklight_on\n"
"\tblink_off\n"
"\tblink_on\n"
"\tclear\n"
"\tdisplay_off\n"
"\tdisplay_on\n"
"\thome\n"
.format(addr=get_my_ip(), port=port))
PiFaceWebHandler.pifacecad.lcd.backlight_on()
# run the server
server_address = ('', port)
try:
httpd = http.server.HTTPServer(server_address, PiFaceWebHandler)
httpd.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down server')
httpd.socket.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment