device: improve features array
This commit is contained in:
parent
ab9e06829a
commit
20a76fb4d3
|
@ -24,7 +24,7 @@ import threading as _threading
|
||||||
|
|
||||||
from struct import pack as _pack
|
from struct import pack as _pack
|
||||||
from struct import unpack as _unpack
|
from struct import unpack as _unpack
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
import yaml as _yaml
|
import yaml as _yaml
|
||||||
|
|
||||||
|
@ -247,14 +247,16 @@ class FeaturesArray(dict):
|
||||||
|
|
||||||
def __init__(self, device):
|
def __init__(self, device):
|
||||||
assert device is not None
|
assert device is not None
|
||||||
self.supported = True
|
self.supported = True # Actually don't know whether it is supported yet
|
||||||
self.device = device
|
self.device = device
|
||||||
self.inverse = {}
|
self.inverse = {}
|
||||||
self.version = {}
|
self.version = {}
|
||||||
self.count = 0
|
self.count = 0
|
||||||
|
|
||||||
def _check(self):
|
def _check(self) -> bool:
|
||||||
if self.supported is False or not self.device.online:
|
if not self.device.online:
|
||||||
|
return False
|
||||||
|
if self.supported is False:
|
||||||
return False
|
return False
|
||||||
if self.device.protocol and self.device.protocol < 2.0:
|
if self.device.protocol and self.device.protocol < 2.0:
|
||||||
self.supported = False
|
self.supported = False
|
||||||
|
@ -263,14 +265,14 @@ class FeaturesArray(dict):
|
||||||
return True
|
return True
|
||||||
reply = self.device.request(0x0000, _pack('!H', FEATURE.FEATURE_SET))
|
reply = self.device.request(0x0000, _pack('!H', FEATURE.FEATURE_SET))
|
||||||
if reply is not None:
|
if reply is not None:
|
||||||
fs_index = ord(reply[0:1])
|
fs_index = reply[0]
|
||||||
if fs_index:
|
if fs_index:
|
||||||
count = self.device.request(fs_index << 8)
|
count = self.device.request(fs_index << 8)
|
||||||
if count is None:
|
if count is None:
|
||||||
logger.warn('FEATURE_SET found, but failed to read features count')
|
logger.warn('FEATURE_SET found, but failed to read features count')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
self.count = ord(count[:1]) + 1 # ROOT feature not included in count
|
self.count = count[0] + 1 # ROOT feature not included in count
|
||||||
self[FEATURE.ROOT] = 0
|
self[FEATURE.ROOT] = 0
|
||||||
self[FEATURE.FEATURE_SET] = fs_index
|
self[FEATURE.FEATURE_SET] = fs_index
|
||||||
return True
|
return True
|
||||||
|
@ -278,7 +280,7 @@ class FeaturesArray(dict):
|
||||||
self.supported = False
|
self.supported = False
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_feature(self, index):
|
def get_feature(self, index: int) -> Optional[_NamedInt]:
|
||||||
feature = self.inverse.get(index)
|
feature = self.inverse.get(index)
|
||||||
if feature is not None:
|
if feature is not None:
|
||||||
return feature
|
return feature
|
||||||
|
@ -296,13 +298,15 @@ class FeaturesArray(dict):
|
||||||
feature = self.get_feature(index)
|
feature = self.get_feature(index)
|
||||||
yield feature, index
|
yield feature, index
|
||||||
|
|
||||||
def get_feature_version(self, feature):
|
def get_feature_version(self, feature: _NamedInt) -> Optional[int]:
|
||||||
if self[feature]:
|
if self[feature]:
|
||||||
return self.version.get(feature, 0)
|
return self.version.get(feature, 0)
|
||||||
|
|
||||||
__bool__ = __nonzero__ = _check
|
def __contains__(self, feature: _NamedInt) -> bool:
|
||||||
|
index = self.__getitem__(feature)
|
||||||
|
return index is not None and index is not False
|
||||||
|
|
||||||
def __getitem__(self, feature):
|
def __getitem__(self, feature: _NamedInt) -> Optional[int]:
|
||||||
index = super().get(feature)
|
index = super().get(feature)
|
||||||
if index is not None:
|
if index is not None:
|
||||||
return index
|
return index
|
||||||
|
@ -322,17 +326,13 @@ class FeaturesArray(dict):
|
||||||
self.inverse[index] = feature
|
self.inverse[index] = feature
|
||||||
|
|
||||||
def __delitem__(self, feature):
|
def __delitem__(self, feature):
|
||||||
if type(super().get(feature)) == int:
|
raise Exception("Don't delete features from FeatureArray")
|
||||||
self.inverse.pop(super().get(feature))
|
|
||||||
super().__delitem__(feature)
|
|
||||||
|
|
||||||
def __contains__(self, feature): # is a feature present
|
def __len__(self) -> int:
|
||||||
index = self.__getitem__(feature)
|
|
||||||
return index is not None and index is not False
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return self.count
|
return self.count
|
||||||
|
|
||||||
|
__bool__ = __nonzero__ = _check
|
||||||
|
|
||||||
|
|
||||||
class ReprogrammableKey:
|
class ReprogrammableKey:
|
||||||
"""Information about a control present on a device with the `REPROG_CONTROLS` feature.
|
"""Information about a control present on a device with the `REPROG_CONTROLS` feature.
|
||||||
|
|
Loading…
Reference in New Issue