def __init__(self): self.smallSheep = SmallSheep() self.bigSheep12 = BigSheep12() self.bigSheep34 = BigSheep34() self.signs = Signs() self.flower = Flower() self.current_block_num = -1 self.animations = [] self.plan = [] self.cycle_num = 0
def __init__(self): self.start_time = datetime.datetime.now() self.flower = Flower() self.sheep = SmallSheep() self.grass = Grass() self.sign = Sign() self.lake = Lake() self.tree = Tree() self.temp_stick = TempStick() self.temp_stick.set_is_on(True) self.sachi_meter = SachiMeter() self.flower_animation = GlowFlowerAnimation(self.flower) self.grass_animation = GlowGrassAnimation(self.grass) self.lake_animation = GlowAnimation(self.lake, [0, 0, 255]) self.sheep_animation = GlowSheepAnimation(self.sheep) self.sign_animation = GlowSignAnimation(self.sign) self.tree_animation = GlowTreeAnimation(self.tree)
def __init__(self, file_name): self.file_name = file_name self.flower_animation = None self.flower_animation_mul = 1 self.grass_animation = None self.grass_animation_mul = 1 self.lake_animation = None self.lake_animation_mul = 1 self.sheep_animation = None self.sheep_animation_mul = 1 self.sign_animation = None self.sign_animation_mul = 1 self.tree_animation = None self.tree_animation_mul = 1 self.flower = Flower() self.sheep = SmallSheep() self.grass = Grass() self.sign = Sign() self.lake = Lake() self.tree = Tree() self.temp_stick = TempStick() self.sachi_meter = SachiMeter() with open(self.file_name, 'r') as f: self.song_yml = yaml.load(f) self.audio_file = "Music/" + self.song_yml['file_name'] self.pieces = self.song_yml['pieces'] self.current_piece_id = 0 self.offset = self.song_yml['offset'] self.is_transition = 'Transitions' in self.audio_file self.temp_stick.set_is_on(self.is_transition) self.temp_stick.set_brightness(1.0 if self.is_transition else 0.15) print str(self.pieces[self.current_piece_id][0]) + " - " + str( self.pieces[self.current_piece_id][1]) self.create_animations(self.pieces[self.current_piece_id][2])
class TransitionsDriver: def __init__(self): self.start_time = datetime.datetime.now() self.flower = Flower() self.sheep = SmallSheep() self.grass = Grass() self.sign = Sign() self.lake = Lake() self.tree = Tree() self.temp_stick = TempStick() self.temp_stick.set_is_on(True) self.sachi_meter = SachiMeter() self.flower_animation = GlowFlowerAnimation(self.flower) self.grass_animation = GlowGrassAnimation(self.grass) self.lake_animation = GlowAnimation(self.lake, [0, 0, 255]) self.sheep_animation = GlowSheepAnimation(self.sheep) self.sign_animation = GlowSignAnimation(self.sign) self.tree_animation = GlowTreeAnimation(self.tree) def play_animations(self, curr_temerature, sachi_meter, input_type): diff_time = (datetime.datetime.now() - self.start_time).total_seconds() time_percent = (diff_time % 3.0) / 3.0 self.temp_stick.set_is_input_mode( input_type == Decisions.InputType.TEMPERATURE) self.sachi_meter.set_is_input_mode( input_type == Decisions.InputType.SACHI) self.sheep_animation.set_is_input_mode( input_type == Decisions.InputType.SACHI) self.flower_animation.apply(time_percent) self.grass_animation.apply(time_percent) self.lake_animation.apply(time_percent) self.sheep_animation.apply(time_percent) self.sign_animation.apply(time_percent) self.tree_animation.apply(time_percent) self.temp_stick.set_temperature(time_percent, curr_temerature) self.sachi_meter.set_sachi_meter(time_percent, sachi_meter) network.send(flower_data=self.flower.get_array(), sheep_data=self.sheep.get_array(), grass_data=self.grass.get_array(), sign_data=self.sign.get_array(), lake_data=self.lake.get_array(), temp_stick=self.temp_stick.get_array(), sachi_meter=self.sachi_meter.get_array(), tree_data=self.tree.get_array())
class Song(object): def __init__(self): self.smallSheep = SmallSheep() self.bigSheep12 = BigSheep12() self.bigSheep34 = BigSheep34() self.signs = Signs() self.flower = Flower() self.current_block_num = -1 self.animations = [] self.plan = [] self.cycle_num = 0 def play(self, pos=0): cycle_num = 0 pygame.mixer.init() pygame.mixer.music.load(self.file) pygame.mixer.music.play(0, pos) clock = pygame.time.Clock() while pygame.mixer.music.get_busy(): song_time = pygame.mixer.music.get_pos() #song_time = song_time/1000.0 song_time = max(song_time / 1000.0 - 0.6, 0) self.apply_animations(song_time) networking.send(cycle_num, self.smallSheep.get_array(), self.bigSheep12.get_array(), self.bigSheep34.get_array(), self.signs.get_array(), self.flower.get_array()) clock.tick(50) cycle_num += 1 def apply_animations(self, current_time): #calculate current block block_num = -1 for i in range(len(self.plan)): block = self.plan[i] start_time = block[0] end_time = block[0] + block[3] if current_time >= start_time and current_time < end_time: block_num = i current_block = self.plan[block_num] if (block_num != self.current_block_num): self.current_block_num = block_num print str(block_num) + " - " + str(current_block) self.show_random_animation(current_block) #self.show_fade_in_out_animation(current_block) percent = (current_time - current_block[0]) / current_block[3] for animation in self.animations: animation.apply(percent) if (block_num == len(self.plan) - 1): # fade out - end of the song power = float(max(1 - percent * 2, 0)) print "fade_out " + str(power) data = self.smallSheep.get_array() + self.bigSheep12.get_array( ) + self.bigSheep34.get_array() + self.signs.get_array( ) + self.flower.get_array() for i in range(len(self.smallSheep.get_array())): self.smallSheep.get_array()[i] = int( math.floor(self.smallSheep.get_array()[i] * power)) for i in range(len(self.bigSheep12.get_array())): self.bigSheep12.get_array()[i] = int( math.floor(self.bigSheep12.get_array()[i] * power)) for i in range(len(self.bigSheep34.get_array())): self.bigSheep34.get_array()[i] = int( math.floor(self.bigSheep34.get_array()[i] * power)) for i in range(len(self.signs.get_array())): self.signs.get_array()[i] = int( math.floor(self.signs.get_array()[i] * power)) for i in range(len(self.flower.get_array())): self.flower.get_array()[i] = int( math.floor(self.flower.get_array()[i] * power)) def show_random_animation(self, current_block): animationType = random.randrange(1, 8) if (animationType == 0): self.show_spinning_head_animation(current_block) elif (animationType == 1): self.show_fade_in_out_animation(current_block) elif (animationType == 2): self.show_alternate_animation(current_block) elif (animationType == 3): self.show_sheep_confetti_animation(current_block) elif (animationType == 4): self.show_rainbow_animation(current_block) elif (animationType == 5): self.show_snake_animation(current_block) elif (animationType == 6): self.show_broken_animation(current_block) else: self.show_fibonacci_animation(current_block) def num_of_blocks(self, original, div): while original % div != 0: div = div / 2 return original / div def show_alternate_animation(self, current_block): print "alternate" num_of_blocks = current_block[1] if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 2) hue1 = random.random() hue2 = hue1 hue3 = hue1 if current_block[2] == 'S': hue2 = Colors.reduce_by_1(hue1 + 0.333) hue3 = Colors.reduce_by_1(hue1 + 0.666) self.animations = [ AlternateAnimation(self.smallSheep, num_of_blocks, hue2), AlternateAnimation(self.bigSheep12, num_of_blocks, hue1), AlternateAnimation(self.bigSheep34, num_of_blocks, hue3), AlternateSignsAnimation(self.signs, num_of_blocks, hue2), AlternateFlowerAnimation(self.flower, num_of_blocks, hue2) ] def show_fade_in_out_animation(self, current_block): print "fade_in_out" num_of_blocks = current_block[1] if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 2) hue1 = random.random() hue2 = hue1 hue3 = hue1 hue4 = hue1 if current_block[2] == 'S': hue2 = Colors.reduce_by_1(hue1 + 0.111) hue3 = Colors.reduce_by_1(hue1 + 0.222) hue3 = Colors.reduce_by_1(hue1 + 0.333) self.animations = [ FadeInOutAnimation(self.smallSheep, num_of_blocks, hue2), FadeInOutAnimation(self.bigSheep12, num_of_blocks, hue1), FadeInOutAnimation(self.bigSheep34, num_of_blocks, hue3), FadeInOutSignsAnimation(self.signs, num_of_blocks, hue2), FadeInOutFlowerAnimation(self.flower, num_of_blocks, hue2) ] def show_sheep_confetti_animation(self, current_block): print "confetti" leds_percent_per_cycle = 0.005 if current_block[2] == 'S': leds_percent_per_cycle = 0.03 self.animations = [ SheepConfettiAnimation(self.smallSheep, leds_percent_per_cycle), SheepConfettiAnimation(self.bigSheep12, leds_percent_per_cycle), SheepConfettiAnimation(self.bigSheep34, leds_percent_per_cycle), SignsConfettiAnimation(self.signs, leds_percent_per_cycle * 2), ConfettiFlowerAnimation(self.flower, leds_percent_per_cycle) ] def show_rainbow_animation(self, current_block): print "rainbow" num_of_blocks = self.num_of_blocks(current_block[1], 2) if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 4) self.animations = [ RainbowAnimation(self.smallSheep, num_of_blocks), RainbowAnimation(self.bigSheep12, num_of_blocks), RainbowAnimation(self.bigSheep34, num_of_blocks), RainbowSignsAnimation(self.signs, num_of_blocks), RainbowFlowerAnimation(self.flower, num_of_blocks) ] def show_snake_animation(self, current_block): print "snake" num_of_blocks = self.num_of_blocks(current_block[1], 2) if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 4) self.animations = [ SnakeAnimation(self.smallSheep, num_of_blocks), SnakeAnimation(self.bigSheep12, num_of_blocks), SnakeAnimation(self.bigSheep34, num_of_blocks), SignsConfettiAnimation(self.signs, 0.1), SnakeFlowerAnimation(self.flower, num_of_blocks) ] def show_spinning_head_animation(self, current_block): print "spinning_head" num_of_blocks = self.num_of_blocks(current_block[1], 1) if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 2) hue = random.random() self.animations = [ SpinningHeadAnimation(self.smallSheep, hue, num_of_blocks), SpinningHeadAnimation(self.bigSheep12, hue, num_of_blocks), SpinningHeadAnimation(self.bigSheep34, hue, num_of_blocks), FadeInOutStarsAnimation(self.stars, num_of_blocks) ] def show_fibonacci_animation(self, current_block): print "fibonacci" num_of_blocks = self.num_of_blocks(current_block[1], 2) if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 4) hue1 = random.random() hue2 = hue1 hue3 = hue1 if current_block[2] == 'S': hue2 = Colors.reduce_by_1(hue1 + 0.111) hue3 = Colors.reduce_by_1(hue1 + 0.222) self.animations = [ FibonacciAnimation(self.smallSheep, num_of_blocks, hue2), FibonacciAnimation(self.bigSheep12, num_of_blocks, hue1), FibonacciAnimation(self.bigSheep34, num_of_blocks, hue3), AlwaysOnSignsAnimation(self.signs, [30, 30, 30]), FibonacciFlowerAnimation(self.flower, num_of_blocks, hue2) ] def show_broken_animation(self, current_block): print "broken" num_of_blocks = self.num_of_blocks(current_block[1], 1) if current_block[2] == 'W': num_of_blocks = self.num_of_blocks(num_of_blocks, 2) self.animations = [ BrokenAnimation(self.smallSheep, num_of_blocks, 3), BrokenAnimation(self.bigSheep12, num_of_blocks, 6), BrokenAnimation(self.bigSheep34, num_of_blocks, 6), BrokenSignsAnimation(self.signs, num_of_blocks, 3), BrokenFlowerAnimation(self.flower, num_of_blocks, 2) ]
#!/usr/bin/env python import sys, os sys.path.append(os.path.abspath('../')) import Network.LedBurnProtocol as network import time from UIElements.Flower import Flower flower = Flower() from ExplosionFlowerAnimation import ExplosionFlowerAnimation from RainbowFlowerAnimation import RainbowFlowerAnimation from AlternateFlowerAnimation import AlternateFlowerAnimation from RoundRobinFlowerAnimation import RoundRobinFlowerAnimation from NaturalFlowerAnimation import NaturalFlowerAnimation from FireFlowerAnimation import FireFlowerAnimation from ConfettiFlowerAnimation import ConfettiFlowerAnimation from GlowFlowerAnimation import GlowFlowerAnimation from SpikesFlowerAnimation import SpikesFlowerAnimation # props = {'only_top' : False, # 'leds_percent_per_beat' : 0.5, # 'brightness': 0.7} # animation = ConfettiFlowerAnimation(flower, props) #animation = SpikesFlowerAnimation(flower, {'color':{'type':'const_color', 'hue':0.5}}) animation = ExplosionFlowerAnimation(flower, None) speed = 200 # in 50 hrz current_time = 0 frame_id = 0;
class Song(): def __init__(self, file_name): self.file_name = file_name self.flower_animation = None self.flower_animation_mul = 1 self.grass_animation = None self.grass_animation_mul = 1 self.lake_animation = None self.lake_animation_mul = 1 self.sheep_animation = None self.sheep_animation_mul = 1 self.sign_animation = None self.sign_animation_mul = 1 self.tree_animation = None self.tree_animation_mul = 1 self.flower = Flower() self.sheep = SmallSheep() self.grass = Grass() self.sign = Sign() self.lake = Lake() self.tree = Tree() self.temp_stick = TempStick() self.sachi_meter = SachiMeter() with open(self.file_name, 'r') as f: self.song_yml = yaml.load(f) self.audio_file = "Music/" + self.song_yml['file_name'] self.pieces = self.song_yml['pieces'] self.current_piece_id = 0 self.offset = self.song_yml['offset'] self.is_transition = 'Transitions' in self.audio_file self.temp_stick.set_is_on(self.is_transition) self.temp_stick.set_brightness(1.0 if self.is_transition else 0.15) print str(self.pieces[self.current_piece_id][0]) + " - " + str( self.pieces[self.current_piece_id][1]) self.create_animations(self.pieces[self.current_piece_id][2]) def get_audio_file(self): return self.audio_file def get_effect(self, animation_dict, name, element, factory): if animation_dict != None and name in animation_dict: if 'Clear' in animation_dict[name]: element.clear() return None else: return factory.create_animation(animation_dict[name], element) else: return None def get_mul(self, animation_dict, name): if animation_dict != None and name in animation_dict and 'beat_mul' in animation_dict[ name]: return animation_dict[name]['beat_mul'] return 1.0 def create_animations(self, animation_dict): self.flower_animation = self.get_effect(animation_dict, 'flower', self.flower, FlowerAnimationFactory) self.flower_animation_mul = self.get_mul(animation_dict, 'flower') self.grass_animation = self.get_effect(animation_dict, 'grass', self.grass, GrassAnimationFactory) self.grass_animation_mul = self.get_mul(animation_dict, 'grass') self.lake_animation = self.get_effect(animation_dict, 'lake', self.lake, LakeAnimationFactory) self.lake_animation_mul = self.get_mul(animation_dict, 'lake') self.sheep_animation = self.get_effect(animation_dict, 'sheep', self.sheep, SheepAnimationFactory) self.sheep_animation_mul = self.get_mul(animation_dict, 'sheep') self.sign_animation = self.get_effect(animation_dict, 'sign', self.sign, SignAnimationFactory) self.sign_animation_mul = self.get_mul(animation_dict, 'sign') self.tree_animation = self.get_effect(animation_dict, 'tree', self.tree, TreeAnimationFactory) self.tree_animation_mul = self.get_mul(animation_dict, 'tree') def apply_animation(self, animation, num_of_beats, duration, relative_song_time): if (animation == None): return if self.is_transition: percent_beat_time = (relative_song_time % 3.0) / 3.0 else: beat_duration = duration / num_of_beats beats_played = math.floor(relative_song_time / beat_duration) relative_beat_time = relative_song_time - beat_duration * beats_played percent_beat_time = relative_beat_time / beat_duration animation.apply(percent_beat_time) def clear_leds(self): self.flower.clear() self.sheep.clear() self.grass.clear() self.sign.clear() self.lake.clear() self.tree.clear() def play_animations(self, song_time, curr_temerature, sachi_meter=None): song_time += self.offset self.temp_stick.set_temperature(None, curr_temerature) self.sachi_meter.set_sachi_meter(None, sachi_meter) if self.current_piece_id < len( self.pieces) - 1 and song_time > self.pieces[ self.current_piece_id + 1][0]: self.current_piece_id += 1 print str(self.pieces[self.current_piece_id][0]) + " - " + str( self.pieces[self.current_piece_id][1]) self.create_animations(self.pieces[self.current_piece_id][2]) if self.current_piece_id == len(self.pieces) - 1: duration = 30.0 else: duration = self.pieces[self.current_piece_id + 1][0] - self.pieces[ self.current_piece_id][0] num_of_beats = self.pieces[self.current_piece_id][1] relative_song_time = song_time - self.pieces[self.current_piece_id][0] if self.flower_animation != None: flower_num_of_beats = num_of_beats * self.flower_animation_mul self.apply_animation(self.flower_animation, flower_num_of_beats, duration, relative_song_time) if self.grass_animation != None: grass_num_of_beats = num_of_beats * self.grass_animation_mul self.apply_animation(self.grass_animation, grass_num_of_beats, duration, relative_song_time) if self.lake_animation != None: lake_num_of_beats = num_of_beats * self.lake_animation_mul self.apply_animation(self.lake_animation, lake_num_of_beats, duration, relative_song_time) if self.sheep_animation != None: sheep_num_of_beats = num_of_beats * self.sheep_animation_mul self.apply_animation(self.sheep_animation, sheep_num_of_beats, duration, relative_song_time) if self.sign_animation != None: sign_num_of_beats = num_of_beats * self.sign_animation_mul self.apply_animation(self.sign_animation, sign_num_of_beats, duration, relative_song_time) if self.tree_animation != None: tree_num_of_beats = num_of_beats * self.tree_animation_mul self.apply_animation(self.tree_animation, tree_num_of_beats, duration, relative_song_time) network.send(flower_data=self.flower.get_array(), sheep_data=self.sheep.get_array())
#!/usr/bin/env python import sys, os sys.path.append(os.path.abspath('../')) import Network.LedBurnProtocol as network import time from UIElements.Flower import Flower from UIElements.SmallSheep import SmallSheep flower = Flower() sheep = SmallSheep() from Animations_Flower.ExplosionFlowerAnimation import ExplosionFlowerAnimation from Animations_Flower.RainbowFlowerAnimation import RainbowFlowerAnimation from Animations_Flower.AlternateFlowerAnimation import AlternateFlowerAnimation from Animations_Flower.RoundRobinFlowerAnimation import RoundRobinFlowerAnimation from Animations_Flower.NaturalFlowerAnimation import NaturalFlowerAnimation from Animations_Flower.FireFlowerAnimation import FireFlowerAnimation from Animations_Flower.ConfettiFlowerAnimation import ConfettiFlowerAnimation from Animations_Flower.GlowFlowerAnimation import GlowFlowerAnimation from Animations_Flower.SpikesFlowerAnimation import SpikesFlowerAnimation animations_flower_arr = [] animations_flower_arr.append({ 'animation': SpikesFlowerAnimation(flower, None), 'speed': 60 }) animations_flower_arr.append({ 'animation': RainbowFlowerAnimation(flower, None),
time_percent = (song_time - self.curr_effect_start) / self.curr_effect_time for e in self.s_effects: e.apply(time_percent, sheep.get_array()) for e in self.f_effects: e.apply(time_percent, flower.get_array()) if __name__ == "__main__": import pygame import Network.LedBurnProtocol as network from UIElements.SmallSheep import SmallSheep from UIElements.Flower import Flower flower = Flower() grass = [0, 0, 0] * 600 sign = [0, 0, 0] * 150 lake = [0, 0, 0] * 1800 sheep = SmallSheep() frame_id = random.randint(0, 10000000) song = MoominsSong(sheep, flower) clock = pygame.time.Clock() pygame.mixer.init() pygame.mixer.music.load(song.get_audio_file()) pygame.mixer.music.play(0, 0) while pygame.mixer.music.get_busy(): song_time = (pygame.mixer.music.get_pos() - 170) / 1000.0