-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tidalcycles:main' into main
- Loading branch information
Showing
8 changed files
with
234 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @strudel/serial | ||
|
||
This package adds webserial functionality to strudel Patterns, for e.g. sending messages to arduino microcontrollers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
mqtt.mjs - for patterning the internet of things from strudel | ||
Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/strudel/blob/main/packages/serial/serial.mjs> | ||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Pattern, isPattern } from '@strudel/core'; | ||
import Paho from 'paho-mqtt'; | ||
|
||
const connections = {}; | ||
|
||
// Handle connection loss | ||
function onConnectionLost(responseObject) { | ||
if (responseObject.errorCode !== 0) { | ||
console.error(' mqtt connection lost: ', responseObject.errorMessage); | ||
} | ||
} | ||
|
||
// Handle received messages | ||
function onMessageArrived(message) { | ||
console.log('incoming mqtt message: ', message.payloadString); // prettier-ignore | ||
} | ||
|
||
function onFailure(err) { | ||
console.error('Connection failed: ', err); | ||
} | ||
|
||
Pattern.prototype.mqtt = function ( | ||
username = undefined, | ||
password = undefined, | ||
topic = undefined, | ||
host = 'wss://localhost:8883/', | ||
client = undefined, | ||
latency = 0, | ||
) { | ||
const key = host + '-' + client; | ||
let connected = false; | ||
if (!client) { | ||
client = 'strudel-' + String(Math.floor(Math.random() * 1000000)); | ||
} | ||
function onConnect() { | ||
console.log('Connected to mqtt broker'); | ||
connected = true; | ||
} | ||
|
||
let cx; | ||
if (connections[key]) { | ||
cx = connections[key]; | ||
} else { | ||
cx = new Paho.Client(host, client); | ||
cx.onConnectionLost = onConnectionLost; | ||
cx.onMessageArrived = onMessageArrived; | ||
const props = { | ||
onSuccess: onConnect, | ||
onFailure: onFailure, | ||
useSSL: true, | ||
}; | ||
|
||
if (username) { | ||
props.userName = username; | ||
props.password = password; | ||
} | ||
cx.connect(props); | ||
} | ||
return this.withHap((hap) => { | ||
const onTrigger = (t_deprecate, hap, currentTime, cps, targetTime) => { | ||
if (!connected) { | ||
return; | ||
} | ||
let message = ''; | ||
if (typeof hap.value === 'object') { | ||
message = JSON.stringify(hap.value); | ||
} else { | ||
message = hap.value; | ||
} | ||
message = new Paho.Message(message); | ||
message.destinationName = topic; | ||
|
||
const offset = (targetTime - currentTime + latency) * 1000; | ||
|
||
window.setTimeout(function () { | ||
cx.send(message); | ||
}, offset); | ||
}; | ||
return hap.setContext({ ...hap.context, onTrigger, dominantTrigger: true }); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@strudel/mqtt", | ||
"version": "1.1.0", | ||
"description": "MQTT API for strudel", | ||
"main": "mqtt.mjs", | ||
"type": "module", | ||
"publishConfig": { | ||
"main": "dist/index.mjs" | ||
}, | ||
"scripts": { | ||
"build": "vite build", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/tidalcycles/strudel.git" | ||
}, | ||
"keywords": [ | ||
"titdalcycles", | ||
"strudel", | ||
"pattern", | ||
"livecoding", | ||
"algorave" | ||
], | ||
"author": "Alex McLean <[email protected]>", | ||
"license": "AGPL-3.0-or-later", | ||
"bugs": { | ||
"url": "https://github.com/tidalcycles/strudel/issues" | ||
}, | ||
"homepage": "https://github.com/tidalcycles/strudel#readme", | ||
"dependencies": { | ||
"@strudel/core": "workspace:*", | ||
"paho-mqtt": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"vite": "^5.0.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { defineConfig } from 'vite'; | ||
import { dependencies } from './package.json'; | ||
import { resolve } from 'path'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [], | ||
build: { | ||
lib: { | ||
entry: resolve(__dirname, 'mqtt.mjs'), | ||
formats: ['es'], | ||
fileName: (ext) => ({ es: 'index.mjs' })[ext], | ||
}, | ||
rollupOptions: { | ||
external: [...Object.keys(dependencies)], | ||
}, | ||
target: 'esnext', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters