Rewrote binary ops on switches, added ~ operator
This commit is contained in:
parent
0b12f2f70e
commit
e551bb233e
1 changed files with 40 additions and 35 deletions
75
c4ctrl.py
75
c4ctrl.py
|
@ -580,27 +580,26 @@ class C4Room: # {{{1
|
||||||
userinput = self._interactive_light_switch()
|
userinput = self._interactive_light_switch()
|
||||||
if userinput == "": return
|
if userinput == "": return
|
||||||
|
|
||||||
# Let's support some geeky binary operations!
|
if userinput == '-':
|
||||||
mode = 'n' # n = normal, a = AND, o = OR, x = XOR.
|
print(self.get_switch_state())
|
||||||
if not userinput.isdecimal():
|
return
|
||||||
if userinput == '-':
|
|
||||||
print(self.get_switch_state())
|
|
||||||
return
|
|
||||||
|
|
||||||
elif userinput[0] == '&' and userinput[1:].strip().isdecimal():
|
# Let's support some binary operations!
|
||||||
# AND operator, applied later after doing some more validation.
|
ops = "" # Store operators.
|
||||||
userinput = userinput[1:].strip()
|
while not userinput.isdecimal():
|
||||||
mode = 'a'
|
if userinput == "":
|
||||||
|
# Huh, no operand given. oO
|
||||||
|
if ops[-1:] == '~':
|
||||||
|
# The NOT operator may work on the current switch state.
|
||||||
|
userinput = self.get_switch_state()
|
||||||
|
else:
|
||||||
|
print("Error: missing operand after '{}'!".format(ops[-1]),
|
||||||
|
file=sys.stderr)
|
||||||
|
return
|
||||||
|
|
||||||
elif userinput[0] == '|' and userinput[1:].strip().isdecimal():
|
elif userinput[0] in "&|^~":
|
||||||
# OR operator, applied later after doing some more validation.
|
ops += userinput[0]
|
||||||
userinput = userinput[1:].strip()
|
userinput = userinput[1:].strip()
|
||||||
mode = 'o'
|
|
||||||
|
|
||||||
elif userinput[0] == '^' and userinput[1:].strip().isdecimal():
|
|
||||||
# XOR operator, applied later after doing some more validation.
|
|
||||||
userinput = userinput[1:].strip()
|
|
||||||
mode = 'x'
|
|
||||||
|
|
||||||
elif (userinput[:2] == ">>" or userinput[:2] == "<<") \
|
elif (userinput[:2] == ">>" or userinput[:2] == "<<") \
|
||||||
and (userinput[2:].strip() == "" or userinput[2:].strip().isdecimal()):
|
and (userinput[2:].strip() == "" or userinput[2:].strip().isdecimal()):
|
||||||
|
@ -649,18 +648,24 @@ class C4Room: # {{{1
|
||||||
print("Error: invalid digit: " + digit, file=sys.stderr)
|
print("Error: invalid digit: " + digit, file=sys.stderr)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if mode == 'a': # AND operator.
|
while ops:
|
||||||
switch_state = self.get_switch_state()
|
# Apply modifiers.
|
||||||
userinput = "".join(map(lambda x, y: str(int(x) & int(y)),
|
if ops[-1] == '~': # NOT operator.
|
||||||
userinput, switch_state))
|
userinput = "".join(map(lambda i: i == '0' and '1' or '0',
|
||||||
elif mode == 'o': # OR operator.
|
userinput))
|
||||||
switch_state = self.get_switch_state()
|
elif ops[-1] == '&': # AND operator.
|
||||||
userinput = "".join(map(lambda x, y: str(int(x) | int(y)),
|
switch_state = self.get_switch_state()
|
||||||
userinput, switch_state))
|
userinput = "".join(map(lambda x, y: str(int(x) & int(y)),
|
||||||
elif mode == 'x': # XOR operator.
|
userinput, switch_state))
|
||||||
switch_state = self.get_switch_state()
|
elif ops[-1] == '|': # OR operator.
|
||||||
userinput = "".join(map(lambda x, y: str(int(x) ^ int(y)),
|
switch_state = self.get_switch_state()
|
||||||
userinput, switch_state))
|
userinput = "".join(map(lambda x, y: str(int(x) | int(y)),
|
||||||
|
userinput, switch_state))
|
||||||
|
elif ops[-1] == '^': # XOR operator.
|
||||||
|
switch_state = self.get_switch_state()
|
||||||
|
userinput = "".join(map(lambda x, y: str(int(x) ^ int(y)),
|
||||||
|
userinput, switch_state))
|
||||||
|
ops = ops[:-1]
|
||||||
|
|
||||||
command=[]
|
command=[]
|
||||||
for i in range(len(self.switches)):
|
for i in range(len(self.switches)):
|
||||||
|
@ -1295,11 +1300,11 @@ if __name__ == "__main__": # {{{1
|
||||||
# Switch control
|
# Switch control
|
||||||
group_sw = parser.add_argument_group(title="light switch control",
|
group_sw = parser.add_argument_group(title="light switch control",
|
||||||
description="BINARY_CODE is a string of 0s or 1s for every light in a \
|
description="BINARY_CODE is a string of 0s or 1s for every light in a \
|
||||||
room. May be given as decimal. May be prepended by '&', '|' or '^' as \
|
room. May be given as decimal. May be prepended by '~', '&', '|' or \
|
||||||
AND, OR and XOR operators. Current switch states will be printed to \
|
'^' as NOT, AND, OR and XOR operators. Current switch states will be \
|
||||||
stdout if BINARY_CODE is '-'. Will show usage information and ask for \
|
printed to stdout if BINARY_CODE is '-'. Will show usage information \
|
||||||
input if BINARY_CODE is omitted. Will read from stdin if BINARY_CODE \
|
and ask for input if BINARY_CODE is omitted. Will read from stdin if \
|
||||||
is omitted and stdin is not connected to a TTY.")
|
BINARY_CODE \ is omitted and stdin is not connected to a TTY.")
|
||||||
group_sw.add_argument(
|
group_sw.add_argument(
|
||||||
"-W", nargs='?', dest="w_switch", const="", metavar="BINARY_CODE",
|
"-W", nargs='?', dest="w_switch", const="", metavar="BINARY_CODE",
|
||||||
help="switch lights in Wohnzimmer on/off")
|
help="switch lights in Wohnzimmer on/off")
|
||||||
|
|
Loading…
Reference in a new issue