Masterarbeit Richard Stern. Flutter App, sich mit einem Bluetooth-Gerät verbindet und Berührungen auf einem Sensor visualisiert.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

touchDataBloc.dart 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'dart:async';
  2. import 'dart:ui';
  3. import 'package:flutter/material.dart';
  4. import 'package:rxdart/rxdart.dart';
  5. import 'package:rxdart/subjects.dart';
  6. import 'package:touch_demonstrator/model/touchData.dart';
  7. class TouchDataBloc {
  8. final int xMax = 1200;
  9. String inputString = "";
  10. final history = List<TouchData>();
  11. // final _touchData$ = BehaviorSubject<TouchData>(); // Touch Data from the input String
  12. final _touchData$ = PublishSubject<TouchData>(); // Touch Data from the input String
  13. final _touchVisualise$ = BehaviorSubject<Map<TouchData, Color>>();
  14. final _touchDataHistory$ = BehaviorSubject<List<TouchData>>(); //All touches
  15. // Controller Input of Data
  16. final _inputTouchDataController = StreamController<String>();
  17. // Controller for outputting
  18. //SINKS (into bloc)
  19. Sink<String> get inputTouchData => _inputTouchDataController.sink;
  20. //STREAMS (output bloc)
  21. Stream<TouchData> get outputTouchData$ => _touchData$.stream; //->
  22. Stream<Map<TouchData,Color>> get outputVisualise$ => _touchVisualise$.stream; //->
  23. Stream<List<TouchData>> get outputHistory$ => _touchDataHistory$.stream; //->
  24. void dispose() {
  25. _inputTouchDataController.close();
  26. _touchData$.close();
  27. _touchVisualise$.close();
  28. _touchDataHistory$.close();
  29. // _touchDataHistory$.close();
  30. }
  31. static List<Color> _colorOfTouches = [
  32. Colors.blue,
  33. Colors.red,
  34. Colors.orange,
  35. Colors.white,
  36. Colors.yellow,
  37. ];
  38. static List<TouchData> _lastFingerTouch = [
  39. TouchData(5, 0, 1200, 1200),
  40. TouchData(5, 1, 1200, 1200),
  41. TouchData(5, 2, 1200, 1200),
  42. TouchData(5, 3, 1200, 1200),
  43. TouchData(5, 4, 1200, 1200),
  44. ];
  45. Map<TouchData, Color> touchCollection = {
  46. _lastFingerTouch[0]: _colorOfTouches[0],
  47. _lastFingerTouch[1]: _colorOfTouches[1],
  48. _lastFingerTouch[2]: _colorOfTouches[2],
  49. _lastFingerTouch[3]: _colorOfTouches[3],
  50. _lastFingerTouch[4]: _colorOfTouches[4],
  51. };
  52. TouchDataBloc() {
  53. print('init: Touch Data BLOC');
  54. _inputTouchDataController.stream.listen(_testTouchEventData);
  55. }
  56. void _updateStreams(TouchData t){
  57. /* print("<- Update Streams (${t.e}|${t.f}|${t.x.toString().padLeft(4,'0')}|"
  58. "${t.y.toString().padLeft(4,'0')})");*/
  59. _lastFingerTouch[t.fingerNumber].touchEvent(t.event, t.x, t.y);
  60. _touchData$.add(t); // Stream of single touches
  61. _touchVisualise$.add(touchCollection);
  62. //new:
  63. history.add(t);
  64. _touchDataHistory$.add(history);
  65. }
  66. void _testTouchEventData(String i) {
  67. print('_testTouchEventData');
  68. TouchData t = TouchData(0, 0, 0, 0);
  69. inputString = i;
  70. // print("input: $inputString");
  71. RegExp regExp = new RegExp(
  72. r"\(\d{1}\|\d{1}\|\d{4}\|\d{4}\)",
  73. caseSensitive: false,
  74. multiLine: false,
  75. );
  76. var matches = regExp.allMatches(inputString);
  77. for (var m in matches) {
  78. // print('${m.group(0)}');
  79. t.event = int.tryParse(m.group(0)[1]);
  80. t.fingerNumber = int.tryParse(m.group(0)[3]);
  81. t.x = int.tryParse(m.group(0).substring(5, 9));
  82. t.y = int.tryParse(m.group(0).substring(10, 14));
  83. if (t.x < 1024 && t.y < 1024 && (t.fingerNumber <=4) && (t.event==1 || t.event==4 || t.event==5)){
  84. _updateStreams(t);
  85. print('straem update');
  86. }
  87. else{
  88. print("Touch not Added!!!!!!!!!!!!");
  89. }
  90. }
  91. }
  92. }