Apr-09-2020, 08:11 AM
Dear Forum users,
for our museum i`m building a simpel morse decoder program. it is working with a very simple script that i changed a little bit.
Stil i have a little issue that i can`t solve
First here part the code:
for our museum i`m building a simpel morse decoder program. it is working with a very simple script that i changed a little bit.
Stil i have a little issue that i can`t solve
First here part the code:
#!/usr/bin/python3 import pygame import time import gpiozero as gpio import _thread as thread from array import array from pygame.locals import * from morse_lookup import * import os os.system("clear") # Linux - OSX pygame.mixer.pre_init(44100, -16, 1, 1024) pygame.init() class ToneSound(pygame.mixer.Sound): def __init__(self, frequency, volume): self.frequency = frequency pygame.mixer.Sound.__init__(self, self.build_samples()) self.set_volume(volume) def build_samples(self): period = int(round(pygame.mixer.get_init()[0] / self.frequency)) samples = array("h", [0] * period) amplitude = 2 ** (abs(pygame.mixer.get_init()[1]) - 1) - 1 for time in range(period): if time < period / 2: samples[time] = amplitude else: samples[time] = -amplitude return samples def decoder_thread(): global key_up_time global buffer new_word = False while True: time.sleep(.01) key_up_length = time.time() - key_up_time if len(buffer) > 0 and key_up_length >= 1.5: new_word = True bit_string = "".join(buffer) try_decode(bit_string) del buffer[:] elif new_word and key_up_length >= 4.5: new_word = False sys.stdout.write(" ") sys.stdout.flush() tone_obj = ToneSound(frequency = 800, volume = .5) pin = 4 key = gpio.Button(pin, pull_up=True) DOT = "." DASH = "-" key_down_time = 0 key_down_length = 0 key_up_time = 0 buffer = [] thread.start_new_thread(decoder_thread, ()) print("Welkom bij de morse emulator van maritiem centrum Ameland") while True: key.wait_for_press() key_down_time = time.time() #record the time when the key went down tone_obj.play(-1) #the -1 means to loop the sound key.wait_for_release() key_up_time = time.time() #record the time when the key was released key_down_length = key_up_time - key_down_time #get the length of time it was held down for tone_obj.stop() buffer.append(DASH if key_down_length > 0.20 else DOT)I want to restart or reset the program when there is a idle time of 2 minutes, so when a new gast arrives the screen is blank. can anybody help me with that?