int.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `int`
 24  ====================================================
 25  
 26  This module provides integer characteristics that are usable directly as attributes.
 27  
 28  """
 29  
 30  from . import Attribute
 31  from . import StructCharacteristic
 32  
 33  __version__ = "0.0.0-auto.0"
 34  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
 35  
 36  
 37  class IntCharacteristic(StructCharacteristic):
 38      """Superclass for different kinds of integer fields."""
 39  
 40      def __init__(
 41          self,
 42          format_string,
 43          min_value,
 44          max_value,
 45          *,
 46          uuid=None,
 47          properties=0,
 48          read_perm=Attribute.OPEN,
 49          write_perm=Attribute.OPEN,
 50          initial_value=None
 51      ):
 52          self._min_value = min_value
 53          self._max_value = max_value
 54          if initial_value is not None:
 55              if not self._min_value <= initial_value <= self._max_value:
 56                  raise ValueError("initial_value out of range")
 57              initial_value = (initial_value,)
 58  
 59          super().__init__(
 60              format_string,
 61              uuid=uuid,
 62              properties=properties,
 63              read_perm=read_perm,
 64              write_perm=write_perm,
 65              initial_value=initial_value,
 66          )
 67  
 68      def __get__(self, obj, cls=None):
 69          if obj is None:
 70              return self
 71          return super().__get__(obj)[0]
 72  
 73      def __set__(self, obj, value):
 74          if not self._min_value <= value <= self._max_value:
 75              raise ValueError("out of range")
 76          super().__set__(obj, (value,))
 77  
 78  
 79  class Int8Characteristic(IntCharacteristic):
 80      """Int8 number."""
 81  
 82      # pylint: disable=too-few-public-methods
 83      def __init__(self, *, min_value=-128, max_value=127, **kwargs):
 84          super().__init__("<b", min_value, max_value, **kwargs)
 85  
 86  
 87  class Uint8Characteristic(IntCharacteristic):
 88      """Uint8 number."""
 89  
 90      # pylint: disable=too-few-public-methods
 91      def __init__(self, *, min_value=0, max_value=0xFF, **kwargs):
 92          super().__init__("<B", min_value, max_value, **kwargs)
 93  
 94  
 95  class Int16Characteristic(IntCharacteristic):
 96      """Int16 number."""
 97  
 98      # pylint: disable=too-few-public-methods
 99      def __init__(self, *, min_value=-32768, max_value=32767, **kwargs):
100          super().__init__("<h", min_value, max_value, **kwargs)
101  
102  
103  class Uint16Characteristic(IntCharacteristic):
104      """Uint16 number."""
105  
106      # pylint: disable=too-few-public-methods
107      def __init__(self, *, min_value=0, max_value=0xFFFF, **kwargs):
108          super().__init__("<H", min_value, max_value, **kwargs)
109  
110  
111  class Int32Characteristic(IntCharacteristic):
112      """Int32 number."""
113  
114      # pylint: disable=too-few-public-methods
115      def __init__(self, *, min_value=-2147483648, max_value=2147483647, **kwargs):
116          super().__init__("<i", min_value, max_value, **kwargs)
117  
118  
119  class Uint32Characteristic(IntCharacteristic):
120      """Uint32 number."""
121  
122      # pylint: disable=too-few-public-methods
123      def __init__(self, *, min_value=0, max_value=0xFFFFFFFF, **kwargs):
124          super().__init__("<I", min_value, max_value, **kwargs)