Ga naar inhoud

temperature

TemperatureService #

Deze klasse bevat de functies die je kan aanspreken in verband met de temperatuur sensor van de micro:bit Deze sensor meet de temperatuur van de micro:bit in graden Celcius.

Dit zijn alle mogelijkheden aangeboden door de bluetooth temperature service

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

Source code in src/kaspersmicrobit/services/temperature.py
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
class TemperatureService:
    """
    This class contains the functions that you can use in connection with the temperature sensor of the micro:bit
    This sensor measures the temperature of the micro:bit in degrees Celsius.

    These are all options offered by the Bluetooth temperature service

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

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

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

        Returns:
            true if the temperature service was found, false if not.
        """
        return self._device.is_service_available(Service.TEMPERATURE)

    def notify(self, callback: Callable[[int], None]):
        """
        You can call this method whenever you want to be notified of the temperature. How often you receive data
        depends on the period. By default the period is 1 second.

        Args:
            callback: a function that is called periodically with the temperature as an argument

        Raises:
            errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
                to activate temperature data notifications (normally does not occur)
        """
        self._device.notify(Service.TEMPERATURE, Characteristic.TEMPERATURE,
                            lambda sender, data: callback(int.from_bytes(data[0:1], 'little', signed=True)))

    def read(self) -> int:
        """
        Reads the temperature of the micro:bit.

        Returns:
            the temperature in degrees Celsius

        Raises:
            errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
                to read the temperature (normally does not occur)
        """
        return int.from_bytes(
            self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE)[0:1], 'little', signed=True)

    def set_period(self, period: int):
        """
        Sets the interval at which you receive the temperature if you requested it via the notify method
        By default the period is 1 second.

        Args:
            period (int): the interval at which you receive temperature data in milliseconds

        Raises:
            errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
                to write the temperature period (normally does not occur)
        """
        self._device.write(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD, period.to_bytes(2, "little"))

    def read_period(self) -> int:
        """
        Returns the interval at which you receive the temperature via notify

        Returns:
            The interval in milliseconds

        Raises:
            errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
            errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
                to read the temperature period (normally does not occur)
        """
        return int.from_bytes(
            self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD)[0:2], "little")

is_available #

is_available() -> bool

Kijkt na of de temperatuur bluetooth service gevonden wordt op de geconnecteerde micro:bit.

Returns:

  • bool

    true als de temperatuur service gevonden werd, false indien niet.

Source code in src/kaspersmicrobit/services/temperature.py
24
25
26
27
28
29
30
31
def is_available(self) -> bool:
    """
    Checks whether the temperature Bluetooth service is found on the connected micro:bit.

    Returns:
        true if the temperature service was found, false if not.
    """
    return self._device.is_service_available(Service.TEMPERATURE)

notify #

notify(callback: Callable[[int], None])

Deze methode kan je oproepen wanneer je verwittigd wil worden van de temperatuur. Hoe vaak je gegevens ontvangt hangt af van de periode. Standaard is de periode 1 seconde.

Parameters:

  • callback (Callable[[int], None]) –

    een functie die periodiek wordt opgeroepen met de temperatuur als argument

Raises:

  • BluetoothServiceNotFound

    Wanneer de temperatuur service niet actief is op de micro:bit

  • BluetoothCharacteristicNotFound

    Wanneer de temperatuur service actief is, maar er geen manier was om de notificaties van temperatuur data te activeren (komt normaal gezien niet voor)

Source code in src/kaspersmicrobit/services/temperature.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def notify(self, callback: Callable[[int], None]):
    """
    You can call this method whenever you want to be notified of the temperature. How often you receive data
    depends on the period. By default the period is 1 second.

    Args:
        callback: a function that is called periodically with the temperature as an argument

    Raises:
        errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
            to activate temperature data notifications (normally does not occur)
    """
    self._device.notify(Service.TEMPERATURE, Characteristic.TEMPERATURE,
                        lambda sender, data: callback(int.from_bytes(data[0:1], 'little', signed=True)))

read #

read() -> int

Leest de teperatuur van de micro:bit.

Returns:

  • int

    de temperatuur in graden Celcius

Raises:

Source code in src/kaspersmicrobit/services/temperature.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def read(self) -> int:
    """
    Reads the temperature of the micro:bit.

    Returns:
        the temperature in degrees Celsius

    Raises:
        errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
            to read the temperature (normally does not occur)
    """
    return int.from_bytes(
        self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE)[0:1], 'little', signed=True)

set_period #

set_period(period: int)

Stelt het interval in waarmee je de temperatuur ontvangt indien je dat gevraagd hebt via de notify methode Standaard is de periode 1 seconde.

Parameters:

  • period (int) –

    het interval waarmee je temperatuurgegevens ontvangt in milliseconden

Raises:

Source code in src/kaspersmicrobit/services/temperature.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def set_period(self, period: int):
    """
    Sets the interval at which you receive the temperature if you requested it via the notify method
    By default the period is 1 second.

    Args:
        period (int): the interval at which you receive temperature data in milliseconds

    Raises:
        errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
            to write the temperature period (normally does not occur)
    """
    self._device.write(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD, period.to_bytes(2, "little"))

read_period #

read_period() -> int

Geeft het interval terug waarmee je via notify de temperatuur ontvangt

Returns:

  • int

    Het interval in milliseconden

Raises:

Source code in src/kaspersmicrobit/services/temperature.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def read_period(self) -> int:
    """
    Returns the interval at which you receive the temperature via notify

    Returns:
        The interval in milliseconds

    Raises:
        errors.BluetoothServiceNotFound: When the temperature service is not active on the micro:bit
        errors.BluetoothCharacteristicNotFound: When the temperature service is active but there was no way
            to read the temperature period (normally does not occur)
    """
    return int.from_bytes(
        self._device.read(Service.TEMPERATURE, Characteristic.TEMPERATURE_PERIOD)[0:2], "little")