{"id":25416408,"url":"https://github.com/forderud/batterysimulator","last_synced_at":"2025-10-31T09:30:39.953Z","repository":{"id":211260569,"uuid":"727636423","full_name":"forderud/BatterySimulator","owner":"forderud","description":"Driver to simulate multi-battery setups and test Windows power management","archived":false,"fork":false,"pushed_at":"2024-10-17T16:41:40.000Z","size":336,"stargazers_count":13,"open_issues_count":5,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-19T11:59:31.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forderud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-05T09:12:03.000Z","updated_at":"2024-10-17T09:28:31.000Z","dependencies_parsed_at":"2024-03-30T08:29:02.581Z","dependency_job_id":"ccf4d944-1a67-4266-bc0a-9b87de8dd890","html_url":"https://github.com/forderud/BatterySimulator","commit_stats":null,"previous_names":["forderud/batterysimulator"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forderud%2FBatterySimulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forderud%2FBatterySimulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forderud%2FBatterySimulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forderud%2FBatterySimulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forderud","download_url":"https://codeload.github.com/forderud/BatterySimulator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239162792,"owners_count":19592343,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-16T16:54:57.205Z","updated_at":"2025-10-31T09:30:34.651Z","avatar_url":"https://github.com/forderud.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"| Project      | Description                                            |\n|--------------|--------------------------------------------------------|\n| **BatteryQuery** | Tool for querying battery parameters. |\n| **DevicePowerQuery** | Tool for enumerating connected devices, their power state and parameters. |\n| **PowerMonitor** | Sample code for handling power events and query system power state. |\n| **simbatt** | Mock driver to simulate multi-battery setups and test Windows power management. Based on the Microsoft [Simulated Battery Driver Sample](https://github.com/microsoft/Windows-driver-samples/tree/main/simbatt) sample with [modifications](/simbatt) to ease multi-battery testing and failure handling without requiring physical battery packs. |\n| **[HidBattery](https://github.com/forderud/HidBattery)** | Arduino battery emulator to emulate one or more battery packs. |\n\n\nTutorial: [Writing Battery Miniclass Drivers](https://learn.microsoft.com/en-us/windows-hardware/drivers/battery/writing-battery-miniclass-drivers)\n\n![image](DriverStack.png)  \n\n## How to test simbatt\nIt's recommended to **test in a disposable virtual machine (VM)** during development, since faulty drivers might crash or corrupt the computer. You can use the \"checkpoint\" feature to roll back the machine to a known good state in case of driver installations problems.\n\n### Prerequisites\nPrerequisites for the _host_ computer:\n* Install [Visual Studio](https://visualstudio.microsoft.com/).\n* Install [Windows Driver Kit (WDK)](https://learn.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk).\n* (optional) Install [WiX toolset](https://wixtoolset.org/) for MSI installer packaging.\n\nPrerequisites for the _target_ computer:\n* Disable Secure Boot in UEFI/BIOS.\n* Enable test-signed drivers: [`bcdedit /set testsigning on`](https://learn.microsoft.com/en-us/windows-hardware/drivers/install/the-testsigning-boot-configuration-option)\n* Enable kernel debugging: `bcdedit /debug on` (optional)\n\n### Test instruction\nYou can with the driver **make Windows believe that it’s being powered by one or more battery packs** – even if running from a AC-powered VM.\n\nSteps:\n* Build solution in Visual Studio or download binaries from [releases](../../releases).\n* Copy `BatteryQuery.exe`, `PowerMonitor.exe` and the `simbatt` folder to the target machine.\n* Run `INSTALL.bat` with admin privileges to install the driver with two simulated batteries.\n* Run `BatteryQuery.exe \u003cN\u003e \u003cCharge\u003e`, where `\u003cN\u003e` is the simulated battery index and `\u003cCharge\u003e` is the new charge level, to modify the battery state. Example: `BatteryQuery.exe 1 90` to set the charge level of the first battery to 90%.\n* Run `PowerMonitor.exe` to monitor power events broadcasted to all application.\n* Run `UNINSTALL.bat` with admin privileges to uninstall the driver and delete simulated batteries.\n\n### Examples\nSimulate 6 battery-packs:  \n![image](https://github.com/forderud/BatterySimulator/assets/2671400/fce5172f-8125-495b-ab06-864e079c19c7)\n\nBattery status icons that can be simulated:\n* Critical level: ![image](https://github.com/forderud/BatterySimulator/assets/2671400/9a6d48aa-3e21-4423-b9ef-753cff2587aa) (7% or less)\n* Low level: ![image](https://github.com/forderud/BatterySimulator/assets/2671400/7e03f6c0-222e-4a87-8a33-4aec937ede94) (10% or less)\n* Battery saver: ![image](https://github.com/forderud/BatterySimulator/assets/2671400/ef038cbd-33a3-43c8-8e18-531878c59004) (20% or less)\n* Half full: ![image](https://github.com/forderud/BatterySimulator/assets/2671400/fdc0fc67-3628-4879-a9ef-9fa2d02feda6)\n* On AC power: ![image](https://github.com/forderud/BatterySimulator/assets/2671400/d258e0a8-5876-4ca4-80da-f16367166ce6)\n\nNotification when simulating low-battery conditions:  \n![image](https://github.com/forderud/BatterySimulator/assets/2671400/80707d03-8ffc-4209-bfff-8bfaa1c4181c)\n\n## Alternative approach\nThe [HidBattery](https://github.com/forderud/HidBattery) project provides firmware for making an Arduino board emulate a battery. It leverages the in-built Windows `hidbatt` driver, so there's no need for installing additional drivers.\n\n## Configuring Windows power management\nWindows handling of low-battery situations can either be configured through the \"Power Options\" UI (run `powercfg.cpl` -\u003e \"Change plan settings\" -\u003e \"Change advanced power settings\"):  \n![image](https://github.com/forderud/BatterySimulator/assets/2671400/c98a64a4-1c29-43d8-9376-3feca6ce1130)\n![image](https://github.com/user-attachments/assets/e90c7e68-6fbb-46d3-9734-7bdfdfb762f1)\n\n\n... or using [Powercfg](https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/powercfg-command-line-options):\n```\n:: Display current power configuration \nPowerCfg.exe /query\n\n:: Select power scheme (in-built schemes: SCHEME_MIN, SCHEME_MAX or SCHEME_BALANCED)\nset SCHEME=SCHEME_BALANCED\n```\n\nPower saving configuration:\n```\n:: Select power sheme\nPowerCfg.exe /setactive %SCHEME%\n\n:: Sleep after after 4min on DC and never sleep on AC\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_SLEEP STANDBYIDLE 240\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_SLEEP STANDBYIDLE 0\n\n:: Turn off display after 5min on DC and 10min on AC\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_VIDEO VIDEOIDLE 300\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_VIDEO VIDEOIDLE 600\n```\nDrivers can expose both standard and custom power setting parameters as documented on [Managing Device Performance States](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/managing-device-performance-states). These power settings can afterwards be configured using `PowerCfg`.\n\n\nPower event configuration for AC \u0026 DC mode:\n```\n:: Low battery level (percentage)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATLEVELLOW 10\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATLEVELLOW 10\n\n:: Low battery notification (0=off, 1=on)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATFLAGSLOW 0\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATFLAGSLOW 0\n:: Delete Low battery notification rule\n:: PowerCfg.exe /deletesetting SUB_BATTERY BATFLAGSLOW\n\n:: Low battery action (0=do nothing, 1=sleep, 2=hibernate, 3=shut down)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATACTIONLOW 0\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATACTIONLOW 0\n:: Delete Low battery action rule\n:: PowerCfg.exe /deletesetting SUB_BATTERY BATACTIONLOW\n\n:: Critical battery level (percentage)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATLEVELCRIT 5\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATLEVELCRIT 5\n\n:: Critical battery notification (0=off, 1=on)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATFLAGSCRIT 0\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATFLAGSCRIT 0\n:: Delete Critical battery notification rule\n:: PowerCfg.exe /deletesetting SUB_BATTERY BATFLAGSCRIT\n\n:: Critical battery action (0=do nothing, 1=sleep, 2=hibernate, 3=shut down)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_BATTERY BATACTIONCRIT 0\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_BATTERY BATACTIONCRIT 0\n:: Delete Critical battery action rule\n:: PowerCfg.exe /deletesetting SUB_BATTERY BATACTIONCRIT\n```\n\nCPU power configuration for AC \u0026 DC mode:\n```\n:: Minimum processor state (percentage)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_PROCESSOR PROCTHROTTLEMIN 5\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_PROCESSOR PROCTHROTTLEMIN 5\n\n:: System cooling policy (0=passive, 1=active)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_PROCESSOR SYSCOOLPOL 1\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_PROCESSOR SYSCOOLPOL 0\n\n:: Maximum processor state (percentage)\nPowerCfg.exe /setacvalueindex %SCHEME% SUB_PROCESSOR PROCTHROTTLEMAX 100\nPowerCfg.exe /setdcvalueindex %SCHEME% SUB_PROCESSOR PROCTHROTTLEMAX 100\n```\n\nThere are also many other settings available. Use `PowerCfg.exe /query` to view all settings. See [PowerSettings.ps1](./PowerSettings.ps1) for an example of API access and modification of power settings from a script.\n\n\n## Battery parameters\nBattery parameters from the battery miniclass driver will automatically be exposed through the [`Win32_Battery`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-battery) WMI class, so there's no need for implementing a WMI provider yourself.\n\n### Per-battery parameters\nPer-battery parameters can either be accessed through a WMI high-level or IOCTL low-level interface:\n* See the [BatteryParams.ps1](./BatteryParams.ps1) script for how to retrieve battery parameters through WMI.\n* See the [BatteryQuery](./BatteryQuery) project for how to retrieve battery parameters through [`IOCTL_BATTERY_QUERY_INFORMATION`](https://learn.microsoft.com/en-us/windows/win32/power/ioctl-battery-query-information) and [`IOCTL_BATTERY_QUERY_STATUS`](https://learn.microsoft.com/en-us/windows/win32/power/ioctl-battery-query-status) control codes.\n\n\n### Windows power events and aggregated parameters\nWindows applications receive [`WM_POWERBROADCAST`](https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast) events when the machine transitions between AC and battery power, as well as when suspening or resuming from low-power modes. Details about the power state and aggregated battery charge level can be retrieved by calling [`GetSystemPowerStatus`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getsystempowerstatus).\n\nThe [PowerMonitor](./PowerMonitor) project demonstrates how to handle power events and query system power state.  \n![image](https://github.com/user-attachments/assets/5df449cd-e2af-47c4-b127-da6505207a8e)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforderud%2Fbatterysimulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforderud%2Fbatterysimulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforderud%2Fbatterysimulator/lists"}