from simplegladeapp import bindtextdomain
from dispatcher import Dispatcher
from led import Led
+import etherled
app_name = "cetherled"
app_version = "1.0"
bindtextdomain(app_name, locale_dir)
-
class MainWindow(SimpleGladeApp):
def __init__(self, path="cetherled.glade", root="main_window",
domain=app_name, **kwargs):
#notificar = Dispatcher(self.actualizar)
+ self.device = etherled.NetworkedDevice()
path = os.path.join(glade_dir, path)
SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
for i in xrange(16):
for j in xrange(16):
led = Led()
- self.table_leds.attach(led, i, i+1, j, j+1)
+ self.table_leds.attach(led, j, j+1, i, i+1)
led.show()
self.tabla[i,j] = led
self.quit()
def on_btn_enviar_clicked(self, widget, *args):
- print self.tabla_a_stream()
+ self.device.matrix = self.leds2matrix()
def on_main_window_delete_event(self, widget, event, *args):
self.quit()
- def imprimir_tabla(self, rotada=False):
- rango = xrange(16)
- if rotada:
- rango = xrange(15, -1, -1)
- for col in rango:
- for row in xrange(16):
- print int(self.tabla[col,row].prendido),
+ def leds2matrix(self):
+ matrix = {}
+ for row in xrange(16):
+ for col in xrange(16):
+ matrix[row,col] = int(self.tabla[row,col].prendido)
+ #print int(self.tabla[row,col].prendido),
+ print (row,col),
print
print
-
- def imprimir_stream(self, stream):
- for i in stream:
- print '0x%02x' % i
-
- def tabla_a_stream(self):
- stream = []
- for col in xrange(15, -1, -1):
- i = 0
- for row in xrange(8):
- i += int(self.tabla[col,row].prendido) << row
- stream.append(i)
- i = 0
- for row in xrange(8, 16):
- i += int(self.tabla[col,row].prendido) << row
- stream.append(i)
- return stream
-
+ etherled.protocol._print_matrix(matrix)
+ return matrix
def main():
gtk.threads_init()
main_window.run()
gtk.threads_leave()
-
if __name__ == '__main__':
main()
# Tamaño del buffer
_BUFSIZ = 65536
+# Cantidad de bytes de la columna de leds
+_LED_BYTES = 2
+
class SendError(socket.error):
pass
class NetworkedDevice(Client):
- LED_BYTES = 2
-
def _getMatrix(self):
stream = self.get(packet.VAR_MATRIX)
- return self._stream2Matrix(stream)
+ return _stream2Matrix(stream)
def _setMatrix(self, matrix):
- stream = self._matrix2Stream(matrix)
+ stream = _matrix2Stream(matrix)
self.set(packet.VAR_MATRIX, stream)
- def _stream2Matrix(self, stream):
- cols = ord(stream[0]) # Obtiene tamaño
- stream = stream[1:1+cols*self.LED_BYTES] # me quedo con el resto
- matrix = {}
- for col in xrange(cols-1, -1, -1):
- for row_byte in xrange(self.LED_BYTES):
- byte = ord(stream[(cols-col-1)*self.LED_BYTES+row_byte])
- for i in xrange(8):
- shift = 8 - i - 1
- matrix[col, row_byte*8+i] = (byte >> shift) & 1
- return matrix
-
- def _matrix2Stream(self, matrix):
- cols = len(matrix) / (self.LED_BYTES*8)
- stream = chr(cols) # primero va el tamaño
- for col in xrange(cols-1, -1, -1):
- for i in xrange(self.LED_BYTES):
- byte = 0
- for row in xrange(8):
- shift = 8 - row - 1
- byte += matrix[col,row] << shift
- stream += chr(byte)
- return stream
-
matrix = property(_getMatrix, _setMatrix, doc='Matriz de leds')
class DummyServer:
while True:
(msg, addr) = self._sock.recvfrom(_BUFSIZ)
pkt = packet.ServerPacket(msg)
+ print 'Recibido:', repr(pkt)
if pkt.type == packet.TYPE_GET:
pkt.data = self._vars[pkt.var]
elif pkt.type == packet.TYPE_SET:
+ if pkt.var == packet.VAR_MATRIX:
+ _print_matrix(_stream2Matrix(pkt.data))
self._vars[pkt.var] = pkt.data
pkt = packet.ServerPacket(pkt.type, pkt.var, pkt.id)
sent = self._sock.sendto(str(pkt), addr)
if sent != len(pkt):
raise SendError, "Sólo se enviaron %d bytes de %d" \
% (sent, len(packet))
+ print 'Enviado:', repr(pkt)
+ if pkt.type == packet.TYPE_GET and pkt.var == packet.VAR_MATRIX:
+ _print_matrix(_stream2Matrix(pkt.data))
def _getHost(self):
return self._host
port = property(_getPort, doc='Puerto al cual enviar datos')
addr = property(_getAddr, doc='Tupla (host, port)')
+def _stream2Matrix(stream):
+ cols = ord(stream[0]) # Obtiene tamaño
+ stream = stream[1:1+cols*_LED_BYTES] # me quedo con el resto
+ matrix = {}
+ for col in xrange(cols-1, -1, -1):
+ for row_byte in xrange(_LED_BYTES):
+ byte = ord(stream[(cols-col-1)*_LED_BYTES+row_byte])
+ for i in xrange(8):
+ shift = 8 - i - 1
+ matrix[row_byte*8+i,col] = (byte >> shift) & 1
+ return matrix
+
+def _matrix2Stream(matrix):
+ cols = len(matrix) / (_LED_BYTES*8)
+ stream = chr(cols) # primero va el tamaño
+ for col in xrange(cols-1, -1, -1):
+ for i in xrange(_LED_BYTES):
+ byte = 0
+ for row in xrange(8):
+ shift = 8 - row - 1
+ byte += matrix[row+i*8,col] << shift
+ stream += chr(byte)
+ return stream
+
def _print_matrix(matrix):
- for row in xrange(NetworkedDevice.LED_BYTES*8):
- for col in xrange(len(matrix)/(NetworkedDevice.LED_BYTES*8)):
+ for row in xrange(_LED_BYTES*8):
+ for col in xrange(len(matrix)/(_LED_BYTES*8)):
print matrix[row,col],
print
print
_print_matrix(dev.matrix)
# Verifico resultado
assert matrix == dev.matrix
+ ###########################
+ # Creo matriz
+ matrix = {}
+ for col in xrange(16):
+ for row in xrange(16):
+ matrix[row,col] = 0
+ matrix[0,0] = 1
+ matrix[0,14] = 1
+ matrix[0,15] = 1
+ matrix[1,15] = 1
+ matrix[13,0] = 1
+ matrix[14,0] = 1
+ matrix[15,0] = 1
+ matrix[15,1] = 1
+ matrix[15,2] = 1
+ matrix[12,15] = 1
+ matrix[13,15] = 1
+ matrix[14,15] = 1
+ matrix[15,15] = 1
+ matrix[15,14] = 1
+ matrix[15,13] = 1
+ matrix[15,12] = 1
+ # Mando matriz
+ print 'Matriz enviada:'
+ _print_matrix(matrix)
+ dev.matrix = matrix
+ print 'Matriz recibida:'
+ _print_matrix(dev.matrix)
+ # Verifico resultado
+ assert matrix == dev.matrix
# Matamos al servidor
os.kill(pid, 15)
else: