Skip to content

Commit 0cd18cf

Browse files
oraumqdot
authored andcommitted
feat: add battery reading for wevibe devices
1 parent 23988bb commit 0cd18cf

4 files changed

Lines changed: 154 additions & 4 deletions

File tree

crates/buttplug_server/src/device/protocol_impl/wevibe.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// for full license information.
77

88
use crate::device::{
9-
hardware::{Hardware, HardwareCommand, HardwareWriteCmd},
9+
hardware::{Hardware, HardwareCommand, HardwareReadCmd, HardwareWriteCmd},
1010
protocol::{
1111
ProtocolHandler,
1212
ProtocolIdentifier,
@@ -16,13 +16,14 @@ use crate::device::{
1616
};
1717
use async_trait::async_trait;
1818
use buttplug_core::errors::ButtplugDeviceError;
19-
use buttplug_core::message::OutputType;
19+
use buttplug_core::message::{InputReadingV4, InputTypeReading, InputValue, OutputType};
2020
use buttplug_server_device_config::Endpoint;
2121
use buttplug_server_device_config::{
2222
ProtocolCommunicationSpecifier,
2323
ServerDeviceDefinition,
2424
UserDeviceIdentifier,
2525
};
26+
use futures_util::{FutureExt, future::BoxFuture};
2627
use std::sync::Arc;
2728
use std::sync::atomic::{AtomicU8, Ordering};
2829
use uuid::{Uuid, uuid};
@@ -113,4 +114,41 @@ impl ProtocolHandler for WeVibe {
113114
HardwareWriteCmd::new(&[WEVIBE_PROTOCOL_UUID], Endpoint::Tx, data, true).into(),
114115
])
115116
}
117+
118+
fn handle_battery_level_cmd(
119+
&self,
120+
device_index: u32,
121+
device: Arc<Hardware>,
122+
feature_index: u32,
123+
feature_id: Uuid,
124+
) -> BoxFuture<'_, Result<InputReadingV4, ButtplugDeviceError>> {
125+
debug!("Trying to get battery reading.");
126+
let fut = device.read_value(&HardwareReadCmd::new(feature_id, Endpoint::Rx, 8, 500));
127+
async move {
128+
let hw_msw = fut.await?;
129+
let data = hw_msw.data();
130+
if data.len() != 8 {
131+
return Err(ButtplugDeviceError::DeviceCommunicationError(
132+
"Wevibe battery data not expected length!".to_owned(),
133+
));
134+
}
135+
// based on https://gist.github.com/bnm12/fcdcef291a500bf51cef734aa1830e4d
136+
let xx = data[3] as i32;
137+
let zz = data[2] as i32;
138+
let mut y = (xx << 8) + zz;
139+
if y < 0 {
140+
y += 0x10000;
141+
}
142+
let battery_level = y * 100 / 0xFFFF;
143+
144+
let battery_reading = InputReadingV4::new(
145+
device_index,
146+
feature_index,
147+
InputTypeReading::Battery(InputValue::new(battery_level as u8)),
148+
);
149+
debug!("Got battery reading: {}", battery_level);
150+
Ok(battery_reading)
151+
}
152+
.boxed()
153+
}
116154
}

crates/buttplug_server_device_config/build-config/buttplug-device-config-v4.json

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"version": {
33
"major": 4,
4-
"minor": 176
4+
"minor": 177
55
},
66
"protocols": {
77
"activejoy": {
@@ -22321,6 +22321,24 @@
2232122321
]
2232222322
}
2232322323
}
22324+
},
22325+
{
22326+
"description": "battery Level",
22327+
"id": "92acc28c-5c54-4c61-be26-d9d8443288e3",
22328+
"index": 2,
22329+
"input": {
22330+
"battery": {
22331+
"command": [
22332+
"Read"
22333+
],
22334+
"value": [
22335+
[
22336+
0,
22337+
100
22338+
]
22339+
]
22340+
}
22341+
}
2232422342
}
2232522343
],
2232622344
"id": "11cd7b68-2c94-4fc8-837f-09d47214cee1",
@@ -22359,6 +22377,24 @@
2235922377
]
2236022378
}
2236122379
}
22380+
},
22381+
{
22382+
"description": "battery Level",
22383+
"id": "1a971255-fcda-446f-9a94-d1f400e0e8b9",
22384+
"index": 2,
22385+
"input": {
22386+
"battery": {
22387+
"command": [
22388+
"Read"
22389+
],
22390+
"value": [
22391+
[
22392+
0,
22393+
100
22394+
]
22395+
]
22396+
}
22397+
}
2236222398
}
2236322399
],
2236422400
"id": "400ef30a-63eb-4648-b293-c7ecc874f509",
@@ -22392,6 +22428,24 @@
2239222428
]
2239322429
}
2239422430
}
22431+
},
22432+
{
22433+
"description": "battery Level",
22434+
"id": "cdf9b997-2430-4120-b836-3af64a9ad170",
22435+
"index": 2,
22436+
"input": {
22437+
"battery": {
22438+
"command": [
22439+
"Read"
22440+
],
22441+
"value": [
22442+
[
22443+
0,
22444+
100
22445+
]
22446+
]
22447+
}
22448+
}
2239522449
}
2239622450
],
2239722451
"id": "b667bb6a-46b1-4534-8c79-83aa0749028a",
@@ -22425,6 +22479,24 @@
2242522479
]
2242622480
}
2242722481
}
22482+
},
22483+
{
22484+
"description": "battery Level",
22485+
"id": "7c0cfd3f-6bd1-4081-95b1-1a6d53b3c2d3",
22486+
"index": 2,
22487+
"input": {
22488+
"battery": {
22489+
"command": [
22490+
"Read"
22491+
],
22492+
"value": [
22493+
[
22494+
0,
22495+
100
22496+
]
22497+
]
22498+
}
22499+
}
2242822500
}
2242922501
],
2243022502
"id": "0e72dab3-4b87-4bae-ae02-aae0bbb0f035",

crates/buttplug_server_device_config/device-config-v4/protocols/wevibe.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ configurations:
6262
- 0
6363
- 15
6464
index: 1
65+
- description: battery Level
66+
id: 92acc28c-5c54-4c61-be26-d9d8443288e3
67+
input:
68+
battery:
69+
value:
70+
- - 0
71+
- 100
72+
command:
73+
- Read
74+
index: 2
6575
id: 11cd7b68-2c94-4fc8-837f-09d47214cee1
6676
- identifier:
6777
- Gala
@@ -81,6 +91,16 @@ configurations:
8191
- 0
8292
- 15
8393
index: 1
94+
- description: battery Level
95+
id: 1a971255-fcda-446f-9a94-d1f400e0e8b9
96+
input:
97+
battery:
98+
value:
99+
- - 0
100+
- 100
101+
command:
102+
- Read
103+
index: 2
84104
id: 400ef30a-63eb-4648-b293-c7ecc874f509
85105
- identifier:
86106
- Nova
@@ -100,6 +120,16 @@ configurations:
100120
- 0
101121
- 15
102122
index: 1
123+
- description: battery Level
124+
id: cdf9b997-2430-4120-b836-3af64a9ad170
125+
input:
126+
battery:
127+
value:
128+
- - 0
129+
- 100
130+
command:
131+
- Read
132+
index: 2
103133
id: b667bb6a-46b1-4534-8c79-83aa0749028a
104134
- identifier:
105135
- Sync
@@ -119,6 +149,16 @@ configurations:
119149
- 0
120150
- 15
121151
index: 1
152+
- description: battery Level
153+
id: 7c0cfd3f-6bd1-4081-95b1-1a6d53b3c2d3
154+
input:
155+
battery:
156+
value:
157+
- - 0
158+
- 100
159+
command:
160+
- Read
161+
index: 2
122162
id: 0e72dab3-4b87-4bae-ae02-aae0bbb0f035
123163
communication:
124164
- btle:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version:
22
major: 4
3-
minor: 176
3+
minor: 177

0 commit comments

Comments
 (0)