forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus_bar.c
More file actions
156 lines (125 loc) · 4.56 KB
/
status_bar.c
File metadata and controls
156 lines (125 loc) · 4.56 KB
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <stdbool.h>
#include "genhdr/mpversion.h"
#include "py/mpconfig.h"
#include "shared-bindings/supervisor/__init__.h"
#include "shared-bindings/supervisor/StatusBar.h"
#include "supervisor/background_callback.h"
#include "supervisor/shared/serial.h"
#include "supervisor/shared/status_bar.h"
#if CIRCUITPY_TERMINALIO
#include "shared-module/terminalio/Terminal.h"
#endif
#if CIRCUITPY_WEB_WORKFLOW
#include "supervisor/shared/web_workflow/web_workflow.h"
#endif
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
#include "supervisor/shared/bluetooth/bluetooth.h"
#endif
#if CIRCUITPY_USB_KEYBOARD_WORKFLOW
#include "supervisor/usb.h"
#endif
static background_callback_t status_bar_background_cb;
static bool _forced_dirty = false;
static bool _suspended = false;
// Clear if possible, but give up if we can't do it now.
void supervisor_status_bar_clear(void) {
if (!_suspended) {
serial_write("\x1b" "]0;" "\x1b" "\\");
}
}
void supervisor_status_bar_update(void) {
if (_suspended) {
supervisor_status_bar_request_update(true);
return;
}
_forced_dirty = false;
shared_module_supervisor_status_bar_updated(&shared_module_supervisor_status_bar_obj);
// Disable status bar console writes if supervisor.status_bar.console is False.
// Also disable if there is no serial connection now. This avoids sending part
// of the status bar update if the serial connection comes up during the update.
bool disable_console_writes =
!shared_module_supervisor_status_bar_get_console(&shared_module_supervisor_status_bar_obj) ||
!serial_connected();
// Disable status bar display writes if supervisor.status_bar.display is False.
bool disable_display_writes =
!shared_module_supervisor_status_bar_get_display(&shared_module_supervisor_status_bar_obj);
// Suppress writes to console and/or display if status bar is not enabled for either or both.
bool prev_console_disable = false;
bool prev_display_disable = false;
if (disable_console_writes) {
prev_console_disable = serial_console_write_disable(true);
}
if (disable_display_writes) {
prev_display_disable = serial_display_write_disable(true);
}
// Neighboring "..." "..." are concatenated by the compiler. Without this separation, the hex code
// doesn't get terminated after two following characters and the value is invalid.
// This is the OSC command to set the title and the icon text. It can be up to 255 characters
// but some may be cut off.
serial_write("\x1b" "]0;");
serial_write("🐍");
#if CIRCUITPY_USB_KEYBOARD_WORKFLOW
usb_keyboard_status();
#endif
#if CIRCUITPY_WEB_WORKFLOW
supervisor_web_workflow_status();
serial_write(" | ");
#endif
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
supervisor_bluetooth_status();
serial_write(" | ");
#endif
supervisor_execution_status();
serial_write(" | ");
serial_write(MICROPY_GIT_TAG);
// Send string terminator
serial_write("\x1b" "\\");
// Restore writes to console and/or display.
if (disable_console_writes) {
serial_console_write_disable(prev_console_disable);
}
if (disable_display_writes) {
serial_display_write_disable(prev_display_disable);
}
}
static void status_bar_background(void *data) {
if (_suspended) {
return;
}
bool dirty = _forced_dirty;
#if CIRCUITPY_WEB_WORKFLOW
dirty = dirty || supervisor_web_workflow_status_dirty();
#endif
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
dirty = dirty || supervisor_bluetooth_status_dirty();
#endif
if (dirty) {
supervisor_status_bar_update();
}
}
void supervisor_status_bar_start(void) {
supervisor_status_bar_request_update(true);
}
void supervisor_status_bar_request_update(bool force_dirty) {
if (force_dirty) {
_forced_dirty = true;
}
background_callback_add_core(&status_bar_background_cb);
}
void supervisor_status_bar_suspend(void) {
_suspended = true;
}
void supervisor_status_bar_resume(void) {
_suspended = false;
supervisor_status_bar_request_update(false);
}
void supervisor_status_bar_init(void) {
status_bar_background_cb.fun = status_bar_background;
status_bar_background_cb.data = NULL;
shared_module_supervisor_status_bar_init(&shared_module_supervisor_status_bar_obj);
}