Skip to content

Commit 4dee246

Browse files
committed
nicla-system: Add documentation to the web app.
1 parent de371ef commit 4dee246

File tree

1 file changed

+23
-0
lines changed
  • libraries/Nicla_System/extras/BatteryMonitor

1 file changed

+23
-0
lines changed

libraries/Nicla_System/extras/BatteryMonitor/app.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// UI elements
12
const connectButton = document.getElementById('connect');
23
const batteryLevelElement = document.getElementById('battery-level');
34
const batteryLabel = document.getElementById('battery-label');
@@ -8,6 +9,7 @@ const serviceUuid = '19b10000-0000-537e-4f6c-d104768a1214';
89
let pollIntervalID;
910
let peripheralDevice;
1011

12+
/// Data structure to hold the characteristics and their values plus data conversion functions.
1113
let data = {
1214
"batteryPercentage": {
1315
"name": "Battery Percentage",
@@ -81,6 +83,12 @@ function onDisconnected(event) {
8183
externalPowerIconElement.style.display = "none";
8284
}
8385

86+
/**
87+
* Connects to the Arduino board and starts reading the characteristics.
88+
* @param {Boolean} usePolling The default is to use notifications, but polling can be used instead.
89+
* In that case a poll interval can be defined.
90+
* @param {Number} pollInterval The interval in milliseconds to poll the characteristics from the device.
91+
*/
8492
async function connectToPeripheralDevice(usePolling = false, pollInterval = 5000){
8593
if (peripheralDevice && peripheralDevice.gatt.connected) {
8694
console.log("Already connected");
@@ -131,6 +139,12 @@ connectButton.addEventListener('click', async () => {
131139
}
132140
});
133141

142+
/**
143+
* Renders the data from the device in the UI.
144+
* It displays the battery level as a visual bar color coded from red to green.
145+
* It also displays the battery voltage and the percentage of the regulated voltage.
146+
* It also displays the charging and external power status.
147+
*/
134148
function displayBatteryData() {
135149
const batteryPercentage = data.batteryPercentage.value;
136150
const batteryVoltage = data.batteryVoltage.value;
@@ -146,6 +160,10 @@ function displayBatteryData() {
146160
externalPowerIconElement.style.display = data.runsOnBattery.value ? "none" : "block";
147161
}
148162

163+
/**
164+
* Used together with polling to read the characteristics from the device.
165+
* After reading the data it is displayed in the UI by calling displayBatteryData().
166+
*/
149167
async function readCharacteristicsData() {
150168
await Promise.all(
151169
Object.keys(data).map(async (key) => {
@@ -158,6 +176,11 @@ async function readCharacteristicsData() {
158176
displayBatteryData();
159177
}
160178

179+
/**
180+
* Callback function that is called when a characteristic value changes.
181+
* Updates the data object with the new value and displays it in the UI by calling displayBatteryData().
182+
* @param {*} event The event that contains the characteristic that changed.
183+
*/
161184
function handleCharacteristicChange(event) {
162185
// Find the characteristic that changed in the data object by matching the UUID
163186
let dataItem = Object.values(data).find(item => item.characteristicUUID === event.target.uuid);

0 commit comments

Comments
 (0)