-
Notifications
You must be signed in to change notification settings - Fork 0
/
brush.py
244 lines (194 loc) · 5.96 KB
/
brush.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Copyright (C) MatrixEditor 2023
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import enum
from typing import List
from caterpillar.model import struct
from caterpillar.shortcuts import ctx, F, this, LittleEndian
from caterpillar.fields import uint8, Enum, uint32
from .advertise import ProtocolVersion
from .model import characteristic, DeviceState, Pressure
from .model import CH_BRUSHING_MODE, CH_BRUSHING_TIME
from .model import CH_QUADRANT, CH_DEVICE_ID, CH_DEVICE_INFO
from .model import CH_BRUSH_MODES
class BrushType(enum.IntEnum):
__struct__ = uint8
UNKNOWN = 153
EXPERIMENTAL = 255
D36_EXPERIMENTAL = 63
D36_X_MODE = 0
D36_6_MODE = 1
D36_5_MODE = 2
D21_EXPERIMENTAL = 127
D21_X_MODE = 64
D21_4_MODE = 65
D21_3_MODE = 66
D21_3_MODE_WHITENING = 69
D21_2A_MODE = 67
D21_2B_MODE = 68
D21_1_MODE = 70
D706_X_MODE = 112
D706_6_MODE = 113
D706_5_MODE = 114
D706_X_MODE_CHINA = 117
D706_6_MODE_CHINA = 118
D706_5_MODE_CHINA = 119
D701_X_MODE = 32
D701_6_MODE = 33
D701_5_MODE = 34
D700_5_MODE = 39
D700_4_MODE = 40
D700_6_MODE = 41
D601_X_MODE = 80
D601_5_MODE = 81
D601_4_MODE = 82
D601_3A_MODE = 83
D601_2A_MODE = 84
D601_2B_MODE = 85
D601_3B_MODE = 86
D601_1_MODE = 87
SONOS_X_MODE = 48
SONOS = 49 # SONOS IO
SONOS_BIG_TI = 50 # SONOS IO (BIG TI)
SONOS_G4 = 52 # SONOS GALAXY (IO 4)
SONOS_G5 = 53 # SONOS GALAXY (IO 5)
SONOS_EPLATFORM = 54
class BrushStatus(enum.IntEnum):
__struct__ = uint8
UNKNOWN = 0xFF
NOT_CONNECTED = 0
PRE_RUN = 1
IDLE = 2
CHARGING = 3
RUN = 4
# the following codes were taken from
# https://github.com/wise86-android/OralBlue_python/blob/master/OralBlue/BrushState.py
SETUP = 0x05
FLIGHT_MENU = 0x06
FINAL_TEST = 0x71
PCB_TEST = 0x72
SLEEP = 0x73
TRANSPORT = 0x74
class Mode(enum.IntEnum):
__struct__ = uint8
UNKNOWN = 0xFF
OFF = 0
DAILY_CLEAN = 1
SENSITIVE = 2
MASSAGE = 3
WHITENING = 4
DEEP_CLEAN = 5
TONGUE_CLEAN = 6
PRO_CLEAN = 7
class V006Mode(enum.IntEnum):
__struct__ = uint8
UNKNOWN = 0xFF
V006_CLEAN = 0
V006_SOFT = 1
V006_MASSAGE = 2
V006_POLISH = 3
V006_TURBO = 4
V006_SOFT_PLUS = 5
V006_TONGUE = 6
V006_OFF = 7
V006_SETTINGS = 8
class Quadrant(enum.IntEnum):
__struct__ = uint8
UNKNOWN = 0xFE
FIRST_QUADRANT = 0
SECOND_QUADRANT = 1
THIRD_QUADRANT = 2
FOURTH_QUADRANT = 3
FIFTH_QUADRANT = 4
SIXTH_QUADRANT = 5
SEVENTH_QUADRANT = 6
EIGHTH_QUADRANT = 7
LAST_QUADRANT = 0xFF
NO_QUADRANTS_DEFINED = 0xF0
@characteristic(CH_BRUSHING_TIME, "brushing_time")
@struct(kw_only=False)
class BrushingTime:
minutes: uint8
seconds: uint8
def _brush_mode_fn(value, context):
if value >= 6 or value == 0:
return F(Enum(V006Mode, uint8))
return F(Enum(Mode, uint8))
@characteristic(CH_BRUSHING_MODE, "brushing_mode")
@struct(kw_only=False)
class BrushingMode:
mode: F(ctx._root.protocol) >> _brush_mode_fn
@characteristic(CH_QUADRANT, "toothbrush_quadrant")
@struct(kw_only=False)
class ToothbrushQuadrant:
quadrant: Quadrant
num_quadrants: uint8
@characteristic(CH_BRUSH_MODES, "brush_modes")
@struct(kw_only=False)
class BrushModes:
modes: uint8[8]
def get_modes(self) -> List[Mode]:
return list(map(Mode, self.modes))
@characteristic(CH_DEVICE_INFO, "brush_info")
@struct(kw_only=False)
class BrushInfo:
type: BrushType
protocol: ProtocolVersion
version: uint8
@characteristic(CH_DEVICE_ID, "brush_id")
@struct(kw_only=False, order=LittleEndian)
class BrushID:
id: uint32
def _brush_status_fn(value: ProtocolVersion, context):
if value <= 5:
# An advertisement before V006 contains
# the current pressure state.
return Enum(Pressure.State, uint8)
return Enum(BrushStatus, uint8)
@struct
class BrushAdvertisement:
"""Bluetooth Low Energy advertise sent from oral-b devices."""
#: The used protocol version. Note that the packet structure
#: may change according to the version configuration
protocol: ProtocolVersion
#: the device type (this value conforms to a model identifier
#: and is not the model name)
type: BrushType
#: the current firmware version
version: uint8
#: Describes the current state of the device.
state: DeviceState.State
#: In packets using protocol version below V006, this value
#: describes the current pressure measurement. Otherwise,
#: another device status is set.
status: F(this.protocol) >> _brush_status_fn
#: Defines the current brushing time in minutes
brush_time_min: uint8
#: Defines the current brushing time in seconds
brush_time_sec: uint8
#: Specifies the currently used brushing mode. The used enumeration
#: varies based on the current protocol version.
brush_mode: F(this.protocol) >> _brush_mode_fn
#: This field displays the current brushing progress (no real
#: use-case?)
brush_progress: uint8
#: describes, which quadrant is currently processed (only during
#: a brushing session)
quadrant_completion: Quadrant
#: This fields specifies either the amount of quadrants or (only in
#: V008) a device substate.
total_quadrants: uint8
# TODO:
# @property
# def pressure(self) -> Pressure: ...