Skip to content

Commit

Permalink
Add Serial.isConnected to check if Bluetooth/USB/etc actually connected
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Nov 15, 2024
1 parent 9ea61f0 commit aec1e1e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
Fix UtilTimer timings when new task added infront of existing tasks (fix #2575)
Graphics: Fix issue where drawLine for 2px horizontal lines only drew a 1px dot
nRF52: Add E.setComparator to enable interrupts from LPCOMP
Add Serial.isConnected to check if Bluetooth/USB/etc actually connected

2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()'
Bangle.js2: After Bangle.showTestScreen, put Bangle.js into a hard off state (not soft off)
Expand Down
9 changes: 7 additions & 2 deletions libs/network/telnet/jswrap_telnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef struct {
uint16_t txBufLen; ///< number of chars in tx buffer
} TelnetServer;

/// Set if Telnet overflow
static bool ovf;
static TelnetServer tnSrv; ///< the telnet server, only one right now
static uint8_t tnSrvMode; ///< current mode for the telnet server

Expand Down Expand Up @@ -174,6 +176,11 @@ bool jswrap_telnet_idle(void) {
return active;
}

/* Is something connected to Telnet? */
bool jswrap_telnet_isConnected() {
return tnSrv.cliSock>0;
}

//===== Internal functions

// Start the listening socket for the telnet console server.
Expand Down Expand Up @@ -258,8 +265,6 @@ bool telnetSendBuf(JsNetwork *net) {
return sent != 0;
}

static bool ovf;

void telnetSendChar(char ch) {
if (tnSrv.sock == 0 || tnSrv.cliSock == 0) return;
if (tnSrv.txBufLen >= TX_CHUNK) {
Expand Down
4 changes: 4 additions & 0 deletions libs/network/telnet/jswrap_telnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ void jswrap_telnet_kill(void);
// was done.
bool jswrap_telnet_idle(void);

/* Is something connected to Telnet? */
bool jswrap_telnet_isConnected();


#endif
39 changes: 39 additions & 0 deletions src/jswrap_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
#include "jsdevices.h"
#include "jsinteractive.h"
#include "jsserial.h"
#ifdef USE_TELNET
#include "jswrap_telnet.h"
#endif
#ifdef BLUETOOTH
#include "jswrap_bluetooth.h"
#endif

/*JSON{
"type" : "class",
Expand Down Expand Up @@ -556,4 +562,37 @@ void jswrap_serial_flush(JsVar *parent) {
IOEventFlags device = jsiGetDeviceFromClass(parent);
if (device == EV_NONE) return;
jshTransmitFlushDevice(device);
}

/*JSON{
"type" : "method",
"class" : "Serial",
"name" : "isConnected",
"generate" : "jswrap_serial_isConnected",
"return" : ["bool","`true` if connected/initialised, false otherwise"]
}
(Added 2v25) Is the given Serial device connected?
* USB/Bluetooth/Telnet/etc: Is this connected?
* Serial1/etc: Has the device been initialised?
* LoopbackA/LoopbackB/Terminal: always return true
*/
bool jswrap_serial_isConnected(JsVar *parent) {
IOEventFlags device = jsiGetDeviceFromClass(parent);
if (device==EV_LOOPBACKA || device==EV_LOOPBACKB) return true;
#ifdef USE_TERMINAL
if (device == EV_TERMINAL) return true;
#endif
#ifdef USE_TELNET
if (device == EV_TELNET) return jswrap_telnet_isConnected();
#endif
#ifdef USB
if (device == EV_USBSERIAL) return jshIsUSBSERIALConnected();
#endif
#ifdef BLUETOOTH
if (device == EV_BLUETOOTH) return jsble_has_peripheral_connection();
#endif
if (DEVICE_IS_USART(device))
return jshIsDeviceInitialised(device);
return false;
}
1 change: 1 addition & 0 deletions src/jswrap_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ void jswrap_serial_println(JsVar *parent, JsVar *str);
void jswrap_serial_write(JsVar *parent, JsVar *data);
void jswrap_serial_inject(JsVar *parent, JsVar *args);
void jswrap_serial_flush(JsVar *parent);
bool jswrap_serial_isConnected(JsVar *parent);

#endif // JSWRAP_SERIAL_H_

0 comments on commit aec1e1e

Please sign in to comment.