def on_timmy_button_press(self, *largs): if not timmy_device.is_active(): return def load_voice_sound(index): try: path = r'D:\vladimir\MyProjects\Timmy\App\Data\Sounds\Timmy_{:02d}.wav'.format(index) # !!! path return SoundLoader.load(path) except e as Exception: print 'Could not load sound: {}'.format(e) return None def timmy(servo_id, sound): current_rotation = 60 # !!! use real data first_rotation = current_rotation - 20 last_rotation = current_rotation + 20 # self.set_servo_rotation(servo_id, first_rotation) sound.play() # speed = 60 / 0.15 # 0.15s -> 60 degree # speed /= 3 # waiting_time = abs(first_rotation - current_rotation) / speed # 30 / (60 / 0.15) = 0.5 * 0.15 = 0.075 # sleep(waiting_time) # self.set_servo_rotation(servo_id, last_rotation) # waiting_time = abs(first_rotation - last_rotation) / speed # sleep(waiting_time) # self.set_servo_rotation(servo_id, current_rotation) def get_rotation(degrees, next_motion=None): return Rotation(timmy_device, servo_id, degrees, next_motion) # action = get_rotation(first_rotation, get_rotation(last_rotation, get_rotation(current_rotation))) action = get_rotation(first_rotation).\ continue_with(get_rotation(last_rotation)).\ continue_with(get_rotation(current_rotation)) action.do() variant = randint(0, 3) sound = load_voice_sound(variant + 1) if not sound: return timmy(variant, sound)
def dance(): def dance_processing(): while not self.is_stopped: elapsed_time = time() - start_time # if elapsed_time > length: # break # if elapsed_time > beats[current_beat]: # current_beat += 1 # if current_beat >= len(beats): # break # state = 'min' # self.app.timmy_device.set_servo_rotation(servo_id, min_rotation) # elif current_beat > 0 and elapsed_time > (beats[current_beat - 1] + beats[current_beat]) / 2 and state == 'min': # state = 'max' # self.app.timmy_device.set_servo_rotation(servo_id, max_rotation) if elapsed_time >= total_duration: break t = int(elapsed_time / total_duration * length) s = self.sound_data.signal[t] * scale rotation = int((max_rotation + min_rotation) / 2 + (max_rotation - min_rotation) / 2 * s / 32768) timmy_device.set_servo_rotation(servo_id, rotation) if not timmy_device.is_active(): return False servo_id = 2 # !!! gui scale = 4 # ??? calc servo_settings = settings.get_servo(servo_id) min_rotation = servo_settings.min_rotation max_rotation = servo_settings.max_rotation timmy_device.set_servo_rotation(servo_id, max_rotation) state = 'max' current_beat = 0 start_time = time() length = len(self.sound_data.signal) #!!!: already calc total_duration = float(length) / self.sound_data.rate dance_thread = Thread(target=dance_processing) dance_thread.start()
def start(self): def go_into_dance_pose(): for i in range(settings.servo_count): servo_settings = settings.get_servo(i) default_init_rotation = (servo_settings.max_rotation + servo_settings.min_rotation) / 2 if i == 1: timmy_device.set_servo_rotation(i, 14) # magic const else: timmy_device.set_servo_rotation(i, default_init_rotation) sleep(1) def dance(): def dance_processing(): while not self.is_stopped: elapsed_time = time() - start_time # if elapsed_time > length: # break # if elapsed_time > beats[current_beat]: # current_beat += 1 # if current_beat >= len(beats): # break # state = 'min' # self.app.timmy_device.set_servo_rotation(servo_id, min_rotation) # elif current_beat > 0 and elapsed_time > (beats[current_beat - 1] + beats[current_beat]) / 2 and state == 'min': # state = 'max' # self.app.timmy_device.set_servo_rotation(servo_id, max_rotation) if elapsed_time >= total_duration: break t = int(elapsed_time / total_duration * length) s = self.sound_data.signal[t] * scale rotation = int((max_rotation + min_rotation) / 2 + (max_rotation - min_rotation) / 2 * s / 32768) timmy_device.set_servo_rotation(servo_id, rotation) if not timmy_device.is_active(): return False servo_id = 2 # !!! gui scale = 4 # ??? calc servo_settings = settings.get_servo(servo_id) min_rotation = servo_settings.min_rotation max_rotation = servo_settings.max_rotation timmy_device.set_servo_rotation(servo_id, max_rotation) state = 'max' current_beat = 0 start_time = time() length = len(self.sound_data.signal) #!!!: already calc total_duration = float(length) / self.sound_data.rate dance_thread = Thread(target=dance_processing) dance_thread.start() #ToDO call stop() if not self.is_stopped: return False if not timmy_device.is_active(): return False self.is_stopped = False go_into_dance_pose() if self.on_device_ready: self.on_device_ready() dance() return True
def on_show(self): fill_sliders_grid(self.ids['sliders_grid'], self.on_slider_value_changed) self.ids['device_info'].text = "Timmy device %s active" % ("is" if timmy_device.is_active() else "isn't",) Clock.schedule_interval(self.update_input, 0.05)
def on_slider_value_changed(self, slider_index, value): if not timmy_device.is_active(): return timmy_device.set_servo_rotation(slider_index, int(value))