From f651b3d79dd69b0322b5bae32a129062732e6f57 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:50:54 -0400 Subject: [PATCH] feat(#970): say when RTTTL defaults are out of order, and fix them in one tap The badge's player reads d=, o=, b= positionally, in that order, so RTTTL from elsewhere that lists them otherwise (owner's Take On Me: o=5,d=8,b=160,b=160) is refused as a stray colon, and firmware's 21-column words do not say why. When the header is the only fault, the editor now says the badge reads d=, o=, b= in that order, each once, and offers Fix order. The fix refuses to pick between two different values for one key, and must itself pass rtttlProblem, so it never hides a second problem. Co-Authored-By: Claude Opus 5.5 --- lib/connector/offband_tones.dart | 37 +++++++++++++++++++ .../settings/tune_slot_editor_screen.dart | 11 ++++++ test/connector/offband_tones_test.dart | 31 ++++++++++++++++ test/screens/settings_tones_parity_test.dart | 22 +++++++++++ 4 files changed, 101 insertions(+) diff --git a/lib/connector/offband_tones.dart b/lib/connector/offband_tones.dart index 5be07d89..901ba69a 100644 --- a/lib/connector/offband_tones.dart +++ b/lib/connector/offband_tones.dart @@ -402,6 +402,43 @@ RtttlProblem rtttlProblem(String rtttl) { return RtttlProblem.ok; } +/// The tune with its defaults rewritten into the order the badge's player +/// reads them (d=, o=, b=, each once), or null when that is not the fix. +/// +/// The player reads defaults positionally, so RTTTL from elsewhere that lists +/// them in another order, or repeats one, is refused as a stray colon. This +/// rewrites only the header between the first two colons, and only when every +/// item there is a d=, o= or b= and a repeated key repeats the same value: two +/// different tempos are a choice the app cannot make for the user. The result +/// must pass [rtttlProblem], so a header fix never hides a second problem. +String? fixRtttlDefaultsOrder(String rtttl) { + if (rtttlProblem(rtttl) == RtttlProblem.ok) return null; + final first = rtttl.indexOf(':'); + if (first < 0) return null; + final second = rtttl.indexOf(':', first + 1); + if (second < 0) return null; + final values = {}; + for (final raw in rtttl.substring(first + 1, second).split(',')) { + final item = raw.trim(); + final eq = item.indexOf('='); + if (eq != 1) return null; + final key = item[0]; + final value = item.substring(2).trim(); + if (!const {'d', 'o', 'b'}.contains(key) || value.isEmpty) return null; + final held = values[key]; + if (held != null && held != value) return null; + values[key] = value; + } + final header = [ + for (final k in const ['d', 'o', 'b']) + if (values[k] != null) '$k=${values[k]}', + ].join(','); + final fixed = + '${rtttl.substring(0, first)}:$header:${rtttl.substring(second + 1)}'; + if (fixed == rtttl || rtttlProblem(fixed) != RtttlProblem.ok) return null; + return fixed; +} + /// Port of firmware `deriveSlotName()`: the text before the first colon, /// trimmed and cut to [maxSlotName]. Empty when there is no usable name. String deriveSlotName(String rtttl) { diff --git a/lib/screens/settings/tune_slot_editor_screen.dart b/lib/screens/settings/tune_slot_editor_screen.dart index 61438d74..aa3bfe6e 100644 --- a/lib/screens/settings/tune_slot_editor_screen.dart +++ b/lib/screens/settings/tune_slot_editor_screen.dart @@ -178,10 +178,16 @@ class _TuneSlotEditorScreenState extends State { final scheme = Theme.of(context).colorScheme; final problem = rtttlProblem(_text); final emptyText = _text.isEmpty; + // Firmware's own words fit 21 badge columns and do not say that order is + // the problem, which it usually is for RTTTL from elsewhere. When the + // header is the only fault, say so and offer the fix. + final orderFix = emptyText ? null : fixRtttlDefaultsOrder(_text); final helper = emptyText ? (_saved == null ? '' : 'Type or paste an RTTTL tune') : problem == RtttlProblem.ok ? 'Plays as "${deriveSlotName(_text)}"' + : orderFix != null + ? 'The badge reads d=, o=, b= in that order, each once' : problem.text; final helperIsProblem = !emptyText && problem != RtttlProblem.ok; final savedHasTune = _saved != null && _saved!.isNotEmpty; @@ -241,6 +247,11 @@ class _TuneSlotEditorScreenState extends State { spacing: 8, runSpacing: 8, children: [ + if (orderFix != null) + TextButton( + onPressed: _busy ? null : () => _controller.text = orderFix, + child: const Text('Fix order'), + ), TextButton( onPressed: _busy || !savedHasTune ? null : _clear, style: TextButton.styleFrom(foregroundColor: scheme.error), diff --git a/test/connector/offband_tones_test.dart b/test/connector/offband_tones_test.dart index a593cdc6..535c5510 100644 --- a/test/connector/offband_tones_test.dart +++ b/test/connector/offband_tones_test.dart @@ -129,6 +129,37 @@ void main() { }); }); + group('fixRtttlDefaultsOrder', () { + // The owner's tune, 2026-10-25: o= before d=, and b= twice. + const takeOnMe = 'Take On Me:o=5,d=8,b=160,b=160:f#,f#,f#,d,p,b4,p,e,p,e5'; + + test('the owner\'s tune is refused, and the fix plays', () { + expect(rtttlProblem(takeOnMe), RtttlProblem.strayColon); + final fixed = fixRtttlDefaultsOrder(takeOnMe); + expect(fixed, 'Take On Me:d=8,o=5,b=160:f#,f#,f#,d,p,b4,p,e,p,e5'); + expect(rtttlProblem(fixed!), RtttlProblem.ok); + }); + + test('a valid tune needs no fix', () { + expect(fixRtttlDefaultsOrder('x:d=4,o=5,b=100:c'), isNull); + }); + + test('two different tempos are not the app\'s to choose', () { + expect(fixRtttlDefaultsOrder('x:o=5,b=160,b=200:c'), isNull); + }); + + test('a header fix never hides a second problem', () { + // Reordered, b=0 still divides by zero in the player. + expect(fixRtttlDefaultsOrder('x:o=5,d=8,b=0:c'), isNull); + // A key the player does not know is not a reorder. + expect(fixRtttlDefaultsOrder('x:o=5,l=8:c'), isNull); + }); + + test('spaces around the defaults are dropped', () { + expect(fixRtttlDefaultsOrder('x: o=5 , d=8 :c'), 'x:d=8,o=5:c'); + }); + }); + group('deriveSlotName', () { test('the text before the first colon, trimmed and capped at 12', () { expect(deriveSlotName(' Mario :d=4:c'), 'Mario'); diff --git a/test/screens/settings_tones_parity_test.dart b/test/screens/settings_tones_parity_test.dart index eb69e717..968286ea 100644 --- a/test/screens/settings_tones_parity_test.dart +++ b/test/screens/settings_tones_parity_test.dart @@ -309,6 +309,28 @@ void main() { }); }); + group('slot editor, defaults out of order', () { + testWidgets('says why and fixes it in one tap', (tester) async { + await openBuzzer(tester, _Radio()); + await tapText(tester, '1 Mario'); + await tester.enterText( + find.byType(TextField), + 'Take On Me:o=5,d=8,b=160,b=160:f#,f#,e5', + ); + await tester.pump(); + expect( + find.text('The badge reads d=, o=, b= in that order, each once'), + findsOneWidget, + ); + + await tester.tap(find.text('Fix order')); + await tester.pump(); + expect(find.text('Take On Me:d=8,o=5,b=160:f#,f#,e5'), findsOneWidget); + expect(find.text('Plays as "Take On Me"'), findsOneWidget); + expect(find.text('Fix order'), findsNothing); + }); + }); + group('Node Settings', () { testWidgets('Screen and Battery only with 0x40', (tester) async { await pump(tester, _Radio(screen: false), const SettingsScreen());