Skip to content

Commit

Permalink
replace this code before porting to python4
Browse files Browse the repository at this point in the history
  • Loading branch information
lounge committed Apr 2, 2022
1 parent fabddb2 commit 184cddc
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 106 deletions.
8 changes: 4 additions & 4 deletions acabsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def send(x,y,r,g,b,t=0,w=WALL):

# recalculate x and y based on input x,y and wall no w
x=((w*WALLSIZEX)+x)
msg = bytearray([x,y,ord('C'),r,g,b,ms>>8,ms&0xFF])
#print list(msg)
msg = bytes([x,y,ord('C'),r,g,b,ms>>8,ms&0xFF])
#print(list(msg))
sock.sendto(msg, (UDPHOST, UDPPORT))

def update(buffered = True):
if buffered:
msg = bytearray([1,0,ord('U'),0,0,0,0,0])
msg = bytes([1,0,ord('U'),0,0,0,0,0])
else:
msg = bytearray([0,0,ord('U'),0,0,0,0,0])
msg = bytes([0,0,ord('U'),0,0,0,0,0])

sock.sendto(msg, (UDPHOST, UDPPORT))

Expand Down
2 changes: 1 addition & 1 deletion animations/curvepointbase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python

# Base class for implementing "point-style" animations using curve functions.

Expand Down
12 changes: 6 additions & 6 deletions animations/dropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import random
import time

tick = 1.8
tick = 1.8

def setcol(col, r, g, b, rtime):
for i in range(0,acabsl.WALLSIZEY):
send(col,i,r,g,b,rtime);
update()
time.sleep(tick)
update()
time.sleep(tick)
h = 0
col = 0
update()
Expand All @@ -23,7 +23,7 @@ def setcol(col, r, g, b, rtime):
h = h % 1.

for col in range(0,acabsl.WALLSIZEX):
r,g,b = colorsys.hsv_to_rgb(h, 1., 1.)
setcol(col, r*255, g*255, b*255, tick*3)
col += 1
r,g,b = colorsys.hsv_to_rgb(h, 1., 1.)
setcol(col, r*255, g*255, b*255, tick*3)
col += 1

59 changes: 29 additions & 30 deletions server/acabsl-config-generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
import acabsl_interface
import thread
import time
import sys
import os
Expand All @@ -21,8 +20,8 @@ def get_matrix(matrix):
return m

def print_matrix(matrix):
print "Currently known matrix:"
print get_matrix(matrix)
print("Currently known matrix:")
print(get_matrix(matrix))

def write_config(serials, interfaces, matrix):
config_file = open(sys.argv[1], 'w')
Expand Down Expand Up @@ -62,20 +61,20 @@ def write_config(serials, interfaces, matrix):

serials1 = set()

raw_input("Please attach the USB to RS485 bridges and press enter.: ")
input("Please attach the USB to RS485 bridges and press enter.: ")

serials2 = set(os.listdir('/dev/serial/by-id'))

serials = ['/dev/serial/by-id/'+serial for serial in serials2 - serials1]

if len(serials) == 0:
print "Found no new devices. Aborting."
print("Found no new devices. Aborting.")
sys.exit(1)

for serial in serials:
print "Found device: ", serial
print("Found device: ", serial)

interfaces = range(len(serials))
interfaces = list(range(len(serials)))

matrix = []

Expand All @@ -87,11 +86,11 @@ def get_number(message, base=10, default=None):
number = None

while number == None:
input = raw_input(message)
if input == '' and default != None:
inp = input(message)
if inp == '' and default != None:
return default
try:
number = int(input, base)
number = int(inp, base)
except:
pass
return number
Expand All @@ -100,25 +99,25 @@ def get_tuple(message, default=None):
tuple = None

while tuple == None:
input = raw_input(message)
if input == '' and default != None:
inp = input(message)
if inp == '' and default != None:
return default
try:
input = input.split(',')
tuple = (int(input[0]), int(input[1]))
inp = inp.split(',')
tuple = (int(inp[0]), int(inp[1]))
except:
pass
return tuple

xsize = get_number("Please enter the horizontal dimension [16]: ", 10, 16)
ysize = get_number("Please enter the vertical dimension [6]: ",10, 6)
print "Your matrix is",xsize,"X",ysize,"big."
print("Your matrix is",xsize,"X",ysize,"big.")

min_address = get_number("Please enter the smallest lamp address (in hexadecimal)[10]: ", 16, 0x10)
max_address = get_number("Please enter the biggest lamp address (in hexadecimal)[90]: ", 16, 0x90)
print "Tested address range: %02X - %02X (%d addresses)"%(min_address, max_address, max_address-min_address+1)
max_address = get_number("Please enter the biggest lamp address (in hexadecimal)[FF]: ", 16, 0xFF)
print("Tested address range: %02X - %02X (%d addresses)"%(min_address, max_address, max_address-min_address+1))

addresses = range(min_address, max_address+1)
addresses = list(range(min_address, max_address+1))

matrix = [[None for x in range(xsize)] for y in range(ysize)]

Expand Down Expand Up @@ -151,7 +150,7 @@ def get_next_unknown_index(matrix):
acabsl_interface.sendSetColor(0, 0, 0, 0, interface)

def half(l):
return l[0:(len(l)+1)/2], l[(len(l)+1)/2:]
return l[0:int((len(l)+1)/2)], l[int((len(l)+1)/2):]

def set_interfaces(interfaces, r, g, b):
for interface in interfaces:
Expand All @@ -173,7 +172,7 @@ def find_interface(interfaces):
red_interfaces, green_interfaces = half(interfaces)
blue_interfaces = []

print "Looking for the correct interface. Press 'Q' to abort. Press 'N' if the lamp does not exist."
print("Looking for the correct interface. Press 'Q' to abort. Press 'N' if the lamp does not exist.")

abort = False
skip = False
Expand All @@ -184,7 +183,7 @@ def find_interface(interfaces):

set_known_addresses(0, 0, 0)

result = raw_input("Which color does the lamp have? (r,g,b): ")
result = input("Which color does the lamp have? (r,g,b): ")
if result == 'r' and len(red_interfaces) == 1:
return red_interfaces[0]
if result == 'r' and len(red_interfaces) > 0:
Expand All @@ -200,7 +199,7 @@ def find_interface(interfaces):
continue

if result != '' and result in 'rgb':
print "There was an error: The lamp ahould not have this color"
print("There was an error: The lamp ahould not have this color")
abort = True

if result == 'Q':
Expand All @@ -210,27 +209,27 @@ def find_interface(interfaces):
skip = True

if abort:
print "Aborting."
print("Aborting.")
return None

if skip:
print "Skipping this lamp"
print("Skipping this lamp")
return 'skip'

def find_lamp_address(interface, addresses):
acabsl_interface.sendSetColor(0, 0, 0, 0, interface)
red_addresses, green_addresses = half(addresses)
blue_addresses = []

print 'Looking for the lamp\'s address. Press Q to abort.'
print('Looking for the lamp\'s address. Press Q to abort.')
abort = False

while not abort:
set_lamps(red_addresses, interface, 255, 0, 0)
set_lamps(green_addresses, interface, 0, 255, 0)
set_lamps(blue_addresses, interface, 0, 0, 0)

result = raw_input("Which color does the lamp have? (r,g,b): ")
result = input("Which color does the lamp have? (r,g,b): ")
if result == 'r' and len(red_addresses) == 1:
return red_addresses[0]
if result == 'r' and len(red_addresses) > 0:
Expand All @@ -246,13 +245,13 @@ def find_lamp_address(interface, addresses):
continue

if result != '' and result in 'rgb':
print "There was an error: The lamp should not have this color"
print("There was an error: The lamp should not have this color")
abort = True

if result == 'Q':
abort = True

print "Aborting."
print("Aborting.")
return None

write_config(serials, interfaces, matrix)
Expand All @@ -266,10 +265,10 @@ def find_lamp_address(interface, addresses):
x,y = get_tuple("Coordinate to test (-1,-1 to quit): [%d,%d]: "%(x,y), (x,y))

if x==-1 and y==-1:
print "Aborting."
print("Aborting.")
break

print "Starting to search for lamp at coordinate (%d,%d):"%(x,y)
print("Starting to search for lamp at coordinate (%d,%d):"%(x,y))

if matrix[y][x] != None:
addresses.append(matrix[y][x][0])
Expand Down
39 changes: 19 additions & 20 deletions server/acabsl_interface.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import serialinterface
import time
import Queue
from queue import Queue
import threading
import thread

updatelock = threading.RLock()
updatequeues = []
Expand All @@ -24,8 +23,8 @@ def interfaceHandler(queue, serial, updatequeue):

def createBridge(dev):
serial = serialinterface.SerialInterface(dev,115200,1)
queue = Queue.Queue(130)
updatequeue = Queue.Queue(1)
queue = Queue()
updatequeue = Queue(1)
updatequeues.append(updatequeue)
return (queue, serial, updatequeue)

Expand All @@ -35,15 +34,16 @@ def init(s, i, m):
interfaces = i
matrix = m

bridges = map(createBridge,serials)
bridges = list(map(createBridge,serials))
for bridge in bridges:
bridge[1].write('\\F')
bridge[1].write(b'\\F')
# Put all lamps into the pause state
msg = '\x00S\x01'
msg = "\x5c\x30%s\x5c\x31"%(msg.replace("\\","\\\\"))
msg = msg.replace("\\","\\\\")
msg = b'\x00S\x01'
msg = b"\\\\0" + msg.replace(b"\\",b"\\\\\\\\") + b"\\\\1"
bridge[1].write(msg)
thread.start_new_thread(interfaceHandler,bridge)
t = threading.Thread(target=interfaceHandler,args=bridge)
t.daemon = True
t.start()

buffered = False

Expand Down Expand Up @@ -83,17 +83,17 @@ def low(x):
def sendSetColor(lamp,r,g,b,interface):
global buffered
if buffered:
cmd = "%cP%c%c%c"%(chr(lamp),chr(r),chr(g),chr(b))
cmd = bytes([lamp,ord("P"),r,g,b])
else:
cmd = "%cC%c%c%c"%(chr(lamp),chr(r),chr(g),chr(b))
cmd = bytes([lamp,ord("C"),r,g,b])
write(cmd,interface);

def sendMSFade(lamp,r,g,b,ms,interface):
global buffered
if buffered:
cmd = "%cc%c%c%c%c%c"%(chr(lamp),chr(r),chr(g),chr(b),chr(high(ms)),chr(low(ms)))
cmd = bytes([lamp,ord("c"),r,g,b,high(ms),low(ms)])
else:
cmd = "%cM%c%c%c%c%c"%(chr(lamp),chr(r),chr(g),chr(b),chr(high(ms)),chr(low(ms)))
cmd = bytes([lamp,ord("M"),r,g,b,high(ms),low(ms)])
write(cmd,interface);

def sendUpdate(mode):
Expand All @@ -104,23 +104,22 @@ def sendUpdate(mode):
sendMSFade(0,0,0,0,1500,interfaces[i])
buffered = mode
if buffered:
cmd = "%cU"%chr(0)
cmd = bytes([0,ord("U")])
for i in interfaces:
write(cmd, interfaces[i])


def write(msg, interface):
msg = "\x5c\x30%s\x5c\x31"%(msg.replace("\\","\\\\"))
msg = msg.replace("\\","\\\\")
msg = b"\\\\0" + msg.replace(b"\\",b"\\\\\\\\") + b"\\\\1"

if not bridges[interface][0].full():
if fullmessage[interface]:
print 'reactivating bridge'
print('reactivating bridge')
fullmessage[interface] = False
bridges[interface][1].write('\\F')
bridges[interface][1].write(b'\\F')
bridges[interface][0].put(msg)
else:
if fullmessage[interface] == False:
print 'ignoring', msg, 'queue for bridge is full'
print('ignoring', msg, 'queue for bridge is full')
fullmessage[interface] = True

16 changes: 8 additions & 8 deletions server/acabslrouter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env python2
#!/usr/bin/env python
import socket
import thread
import threading
import time
import Queue
import sys
import select

Expand All @@ -19,7 +18,8 @@
"""

config = sys.argv[1]
execfile(config)
with open(config) as f:
exec(f.read())

#inputs = [[port, priority, timeout, socket],
inputs = [{'port': router_base_port + 0, 'priority': 0, 'timeout': 1},
Expand All @@ -40,7 +40,7 @@
for w in walls:
w['socket'] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
w['simsocket'] = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#q = Queue.Queue(100)

selected_input = inputs[0]


Expand Down Expand Up @@ -73,15 +73,15 @@ def forward(data, source):
if source not in tainted:
tainted['source'] = []

x = ord(data[0])
y = ord(data[1])
x = data[0]
y = data[1]
cmd = data[2]
#print x,y,cmd

if cmd != 'U':
wall = find_wall(x, y)
x,y = translate_for_wall(x,y,wall)
data = '%c%c%s'%(chr(x), chr(y), data[2:])
data = bytes([x, y]) + data[2:]
#print '->', x, y, wall['simport']
send_to_wall(data, wall)
if source not in tainted:
Expand Down
Loading

0 comments on commit 184cddc

Please sign in to comment.