From c6f222c1e4d1a5ed7dfb4017f3bc7d4289642c41 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Sun, 8 Jun 2014 22:22:25 +0300 Subject: Out to file works --- client.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/client.py b/client.py index 30bb3fe..4665e82 100644 --- a/client.py +++ b/client.py @@ -1,4 +1,6 @@ +from itertools import cycle from pprint import pprint +from time import sleep from unipath import FSPath as Path DATA_DIR = Path(__file__).absolute().ancestor(1).child('data') @@ -8,12 +10,17 @@ class LightLoopClient(object): def __init__(self): self.sequences = {} self.composition = [] - self.tempo = 300 + self.tempo = 0.3 + self.init_serial() self.init_data() + self.prepare_data() def __str__(self): return '{}\n{}\n{}'.format(self.sequences, self.composition, self.tempo) + def init_serial(self): + self.serial = open(DATA_DIR.child('serial.out'), 'a') + def init_data(self): with open(DATA_DIR.child('data.txt'), 'r') as f: sequence_data, composition_data = f.read().split('\n===\n') @@ -28,7 +35,7 @@ class LightLoopClient(object): def init_composition(self, composition_data): composition_data = filter(lambda x: x != '', composition_data.split('\n')) - self.tempo = composition_data[0].split('_')[1] + self.tempo = float(composition_data[0].split('_')[1]) / 1000.0 self.composition = [c.split(',') for c in composition_data[1:]] def gen_packet(self, tree, channel, mode): @@ -47,7 +54,7 @@ class LightLoopClient(object): self.data_packets = [] for sequence in data: for frame in sequence: - frame_packets = [] + frame_packets = bytearray() for tree, tree_frame in enumerate(frame): for channel, mode in enumerate(tree_frame): tree, channel, mode = map(int, [tree, channel, mode]) @@ -57,10 +64,14 @@ class LightLoopClient(object): self.data_packets.append(frame_packets) # print 'sleep' + def write(self, packets): + self.serial.write(packets) + self.serial.flush() def loop(self): - self.prepare_data() - print self.data_packets + for frame in cycle(self.data_packets): + self.write(frame) + sleep(self.tempo) if __name__ == '__main__': -- cgit v1.3.1