-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.dart
More file actions
385 lines (337 loc) · 12.5 KB
/
main.dart
File metadata and controls
385 lines (337 loc) · 12.5 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
157
158
159
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
186
187
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:logging/logging.dart';
import 'package:frame_msg/rx/tap.dart';
import 'package:simple_frame_app/text_utils.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'package:frame_msg/tx/code.dart';
import 'package:frame_msg/tx/image_sprite_block.dart';
import 'package:frame_msg/tx/sprite.dart';
import 'package:frame_msg/tx/plain_text.dart';
import 'package:speech_to_text/speech_recognition_error.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'wiki.dart';
void main() => runApp(const MainApp());
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
MainAppState createState() => MainAppState();
}
/// SimpleFrameAppState mixin helps to manage the lifecycle of the Frame connection outside of this file
class MainAppState extends State<MainApp> with SimpleFrameAppState {
MainAppState() {
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: [${record.loggerName}] ${record.time}: ${record.message}');
});
}
// Speech to text members
final SpeechToText _speechToText = SpeechToText();
bool _speechEnabled = false;
String _partialResult = "N/A";
String _finalResult = "N/A";
String? _prevText;
// Wiki members
List<String>? _extract;
int _currentPage = -1; // 5 lines of the wiki extract per page
Image? _image;
// tap subscription
StreamSubscription<int>? _tapSubs;
static const _textStyle = TextStyle(fontSize: 30);
@override
void initState() {
super.initState();
// asynchronously kick off Speech-to-text initialization
currentState = ApplicationState.initializing;
_initSpeech();
}
@override
void dispose() async {
_speechToText.cancel();
super.dispose();
}
/// This has to happen only once per app, but microphone permission must be provided
void _initSpeech() async {
_speechEnabled = await _speechToText.initialize(onError: _onSpeechError);
if (!_speechEnabled) {
_finalResult = 'The user has denied the use of speech recognition. Microphone permission must be added manually in device settings.';
_log.severe(_finalResult);
currentState = ApplicationState.disconnected;
}
else {
_log.fine('Speech-to-text initialized');
// this will initialise before Frame is connected, so proceed to disconnected state
currentState = ApplicationState.disconnected;
}
if (mounted) setState(() {});
}
/// Manually stop the active speech recognition session, but timeouts will also stop the listening
Future<void> _stopListening() async {
await _speechToText.stop();
}
/// Timeouts invoke this function, but also other permanent errors
void _onSpeechError(SpeechRecognitionError error) {
if (error.errorMsg != 'error_speech_timeout') {
_log.severe(error.errorMsg);
currentState = ApplicationState.ready;
}
else {
currentState = ApplicationState.running;
}
if (mounted) setState(() {});
}
/// This application uses platform speech-to-text to listen to audio from the host mic, convert to text,
/// and send the text to the Frame.
/// A Wiki query is also sent, and the resulting content is shown in Frame.
/// The lifetime of this run() is short, it sets up the listeners for taps and speech but keeps application state as running.
/// It has a running main loop on the Frame (frame_app.lua)
@override
Future<void> run() async {
currentState = ApplicationState.running;
if (mounted) setState(() {});
// listen for taps for next(1)/prev(2) page and new query (3)
_tapSubs?.cancel();
_tapSubs = RxTap().attach(frame!.dataResponse)
.listen((taps) async {
_log.fine(() => 'taps: $taps, currentPageBeforeTap: $_currentPage, extractLength: ${_extract?.length}');
switch (taps) {
case 1:
// next
if (_extract != null) {
if (nextPage()) {
await frame!.sendMessage(0x0a, TxPlainText(text: getCurrentPageText()).pack());
setState((){});
}
}
break;
case 2:
// prev
if (_extract != null) {
if (previousPage()) {
await frame!.sendMessage(0x0a, TxPlainText(text: getCurrentPageText()).pack());
setState((){});
}
}
break;
case 3:
// new query
_currentPage = -1;
setState((){});
await _speechToText.listen(
listenOptions: SpeechListenOptions(
cancelOnError: true, onDevice: true, listenMode: ListenMode.search
),
onResult: processSpeechRecognitionResult,
);
break;
default:
}
}
);
// let Frame know to subscribe for taps and send them to us
await frame!.sendMessage(0x10, TxCode(value: 1).pack());
// prompt the user to begin tapping
await frame!.sendMessage(0x0a, TxPlainText(text: '3-Tap for new query\n____\n1-Tap next page\n2-Tap previous page').pack());
}
/// The run() function will run for 5 seconds or so, but if the user
/// interrupts it, we can cancel the speech to text/wiki search and return to ApplicationState.ready state.
@override
Future<void> cancel() async {
// cancel listening (only if we currently are)
await _stopListening();
// let Frame know to stop sending taps
await frame!.sendMessage(0x10, TxCode(value: 0).pack());
// clear the display
await frame!.sendMessage(0x0a, TxPlainText(text: ' ').pack());
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
void processSpeechRecognitionResult(SpeechRecognitionResult result) async {
if (currentState == ApplicationState.ready) {
// user has cancelled already, don't process result
return;
}
if (result.finalResult) {
// on a final result we fetch the wiki content
_finalResult = result.recognizedWords;
_partialResult = '';
_log.fine('Final result: $_finalResult');
_stopListening();
// send final query text to Frame line 1 (before we confirm the title)
if (_finalResult != _prevText) {
await frame!.sendMessage(0x0a, TxPlainText(text: _finalResult).pack());
_prevText = _finalResult;
}
// kick off the http request sequence
String? error;
String? title;
(title, error) = await findBestPage(_finalResult);
if (title != null) {
// send page title to Frame on row 1
if (title != _prevText) {
await frame!.sendMessage(0x0a, TxPlainText(text: title).pack());
_prevText = title;
}
WikiResult? result;
String? error;
(result, error) = await fetchExtract(title);
if (result != null) {
_extract = TextUtils.wrapText('${result.title}\n${result.extract}', 400, 4);
_currentPage = 0;
_finalResult = result.title;
if (mounted) setState((){});
// send first 5 rows of result.extract to Frame ( TODO regex strip non-printable? )
await frame!.sendMessage(0x0a, TxPlainText(text: getCurrentPageText()).pack());
_prevText = '';
if (result.thumbUri != null) {
// first, download the image into an image/image
Uint8List? imageBytes;
(imageBytes, error) = await fetchThumbnail(result.thumbUri!);
if (imageBytes != null) {
try {
// Update the UI based on the original image
setState(() {
_image = Image.memory(imageBytes!, gaplessPlayback: true, fit: BoxFit.cover);
});
// yield here a moment in order to show the first image first
await Future.delayed(const Duration(milliseconds: 10));
var sprite = TxSprite.fromImageBytes(imageBytes: imageBytes);
// Update the UI with the modified image
setState(() {
_image = Image.memory(img.encodePng(sprite.toImage()), gaplessPlayback: true, fit: BoxFit.cover);
});
// create the image sprite block header and its sprite lines
// based on the sprite
TxImageSpriteBlock isb = TxImageSpriteBlock(
image: sprite,
spriteLineHeight: 20,
progressiveRender: true);
// and send the block header then the sprite lines to Frame
await frame!.sendMessage(0x0d, isb.pack());
for (var sprite in isb.spriteLines) {
await frame!.sendMessage(0x0d, sprite.pack());
}
}
catch (e) {
_log.severe('Error processing image: $e');
}
}
else {
_log.fine('Error fetching thumbnail for "$_finalResult": "${result.thumbUri!}" - "$error"');
}
}
else {
// no thumbnail for this entry
_image = null;
}
}
}
else {
_log.fine('Error searching for "$_finalResult" - "$error"');
_extract = [];
await frame!.sendMessage(0x0a, TxPlainText(text: TextUtils.wrapText(error!, 400, 4).join('\n')).pack());
_image = null;
setState((){});
}
// final result is done
if (mounted) setState(() {});
}
else {
// partial result - just display in-progress text
_partialResult = result.recognizedWords;
if (mounted) setState((){});
_log.fine('Partial result: $_partialResult, ${result.alternates}');
if (_partialResult != _prevText) {
// send partial result to Frame line 1
await frame!.sendMessage(0x0a, TxPlainText(text: _partialResult).pack());
_prevText = _partialResult;
}
}
}
String getCurrentPageText() {
if (_extract != null && _currentPage >= 0) {
return _extract!.sublist(_currentPage * 5, min((_currentPage + 1) * 5, _extract!.length)).join('\n');
}
else {
return '';
}
}
bool nextPage() {
if (_extract != null && _extract!.length > ((_currentPage + 1) * 5) && _currentPage >= 0) {
_currentPage++;
return true;
}
else {
return false;
}
}
bool previousPage() {
if (_extract != null && _currentPage > 0) {
_currentPage--;
return true;
}
else {
return false;
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Wiki Frame',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text("Wiki Frame"),
actions: [getBatteryWidget()]
),
body: Center(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Align(alignment: Alignment.centerLeft,
child: Text('Query: ${_partialResult == '' ? _finalResult : _partialResult}', style: _textStyle)
),
const Divider(),
SizedBox(
width: 640,
height: 400,
child: Row(
children: [
Expanded(
flex: 5,
child: Container(
alignment: Alignment.topCenter,
color: Colors.black,
child: Text(getCurrentPageText(),
style: const TextStyle(color: Colors.white, fontSize: 18),
),
),
),
Expanded(
flex: 3,
child: Container(
alignment: Alignment.topCenter,
color: Colors.black,
child: (_image != null) ? _image! : null
),
),
],
),
),
],
),
),
),
floatingActionButton: getFloatingActionButtonWidget(const Icon(Icons.search), const Icon(Icons.cancel)),
persistentFooterButtons: getFooterButtonsWidget(),
),
);
}
}