Beispiel #1
0
class FileTransferService(Service):
    """Simple (not necessarily fast) BLE file transfer service. It implements basic CRUD operations.

    The server dictates data transfer chunk sizes so it can minimize buffer sizes on its end.
    """

    uuid = StandardUUID(0xfebb)
    version = Uint32Characteristic(uuid=FileTransferUUID(0x0100))
    raw = _TransferCharacteristic()
    # _raw gets shadowed for each MIDIService instance by a PacketBuffer. PyLint doesn't know this
    # so it complains about missing members.
    # pylint: disable=no-member

    # Actions
    INVALID = 0x00
    READ = 0x01
    WRITE = 0x02
    DELETE = 0x03
    MKDIR = 0x04
    LISTDIR = 0x05

    # Responses
    # 0x00 is INVALID
    OK = 0x81
    ERR = 0x82

    ERR_NO_FILE = 0xb0

    DIRECTORY = 0x01
Beispiel #2
0
 def service_version_charac(cls, version=1):
     """Create a service_version Characteristic for use by a subclass."""
     return Uint32Characteristic(
         uuid=cls.adafruit_service_uuid(0x0002),
         properties=Characteristic.READ,
         write_perm=Attribute.NO_ACCESS,
         initial_value=version,
     )
Beispiel #3
0
class ButtonService(AdafruitService):
    """Status of buttons and switches on the board."""

    uuid = AdafruitService.adafruit_service_uuid(0x600)
    pressed = Uint32Characteristic(
        uuid=AdafruitService.adafruit_service_uuid(0x601),
        properties=(Characteristic.READ | Characteristic.NOTIFY),
        read_perm=Attribute.OPEN,
        write_perm=Attribute.NO_ACCESS,
    )
    """
    bit 0: slide switch: 1 for left; 0 for right
    bit 1: 1 if button A is pressed
    bit 2: 1 if button B is pressed
    other bits are available for future buttons and switches
    """
    measurement_period = AdafruitService.measurement_period_charac(0)
    """Initially 0: send notification only on changes. -1 means stop reading."""

    def set_pressed(self, switch, button_a, button_b):
        """Update the pressed value all at once."""
        pressed = 0
        if switch:
            pressed |= 0x1
        if button_a:
            pressed |= 0x2
        if button_b:
            pressed |= 0x4
        if pressed != self.pressed:
            self.pressed = pressed

    @property
    def switch(self):
        """``True`` when the slide switch is set to the left; ``False`` when to the right."""
        return bool(self.pressed & 0x1)

    @property
    def button_a(self):
        """``True`` when Button A is pressed."""
        return bool(self.pressed & 0x2)

    @property
    def button_b(self):
        """``True`` when Button B is pressed."""
        return bool(self.pressed & 0x4)