mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add ability to get battery voltage
This commit is contained in:
parent
8125faaccf
commit
f6cfe37449
3 changed files with 58 additions and 0 deletions
11
index.html
11
index.html
|
|
@ -101,6 +101,9 @@
|
|||
<button @click="reboot" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
Reboot
|
||||
</button>
|
||||
<button @click="getBatteryVoltage" class="border border-gray-500 px-2 bg-gray-100 hover:bg-gray-200 rounded">
|
||||
GetBatteryVoltage
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -393,6 +396,14 @@
|
|||
alert("Failed to reboot!");
|
||||
}
|
||||
},
|
||||
async getBatteryVoltage() {
|
||||
try {
|
||||
const response = await this.connection.getBatteryVoltage();
|
||||
alert(`Battery Voltage: ${response.batteryMilliVolts}mV`);
|
||||
} catch(e) {
|
||||
alert("Failed to get battery voltage!");
|
||||
}
|
||||
},
|
||||
bytesToHex(uint8Array) {
|
||||
return Array.from(uint8Array).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
||||
},
|
||||
|
|
|
|||
|
|
@ -178,6 +178,12 @@ class Connection extends EventEmitter {
|
|||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
async sendCommandGetBatteryVoltage() {
|
||||
const data = new BufferWriter();
|
||||
data.writeByte(Constants.CommandCodes.GetBatteryVoltage);
|
||||
await this.sendToRadioFrame(data.toBytes());
|
||||
}
|
||||
|
||||
onFrameReceived(frame) {
|
||||
|
||||
// emit received frame
|
||||
|
|
@ -210,6 +216,8 @@ class Connection extends EventEmitter {
|
|||
this.onSentResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.ExportContact){
|
||||
this.onExportContactResponse(bufferReader);
|
||||
} else if(responseCode === Constants.ResponseCodes.BatteryVoltage){
|
||||
this.onBatteryVoltageResponse(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.Advert){
|
||||
this.onAdvertPush(bufferReader);
|
||||
} else if(responseCode === Constants.PushCodes.PathUpdated){
|
||||
|
|
@ -302,6 +310,12 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
onBatteryVoltageResponse(bufferReader) {
|
||||
this.emit(Constants.ResponseCodes.BatteryVoltage, {
|
||||
batteryMilliVolts: bufferReader.readUInt16LE(),
|
||||
});
|
||||
}
|
||||
|
||||
onSelfInfoResponse(bufferReader) {
|
||||
this.emit(Constants.ResponseCodes.SelfInfo, {
|
||||
type: bufferReader.readByte(),
|
||||
|
|
@ -972,6 +986,37 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
getBatteryVoltage() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// resolve promise when we receive battery voltage
|
||||
const onBatteryVoltage = (response) => {
|
||||
this.off(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
resolve(response);
|
||||
}
|
||||
|
||||
// reject promise when we receive err
|
||||
const onErr = () => {
|
||||
this.off(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage);
|
||||
this.off(Constants.ResponseCodes.Err, onErr);
|
||||
reject();
|
||||
}
|
||||
|
||||
// listen for events
|
||||
this.once(Constants.ResponseCodes.BatteryVoltage, onBatteryVoltage);
|
||||
this.once(Constants.ResponseCodes.Err, onErr);
|
||||
|
||||
// get battery voltage
|
||||
await this.sendCommandGetBatteryVoltage();
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ class Constants {
|
|||
ExportContact: 17,
|
||||
ImportContact: 18,
|
||||
Reboot: 19,
|
||||
GetBatteryVoltage: 20,
|
||||
}
|
||||
|
||||
static ResponseCodes = {
|
||||
|
|
@ -46,6 +47,7 @@ class Constants {
|
|||
CurrTime: 9,
|
||||
NoMoreMessages: 10,
|
||||
ExportContact: 11,
|
||||
BatteryVoltage: 12,
|
||||
}
|
||||
|
||||
static PushCodes = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue