summaryrefslogtreecommitdiff
path: root/wasp/apps/disaBLE.py
blob: 4293eed45502c3e045d041386e1265f4c8ffdfdd (plain)
1
2
3
4
5
6
7
8
9
10
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
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2021 Francesco Gazzetta

"""DisaBLE
~~~~~~~~~~

Disable BLE to save energy and enhance privacy.

This app shows the bluetooth status and provides a button to disable/enable it.
Unfortunately, re-enabling bluetooth normally has some issues, so as a
workaround the "enable" button restarts the watch.

.. figure:: res/DisaBLEApp.png
    :width: 179
"""

import wasp
import widgets
import ble

class DisaBLEApp():
    NAME = 'DisaBLE'
    # 1-bit RLE, 96x64, generated from res/disaBLE_icon.png, 167 bytes
    ICON = (
        96, 64,
        b'\xff\x00\xff\x00\xff\x00\xff\x00g\x02]\x03\\\x03\\\x03'
        b'\\\x03J\x01\x11\x03K\x02\x0f\x03L\x03\r\x03M\x04'
        b'\x0b\x03N\x05\t\x03O\x06\x07\x03P\x07\x05\x03Q\x03'
        b'\x01\x04\x03\x03L\x02\x04\x03\x02\x04\x01\x03L\x04\x03\x03'
        b'\x03\x06N\x04\x02\x03\x02\x06P\x04\x01\x03\x01\x06R\r'
        b'T\x0bV\tX\x07Y\x06Y\x07X\tV\x0bT\x08'
        b'\x01\x04R\t\x02\x04P\x06\x01\x03\x03\x04P\x04\x02\x03'
        b'\x02\x04Q\x03\x03\x03\x01\x04Q\x03\x04\x07Q\x03\x05\x06'
        b'Q\x03\x06\x05Q\x03\x07\x04Q\x03\x08\x03Q\x03\t\x02'
        b'Q\x03\n\x01Q\x03\\\x03\\\x03\\\x03]\x02\xff\x00'
        b'\xff\x00\xff\x00\xff\x00g'
    )


    def __init__(self):
        self._btn = widgets.Button(10, 120, 220, 80, 'Disable' if ble.enabled() else 'Reboot to enable')

    def foreground(self):
        self._draw()
        wasp.system.request_event(wasp.EventMask.TOUCH)

    def _draw(self):
        draw = wasp.watch.drawable
        draw.set_color(wasp.system.theme('bright'))
        draw.fill()
        draw.string('BLE status: ' + ('ON' if ble.enabled() else 'OFF'), 0, 60, width=240)
        self._btn.draw()

    def touch(self, event):
        if self._btn.touch(event):
            if ble.enabled():
                ble.disable()
                self._btn = widgets.Button(10, 120, 220, 80, 'Reboot to enable')
                self._draw()
            else:
                wasp.machine.reset()