Skip to content

Commit

Permalink
Added ultrasonic widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Dec 9, 2023
1 parent 9448c47 commit 3224e37
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/services/nt4_widget_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/split_button_c
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/subsystem_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/swerve_drive.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/three_axis_accelerometer.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/ultrasonic.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/boolean_box.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/single_topic/graph.dart';
Expand Down Expand Up @@ -116,6 +117,7 @@ class NT4WidgetBuilder {
SwerveDriveWidget.widgetType: SwerveDriveWidget.fromJson,
ThreeAxisAccelerometer.widgetType: ThreeAxisAccelerometer.fromJson,
'3AxisAccelerometer': ThreeAxisAccelerometer.fromJson,
Ultrasonic.widgetType: Ultrasonic.fromJson,
});

// Used when building widgets from network tables (drag and drop)
Expand Down Expand Up @@ -149,6 +151,7 @@ class NT4WidgetBuilder {
SwerveDriveWidget.widgetType: SwerveDriveWidget.new,
ThreeAxisAccelerometer.widgetType: ThreeAxisAccelerometer.new,
'3AxisAccelerometer': ThreeAxisAccelerometer.new,
Ultrasonic.widgetType: Ultrasonic.new,
});

// Min width and height
Expand All @@ -168,6 +171,7 @@ class NT4WidgetBuilder {
RobotPreferences.widgetType: _normalSize * 2,
SubsystemWidget.widgetType: _normalSize * 2,
SwerveDriveWidget.widgetType: _normalSize * 2,
Ultrasonic.widgetType: _normalSize * 2,
});

_minimumHeightMap.addAll({
Expand Down Expand Up @@ -202,6 +206,7 @@ class NT4WidgetBuilder {
RobotPreferences.widgetType: 2,
SubsystemWidget.widgetType: 2,
SwerveDriveWidget.widgetType: 2,
Ultrasonic.widgetType: 2,
});

_defaultHeightMap.addAll({
Expand Down
88 changes: 88 additions & 0 deletions lib/widgets/nt4_widgets/multi-topic/ultrasonic.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import 'package:flutter/material.dart';

import 'package:dot_cast/dot_cast.dart';
import 'package:provider/provider.dart';

import 'package:elastic_dashboard/services/nt4.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';

class Ultrasonic extends NT4Widget {
static const String widgetType = 'Ultrasonic';
@override
String type = widgetType;

late String valueTopic;
late NT4Subscription valueSubscription;

Ultrasonic({super.key, required super.topic, super.period}) : super();

Ultrasonic.fromJson({super.key, required super.jsonData}) : super.fromJson();

@override
void init() {
super.init();

valueTopic = '$topic/Value';
valueSubscription = nt4Connection.subscribe(valueTopic, super.period);
}

@override
void resetSubscription() {
nt4Connection.unSubscribe(valueSubscription);

valueTopic = '$topic/Value';
valueSubscription = nt4Connection.subscribe(valueTopic, super.period);

super.resetSubscription();
}

@override
void unSubscribe() {
nt4Connection.unSubscribe(valueSubscription);

super.unSubscribe();
}

@override
Widget build(BuildContext context) {
notifier = context.watch<NT4WidgetNotifier?>();

return StreamBuilder(
stream: valueSubscription.periodicStream(yieldAll: false),
initialData: nt4Connection.getLastAnnouncedValue(valueTopic),
builder: (context, snapshot) {
notifier = context.watch<NT4WidgetNotifier?>();

double value = tryCast(snapshot.data) ?? 0.0;

return Row(
children: [
const Text('Range'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade700,
width: 1.5,
),
),
),
child: SelectableText(
'${value.toStringAsPrecision(5)} in',
maxLines: 1,
showCursor: true,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
overflow: TextOverflow.ellipsis,
),
),
),
),
],
);
},
);
}
}

0 comments on commit 3224e37

Please sign in to comment.