def button_handler(source, event): if source not in buttons: buttons[source] = [[0 for x in xrange(0, 16)] for x in xrange(0, 16)] x = event["x"] y = event["y"] buttons[source][x][y] ^= 1 if buttons[source][x][y]: responseq.put( { "type" : monomeio.DEVICE_TYPE_MONOME, "command" : monomemodel.COMMAND_LED_ON, "target" : source, "x" : x, "y" : y }) else: responseq.put( { "type" : monomeio.DEVICE_TYPE_MONOME, "command" : monomemodel.COMMAND_LED_OFF, "target" : source, "x" : x, "y" : y })
def button_handler(self, source, device, event): if source not in self.sources: self.sources.append(source) self.devices[source] = device self.cells[source] = [] self.buttons[source] = [[0 for x in xrange(0, device['columns'])] for x in xrange(0, device['rows'])] logservice.log(logservice.LOG_ERR, "Added a new oto source: " + str(source)) logservice.log(logservice.LOG_ERR, "Columns: " + str(device['columns'])) logservice.log(logservice.LOG_ERR, "Rows: " + str(device['rows'])) # Add this cell direction = CELL_UP # If it was added on an edge, correct the direction if event['x'] == 0: direction = CELL_RIGHT if event['x'] == device['columns'] - 1: direction = CELL_LEFT if event['y'] == 0: direction = CELL_DOWN remove_action = False # If it collides with an existing cell, then remove them both for cell in self.cells[source]: if cell['row'] == event['y'] and cell['column'] == event['x'] and cell['serial'] != self.serial: self.cells[source].remove(cell) remove_action = True if not remove_action: self.cells[source].append( { 'direction': direction, 'column': event['x'], 'row': event['y'], 'serial' : self.serial }) self.serial += 1 x = event["x"] y = event["y"] self.buttons[source][y][x] ^= 1 if self.buttons[source][y][x]: responseq.put({ "type": monomeio.DEVICE_TYPE_MONOME, "command": monomemodel.COMMAND_LED_ON, "target": source, "x": x, "y": y }) else: responseq.put({ "type": monomeio.DEVICE_TYPE_MONOME, "command": monomemodel.COMMAND_LED_OFF, "target": source, "x": x, "y": y })
def clock(self, t): for source in self.sources: topevent = 0 leftevent = 0 bottomevent = 0 rightevent = 0 collisionevent = 0 # Iterate through any live cells we have and move them in the proper direction for cell in self.cells[source]: # UP UP UP if cell['direction'] == CELL_UP: cell['row'] -= 1 if cell['row'] <= 0: cell['row'] = 0 topevent = cell cell['direction'] = CELL_DOWN # LEFT LEFT LEFT elif cell['direction'] == CELL_RIGHT: cell['column'] += 1 if cell['column'] >= self.devices[source]['columns'] - 1: cell['column'] = self.devices[source]['columns'] - 1 rightevent = cell cell['direction'] = CELL_LEFT # DOWN DOWN DOWN elif cell['direction'] == CELL_DOWN: cell['row'] += 1 if cell['row'] >= self.devices[source]['rows'] - 1: cell['row'] = self.devices[source]['rows'] - 1 bottomevent = cell cell['direction'] = CELL_UP # RIGHT RIGHT RIGHT elif cell['direction'] == CELL_LEFT: cell['column'] -= 1 if cell['column'] <= 0: cell['column'] = 0 leftevent = cell cell['direction'] = CELL_RIGHT collided = [] # Look for any collisions and rotate the direction of those cells for cell1 in self.cells[source]: for cell2 in self.cells[source]: if cell1['serial'] != cell2['serial'] and cell1['column'] == cell2['column'] and cell1['row'] == cell2['row'] and cell2['serial'] not in collided and cell1['serial'] not in collided: cell1['direction'] = (cell1['direction'] + 1) % 4 cell2['direction'] = (cell2['direction'] + 1) % 4 collided.append(cell2['serial']) collided.append(cell1['serial']) collisionevent += 1 # Process Events if topevent: pass if leftevent: pass if bottomevent: pass if rightevent: pass if collisionevent: pass # Update the display with a map command self.buttons[source] = [[0 for x in xrange(0, self.devices[source]['columns'])] for x in xrange(0, self.devices[source]['rows'])] for cell in self.cells[source]: self.buttons[source][cell['row']][cell['column']] = 1 responseq.put({ "type": monomeio.DEVICE_TYPE_MONOME, "command": monomemodel.COMMAND_LED_MAP, "target": source, "data": self.buttons[source] })