@hangtime/grip-connect 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,10 +60,14 @@ motherboardButton.addEventListener("click", () => {
60
60
  console.log(data)
61
61
  })
62
62
 
63
- // Check if device is being used
64
- active((value) => {
65
- console.log(value)
66
- })
63
+ // Reactive check if device is active
64
+ active(
65
+ (isActive) => {
66
+ console.log(isActive)
67
+ },
68
+ // Optionally using a weight threshold and duration
69
+ { threshold: 2.5, duration: 1000 },
70
+ )
67
71
 
68
72
  // Read info: battery + firmware
69
73
  const batteryLevel = await battery(Motherboard)
package/dist/battery.d.ts CHANGED
@@ -6,7 +6,5 @@ import type { Device } from "./types/devices";
6
6
  *
7
7
  * @param {Device} board - The device from which to retrieve battery information.
8
8
  * @returns {Promise<string | undefined>} A Promise that resolves with the battery or voltage information,
9
- * or rejects with an error if the device is not connected.
10
- * @throws {Error} Throws an error if the device is not connected.
11
9
  */
12
10
  export declare const battery: (board: Device) => Promise<string | undefined>;
package/dist/battery.js CHANGED
@@ -10,8 +10,6 @@ import { ProgressorCommands } from "./commands";
10
10
  *
11
11
  * @param {Device} board - The device from which to retrieve battery information.
12
12
  * @returns {Promise<string | undefined>} A Promise that resolves with the battery or voltage information,
13
- * or rejects with an error if the device is not connected.
14
- * @throws {Error} Throws an error if the device is not connected.
15
13
  */
16
14
  export const battery = async (board) => {
17
15
  // Check if the device is connected
@@ -30,8 +28,7 @@ export const battery = async (board) => {
30
28
  });
31
29
  return response;
32
30
  }
33
- // If device is not found, return undefined
34
- return;
35
31
  }
36
- throw new Error("Not connected.");
32
+ // If device is not found, return undefined
33
+ return undefined;
37
34
  };
@@ -17,4 +17,5 @@ export const getCharacteristic = (board, serviceId, characteristicId) => {
17
17
  }
18
18
  }
19
19
  // Return undefined if the service or characteristic is not found
20
+ return undefined;
20
21
  };
@@ -1,6 +1,8 @@
1
1
  import type { Device } from "./types/devices";
2
2
  /**
3
- * Disconnects the device if it is connected.
4
- * @param {Device} board - The device to disconnect.
3
+ * Disconnects the device if it is currently connected.
4
+ * - Checks if the device is connected via its GATT server.
5
+ * - If the device is connected, it attempts to gracefully disconnect.
6
+ * @param {Device} board - The device to be disconnected. The device must have a `gatt` property accessible through `board.device`.
5
7
  */
6
8
  export declare const disconnect: (board: Device) => void;
@@ -1,12 +1,14 @@
1
1
  import { isConnected } from "./is-connected";
2
2
  /**
3
- * Disconnects the device if it is connected.
4
- * @param {Device} board - The device to disconnect.
3
+ * Disconnects the device if it is currently connected.
4
+ * - Checks if the device is connected via its GATT server.
5
+ * - If the device is connected, it attempts to gracefully disconnect.
6
+ * @param {Device} board - The device to be disconnected. The device must have a `gatt` property accessible through `board.device`.
5
7
  */
6
8
  export const disconnect = (board) => {
7
- // Check if the device is connected
9
+ // Verify that the device is connected using the provided helper function
8
10
  if (isConnected(board)) {
9
- // Disconnect the device using optional chaining
11
+ // Safely attempt to disconnect the device's GATT server, if available
10
12
  board.device?.gatt?.disconnect();
11
13
  }
12
14
  };
@@ -6,7 +6,5 @@ import type { Device } from "./types/devices";
6
6
  *
7
7
  * @param {Device} board - The device from which to retrieve firmware version.
8
8
  * @returns {Promise<string>} A Promise that resolves with the firmware version,
9
- * or rejects with an error if the device is not connected.
10
- * @throws {Error} Throws an error if the device is not connected.
11
9
  */
12
10
  export declare const firmware: (board: Device) => Promise<string | undefined>;
package/dist/firmware.js CHANGED
@@ -10,8 +10,6 @@ import { isMotherboard, isProgressor } from "./is-device";
10
10
  *
11
11
  * @param {Device} board - The device from which to retrieve firmware version.
12
12
  * @returns {Promise<string>} A Promise that resolves with the firmware version,
13
- * or rejects with an error if the device is not connected.
14
- * @throws {Error} Throws an error if the device is not connected.
15
13
  */
16
14
  export const firmware = async (board) => {
17
15
  // Check if the device is connected
@@ -30,8 +28,7 @@ export const firmware = async (board) => {
30
28
  });
31
29
  return response;
32
30
  }
33
- // If device is not found, return undefined
34
- return;
35
31
  }
36
- throw new Error("Not connected.");
32
+ // If device is not found, return undefined
33
+ return undefined;
37
34
  };
@@ -5,7 +5,5 @@ import type { Device } from "./types/devices";
5
5
  *
6
6
  * @param {Device} board - The device from which to retrieve hardware version.
7
7
  * @returns {Promise<string>} A Promise that resolves with the hardware version,
8
- * or rejects with an error if the device is not connected.
9
- * @throws {Error} Throws an error if the device is not connected.
10
8
  */
11
9
  export declare const hardware: (board: Device) => Promise<string | undefined>;
package/dist/hardware.js CHANGED
@@ -7,8 +7,6 @@ import { isMotherboard } from "./is-device";
7
7
  *
8
8
  * @param {Device} board - The device from which to retrieve hardware version.
9
9
  * @returns {Promise<string>} A Promise that resolves with the hardware version,
10
- * or rejects with an error if the device is not connected.
11
- * @throws {Error} Throws an error if the device is not connected.
12
10
  */
13
11
  export const hardware = async (board) => {
14
12
  // Check if the device is connected
@@ -18,8 +16,7 @@ export const hardware = async (board) => {
18
16
  // Read hardware version from the Motherboard
19
17
  return await read(board, "device", "hardware", 250);
20
18
  }
21
- // If device is not found, return undefined
22
- return;
23
19
  }
24
- throw new Error("Not connected.");
20
+ // If device is not found, return undefined
21
+ return;
25
22
  };
@@ -9,16 +9,24 @@ type IsActiveCallback = (value: boolean) => void;
9
9
  */
10
10
  export declare let isActive: boolean;
11
11
  /**
12
- * Sets the callback function to be called when the activity status changes.
12
+ * Sets the callback function to be called when the activity status changes,
13
+ * and optionally sets the configuration for threshold and duration.
13
14
  *
14
15
  * This function allows you to specify a callback that will be invoked whenever
15
16
  * the activity status changes, indicating whether the device is currently active.
17
+ * It also allows optionally configuring the threshold and duration used to determine activity.
16
18
  *
17
19
  * @param {IsActiveCallback} callback - The callback function to be set. This function
18
20
  * receives a boolean value indicating the new activity status.
21
+ * @param {object} [options] - Optional configuration object containing the threshold and duration.
22
+ * @param {number} [options.threshold=2.5] - The threshold value for determining activity.
23
+ * @param {number} [options.duration=1000] - The duration (in milliseconds) to monitor the input for activity.
19
24
  * @returns {void}
20
25
  */
21
- export declare const active: (callback: IsActiveCallback) => void;
26
+ export declare const active: (callback: IsActiveCallback, options?: {
27
+ threshold?: number;
28
+ duration?: number;
29
+ }) => void;
22
30
  /**
23
31
  * Checks if a dynamic value is active based on a threshold and duration.
24
32
  *
@@ -27,11 +35,7 @@ export declare const active: (callback: IsActiveCallback) => void;
27
35
  * the previous state, the callback function is called with the updated activity status.
28
36
  *
29
37
  * @param {number} input - The dynamic value to check for activity status.
30
- * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
31
- * Defaults to 2.5 if not provided.
32
- * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
33
- * Defaults to 1000 milliseconds if not provided.
34
38
  * @returns {Promise<void>} A promise that resolves once the activity check is complete.
35
39
  */
36
- export declare const checkActivity: (input: number, threshold?: number, duration?: number) => Promise<void>;
40
+ export declare const checkActivity: (input: number) => Promise<void>;
37
41
  export {};
package/dist/is-active.js CHANGED
@@ -5,17 +5,31 @@ let activeCallback;
5
5
  */
6
6
  export let isActive = false;
7
7
  /**
8
- * Sets the callback function to be called when the activity status changes.
8
+ * Configuration for threshold and duration.
9
+ */
10
+ let config = { threshold: 2.5, duration: 1000 };
11
+ /**
12
+ * Sets the callback function to be called when the activity status changes,
13
+ * and optionally sets the configuration for threshold and duration.
9
14
  *
10
15
  * This function allows you to specify a callback that will be invoked whenever
11
16
  * the activity status changes, indicating whether the device is currently active.
17
+ * It also allows optionally configuring the threshold and duration used to determine activity.
12
18
  *
13
19
  * @param {IsActiveCallback} callback - The callback function to be set. This function
14
20
  * receives a boolean value indicating the new activity status.
21
+ * @param {object} [options] - Optional configuration object containing the threshold and duration.
22
+ * @param {number} [options.threshold=2.5] - The threshold value for determining activity.
23
+ * @param {number} [options.duration=1000] - The duration (in milliseconds) to monitor the input for activity.
15
24
  * @returns {void}
16
25
  */
17
- export const active = (callback) => {
26
+ export const active = (callback, options) => {
18
27
  activeCallback = callback;
28
+ // Update the config values only if provided, otherwise use defaults
29
+ config = {
30
+ threshold: options?.threshold ?? config.threshold, // Use new threshold if provided, else use default
31
+ duration: options?.duration ?? config.duration, // Use new duration if provided, else use default
32
+ };
19
33
  };
20
34
  /**
21
35
  * Checks if a dynamic value is active based on a threshold and duration.
@@ -25,18 +39,14 @@ export const active = (callback) => {
25
39
  * the previous state, the callback function is called with the updated activity status.
26
40
  *
27
41
  * @param {number} input - The dynamic value to check for activity status.
28
- * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
29
- * Defaults to 2.5 if not provided.
30
- * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
31
- * Defaults to 1000 milliseconds if not provided.
32
42
  * @returns {Promise<void>} A promise that resolves once the activity check is complete.
33
43
  */
34
- export const checkActivity = (input, threshold = 2.5, duration = 1000) => {
44
+ export const checkActivity = (input) => {
35
45
  return new Promise((resolve) => {
36
46
  // Check the activity status after the specified duration
37
47
  setTimeout(() => {
38
- // Determine the activity status based on the threshold
39
- const activeNow = input > threshold;
48
+ // Determine the activity status based on the stored threshold in the config
49
+ const activeNow = input > config.threshold;
40
50
  if (isActive !== activeNow) {
41
51
  isActive = activeNow;
42
52
  if (activeCallback) {
@@ -44,6 +54,6 @@ export const checkActivity = (input, threshold = 2.5, duration = 1000) => {
44
54
  }
45
55
  }
46
56
  resolve();
47
- }, duration);
57
+ }, config.duration);
48
58
  });
49
59
  };
package/dist/led.js CHANGED
@@ -190,7 +190,6 @@ export const led = async (board, config) => {
190
190
  const [redValue, greenValue] = colorMapping[color];
191
191
  await write(board, "led", "red", new Uint8Array(redValue));
192
192
  await write(board, "led", "green", new Uint8Array(greenValue), 1250);
193
- return;
194
193
  }
195
- return;
194
+ return undefined;
196
195
  };
@@ -5,7 +5,5 @@ import type { Device } from "./types/devices";
5
5
  *
6
6
  * @param {Device} board - The device from which to retrieve manufacturer information.
7
7
  * @returns {Promise<string>} A Promise that resolves with the manufacturer information,
8
- * or rejects with an error if the device is not connected.
9
- * @throws {Error} Throws an error if the device is not connected.
10
8
  */
11
9
  export declare const manufacturer: (board: Device) => Promise<string | undefined>;
@@ -7,8 +7,6 @@ import { isMotherboard } from "./is-device";
7
7
  *
8
8
  * @param {Device} board - The device from which to retrieve manufacturer information.
9
9
  * @returns {Promise<string>} A Promise that resolves with the manufacturer information,
10
- * or rejects with an error if the device is not connected.
11
- * @throws {Error} Throws an error if the device is not connected.
12
10
  */
13
11
  export const manufacturer = async (board) => {
14
12
  // Check if the device is connected
@@ -18,8 +16,7 @@ export const manufacturer = async (board) => {
18
16
  // Read manufacturer information from the Motherboard
19
17
  return await read(board, "device", "manufacturer", 250);
20
18
  }
21
- // If device is not found, return undefined
22
- return;
23
19
  }
24
- throw new Error("Not connected.");
20
+ // If device is not found, return undefined
21
+ return undefined;
25
22
  };
package/dist/read.js CHANGED
@@ -40,8 +40,5 @@ export const read = (board, serviceId, characteristicId, duration = 0) => {
40
40
  reject(new Error("Characteristic is undefined"));
41
41
  }
42
42
  }
43
- else {
44
- reject(new Error("Device is not connected"));
45
- }
46
43
  });
47
44
  };
package/dist/serial.d.ts CHANGED
@@ -5,7 +5,5 @@ import type { Device } from "./types/devices";
5
5
  *
6
6
  * @param {Device} board - The device from which to retrieve serial number.
7
7
  * @returns {Promise<string>} A Promise that resolves with the serial number,
8
- * or rejects with an error if the device is not connected.
9
- * @throws {Error} Throws an error if the device is not connected.
10
8
  */
11
9
  export declare const serial: (board: Device) => Promise<string | undefined>;
package/dist/serial.js CHANGED
@@ -8,8 +8,6 @@ import { isMotherboard } from "./is-device";
8
8
  *
9
9
  * @param {Device} board - The device from which to retrieve serial number.
10
10
  * @returns {Promise<string>} A Promise that resolves with the serial number,
11
- * or rejects with an error if the device is not connected.
12
- * @throws {Error} Throws an error if the device is not connected.
13
11
  */
14
12
  export const serial = async (board) => {
15
13
  // Check if the device is connected
@@ -23,8 +21,7 @@ export const serial = async (board) => {
23
21
  });
24
22
  return response;
25
23
  }
26
- // If device is not found, return undefined
27
- return;
28
24
  }
29
- throw new Error("Not connected.");
25
+ // If device is not found, return undefined
26
+ return undefined;
30
27
  };
package/dist/text.d.ts CHANGED
@@ -8,7 +8,5 @@ import type { Device } from "./types/devices";
8
8
  *
9
9
  * @param {Device} board - The device from which to retrieve text information.
10
10
  * @returns {Promise<string>} A Promise that resolves with the 320-byte memory content as a string,
11
- * which includes both the written data and any unused, whitespace-padded segments.
12
- * @throws {Error} Throws an error if the device is not connected.
13
11
  */
14
12
  export declare const text: (board: Device) => Promise<string | undefined>;
package/dist/text.js CHANGED
@@ -11,8 +11,6 @@ import { MotherboardCommands } from "./commands";
11
11
  *
12
12
  * @param {Device} board - The device from which to retrieve text information.
13
13
  * @returns {Promise<string>} A Promise that resolves with the 320-byte memory content as a string,
14
- * which includes both the written data and any unused, whitespace-padded segments.
15
- * @throws {Error} Throws an error if the device is not connected.
16
14
  */
17
15
  export const text = async (board) => {
18
16
  // Check if the device is connected
@@ -26,8 +24,7 @@ export const text = async (board) => {
26
24
  });
27
25
  return response;
28
26
  }
29
- // If device is not found, return undefined
30
- return;
31
27
  }
32
- throw new Error("Not connected.");
28
+ // If device is not found, return undefined
29
+ return undefined;
33
30
  };
package/dist/write.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare let writeCallback: WriteCallback;
22
22
  *
23
23
  * @returns {Promise<void>} A promise that resolves once the write operation is complete.
24
24
  *
25
- * @throws {Error} Throws an error if the characteristic is undefined or if the device is not connected.
25
+ * @throws {Error} Throws an error if the characteristic is undefined.
26
26
  *
27
27
  * @example
28
28
  * // Example usage of the write function with a custom callback
package/dist/write.js CHANGED
@@ -23,7 +23,7 @@ export let writeCallback = (data) => {
23
23
  *
24
24
  * @returns {Promise<void>} A promise that resolves once the write operation is complete.
25
25
  *
26
- * @throws {Error} Throws an error if the characteristic is undefined or if the device is not connected.
26
+ * @throws {Error} Throws an error if the characteristic is undefined.
27
27
  *
28
28
  * @example
29
29
  * // Example usage of the write function with a custom callback
@@ -32,29 +32,28 @@ export let writeCallback = (data) => {
32
32
  * });
33
33
  */
34
34
  export const write = async (board, serviceId, characteristicId, message, duration = 0, callback = writeCallback) => {
35
- if (!isConnected(board)) {
36
- throw new Error("Device is not connected");
37
- }
38
- // Check if message is provided
39
- if (message === undefined) {
40
- // If not provided, return without performing write operation
41
- return;
42
- }
43
- // Get the characteristic from the device using serviceId and characteristicId
44
- const characteristic = getCharacteristic(board, serviceId, characteristicId);
45
- if (!characteristic) {
46
- throw new Error("Characteristic is undefined");
47
- }
48
- // Convert the message to Uint8Array if it's a string
49
- const valueToWrite = typeof message === "string" ? new TextEncoder().encode(message) : message;
50
- // Write the value to the characteristic
51
- await characteristic.writeValue(valueToWrite);
52
- // Update the last written message
53
- lastWrite = message;
54
- // Assign the provided callback to `writeCallback`
55
- writeCallback = callback;
56
- // If a duration is specified, resolve the promise after the duration
57
- if (duration > 0) {
58
- await new Promise((resolve) => setTimeout(resolve, duration));
35
+ if (isConnected(board)) {
36
+ // Check if message is provided
37
+ if (message === undefined) {
38
+ // If not provided, return without performing write operation
39
+ return;
40
+ }
41
+ // Get the characteristic from the device using serviceId and characteristicId
42
+ const characteristic = getCharacteristic(board, serviceId, characteristicId);
43
+ if (!characteristic) {
44
+ throw new Error("Characteristic is undefined");
45
+ }
46
+ // Convert the message to Uint8Array if it's a string
47
+ const valueToWrite = typeof message === "string" ? new TextEncoder().encode(message) : message;
48
+ // Write the value to the characteristic
49
+ await characteristic.writeValue(valueToWrite);
50
+ // Update the last written message
51
+ lastWrite = message;
52
+ // Assign the provided callback to `writeCallback`
53
+ writeCallback = callback;
54
+ // If a duration is specified, resolve the promise after the duration
55
+ if (duration > 0) {
56
+ await new Promise((resolve) => setTimeout(resolve, duration));
57
+ }
59
58
  }
60
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hangtime/grip-connect",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "A client that can establish connections with various Force-Sensing Hangboards/Plates used by climbers for strength measurement. Examples of such hangboards include the Griptonite Motherboard, Climbro, SmartBoard, Entralpi or Tindeq Progressor",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/battery.ts CHANGED
@@ -12,8 +12,6 @@ import { ProgressorCommands } from "./commands"
12
12
  *
13
13
  * @param {Device} board - The device from which to retrieve battery information.
14
14
  * @returns {Promise<string | undefined>} A Promise that resolves with the battery or voltage information,
15
- * or rejects with an error if the device is not connected.
16
- * @throws {Error} Throws an error if the device is not connected.
17
15
  */
18
16
  export const battery = async (board: Device): Promise<string | undefined> => {
19
17
  // Check if the device is connected
@@ -32,8 +30,7 @@ export const battery = async (board: Device): Promise<string | undefined> => {
32
30
  })
33
31
  return response
34
32
  }
35
- // If device is not found, return undefined
36
- return
37
33
  }
38
- throw new Error("Not connected.")
34
+ // If device is not found, return undefined
35
+ return undefined
39
36
  }
@@ -25,4 +25,5 @@ export const getCharacteristic = (
25
25
  }
26
26
  }
27
27
  // Return undefined if the service or characteristic is not found
28
+ return undefined
28
29
  }
package/src/disconnect.ts CHANGED
@@ -2,13 +2,15 @@ import type { Device } from "./types/devices"
2
2
  import { isConnected } from "./is-connected"
3
3
 
4
4
  /**
5
- * Disconnects the device if it is connected.
6
- * @param {Device} board - The device to disconnect.
5
+ * Disconnects the device if it is currently connected.
6
+ * - Checks if the device is connected via its GATT server.
7
+ * - If the device is connected, it attempts to gracefully disconnect.
8
+ * @param {Device} board - The device to be disconnected. The device must have a `gatt` property accessible through `board.device`.
7
9
  */
8
10
  export const disconnect = (board: Device): void => {
9
- // Check if the device is connected
11
+ // Verify that the device is connected using the provided helper function
10
12
  if (isConnected(board)) {
11
- // Disconnect the device using optional chaining
13
+ // Safely attempt to disconnect the device's GATT server, if available
12
14
  board.device?.gatt?.disconnect()
13
15
  }
14
16
  }
package/src/firmware.ts CHANGED
@@ -12,8 +12,6 @@ import { isMotherboard, isProgressor } from "./is-device"
12
12
  *
13
13
  * @param {Device} board - The device from which to retrieve firmware version.
14
14
  * @returns {Promise<string>} A Promise that resolves with the firmware version,
15
- * or rejects with an error if the device is not connected.
16
- * @throws {Error} Throws an error if the device is not connected.
17
15
  */
18
16
  export const firmware = async (board: Device): Promise<string | undefined> => {
19
17
  // Check if the device is connected
@@ -32,8 +30,7 @@ export const firmware = async (board: Device): Promise<string | undefined> => {
32
30
  })
33
31
  return response
34
32
  }
35
- // If device is not found, return undefined
36
- return
37
33
  }
38
- throw new Error("Not connected.")
34
+ // If device is not found, return undefined
35
+ return undefined
39
36
  }
package/src/hardware.ts CHANGED
@@ -9,8 +9,6 @@ import { isMotherboard } from "./is-device"
9
9
  *
10
10
  * @param {Device} board - The device from which to retrieve hardware version.
11
11
  * @returns {Promise<string>} A Promise that resolves with the hardware version,
12
- * or rejects with an error if the device is not connected.
13
- * @throws {Error} Throws an error if the device is not connected.
14
12
  */
15
13
  export const hardware = async (board: Device): Promise<string | undefined> => {
16
14
  // Check if the device is connected
@@ -20,8 +18,7 @@ export const hardware = async (board: Device): Promise<string | undefined> => {
20
18
  // Read hardware version from the Motherboard
21
19
  return await read(board, "device", "hardware", 250)
22
20
  }
23
- // If device is not found, return undefined
24
- return
25
21
  }
26
- throw new Error("Not connected.")
22
+ // If device is not found, return undefined
23
+ return
27
24
  }
package/src/is-active.ts CHANGED
@@ -13,17 +13,33 @@ let activeCallback: IsActiveCallback | undefined
13
13
  export let isActive = false
14
14
 
15
15
  /**
16
- * Sets the callback function to be called when the activity status changes.
16
+ * Configuration for threshold and duration.
17
+ */
18
+ let config = { threshold: 2.5, duration: 1000 }
19
+
20
+ /**
21
+ * Sets the callback function to be called when the activity status changes,
22
+ * and optionally sets the configuration for threshold and duration.
17
23
  *
18
24
  * This function allows you to specify a callback that will be invoked whenever
19
25
  * the activity status changes, indicating whether the device is currently active.
26
+ * It also allows optionally configuring the threshold and duration used to determine activity.
20
27
  *
21
28
  * @param {IsActiveCallback} callback - The callback function to be set. This function
22
29
  * receives a boolean value indicating the new activity status.
30
+ * @param {object} [options] - Optional configuration object containing the threshold and duration.
31
+ * @param {number} [options.threshold=2.5] - The threshold value for determining activity.
32
+ * @param {number} [options.duration=1000] - The duration (in milliseconds) to monitor the input for activity.
23
33
  * @returns {void}
24
34
  */
25
- export const active = (callback: IsActiveCallback): void => {
35
+ export const active = (callback: IsActiveCallback, options?: { threshold?: number; duration?: number }): void => {
26
36
  activeCallback = callback
37
+
38
+ // Update the config values only if provided, otherwise use defaults
39
+ config = {
40
+ threshold: options?.threshold ?? config.threshold, // Use new threshold if provided, else use default
41
+ duration: options?.duration ?? config.duration, // Use new duration if provided, else use default
42
+ }
27
43
  }
28
44
 
29
45
  /**
@@ -34,18 +50,14 @@ export const active = (callback: IsActiveCallback): void => {
34
50
  * the previous state, the callback function is called with the updated activity status.
35
51
  *
36
52
  * @param {number} input - The dynamic value to check for activity status.
37
- * @param {number} [threshold=2.5] - The threshold value to determine if the input is considered active.
38
- * Defaults to 2.5 if not provided.
39
- * @param {number} [duration=1000] - The duration (in milliseconds) to monitor the input for activity.
40
- * Defaults to 1000 milliseconds if not provided.
41
53
  * @returns {Promise<void>} A promise that resolves once the activity check is complete.
42
54
  */
43
- export const checkActivity = (input: number, threshold = 2.5, duration = 1000): Promise<void> => {
55
+ export const checkActivity = (input: number): Promise<void> => {
44
56
  return new Promise((resolve) => {
45
57
  // Check the activity status after the specified duration
46
58
  setTimeout(() => {
47
- // Determine the activity status based on the threshold
48
- const activeNow = input > threshold
59
+ // Determine the activity status based on the stored threshold in the config
60
+ const activeNow = input > config.threshold
49
61
  if (isActive !== activeNow) {
50
62
  isActive = activeNow
51
63
  if (activeCallback) {
@@ -53,6 +65,6 @@ export const checkActivity = (input: number, threshold = 2.5, duration = 1000):
53
65
  }
54
66
  }
55
67
  resolve()
56
- }, duration)
68
+ }, config.duration)
57
69
  })
58
70
  }
package/src/led.ts CHANGED
@@ -205,7 +205,6 @@ export const led = async (
205
205
  const [redValue, greenValue] = colorMapping[color]
206
206
  await write(board, "led", "red", new Uint8Array(redValue))
207
207
  await write(board, "led", "green", new Uint8Array(greenValue), 1250)
208
- return
209
208
  }
210
- return
209
+ return undefined
211
210
  }
@@ -9,8 +9,6 @@ import { isMotherboard } from "./is-device"
9
9
  *
10
10
  * @param {Device} board - The device from which to retrieve manufacturer information.
11
11
  * @returns {Promise<string>} A Promise that resolves with the manufacturer information,
12
- * or rejects with an error if the device is not connected.
13
- * @throws {Error} Throws an error if the device is not connected.
14
12
  */
15
13
  export const manufacturer = async (board: Device): Promise<string | undefined> => {
16
14
  // Check if the device is connected
@@ -20,8 +18,7 @@ export const manufacturer = async (board: Device): Promise<string | undefined> =
20
18
  // Read manufacturer information from the Motherboard
21
19
  return await read(board, "device", "manufacturer", 250)
22
20
  }
23
- // If device is not found, return undefined
24
- return
25
21
  }
26
- throw new Error("Not connected.")
22
+ // If device is not found, return undefined
23
+ return undefined
27
24
  }
package/src/read.ts CHANGED
@@ -41,8 +41,6 @@ export const read = (board: Device, serviceId: string, characteristicId: string,
41
41
  } else {
42
42
  reject(new Error("Characteristic is undefined"))
43
43
  }
44
- } else {
45
- reject(new Error("Device is not connected"))
46
44
  }
47
45
  })
48
46
  }
package/src/serial.ts CHANGED
@@ -10,8 +10,6 @@ import { isMotherboard } from "./is-device"
10
10
  *
11
11
  * @param {Device} board - The device from which to retrieve serial number.
12
12
  * @returns {Promise<string>} A Promise that resolves with the serial number,
13
- * or rejects with an error if the device is not connected.
14
- * @throws {Error} Throws an error if the device is not connected.
15
13
  */
16
14
  export const serial = async (board: Device): Promise<string | undefined> => {
17
15
  // Check if the device is connected
@@ -25,8 +23,7 @@ export const serial = async (board: Device): Promise<string | undefined> => {
25
23
  })
26
24
  return response
27
25
  }
28
- // If device is not found, return undefined
29
- return
30
26
  }
31
- throw new Error("Not connected.")
27
+ // If device is not found, return undefined
28
+ return undefined
32
29
  }
package/src/text.ts CHANGED
@@ -13,8 +13,6 @@ import { MotherboardCommands } from "./commands"
13
13
  *
14
14
  * @param {Device} board - The device from which to retrieve text information.
15
15
  * @returns {Promise<string>} A Promise that resolves with the 320-byte memory content as a string,
16
- * which includes both the written data and any unused, whitespace-padded segments.
17
- * @throws {Error} Throws an error if the device is not connected.
18
16
  */
19
17
  export const text = async (board: Device): Promise<string | undefined> => {
20
18
  // Check if the device is connected
@@ -28,8 +26,7 @@ export const text = async (board: Device): Promise<string | undefined> => {
28
26
  })
29
27
  return response
30
28
  }
31
- // If device is not found, return undefined
32
- return
33
29
  }
34
- throw new Error("Not connected.")
30
+ // If device is not found, return undefined
31
+ return undefined
35
32
  }
package/src/write.ts CHANGED
@@ -30,7 +30,7 @@ export let writeCallback: WriteCallback = (data: string) => {
30
30
  *
31
31
  * @returns {Promise<void>} A promise that resolves once the write operation is complete.
32
32
  *
33
- * @throws {Error} Throws an error if the characteristic is undefined or if the device is not connected.
33
+ * @throws {Error} Throws an error if the characteristic is undefined.
34
34
  *
35
35
  * @example
36
36
  * // Example usage of the write function with a custom callback
@@ -46,31 +46,30 @@ export const write = async (
46
46
  duration = 0,
47
47
  callback: WriteCallback = writeCallback,
48
48
  ): Promise<void> => {
49
- if (!isConnected(board)) {
50
- throw new Error("Device is not connected")
51
- }
52
- // Check if message is provided
53
- if (message === undefined) {
54
- // If not provided, return without performing write operation
55
- return
56
- }
57
- // Get the characteristic from the device using serviceId and characteristicId
58
- const characteristic = getCharacteristic(board, serviceId, characteristicId)
59
- if (!characteristic) {
60
- throw new Error("Characteristic is undefined")
61
- }
62
- // Convert the message to Uint8Array if it's a string
63
- const valueToWrite: Uint8Array = typeof message === "string" ? new TextEncoder().encode(message) : message
64
- // Write the value to the characteristic
65
- await characteristic.writeValue(valueToWrite)
66
- // Update the last written message
67
- lastWrite = message
68
- // Assign the provided callback to `writeCallback`
49
+ if (isConnected(board)) {
50
+ // Check if message is provided
51
+ if (message === undefined) {
52
+ // If not provided, return without performing write operation
53
+ return
54
+ }
55
+ // Get the characteristic from the device using serviceId and characteristicId
56
+ const characteristic = getCharacteristic(board, serviceId, characteristicId)
57
+ if (!characteristic) {
58
+ throw new Error("Characteristic is undefined")
59
+ }
60
+ // Convert the message to Uint8Array if it's a string
61
+ const valueToWrite: Uint8Array = typeof message === "string" ? new TextEncoder().encode(message) : message
62
+ // Write the value to the characteristic
63
+ await characteristic.writeValue(valueToWrite)
64
+ // Update the last written message
65
+ lastWrite = message
66
+ // Assign the provided callback to `writeCallback`
69
67
 
70
- writeCallback = callback
71
- // If a duration is specified, resolve the promise after the duration
68
+ writeCallback = callback
69
+ // If a duration is specified, resolve the promise after the duration
72
70
 
73
- if (duration > 0) {
74
- await new Promise<void>((resolve) => setTimeout(resolve, duration))
71
+ if (duration > 0) {
72
+ await new Promise<void>((resolve) => setTimeout(resolve, duration))
73
+ }
75
74
  }
76
75
  }