Reading Battery info gives strange results
I am just getting started building my application. I decided to try something simple like reading the battery information. I am using the UWP starter template along with the WinRT C++ API. I am able to subscribe and read the battery information but the values that I get back are suspect. In particular the voltage values seem to keep going up and then will all of a sudden reset and then go up again. I must be doing something wrong here but cannot figure out what else to do. I have the following code.
protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
BluetoothLEDevice selectedDevice = e.Parameter as BluetoothLEDevice;
// get the selected device and load into MetawareBoard Model
MetaWearBoard mwBoard= MetaWearBoard.getMetaWearBoardInstance(selectedDevice);
cppBoard = mwBoard.cppBoard;
// subscribe to battery info
// getting battery signal from the selected board
batterySignal = mbl_mw_settings_get_battery_state_data_signal(cppBoard);
// subscribe to receive battery info
mbl_mw_datasignal_subscribe(batterySignal, new Fn_IntPtr(sensorData =>
{
bstate = Marshal.PtrToStructure<batterystate>(sensorData);
System.Diagnostics.Debug.WriteLine("Battery data: voltage {0}, charge{1}", bstate.voltage.ToString(), bstate.charge.ToString());
}));
// this is a inifinite test loop to see whether the values recieved "settle" down. currently voltage keeps increasing.
while (true)
{
mbl_mw_datasignal_read(batterySignal);
System.Threading.Tasks.Task.Delay(500).Wait();
}
Here is the data that I am getting back...,
Battery data: voltage 58685, charge67
Battery data: voltage 59165, charge67
Battery data: voltage 59705, charge67
Battery data: voltage 60185, charge67
Battery data: voltage 60665, charge67
Battery data: voltage 61205, charge67
Battery data: voltage 61685, charge67
Battery data: voltage 62165, charge67
Battery data: voltage 62705, charge67
Battery data: voltage 63185, charge67
Battery data: voltage 63725, charge67
Battery data: voltage 64205, charge67
Battery data: voltage 64685, charge67
Battery data: voltage 65226, charge67
Battery data: voltage 170, charge68
Battery data: voltage 649, charge68
Battery data: voltage 1189, charge68
Battery data: voltage 1670, charge68
Battery data: voltage 2150, charge68
Battery data: voltage 2689, charge68
Battery data: voltage 3170, charge68
Battery data: voltage 3649, charge68
Battery data: voltage 4189, charge68
Battery data: voltage 4670, charge68
Battery data: voltage 5210, charge68
Battery data: voltage 5690, charge68
Battery data: voltage 6170, charge68
Battery data: voltage 6710, charge68
Battery data: voltage 7190, charge68
Battery data: voltage 7669, charge68
Battery data: voltage 8210, charge68
Battery data: voltage 8690, charge68
Battery data: voltage 9170, charge68
Battery data: voltage 9710, charge68
Battery data: voltage 10190, charge68
Battery data: voltage 10670, charge68
Battery data: voltage 11210, charge68
Battery data: voltage 11690, charge68
Battery data: voltage 12230, charge68
Battery data: voltage 12770, charge68
Battery data: voltage 13190, charge68
Battery data: voltage 13730, charge68
Battery data: voltage 14210, charge68
Battery data: voltage 14690, charge68
Battery data: voltage 15230, charge68
Battery data: voltage 15710, charge68
Battery data: voltage 16190, charge68
Battery data: voltage 16730, charge68
Battery data: voltage 17210, charge68
Battery data: voltage 59165, charge67
Battery data: voltage 59705, charge67
Battery data: voltage 60185, charge67
Battery data: voltage 60665, charge67
Battery data: voltage 61205, charge67
Battery data: voltage 61685, charge67
Battery data: voltage 62165, charge67
Battery data: voltage 62705, charge67
Battery data: voltage 63185, charge67
Battery data: voltage 63725, charge67
Battery data: voltage 64205, charge67
Battery data: voltage 64685, charge67
Battery data: voltage 65226, charge67
Battery data: voltage 170, charge68
Battery data: voltage 649, charge68
Battery data: voltage 1189, charge68
Battery data: voltage 1670, charge68
Battery data: voltage 2150, charge68
Battery data: voltage 2689, charge68
Battery data: voltage 3170, charge68
Battery data: voltage 3649, charge68
Battery data: voltage 4189, charge68
Battery data: voltage 4670, charge68
Battery data: voltage 5210, charge68
Battery data: voltage 5690, charge68
Battery data: voltage 6170, charge68
Battery data: voltage 6710, charge68
Battery data: voltage 7190, charge68
Battery data: voltage 7669, charge68
Battery data: voltage 8210, charge68
Battery data: voltage 8690, charge68
Battery data: voltage 9170, charge68
Battery data: voltage 9710, charge68
Battery data: voltage 10190, charge68
Battery data: voltage 10670, charge68
Battery data: voltage 11210, charge68
Battery data: voltage 11690, charge68
Battery data: voltage 12230, charge68
Battery data: voltage 12770, charge68
Battery data: voltage 13190, charge68
Battery data: voltage 13730, charge68
Battery data: voltage 14210, charge68
Battery data: voltage 14690, charge68
Battery data: voltage 15230, charge68
Battery data: voltage 15710, charge68
Battery data: voltage 16190, charge68
Battery data: voltage 16730, charge68
Battery data: voltage 17210, charge68
This discussion has been closed.
Comments
I spotted the issue with the sample code you provided. The the
sensorData
parameter is a pointer to theData
structure not the sensor's value. It needs to be marshalled to the Data struct first, then the value parameter is marshalled to the battery state struct.private Fn_IntPtr dataHandler = new Fn_IntPtr(pointer => { var data = Marshal.PtrToStructure<data>(pointer); System.Diagnostics.Debug.WriteLine(Marshal.PtrToStructure<batterystate>(data.value)); });
You should also keep a reference to the delegate otherwise it may be garbage collected.