From 945fcbe3e6bd7a63a55ec6c1c66bc328425a2180 Mon Sep 17 00:00:00 2001 From: Luke Franzke Date: Fri, 7 Jun 2024 14:02:41 +0200 Subject: [PATCH 1/2] fixed issue with time out on mqtt connection --- src/tuioProcessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tuioProcessor.py b/src/tuioProcessor.py index 023e80a..d520dc2 100644 --- a/src/tuioProcessor.py +++ b/src/tuioProcessor.py @@ -207,7 +207,7 @@ async def init_mqtt_broadcast(): mqtt_password = utils.get_key('mqtt_password') mqttc.username_pw_set(mqtt_username, mqtt_password) mqttc.on_connect = on_connect - mqttc.connect(mqttbroker_address, mqttbroker_port, 60) + mqttc.connect_async(mqttbroker_address, mqttbroker_port, 60) mqttc.loop_start() print("Ready to broadcast MQTT messages") From 87a199d5bcf6cef41c43dd07350dd7b01a3a6da5 Mon Sep 17 00:00:00 2001 From: Luke Franzke Date: Fri, 7 Jun 2024 15:44:48 +0200 Subject: [PATCH 2/2] added processing example --- .../ProcessingExample/ProcessingExample.pde | 128 ++++++++++++++++++ src/tuioProcessor.py | 2 +- 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 examples/ProcessingExample/ProcessingExample.pde diff --git a/examples/ProcessingExample/ProcessingExample.pde b/examples/ProcessingExample/ProcessingExample.pde new file mode 100644 index 0000000..1c41975 --- /dev/null +++ b/examples/ProcessingExample/ProcessingExample.pde @@ -0,0 +1,128 @@ +import oscP5.*; +import netP5.*; + +OscP5 oscP5; +ArrayList tuioTags = new ArrayList(); +ArrayList toAdd = new ArrayList(); + +void setup() { + size(800, 600); + oscP5 = new OscP5(this, 3334); // Listen on port 3333 +} + +void draw() { + background(0); + for (TuioTag tag : tuioTags) { + tag.display(); + } + tuioTags.addAll(toAdd); + toAdd.removeAll(toAdd); +} + +void oscEvent(OscMessage theOscMessage) { + + try { + // print(" addrpattern: "+theOscMessage.get(0)); + String jsonString = theOscMessage.get(0).stringValue(); + JSONObject json = JSONObject.parse(jsonString); + int tagId = json.getInt("tagid"); + String change = json.getString("change"); + + if (change.equals("moved") || change.equals("added")) { + float xpos = json.getFloat("xpos"); + float ypos = json.getFloat("ypos"); + float angle = json.getFloat("ang"); + float xvel = json.getFloat("xvel"); + float yvel = json.getFloat("yvel"); + float angvel = json.getFloat("angvel"); + TuioTag tag = findTagById(tagId); + + if (tag == null) { + tag = new TuioTag(tagId, xpos, ypos, angle); + toAdd.add(tag); + } else { + tag.setVisible(true); + tag.update(xpos, ypos, angle); + } + } else if (change.equals("disappeared")) { + TuioTag tag = findTagById(tagId); + if (tag != null) { + tag.setVisible(false); + } + } + } + catch (Exception e) { + println("Error parsing JSON: " + e.getMessage()); + } +} + + +TuioTag findTagById(int id) { + for (TuioTag tag : tuioTags) { + if (tag.id == id) { + return tag; + } + } + return null; +} + +class TuioTag { + int id; + float x, y, angle, xAverage, yAverage, angleAverage, displayAngle; + boolean visible; + + TuioTag(int id, float x, float y, float angle) { + this.id = id; + this.x = x * width; + this.y = y * height; + this.angle = angle; + this.xAverage = this.x; + this.yAverage = this.y; + this.angleAverage = this.angle; + this.displayAngle = this.angle; + this.visible = true; + } + + void update(float xpos, float ypos, float newAngle) { + this.x = xpos * width; + this.y = ypos * height; + this.angle = newAngle; + } + + void setVisible(boolean set) { + this.visible = set; + } + void display() { + if (this.visible) { + // smooth out position and angle + float factor = 0.95f; + this.xAverage = this.xAverage*factor; + this.xAverage += this.x*(1.0f-factor); + + this.yAverage = this.yAverage*factor; + this.yAverage += this.y*(1.0f-factor); + + + float delta = (TWO_PI - (this.angleAverage-this.angle))% TWO_PI; + + if (delta > PI) { + delta = (TWO_PI-delta); + this.angleAverage = this.angleAverage - delta; + } else { + delta = (TWO_PI+delta); + this.angleAverage = this.angleAverage + delta; + } + + pushMatrix(); + translate(this.xAverage, this.yAverage); + rotate(this.angleAverage); + noStroke(); + fill(255, 255, 0); + ellipse(0, 0, 40, 40); + fill(0); + textAlign(CENTER, CENTER); + text(id, 0, 0); + popMatrix(); + } + } +} diff --git a/src/tuioProcessor.py b/src/tuioProcessor.py index d520dc2..1c2d48d 100644 --- a/src/tuioProcessor.py +++ b/src/tuioProcessor.py @@ -46,7 +46,7 @@ def broadcast_data(address, data): oscclient.send_message(address, message) if mqttc: #mqttc.publish("test", message) - print(data) + #print(data) if "ang" in data and data['tagid'] in tags_homeassistant_config and tags_homeassistant_config[data['tagid']] == "number": ang = round(utils.radians_to_number(data['ang'])) print(ang)