Skip to content

[AE-74] Revamp Battery API for Nicla Sense ME #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 48 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
965fb69
nicla-system: Rework battery API.
sebromero Apr 26, 2023
d8fc798
nicla-sense-pmic: Use function to toggle Hi-Z mode.
sebromero Apr 26, 2023
20ee494
nicla-system: Add battery monitor web app.
sebromero Apr 27, 2023
864d9a3
nicla-system: Add battery check sketch for Nicla Sense.
sebromero Apr 27, 2023
bd0ee3e
nicla-system: Add function for toggling battery NTC.
sebromero Apr 28, 2023
a47ec88
nicla-system: Refactor names.
sebromero Apr 28, 2023
254eecb
nicla-system: Remove duplicate functionality.
sebromero May 2, 2023
66bc141
nicla-system: Add API for operating system.
sebromero May 2, 2023
3b0af63
nicla-system: Rename function.
sebromero May 2, 2023
edaf109
nicla-system: Use getFaultsRegister to query faults register.
sebromero May 2, 2023
03cbd92
nicla-system: Add documentation.
sebromero May 2, 2023
1c55bb5
nicla-system: Remove battery status function.
sebromero May 2, 2023
c29e363
nicla-system: Replace defines with enum for battery level.
sebromero May 2, 2023
9f7b9a9
nicla-system: Add getter for fast charge register.
sebromero May 2, 2023
f14d519
nicla-system: Replace battery temperature with enum.
sebromero May 2, 2023
6e8f454
nicla-system: Add API to set regulated battery voltage.
sebromero May 2, 2023
d2c93a0
nicla-system: Move function to read LDO register to pmic driver.
sebromero May 2, 2023
56b67be
nicla-system: Catch error case.
sebromero May 3, 2023
d69d3ad
nicla-system: Rename function name.
sebromero May 3, 2023
444a74d
nicla-system: Improve efficiency of voltage calculation.
sebromero May 3, 2023
0708980
nicla-system: Fix incorrect INT disable.
sebromero May 3, 2023
e6ff5c5
nicla-system: Add documentation to charging function.
sebromero May 3, 2023
6d8c08a
nicla-system: Rename charging function.
sebromero May 3, 2023
9f1b6cc
nicla-system: Avoid High-Z mode when powered from VIN.
sebromero May 3, 2023
ad89fb1
nicla-system: Add function to check if board runs on battery.
sebromero May 3, 2023
6ab5b66
nicla-system: Add documentation to LDO functions.
sebromero May 4, 2023
6286728
nicla-system: Add function to disable charging.
sebromero May 4, 2023
abbc54f
nicla-system: Disable charging while taking a battery reading.
sebromero May 4, 2023
2871417
nicla-system: Display charging and power status.
sebromero May 4, 2023
de371ef
nicla-system: Make BLE updates more robust.
sebromero May 4, 2023
4dee246
nicla-system: Add documentation to the web app.
sebromero May 4, 2023
48fb48e
nicla-system: Change order of setup instructions.
sebromero May 4, 2023
cea8fa2
nicla-system: Add function for backwards compatibility.
sebromero May 4, 2023
b16d5ea
nicla-system: Give PMIC more time to wake up.
sebromero May 5, 2023
b3d7f82
nicla-system: Add error messages to sketch.
sebromero May 5, 2023
f7845c2
nicla-system: Fix incorrect battery voltage reading.
sebromero May 9, 2023
d47a6db
nicla-system: Add documentation about charging rate.
sebromero May 9, 2023
a0b13de
nicla-system: Remove NTC toggling from charge function.
sebromero May 9, 2023
f5e6718
nicla-system: Add more documentation.
sebromero May 10, 2023
d2271c1
nicla-system: Simplify the watchdog reset machanism.
sebromero May 10, 2023
14117b6
nicla-system: Remove unused code.
sebromero May 10, 2023
d399ee3
nicla-system: Increase safety charge time from 3 to 9 hours.
sebromero May 10, 2023
9bce6e8
nicla-system: Add more documentation.
sebromero May 11, 2023
c316d06
nicla-system: Add sketch that shows how to charge the battery.
sebromero May 11, 2023
21a7c56
nicla-system: Add new measurement of charge current to docs.
sebromero May 11, 2023
e51a021
nicla-system: Add option to configure safety timer.
sebromero May 11, 2023
5fff648
nicla-system: Make the safety timer configuration explicit.
sebromero May 11, 2023
1ca6fc3
nicla-system: Apply suggestions from Ali's code review.
sebromero May 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nicla-system: Add battery monitor web app.
  • Loading branch information
sebromero committed Apr 28, 2023
commit 20ee494c6e96f31d5ae4a9aa43a9f12f106fb5c6
141 changes: 141 additions & 0 deletions libraries/Nicla_System/extras/BatteryMonitor/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const connectButton = document.getElementById('connect');
const batteryLevelElement = document.getElementById('battery-level');
const batteryLabel = document.getElementById('battery-label');

const serviceUuid = '19b10000-0000-537e-4f6c-d104768a1214';
let pollIntervalID;
let peripheralDevice;

let data = {
"batteryPercentage": {
"name": "Battery Percentage",
"value": 0,
"unit": "%",
"characteristic": null,
"characteristicUUID": "19b10000-1001-537e-4f6c-d104768a1214",
"extractData": function(dataView) {
return dataView.getInt8(0);
}
},
"batteryVoltage": {
"name": "Battery Voltage",
"value": 0,
"unit": "V",
"characteristic": null,
"characteristicUUID": "19b10000-1002-537e-4f6c-d104768a1214",
"extractData": function(dataView) {
return dataView.getFloat32(0, true);
}
},
"batteryChargeLevel": {
"name": "Battery Charge Level",
"value": 0,
"unit": "",
"characteristic": null,
"characteristicUUID": "19b10000-1003-537e-4f6c-d104768a1214",
"extractData": function(dataView) {
return dataView.getInt8(0);
},
"getColor": function(value) {
// Red to green range with 5 steps and white for the unknown state
const colors = ["#ffffff", "#ff2d2d", "#fc9228", "#ffea00", "#adfd5c", "#00c600"];
return colors[value];
}
}
};

function onDisconnected(event) {
let device = event.target;
connectButton.disabled = false;
connectButton.style.opacity = 1;
if(pollIntervalID) clearInterval(pollIntervalID);
console.log(`Device ${device.name} is disconnected.`);

// Reset the battery level display
batteryLevelElement.style.width = "0px";
batteryLabel.textContent = "";
}

async function connectToPeripheralDevice(usePolling = false, pollInterval = 5000){
if (peripheralDevice && peripheralDevice.gatt.connected) {
console.log("Already connected");
return;
}

peripheralDevice = await navigator.bluetooth.requestDevice({
filters: [{ services: [serviceUuid] }]
});
peripheralDevice.addEventListener('gattserverdisconnected', onDisconnected);

const server = await peripheralDevice.gatt.connect();
console.log("Connected to: " + peripheralDevice.name);
const service = await server.getPrimaryService(serviceUuid);

await Promise.all(
Object.keys(data).map(async (key) => {
let item = data[key];
const characteristic = await service.getCharacteristic(item.characteristicUUID);
item.characteristic = characteristic;

if (!usePolling) {
characteristic.addEventListener('characteristicvaluechanged', handleCharacteristicChange);
characteristic.readValue(); // Perform an initial read
await characteristic.startNotifications();
}
})
);

if (usePolling) {
pollIntervalID = setInterval(readCharacteristicsData, pollInterval);
await readCharacteristicsData();
}
}

connectButton.addEventListener('click', async () => {
try {
await connectToPeripheralDevice(true);
connectButton.disabled = true;
connectButton.style.opacity = 0.5;
} catch (error) {
if(error.message === "User cancelled the requestDevice() chooser."){
return;
}

console.error('Error:', error);
connectButton.style.backgroundColor = "red";
}
});

function displayBatteryLevel() {
const batteryPercentage = data.batteryPercentage.value;
const batteryVoltage = data.batteryVoltage.value;
const regulatedVoltage = (batteryVoltage / batteryPercentage * 100).toFixed(2);

// Map the range from 0-5 to 0-100
const batteryPercentageMapped = data.batteryChargeLevel.value * 20;
batteryLevelElement.style.width = `${batteryPercentageMapped * 0.56}px`; // Scale the battery level to the width of the battery div
batteryLevelElement.style.backgroundColor = data.batteryChargeLevel.getColor(data.batteryChargeLevel.value);
batteryLabel.textContent = `${batteryVoltage.toFixed(2)}V (${batteryPercentage}% of ${regulatedVoltage}V)`;
}

async function readCharacteristicsData() {
await Promise.all(
Object.keys(data).map(async (key) => {
let item = data[key];
console.log("Requesting " + item.name + "...");
item.value = item.extractData(await item.characteristic.readValue());
console.log(item.name + ": " + item.value + item.unit);
})
);
displayBatteryLevel();
}

function handleCharacteristicChange(event) {
// Find the characteristic that changed in the data object by matching the UUID
let dataItem = Object.values(data).find(item => item.characteristicUUID === event.target.uuid);
let dataView = event.target.value;
dataItem.value = dataItem.extractData(dataView);

console.log(dataItem.name + " changed: " + dataItem.value + dataItem.unit);
displayBatteryLevel();
}
25 changes: 25 additions & 0 deletions libraries/Nicla_System/extras/BatteryMonitor/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebBLE Battery Monitor</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;700&family=Roboto+Mono:wght@400;700&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script defer src="app.js"></script>
</head>
<body>
<h1>WebBLE Battery Monitor 🔋</h1>
<div class="container">
<div class="battery">
<div class="battery-level" id="battery-level"></div>
</div>
<p id="battery-label"></p>
<button id="connect">Connect</button>
</div>
</body>
</html>
59 changes: 59 additions & 0 deletions libraries/Nicla_System/extras/BatteryMonitor/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
:root {
--main-control-color: #008184;
--main-control-color-hover: #005c5f;
--main-flexbox-gap: 16px;
--secondary-text-color: #87898b;
}

body {
font-family: 'Open Sans', sans-serif;
text-align: center;
}

.container {
display: inline-block;
margin-top: 20px;
}

button {
font-family: 'Open Sans', sans-serif;
font-weight: 700;
font-size: 1rem;
justify-content: center;
background-color: var(--main-control-color);
color: #fff;
cursor: pointer;
letter-spacing: 1.28px;
line-height: normal;
outline: none;
padding: 8px 18px;
text-align: center;
text-decoration: none;
border: 2px solid transparent;
border-radius: 32px;
text-transform: uppercase;
box-sizing: border-box;
}

button:hover {
background-color: var(--main-control-color-hover);
}

.battery {
width: 60px;
height: 30px;
border: 2px solid #999;
border-radius: 5px;
position: relative;
margin: 20px auto;
}

.battery-level {
position: absolute;
bottom: 2px;
left: 2px;
background-color: green;
border-radius: 2px;
width: 0;
height: 26px;
}