1
+ /// UI elements
1
2
const connectButton = document . getElementById ( 'connect' ) ;
2
3
const batteryLevelElement = document . getElementById ( 'battery-level' ) ;
3
4
const batteryLabel = document . getElementById ( 'battery-label' ) ;
@@ -8,6 +9,7 @@ const serviceUuid = '19b10000-0000-537e-4f6c-d104768a1214';
8
9
let pollIntervalID ;
9
10
let peripheralDevice ;
10
11
12
+ /// Data structure to hold the characteristics and their values plus data conversion functions.
11
13
let data = {
12
14
"batteryPercentage" : {
13
15
"name" : "Battery Percentage" ,
@@ -79,6 +81,12 @@ function onDisconnected(event) {
79
81
batteryLabel . textContent = "" ;
80
82
}
81
83
84
+ /**
85
+ * Connects to the Arduino board and starts reading the characteristics.
86
+ * @param {Boolean } usePolling The default is to use notifications, but polling can be used instead.
87
+ * In that case a poll interval can be defined.
88
+ * @param {Number } pollInterval The interval in milliseconds to poll the characteristics from the device.
89
+ */
82
90
async function connectToPeripheralDevice ( usePolling = false , pollInterval = 5000 ) {
83
91
if ( peripheralDevice && peripheralDevice . gatt . connected ) {
84
92
console . log ( "Already connected" ) ;
@@ -129,6 +137,12 @@ connectButton.addEventListener('click', async () => {
129
137
}
130
138
} ) ;
131
139
140
+ /**
141
+ * Renders the data from the device in the UI.
142
+ * It displays the battery level as a visual bar color coded from red to green.
143
+ * It also displays the battery voltage and the percentage of the regulated voltage.
144
+ * It also displays the charging and external power status.
145
+ */
132
146
function displayBatteryData ( ) {
133
147
const batteryPercentage = data . batteryPercentage . value ;
134
148
const batteryVoltage = data . batteryVoltage . value ;
@@ -144,6 +158,10 @@ function displayBatteryData() {
144
158
externalPowerIconElement . style . display = data . runsOnBattery . value ? "none" : "block" ;
145
159
}
146
160
161
+ /**
162
+ * Used together with polling to read the characteristics from the device.
163
+ * After reading the data it is displayed in the UI by calling displayBatteryData().
164
+ */
147
165
async function readCharacteristicsData ( ) {
148
166
await Promise . all (
149
167
Object . keys ( data ) . map ( async ( key ) => {
@@ -156,6 +174,11 @@ async function readCharacteristicsData() {
156
174
displayBatteryData ( ) ;
157
175
}
158
176
177
+ /**
178
+ * Callback function that is called when a characteristic value changes.
179
+ * Updates the data object with the new value and displays it in the UI by calling displayBatteryData().
180
+ * @param {* } event The event that contains the characteristic that changed.
181
+ */
159
182
function handleCharacteristicChange ( event ) {
160
183
// Find the characteristic that changed in the data object by matching the UUID
161
184
let dataItem = Object . values ( data ) . find ( item => item . characteristicUUID === event . target . uuid ) ;
0 commit comments