From dc76ad93cb9fe57977dd2e168b8a53c90fad6669 Mon Sep 17 00:00:00 2001 From: 8cH9azbsFifZ Date: Thu, 16 Nov 2023 22:32:42 +0100 Subject: [PATCH] duration string to mopp pip --- CHANGELOG.md | 1 + beep.py | 64 ---------------------------------------------------- udp2mqtt.py | 14 ++++-------- 3 files changed, 6 insertions(+), 73 deletions(-) delete mode 100644 beep.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b60ca..84d21ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +0.0.8 - move json duration string to mopp module (pip) 0.0.7 - topic for durations array 0.0.6 - topic changed 0.0.5 - name of chatserver for connection diff --git a/beep.py b/beep.py deleted file mode 100644 index 2ecfc37..0000000 --- a/beep.py +++ /dev/null @@ -1,64 +0,0 @@ -#import pygame, numpy, pygame.sndarray -import json - -class Beep: - def __init__(self, speed = 20): - - # Ref timings: https://morsecode.world/international/timing.html#:~:text=It's%20clear%20that%20this%20makes,%22)%20which%20also%20makes%20sense. - speed_wpm = speed - self.dit_duration = int(60 / (50*speed_wpm)*1000) - self.dah_duration = 3*self.dit_duration - self.eoc_duration = 3*self.dit_duration - self.eow_duration = 7*self.dit_duration - - # Ref for sound: https://gist.github.com/nekozing/5774628 - sampleRate = 44100 - # 44.1kHz, 16-bit signed, mono - #pygame.mixer.pre_init(sampleRate, -16, 1) - #pygame.init() - # 4096 : the peak ; volume ; loudness - # 440 : the frequency in hz - # ?not so sure? if astype int16 not specified sound will get very noisy, because we have defined it as 16 bit mixer at mixer.pre_init() - # ( probably due to int overflow resulting in non continuous sound data) - #arr = numpy.array([4096 * numpy.sin(2.0 * numpy.pi * 440 * x / sampleRate) for x in range(0, sampleRate)]).astype(numpy.int16) - #self.sound = pygame.sndarray.make_sound(arr) - - # def _beep(self, symbol): - # if symbol == ".": - # self.sound.play(-1) - # pygame.time.delay(self.dit_duration) - # self.sound.stop() - # pygame.time.delay(self.dit_duration) - # elif symbol == "-": - # self.sound.play(-1) - # pygame.time.delay(self.dah_duration) - # self.sound.stop() - # pygame.time.delay(self.dit_duration) - # elif symbol == "C": # EOC - # pygame.time.delay(self.eoc_duration) - # elif symbol == "W": # EOW - # pygame.time.delay(self.eow_duration) - - def beep_message(self, message): - for s in message: - self._beep (s) - - # FIXME -> put to a separate class (or lib) - def return_duration_json(self, message): - json_string = '{"durations": []}' - data = json.loads(json_string) - - for symbol in message: - if symbol == ".": - data['durations'].append(self.dit_duration) - data['durations'].append(-self.dit_duration) - elif symbol == "-": - data['durations'].append(self.dah_duration) - data['durations'].append(-self.dit_duration) - elif symbol == "C": # EOC - data['durations'].append(-self.eoc_duration) - elif symbol == "W": # EOW - data['durations'].append(-self.eow_duration) - - updated_json_string = json.dumps(data, indent=2) - return updated_json_string \ No newline at end of file diff --git a/udp2mqtt.py b/udp2mqtt.py index 1cd2b4f..4d21f5c 100644 --- a/udp2mqtt.py +++ b/udp2mqtt.py @@ -7,7 +7,6 @@ import socket import time import sys -from beep import * logging.basicConfig(level=logging.DEBUG, format='%(message)s', ) @@ -66,7 +65,7 @@ def on_log(mqttc, obj, level, string): # Main loop while KeyboardInterrupt: - time.sleep(0.2) # anti flood + time.sleep(0.2) # anti flood # FIXME: ADD KEEPALIVE SIGNAL @@ -77,20 +76,17 @@ def on_log(mqttc, obj, level, string): print (r) sys.stdout.flush() # TODO: use logging - # Beep if message received + # If message received send the duration json over mqtt if not "Keepalive" in r: - b = Beep(speed=r["Speed"]) + b = Mopp(speed=r["Speed"]) if not last_r == r: - #b.beep_message(r["Message"]) last_r = r - # decode the message - print (r["Speed"]) - print (r["Message"]) + # decode the mopp message to a json string containing the durations mydecoded = b.return_duration_json(r["Message"]) # And send mqtt - #infot = mqttc.publish(config.TOPIC, data_bytes, qos=2) + #infot = mqttc.publish(config.TOPIC, data_bytes, qos=2) // publish raw mopp data to another topic? infot = mqttc.publish(config.TOPICDURATIONS, mydecoded, qos=2) infot.wait_for_publish()