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 <noreply@anthropic.com>
pull/994/head
diffwireauto 1 month ago committed by Strycher
parent 1827308339
commit f651b3d79d

@ -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 = <String, String>{};
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) {

@ -178,10 +178,16 @@ class _TuneSlotEditorScreenState extends State<TuneSlotEditorScreen> {
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<TuneSlotEditorScreen> {
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),

@ -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');

@ -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());

Loading…
Cancel
Save

Powered by TurnKey Linux.