Ga naar inhoud

io_pin

Pin #

Bases: IntEnum

De pins beschikbaar voor configuratie en uitlezen via de io pin service.

Pins 17 en 18 zijn niet beschikbaar

Source code in src/kaspersmicrobit/services/io_pin.py
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
class Pin(IntEnum):
    """
    The pins available for configuration and reading via the io pin service.

    Pins 17 and 18 are not available
    """
    P0 = 0
    P1 = 1
    P2 = 2
    P3 = 3
    P4 = 4
    P5 = 5
    P6 = 6
    P7 = 7
    P8 = 8
    P9 = 9
    P10 = 10
    P11 = 11
    P12 = 12
    P13 = 13
    P14 = 14
    P15 = 15
    P16 = 16
    P19 = 17
    P20 = 18

PinAD #

Bases: Enum

De analoog-digitaal configuratie van een pin

Source code in src/kaspersmicrobit/services/io_pin.py
40
41
42
43
44
45
class PinAD(Enum):
    """The analog-digital configuration of a pin"""
    DIGITAL = 0
    """Indicates that a pin is used for a digital signal"""
    ANALOG = 1
    """Indicates that a pin is used for an analog signal"""

DIGITAL class-attribute instance-attribute #

DIGITAL = 0

Geeft aan dat een pin wordt gebruikt voor een digitaal signaal

ANALOG class-attribute instance-attribute #

ANALOG = 1

Geeft aan dat een pin wordt gebruikt voor een analoog signaal

PinIO #

Bases: Enum

De input-output configuratie van een pin

Source code in src/kaspersmicrobit/services/io_pin.py
48
49
50
51
52
53
class PinIO(Enum):
    """The input-output configuration of a pin"""
    OUTPUT = 0
    """Indicates that a pin is being used to read from"""
    INPUT = 1
    """Indicates that a pin is being used to write to"""

OUTPUT class-attribute instance-attribute #

OUTPUT = 0

Geeft aan dat een pin wordt gebruikt om uit te lezen

INPUT class-attribute instance-attribute #

INPUT = 1

Geeft aan dat een pin wordt gebruikt om naar te schrijven

PinIOConfiguration #

Bases: PinConfiguration[PinIO]

De pin IO configuratie. Bevat voor iedere pin of die voor INPUT of OUTPUT gebruikt wordt

Source code in src/kaspersmicrobit/services/io_pin.py
 94
 95
 96
 97
 98
 99
100
101
102
103
class PinIOConfiguration(PinConfiguration[PinIO]):
    """
    The IO pin configuration. Contains for each pin whether it is used for INPUT or OUTPUT
    """
    def __init__(self, configuration: List[T] = None):
        super(PinIOConfiguration, self).__init__(PinIO, configuration=configuration)

    @staticmethod
    def from_bytes(value: ByteData) -> 'PinIOConfiguration':
        return PinIOConfiguration(PinConfiguration.list_from_bytes(PinIO, value))

PinADConfiguration #

Bases: PinConfiguration[PinAD]

De pin AD configuratie. Bevat voor iedere pin of die voor ANALOG of DIGITAL gebruik is

Source code in src/kaspersmicrobit/services/io_pin.py
106
107
108
109
110
111
112
113
114
115
class PinADConfiguration(PinConfiguration[PinAD]):
    """
    The AD pin configuration. Contains for each pin whether it is for ANALOG or DIGITAL use
    """
    def __init__(self, configuration: List[T] = None):
        super(PinADConfiguration, self).__init__(PinAD, configuration=configuration)

    @staticmethod
    def from_bytes(value: ByteData) -> 'PinADConfiguration':
        return PinADConfiguration(PinADConfiguration.list_from_bytes(PinAD, value))

PinValue dataclass #

De pin en zijn waarde

Attributes:

  • pin (Pin) –

    de pin

  • value (int) –

    de waarde van de pin (door hoe deze waarde verzonden wordt over bluetooth) verliest de waarde precisie (de minst significante 2 bits worden niet verzonden) dit betekent bvb dat een waarde 1-3 als 0 en 255 als 252 wordt verzonden.

Source code in src/kaspersmicrobit/services/io_pin.py
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
@dataclass
class PinValue:
    """
    The pin and its value

    Attributes:
        pin (Pin): the pin
        value (int): the value of the pin (given how this value is sent over Bluetooth)
            the value loses precision (the least significant 2 bits are not sent)
            This means, for example, that a value 1-3 is sent as 0 and 255 as 252.
    """
    pin: Pin
    value: int

    @staticmethod
    def from_bytes(ad_config: PinADConfiguration, values: ByteData) -> 'PinValue':
        pin = Pin(int.from_bytes(values[0:1], "little"))
        compressed_value = int.from_bytes(values[1:2], "little")
        decompressed_value = compressed_value if ad_config[pin] == PinAD.DIGITAL else compressed_value << 2
        return PinValue(pin, decompressed_value)

    def to_bytes(self, ad_config: PinADConfiguration) -> bytes:
        compressed_value = self.value if ad_config[self.pin] == PinAD.DIGITAL else (self.value >> 2)
        return self.pin.value.to_bytes(1, "little") + compressed_value.to_bytes(1, "little")

    @staticmethod
    def list_from_bytes(ad_config: PinADConfiguration, values: ByteData) -> List['PinValue']:
        result = []
        for i in range(0, len(values), 2):
            result.append(PinValue.from_bytes(ad_config, values[i:i+2]))

        return result

    @staticmethod
    def list_to_bytes(ad_config: PinADConfiguration, values: List['PinValue']) -> bytes:
        result = bytes()
        for pin_value in values:
            result += pin_value.to_bytes(ad_config)

        return result

PwmControlData dataclass #

Een klasse om PWM opdrachten te geven aan de micro:bit

Attributes:

  • pin (Pin) –

    de pin waar de opdracht voor dient

  • value (int) –

    een waarde in het bereik (0-1024) (0 betekent uitgeschakeld)

  • period (int) –

    de periode in microseconden

Source code in src/kaspersmicrobit/services/io_pin.py
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
@dataclass
class PwmControlData:
    """
    A class to give PWM commands to the micro:bit

    Attributes:
        pin (Pin): the pin for which the command is intended
        value (int): a value in the range (0-1024) (0 means disabled)
        period (int): the period in microseconds
    """
    pin: Pin
    value: int
    period: int

    @staticmethod
    def from_bytes(values: ByteData) -> 'PwmControlData':
        return PwmControlData(
            Pin(int.from_bytes(values[0:1], "little")),
            int.from_bytes(values[1:3], "little"),
            int.from_bytes(values[3:7], "little"),
        )

    def to_bytes(self) -> bytes:
        return self.pin.value.to_bytes(1, "little") \
               + self.value.to_bytes(2, "little") \
               + self.period.to_bytes(4, "little")

IOPinService #

Deze klasse bevat de functies die je kan aanspreken in verband met de io pins op de rand van de micro:bit

Dit zijn alle mogelijkheden aangeboden door de bluetooth io pin service

Zie ook: https://tech.microbit.org/hardware/edgeconnector/

Zie ook: https://www.micro-bit.nl/kennisbank-pinnen

Zie ook: https://lancaster-university.github.io/microbit-docs/ble/iopin-service/

Source code in src/kaspersmicrobit/services/io_pin.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
class IOPinService:
    """
    This class contains the functions that you can access in connection with the io pins on the edge of the micro:bit

    These are all options offered by the Bluetooth IO PIN service

    See Also: https://tech.microbit.org/hardware/edgeconnector/

    See Also: https://www.micro-bit.nl/kennisbank-pinnen

    See Also: https://lancaster-university.github.io/microbit-docs/ble/iopin-service/
    """

    def __init__(self, device: BluetoothDevice):
        self._pin_ad_config = PinADConfiguration()
        self._device = device

    def is_available(self) -> bool:
        """
        Checks whether the I/O pin Bluetooth service is found on the connected micro:bit.

        Returns:
            true if the I/O pin service was found, false if not.
        """
        return self._device.is_service_available(Service.IO_PIN)

    def notify_data(self, callback: Callable[[List[PinValue]], None]):
        """
        You can call this method when you want to be notified of the value of pins. You need these pins
        previously configured as PinIO.INPUT pins via write_io_configuration. You will be notified when
        the value changes.

        Args:
            callback: a function called with a list of PinValue objects

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to activate the notifications for the PIN data (normally does not occur)
        """
        self._device.notify(Service.IO_PIN, Characteristic.PIN_DATA,
                            lambda sender, data: callback(PinValue.list_from_bytes(self._pin_ad_config, data)))

    def read_data(self) -> List[PinValue]:
        """
        Returns the values for each pin configured as PinIO.INPUT via write_io_configuration.

        Returns:
            A list of input pins and their associated values

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to read the pin data (normally does not occur)
        """
        return PinValue.list_from_bytes(self._pin_ad_config, self._device.read(Service.IO_PIN, Characteristic.PIN_DATA))

    def write_data(self, values: List[PinValue]):
        """
        Writes values to 1 or more pins. You must have previously configured these pins as PinIO.OUTPUT pins
        via write_io_configuration. When you write to an input pin it will be ignored

        Args:
            values (List[PinValue]): the output pins and their associated values

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to write the pin data (normally does not occur)
        """
        if values:
            self._device.write(Service.IO_PIN, Characteristic.PIN_DATA,
                               PinValue.list_to_bytes(self._pin_ad_config, values))

    def read_ad_configuration(self) -> PinADConfiguration:
        """
        Returns for each pin whether it is configured as a PinAD.DIGITAL or PinAD.ANALOG pin.

        Returns:
            The analog-digital configuration for each pin

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to read the pin analog-digital configuration (normally does not occur)
        """
        return PinADConfiguration.from_bytes(self._device.read(Service.IO_PIN, Characteristic.PIN_AD_CONFIGURATION))

    def write_ad_configuration(self, config: PinADConfiguration):
        """
        Configure each pin for analog or digital use.

        Args:
            config (PinADConfiguration): The analog-digital configuration for each pin

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to write the analog-digital configuration (normally not present)
        """
        self._device.write(Service.IO_PIN, Characteristic.PIN_AD_CONFIGURATION, config.to_bytes())
        self._pin_ad_config = PinADConfiguration(config[:])

    def read_io_configuration(self) -> PinIOConfiguration:
        """
        Returns for each pin whether it is configured as a PinIO.INPUT or PinIO.OUTPUT pin.

        Returns:
            The input-output configuration for each pin

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to read the input-output configuration (normally does not occur)
        """
        return PinIOConfiguration.from_bytes(self._device.read(Service.IO_PIN, Characteristic.PIN_IO_CONFIGURATION))

    def write_io_configuration(self, config: PinIOConfiguration):
        """
        Configure each pin for input or output.
        Only values of pins configured as PinIO.INPUT can be read with read_data,
        and for these pins only you can get updates with notify_data.
        You can write values only to pins configured as PinIO.OUTPUT.

        Args:
            config (PinIOConfiguration): The input-output configuration for each pin

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to write the input-output configuration (normally not present)
        """
        self._device.write(Service.IO_PIN, Characteristic.PIN_IO_CONFIGURATION, config.to_bytes())

    def write_pwm_control_data(self, pwm_control1: PwmControlData, pwm_control2: PwmControlData = None):
        """
        Writes Pulse Width Modulation commands to the micro:bit. 1 or 2 assignments can be given at the same time

        See Also: https://microbit-micropython.readthedocs.io/en/latest/pin.html

        See Also: https://www.micro-bit.nl/kennisbank-pinnen

        Args:
            pwm_control1 (PwmControlData): a PWM command
            pwm_control2 (PwmControlData): an optional PWM command

        Raises:
            errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
                to write the pwm control data (normally does not occur)
        """
        data = pwm_control1.to_bytes() + pwm_control2.to_bytes() if pwm_control2 else pwm_control1.to_bytes()
        self._device.write(Service.IO_PIN, Characteristic.PWM_CONTROL, data)

is_available #

is_available() -> bool

Kijkt na of de I/O pin bluetooth service gevonden wordt op de geconnecteerde micro:bit.

Returns:

  • bool

    true als de I/O pin service gevonden werd, false indien niet.

Source code in src/kaspersmicrobit/services/io_pin.py
205
206
207
208
209
210
211
212
def is_available(self) -> bool:
    """
    Checks whether the I/O pin Bluetooth service is found on the connected micro:bit.

    Returns:
        true if the I/O pin service was found, false if not.
    """
    return self._device.is_service_available(Service.IO_PIN)

notify_data #

notify_data(callback: Callable[[List[PinValue]], None])

Deze methode kan je oproepen wanneer je verwittigd wil worden van de waarde van pins. Deze pins moet je voorafgaand geconfigureerd hebben als PinIO.INPUT pins vie write_io_configuration. Je wordt verwittigd wanneer de waarde wijzigt.

Parameters:

  • callback (Callable[[List[PinValue]], None]) –

    een functie die wordt opgeroepen met een lijst van PinValue objecten

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def notify_data(self, callback: Callable[[List[PinValue]], None]):
    """
    You can call this method when you want to be notified of the value of pins. You need these pins
    previously configured as PinIO.INPUT pins via write_io_configuration. You will be notified when
    the value changes.

    Args:
        callback: a function called with a list of PinValue objects

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to activate the notifications for the PIN data (normally does not occur)
    """
    self._device.notify(Service.IO_PIN, Characteristic.PIN_DATA,
                        lambda sender, data: callback(PinValue.list_from_bytes(self._pin_ad_config, data)))

read_data #

read_data() -> List[PinValue]

Geeft de waarden voor iedere pin die geconfigureerd is als PinIO.INPUT via write_io_configuration.

Returns:

  • List[PinValue]

    Een lijst van input pins en hun bijhorende waarde

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
231
232
233
234
235
236
237
238
239
240
241
242
243
def read_data(self) -> List[PinValue]:
    """
    Returns the values for each pin configured as PinIO.INPUT via write_io_configuration.

    Returns:
        A list of input pins and their associated values

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to read the pin data (normally does not occur)
    """
    return PinValue.list_from_bytes(self._pin_ad_config, self._device.read(Service.IO_PIN, Characteristic.PIN_DATA))

write_data #

write_data(values: List[PinValue])

Schrijft waarden naar 1 of meer pins. Deze pins moet je voorafgaand geconfigureerd hebben als PinIO.OUTPUT pins via write_io_configuration. Wanneer jes chrijft naar een input pin zal dit genegeerd worden

Parameters:

  • values (List[PinValue]) –

    de output pins en hun bijhorende waarde

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def write_data(self, values: List[PinValue]):
    """
    Writes values to 1 or more pins. You must have previously configured these pins as PinIO.OUTPUT pins
    via write_io_configuration. When you write to an input pin it will be ignored

    Args:
        values (List[PinValue]): the output pins and their associated values

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to write the pin data (normally does not occur)
    """
    if values:
        self._device.write(Service.IO_PIN, Characteristic.PIN_DATA,
                           PinValue.list_to_bytes(self._pin_ad_config, values))

read_ad_configuration #

read_ad_configuration() -> PinADConfiguration

Geeft voor iedere pin of die geconfigureerd is als een PinAD.DIGITAL of PinAD.ANALOG pin.

Returns:

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
262
263
264
265
266
267
268
269
270
271
272
273
274
def read_ad_configuration(self) -> PinADConfiguration:
    """
    Returns for each pin whether it is configured as a PinAD.DIGITAL or PinAD.ANALOG pin.

    Returns:
        The analog-digital configuration for each pin

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to read the pin analog-digital configuration (normally does not occur)
    """
    return PinADConfiguration.from_bytes(self._device.read(Service.IO_PIN, Characteristic.PIN_AD_CONFIGURATION))

write_ad_configuration #

write_ad_configuration(config: PinADConfiguration)

Configureer iedere pin voor analoog of digitaal gebruik.

Parameters:

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
def write_ad_configuration(self, config: PinADConfiguration):
    """
    Configure each pin for analog or digital use.

    Args:
        config (PinADConfiguration): The analog-digital configuration for each pin

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to write the analog-digital configuration (normally not present)
    """
    self._device.write(Service.IO_PIN, Characteristic.PIN_AD_CONFIGURATION, config.to_bytes())
    self._pin_ad_config = PinADConfiguration(config[:])

read_io_configuration #

read_io_configuration() -> PinIOConfiguration

Geeft voor iedere pin of die geconfigureerd is als een PinIO.INPUT of PinIO.OUTPUT pin.

Returns:

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
291
292
293
294
295
296
297
298
299
300
301
302
303
def read_io_configuration(self) -> PinIOConfiguration:
    """
    Returns for each pin whether it is configured as a PinIO.INPUT or PinIO.OUTPUT pin.

    Returns:
        The input-output configuration for each pin

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to read the input-output configuration (normally does not occur)
    """
    return PinIOConfiguration.from_bytes(self._device.read(Service.IO_PIN, Characteristic.PIN_IO_CONFIGURATION))

write_io_configuration #

write_io_configuration(config: PinIOConfiguration)

Configureer iedere pin voor input of output. Enkel waarden van pins die als PinIO.INPUT geconfigureerd worden kunnen uitgelezen worden met read_data, en enkel voor deze pins kan je updates krijgen met notify_data. Enkel naar pins die geconfigureerd zijn als PinIO.OUTPUT kan je waarden schrijven.

Parameters:

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
def write_io_configuration(self, config: PinIOConfiguration):
    """
    Configure each pin for input or output.
    Only values of pins configured as PinIO.INPUT can be read with read_data,
    and for these pins only you can get updates with notify_data.
    You can write values only to pins configured as PinIO.OUTPUT.

    Args:
        config (PinIOConfiguration): The input-output configuration for each pin

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to write the input-output configuration (normally not present)
    """
    self._device.write(Service.IO_PIN, Characteristic.PIN_IO_CONFIGURATION, config.to_bytes())

write_pwm_control_data #

write_pwm_control_data(
    pwm_control1: PwmControlData,
    pwm_control2: PwmControlData = None,
)

Schrijft Pulse Width Modulation opdrachten naar de micro:bit. 1 of 2 opdrachten kunnen tegelijk gegeven worden

Zie ook: https://microbit-micropython.readthedocs.io/en/latest/pin.html

Zie ook: https://www.micro-bit.nl/kennisbank-pinnen

Parameters:

Raises:

Source code in src/kaspersmicrobit/services/io_pin.py
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
def write_pwm_control_data(self, pwm_control1: PwmControlData, pwm_control2: PwmControlData = None):
    """
    Writes Pulse Width Modulation commands to the micro:bit. 1 or 2 assignments can be given at the same time

    See Also: https://microbit-micropython.readthedocs.io/en/latest/pin.html

    See Also: https://www.micro-bit.nl/kennisbank-pinnen

    Args:
        pwm_control1 (PwmControlData): a PWM command
        pwm_control2 (PwmControlData): an optional PWM command

    Raises:
        errors.BluetoothServiceNotFound: When the I/O pin service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the I/O pin service is active but there was no way
            to write the pwm control data (normally does not occur)
    """
    data = pwm_control1.to_bytes() + pwm_control2.to_bytes() if pwm_control2 else pwm_control1.to_bytes()
    self._device.write(Service.IO_PIN, Characteristic.PWM_CONTROL, data)