Skip to content

Commit

Permalink
Controller now runs on a single connection
Browse files Browse the repository at this point in the history
faster and better performance + I changed how brakes work.
  • Loading branch information
MoazSalem committed Jul 4, 2023
1 parent 7c4e945 commit fa84920
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
6 changes: 5 additions & 1 deletion assets/translations/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"Current Theme": "الثيم الحالي",
"Dynamic Colors": "ألوان ديناميكية",
"T4": "يستخدم الثيم الخاص بالنظام, يعمل بشكل مناسب على اندرويد 12+ فقط",
"Speed": "السرعة"
"Speed": "السرعة",
"BRAKES": "الفرامل",
"RELEASE BRAKES": "ترك الفرامل",
"HIGH LIGHTS": "اضاءة عالية",
"LOW LIGHTS": "اضاءة منخفظة"
}
6 changes: 5 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"Current Theme": "Current Theme",
"Dynamic Colors": "Dynamic Colors",
"T4": "Use systems theme, this will only work as intended on android 12+",
"Speed": "Speed"
"Speed": "Speed",
"BRAKES": "BRAKES",
"RELEASE BRAKES": "RELEASE BRAKES",
"HIGH LIGHTS": "HIGH LIGHTS",
"LOW LIGHTS": "LOW LIGHTS"
}
36 changes: 13 additions & 23 deletions lib/Screens/controller_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ List<int> _lastMessage = [0, 0, 0, 0, 0];
int _hlPressed = 0;
int _llPressed = 0;
late dynamic _bytesSocketHandler;
bool _brakes = false;

class ControllerPage extends StatefulWidget {
const ControllerPage({super.key});
Expand Down Expand Up @@ -84,59 +85,49 @@ Widget theScaffold({required BuildContext context, numDevices}) {
mode: JoystickMode.horizontal,
listener: (details) async {
// Most RIGHT returns 0.99 while most LEFT returns -0.99
// Connecting to server:
await _bytesSocketHandler.connect();
_lastMessage[0] = 0;
_lastMessage[3] = details.x > 0 ? (details.x * 5).round() : 0;
_lastMessage[4] = details.x < 0 ? (-details.x * 5).round() : 0;
final bytesMessage =
utf8.encode("${_lastMessage.join()}$_hlPressed$_llPressed");
_bytesSocketHandler.sendMessage(bytesMessage);
await _bytesSocketHandler.disconnect('');
_brakes ? null : _bytesSocketHandler.sendMessage(bytesMessage);
B.stateChanged();
}),
Column(
children: [
ElevatedButton(
onPressed: () async {
// Connecting to server:
await _bytesSocketHandler.connect();
_lastMessage = [1, 0, 0, 0, 0];
_brakes = !_brakes;
_lastMessage = _brakes ? [1, 0, 0, 0, 0] : [0, 0, 0, 0, 0];
final bytesMessage =
utf8.encode("${_lastMessage.join()}$_hlPressed$_llPressed");
_bytesSocketHandler.sendMessage(bytesMessage);
await _bytesSocketHandler.disconnect('');
B.stateChanged();
},
child: const Text("BRAKES"),
child: _brakes
? const Text("RELEASE BRAKES").tr()
: const Text("BRAKES").tr(),
),
ElevatedButton(
onPressed: () async {
// Connecting to server:
await _bytesSocketHandler.connect();
_hlPressed == 0 ? _hlPressed = 1 : _hlPressed = 0;
final bytesMessage =
utf8.encode("${_lastMessage.join()}$_hlPressed$_llPressed");
_bytesSocketHandler.sendMessage(bytesMessage);
await _bytesSocketHandler.disconnect('');
B.stateChanged();
},
child: Text("HIGH LIGHTS",
child: Text("HIGH LIGHTS".tr(),
style:
TextStyle(color: _hlPressed == 1 ? Colors.red : B.theme.primary)),
),
ElevatedButton(
onPressed: () async {
// Connecting to server:
await _bytesSocketHandler.connect();
_llPressed == 0 ? _llPressed = 1 : _llPressed = 0;
final bytesMessage =
utf8.encode("${_lastMessage.join()}$_hlPressed$_llPressed");
_bytesSocketHandler.sendMessage(bytesMessage);
await _bytesSocketHandler.disconnect('');
B.stateChanged();
},
child: Text("LOW LIGHTS",
child: Text("LOW LIGHTS".tr(),
style:
TextStyle(color: _llPressed == 1 ? Colors.red : B.theme.primary)),
)
Expand All @@ -146,15 +137,11 @@ Widget theScaffold({required BuildContext context, numDevices}) {
mode: JoystickMode.vertical,
listener: (details) async {
// Most DOWN returns 0.99 while most UP returns -0.99
// Connecting to server:
await _bytesSocketHandler.connect();
_lastMessage[0] = 0;
_lastMessage[1] = details.y < 0 ? (-details.y * 5).round() : 0;
_lastMessage[2] = details.y > 0 ? (details.y * 5).round() : 0;
final bytesMessage =
utf8.encode("${_lastMessage.join()}$_hlPressed$_llPressed");
_bytesSocketHandler.sendMessage(bytesMessage);
await _bytesSocketHandler.disconnect('');
_brakes ? null : _bytesSocketHandler.sendMessage(bytesMessage);
B.stateChanged();
}),
],
Expand All @@ -180,4 +167,7 @@ connect() async {
bytesSocketProcessor,
connectionOptions: connectionOptions,
);

// Connecting to server:
await _bytesSocketHandler.connect();
}

0 comments on commit fa84920

Please sign in to comment.