You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*
* Copyright (c) 2016-2019 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/
/*
Nordic UART Service Server
The Nordic UART Service is a simple GATT-based service with TX and RX characteristics.
Strings written to the UART Service server are echoed back to the client as characteristic notifications.
https://devzone.nordicsemi.com/f/nordic-q-a/10567/what-is-nus-nordic-uart-service
*/
import BLEServer from "bleserver"; // import the bleserver module
import {uuid} from "btutils"; // import the uuid module";
// Import the tricolor led module
import Digital from "pins/digital";
import Timer from "timer";
import NeoPixel from 'neopixel';
import Pins from 'pins/digital'; // import the pins module
const SERVICE_UUID = uuid`6E400001-B5A3-F393-E0A9-E50E24DCCA9E`; // create a uuid for the service
// create a uuid for the characteristic
const UART_SERVER = "UART Server"; // create a name for the service
// pin for the neopixel
const neopixelPin = 10;
let neopixel = new NeoPixel({length: 12, pin: neopixelPin, order: "GRB"});
const red = neopixel.makeRGB(255, 0, 0);
const yellow = neopixel.makeRGB(255, 255, 0);
const orange = neopixel.makeRGB(255, 165, 0);
const purple = neopixel.makeRGB(128, 0, 128);
const pink = neopixel.makeRGB(255, 192, 203);
const cyan = neopixel.makeRGB(0, 255, 255);
const magenta = neopixel.makeRGB(255, 0, 255);
const green = neopixel.makeRGB(0, 255, 0);
const blue = neopixel.makeRGB(0, 0, 255);
const white = neopixel.makeRGB(255, 255, 255);
const black = neopixel.makeRGB(0, 0, 0);
function setAll(color) {
for (let i = 0; i <= neopixel.length; i++) {
neopixel.setPixel(i, color);
}
neopixel.update({ length: neopixel.length, start: 0, end: neopixel.length, repeat: 0, delay: 0, offset: 0, count: neopixel.length });
}
function setPixel(pixel, color) {
neopixel.setPixel(pixel, color);
neopixel.update();
}
function clearAll() {
setAll(black);
}
function setAllColor(color) {
setAll(color);
}
class UARTServer extends BLEServer {
onReady() {
this.deviceName = "VEGAONE";
this.onDisconnected();
}
onConnected() {
this.stopAdvertising();
}
onDisconnected() {
delete this.tx;
this.startAdvertising({
advertisingData: {flags: 6, completeName: this.deviceName, completeUUID128List: [SERVICE_UUID]}
});
}
onCharacteristicNotifyEnabled(characteristic) {
this.tx = characteristic;
}
onCharacteristicNotifyDisabled(characteristic) {
delete this.tx;
}
onPasskeyConfirm(connection, passkey) {
trace("Confirm connection: " + connection + " Passkey: " + passkey + "\n");
}
onAuthenticationComplete(connection, result) {
trace("Authentication complete connection: " + connection + " result: " + result + "\n");
}
onCharacteristicRead(characteristic) {
trace("Read characteristic: " + characteristic + "\n");
}
onCharacteristicWritten(characteristic, value) {
const timeoutMs = 5000; // Set the timeout value in milliseconds (e.g., 5000 for 5 seconds)
let timeoutId;
const handleTimeout = () => {
// Handle the timeout event here
// You can perform any necessary actions when the write operation times out
// For example, you could log an error message or retry the operation
trace("Timeout\n");
// Example actions for a timeout:
// 1. Log an error message
trace("Error: Write operation timed out\n");
// 2. Retry the operation
// 2. Retry the operation
// Retry the write operation here
if (this.tx) {
this.notifyValue(this.tx, value);
}
// 3. Clear the timeout and handle the write operation
// Clear the timeout and handle the write operation
clearTimeoutAndHandleWrite();
};
const clearTimeoutAndHandleWrite = () => {
Timer.clear(timeoutId); // Clear the timeout if the write operation completes within the timeout duration
// Perform your existing logic for handling the write operation here
// This code block includes the existing if statements and actions based on the value
trace.left(value, UART_SERVER);
if (this.tx) {
this.notifyValue(this.tx, value);
trace("value: " + value);
}
// Example actions for a value of 2:
if (value == 1) {
clearAll();
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 2) {
trace("two: " + value);
// this.notifyValue(this.tx, value);
setAllColor(red);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 3) {
trace("three: " + value);
// this.notifyValue(this.tx, value);
setAllColor(green);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 4) {
trace("four: " + value);
// this.notifyValue(this.tx, value);
setAllColor(blue);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 5) {
trace("five: " + value);
// this.notifyValue(this.tx, value);
setAllColor(yellow);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 6) {
trace("six: " + value);
// this.notifyValue(this.tx, value);
setAllColor(orange);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 7) {
trace("seven: " + value);
// this.notifyValue(this.tx, value);
setAllColor(purple);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 8) {
trace("eight: " + value);
// this.notifyValue(this.tx, value);
setAllColor(pink);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 9) {
trace("nine: " + value);
// this.notifyValue(this.tx, value);
setAllColor(cyan);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
if (value == 10) {
trace("ten: " + value);
// this.notifyValue(this.tx, value);
setAllColor(magenta);
// Set a timeout for the write operation
timeoutId = Timer.set(() => handleTimeout(), timeoutMs);
}
};
// Set the timeout and handle the write operation
timeoutId = Timer.set(handleTimeout, timeoutMs);
clearTimeoutAndHandleWrite();
// Note: If you are using multiple characteristics, you can use a different timeout value for each characteristic
// For example, you could set a longer timeout for a characteristic that requires more time to process the data
}
}
let server = new UARTServer;
and Im running capacitor in vue with the following code in my component:
I know that the blufruit app (ios)writes to uart perfectly fine and no disconnection so there nothing wrong with moddable code used if thats useful, I have not yet tested android as i have none but will get hold of one and test, np
Describe the bug
I am running Moddable on a ESP32-S3 TFT
and Im running capacitor in vue with the following code in my component:
Im getting disconnected on write while and Im not sure why it seems fine when i use the bluefruit app on the uart service.
The errors reported are:
Expected behavior
A clear and concise description of what you expected to happen.
I don't expect the timeout and the timeout seems to happen immediately regardless of timeout option supplied
Screenshots
If applicable, add screenshots to help explain your problem.
Plugin version:
Desktop (please complete the following information):
Smartphone (please complete the following information):
iPhone 11
latest OS
The text was updated successfully, but these errors were encountered: