Skip to content

Commit

Permalink
Added encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Nov 26, 2023
1 parent d34fe3d commit 4e935c1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/camera_stream.
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/command_scheduler.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/command_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/differential_drive.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/encoder_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/field_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/fms_info.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/gyro.dart';
Expand Down Expand Up @@ -76,6 +77,8 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
switch (widget?.type) {
case 'Gyro':
return normalSize * 2;
case 'Encoder':
return normalSize * 2;
case 'Camera Stream':
return normalSize * 2;
case 'Field':
Expand Down Expand Up @@ -262,6 +265,12 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer {
key: UniqueKey(),
jsonData: widgetProperties,
);
case 'Encoder':
case 'Quadrature Encoder':
return EncoderWidget.fromJson(
key: UniqueKey(),
jsonData: widgetProperties,
);
case 'Accelerometer':
return AccelerometerWidget.fromJson(
key: UniqueKey(),
Expand Down
6 changes: 6 additions & 0 deletions lib/widgets/network_tree/tree_row.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/camera_stream.
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/command_scheduler.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/command_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/differential_drive.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/encoder_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/field_widget.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/fms_info.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/multi-topic/gyro.dart';
Expand Down Expand Up @@ -164,6 +165,9 @@ class TreeRow {
return ThreeAxisAccelerometer(key: UniqueKey(), topic: topic);
case 'Accelerometer':
return AccelerometerWidget(key: UniqueKey(), topic: topic);
case 'Encoder':
case 'Quadrature Encoder':
return EncoderWidget(key: UniqueKey(), topic: topic);
case 'Field2d':
return FieldWidget(key: UniqueKey(), topic: topic);
case 'PowerDistribution':
Expand Down Expand Up @@ -208,6 +212,8 @@ class TreeRow {
if (primary is Gyro) {
width = normalGridSize * 2;
height = normalGridSize * 2;
} else if (primary is EncoderWidget) {
width = normalGridSize * 2;
} else if (primary is CameraStreamWidget) {
width = normalGridSize * 2;
height = normalGridSize * 2;
Expand Down
117 changes: 117 additions & 0 deletions lib/widgets/nt4_widgets/multi-topic/encoder_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:dot_cast/dot_cast.dart';
import 'package:elastic_dashboard/services/globals.dart';
import 'package:elastic_dashboard/services/nt4_connection.dart';
import 'package:elastic_dashboard/widgets/nt4_widgets/nt4_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class EncoderWidget extends StatelessWidget with NT4Widget {
@override
String type = 'Encoder';

late String distanceTopic;
late String speedTopic;

EncoderWidget({super.key, required topic, period = Globals.defaultPeriod}) {
super.topic = topic;
super.period = period;

init();
}

EncoderWidget.fromJson({super.key, required Map<String, dynamic> jsonData}) {
topic = tryCast(jsonData['topic']) ?? '';
period = tryCast(jsonData['period']) ?? Globals.defaultPeriod;

init();
}

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

distanceTopic = '$topic/Distance';
speedTopic = '$topic/Speed';
}

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

distanceTopic = '$topic/Distance';
speedTopic = '$topic/Speed';
}

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

return StreamBuilder(
stream: subscription?.periodicStream(),
builder: (context, snapshot) {
double distance =
tryCast(nt4Connection.getLastAnnouncedValue(distanceTopic)) ?? 0.0;
double speed =
tryCast(nt4Connection.getLastAnnouncedValue(speedTopic)) ?? 0.0;

return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children: [
const Text('Distance'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade700,
width: 1.5,
),
),
),
child: SelectableText(
distance.toStringAsPrecision(10),
maxLines: 1,
showCursor: true,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
overflow: TextOverflow.ellipsis,
),
),
),
),
],
),
Row(
children: [
const Text('Speed'),
const SizedBox(width: 10),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey.shade700,
width: 1.5,
),
),
),
child: SelectableText(
speed.toStringAsPrecision(10),
maxLines: 1,
showCursor: true,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
overflow: TextOverflow.ellipsis,
),
),
),
),
],
),
],
);
},
);
}
}

0 comments on commit 4e935c1

Please sign in to comment.