Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ It can also be used in NodeJS to connect to MeshCore Companion devices over TCP/
npm install @liamcottle/meshcore.js
```

## Simple Example
## Simple Examples

```
### Enumerate Contacts (Node.js)

```javascript
import { TCPConnection, NodeJSSerialConnection } from "@liamcottle/meshcore.js";

// serial connections are supported by "companion_radio_usb" firmware
Expand Down Expand Up @@ -53,6 +55,34 @@ connection.on("connected", async () => {
await connection.connect();
```

### Enumerate Contacts (Browser)

```html
<button id="connect-serial">Connect via Serial</button>

<script type="module">
import { WebBleConnection, WebSerialConnection } from "@liamcottle/meshcore.js";

// wait until connected
async function onConnected(connection) {

// we are now connected
console.log("connected!");

// log contacts
const contacts = await connection.getContacts();
for (const contact of contacts) {
console.log(`Contact: ${contact.advName}`);
}
}

document.getElementById("connect-serial").addEventListener("click", async () => {
const connection = await WebSerialConnection.open();
connection.on("connected", () => onConnected(connection));
});
</script>
```

## Examples

There's a few other examples scripts in the [examples](./examples) folder.
Expand Down
12 changes: 7 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@
</div>

<script type="module">
import Constants from "./src/constants.js";
import WebSerialConnection from "./src/connection/web_serial_connection.js";
import WebBleConnection from "./src/connection/web_ble_connection.js";
import BufferUtils from "./src/buffer_utils.js";
import {
Constants,
WebSerialConnection,
WebBleConnection,
BufferUtils,
} from "./src/browser.js";
Vue.createApp({
data() {
return {
Expand Down Expand Up @@ -696,4 +698,4 @@
</script>

</body>
</html>
</html>
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
"description": "",
"main": "src/index.js",
"type": "module",
"exports": {
".": {
"browser": "./src/browser.js",
"import": "./src/index.js",
"default": "./src/index.js"
},
"./browser": "./src/browser.js",
"./node": "./src/node.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
20 changes: 20 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Browser-only entrypoint – does NOT import Node-only transports (TCP, NodeJSSerial)
import Connection from "./connection/connection.js";
import WebBleConnection from "./connection/web_ble_connection.js";
import WebSerialConnection from "./connection/web_serial_connection.js";
import Constants from "./constants.js";
import Advert from "./advert.js";
import Packet from "./packet.js";
import BufferUtils from "./buffer_utils.js";
import CayenneLpp from "./cayenne_lpp.js";

export {
Connection,
WebBleConnection,
WebSerialConnection,
Constants,
Advert,
Packet,
BufferUtils,
CayenneLpp,
};
2 changes: 2 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Node-only entrypoint – re-exports everything including Node transports
export * from "./index.js";