2017-03-14 14:57:02 +01:00
|
|
|
#!/usr/bin/env python3
|
2017-03-14 14:46:51 +01:00
|
|
|
#
|
2017-04-13 18:08:19 +02:00
|
|
|
# kitchentext: Read text from stdin and put it on the Kitchenlight.
|
2017-04-14 10:41:53 +02:00
|
|
|
#
|
|
|
|
# Author: Shy
|
2017-04-14 17:21:04 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
2017-03-14 14:46:51 +01:00
|
|
|
|
2017-04-14 10:41:53 +02:00
|
|
|
def kitchentext(delay=200, single=False, wait=False, restore=False,
|
|
|
|
poweron=False, verbose=False, debug=False):
|
2017-03-14 14:46:51 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
from c4ctrl import C4Interface, Kitchenlight
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
charwidth = { # Width of characters.
|
2017-04-14 10:41:53 +02:00
|
|
|
'a' : 5, 'A' : 5, 'b' : 4, 'B' : 5, 'c' : 3, 'C' : 5,
|
|
|
|
'd' : 4, 'D' : 5, 'e' : 4, 'E' : 5, 'f' : 3, 'F' : 5,
|
|
|
|
'g' : 3, 'G' : 5, 'h' : 3, 'H' : 5, 'i' : 1, 'I' : 5,
|
|
|
|
'j' : 2, 'J' : 5, 'k' : 3, 'K' : 5, 'l' : 3, 'L' : 5,
|
|
|
|
'm' : 5, 'M' : 5, 'n' : 4, 'N' : 5, 'o' : 3, 'O' : 5,
|
|
|
|
'p' : 3, 'P' : 5, 'q' : 3, 'Q' : 5, 'r' : 3, 'R' : 5,
|
|
|
|
's' : 4, 'S' : 5, 't' : 3, 'T' : 5, 'u' : 3, 'U' : 5,
|
|
|
|
'v' : 3, 'V' : 5, 'w' : 5, 'W' : 5, 'x' : 3, 'X' : 5,
|
|
|
|
'y' : 3, 'Y' : 5, 'z' : 3, 'Z' : 5, '0' : 5, '1' : 4,
|
|
|
|
'2' : 5, '3' : 5, '4' : 5, '5' : 5, '6' : 5, '7' : 5,
|
|
|
|
'8' : 5, '9' : 5, '@' : 5, '=' : 3, '!' : 1, '"' : 3,
|
|
|
|
'_' : 5, '-' : 3, '.' : 2, ',' : 2, '*' : 5, ':' : 2,
|
|
|
|
'\'' : 1, '/' : 5, '(' : 2, ')' : 2, '{' : 3, '}' : 3,
|
|
|
|
'[' : 2, ']' : 2, '<' : 3, '>' : 4, '+' : 5, '#' : 5,
|
|
|
|
'$' : 5, '%' : 5, '$' : 5, '~' : 5, '?' : 3, ';' : 2,
|
|
|
|
'\\' : 5, '^' : 3, '|' : 1, '`' : 2, ' ' : 3, '\t' : 5
|
|
|
|
}
|
2017-03-14 14:46:51 +01:00
|
|
|
|
|
|
|
C4Interface.debug = debug
|
|
|
|
kl = Kitchenlight(autopower=poweron)
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
# Enforce wait=True if restore=True or single=False.
|
2017-03-14 14:46:51 +01:00
|
|
|
wait = wait or restore or not single
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
# Store previous state.
|
2017-03-14 14:46:51 +01:00
|
|
|
if restore:
|
2017-04-08 16:27:37 +02:00
|
|
|
c4 = C4Interface()
|
2017-04-14 10:41:53 +02:00
|
|
|
saved_state = c4.pull([kl.topic, kl.powertopic])
|
2017-03-14 14:46:51 +01:00
|
|
|
|
|
|
|
try:
|
2017-04-13 18:08:19 +02:00
|
|
|
while True:
|
2017-03-14 14:46:51 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
text = sys.stdin.readline()
|
2017-04-13 18:08:19 +02:00
|
|
|
except KeyboardInterrupt: # Exit a bit more graceful on CTRL-C.
|
2017-04-14 10:41:53 +02:00
|
|
|
verbose and print("\nInterrupted by user.", file=sys.stderr)
|
2017-03-14 14:46:51 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if single and text.rstrip('\n') == "":
|
|
|
|
verbose and sys.stderr.write("Error: empty input!\n")
|
|
|
|
sys.exit(1)
|
2017-04-13 18:08:19 +02:00
|
|
|
elif text == "\n": # Empty line.
|
2017-03-14 14:46:51 +01:00
|
|
|
verbose and print("Info: skipping empty line")
|
|
|
|
continue
|
2017-04-13 18:08:19 +02:00
|
|
|
elif text == "": # EOF.
|
2017-03-14 14:46:51 +01:00
|
|
|
break
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
# Strip chars Kitchenlight can not display.
|
2017-03-14 14:46:51 +01:00
|
|
|
text = text.rstrip('\n').encode("ascii", "ignore").decode("ascii")
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
try: # We might well get interrupted while waiting.
|
2017-03-14 14:46:51 +01:00
|
|
|
kl.text(text, delay)
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
from time import sleep
|
|
|
|
# How long shall we wait?
|
2017-04-14 10:41:53 +02:00
|
|
|
# Kitchenlight has 30 columns, each char uses 2-5 columns
|
|
|
|
# followed by an empty one. The traversal of one column
|
|
|
|
# takes 30 * <delay> ms.
|
2017-03-14 14:46:51 +01:00
|
|
|
twidth = 0
|
|
|
|
for c in text:
|
|
|
|
try:
|
|
|
|
twidth += (charwidth[c] + 1)
|
|
|
|
except KeyError:
|
|
|
|
twidth += 4 # Fallback
|
2017-04-14 10:41:53 +02:00
|
|
|
wait_for = ((30 * delay) + (twidth * delay)) / 1000
|
2017-03-14 14:46:51 +01:00
|
|
|
verbose and print("Waiting for {} seconds ...".format(wait_for))
|
|
|
|
sleep(wait_for)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2017-04-14 10:41:53 +02:00
|
|
|
verbose and print("\nInterrupted by user.", file=sys.stderr)
|
2017-03-14 14:46:51 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
finally:
|
2017-04-13 18:08:19 +02:00
|
|
|
# Always restore privious state if "restore" is set.
|
2017-03-14 14:46:51 +01:00
|
|
|
if restore:
|
|
|
|
re = []
|
2017-04-14 10:41:53 +02:00
|
|
|
for top in saved_state: re.append((top.topic, top.payload))
|
2017-03-15 09:53:00 +01:00
|
|
|
c4.push(re)
|
2017-03-14 14:46:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Read text from stdin and put it on the Kitchenlight")
|
|
|
|
parser.add_argument(
|
|
|
|
"-d", "--delay", type=int, default=200,
|
|
|
|
help="delay in ms (speed of the text, default is 200)")
|
|
|
|
parser.add_argument(
|
|
|
|
"-s", "--single", action="store_true",
|
|
|
|
help="only read a single line")
|
|
|
|
parser.add_argument(
|
|
|
|
"-w", "--wait", action="store_true", default=False,
|
|
|
|
help="wait until the text has been displayed")
|
|
|
|
parser.add_argument(
|
|
|
|
"-r", "--restore", action="store_true", default=False,
|
2017-04-14 10:41:53 +02:00
|
|
|
help="restore the Kitchenlight to its prior state after the text has \
|
|
|
|
been displayed (implies --wait)")
|
2017-03-14 14:46:51 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-p", "--power-on", action="store_true", default=False,
|
|
|
|
help="turn on Kitchenlight if it is powered off")
|
2017-04-13 18:08:19 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"-f", "--fork", action="store_true",
|
|
|
|
help="fork to background")
|
2017-03-14 14:46:51 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-v", "--verbose", action="store_true",
|
|
|
|
help="be (not much) more verbose")
|
|
|
|
parser.add_argument(
|
|
|
|
"--debug", action="store_true",
|
2017-04-14 10:41:53 +02:00
|
|
|
help="display what would be send to the MQTT broker, but do not \
|
|
|
|
actually connect")
|
2017-03-14 14:46:51 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
if args.fork:
|
|
|
|
import sys
|
|
|
|
from posix import fork
|
|
|
|
|
2017-04-14 10:41:53 +02:00
|
|
|
if sys.stdin.isatty():
|
|
|
|
print("Error: cannot fork when stdin is connected to a terminal!",
|
|
|
|
file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2017-04-13 18:08:19 +02:00
|
|
|
child_pid = fork()
|
|
|
|
if child_pid != 0:
|
|
|
|
args.verbose and print("Forked to PID", child_pid)
|
|
|
|
sys.exit()
|
|
|
|
|
2017-03-14 14:46:51 +01:00
|
|
|
kitchentext(delay=args.delay,
|
|
|
|
single=args.single,
|
|
|
|
wait=args.wait,
|
|
|
|
restore=args.restore,
|
|
|
|
poweron=args.power_on,
|
|
|
|
verbose=args.verbose,
|
|
|
|
debug=args.debug)
|
|
|
|
|