add vibration effect in camera file when object is detected
This commit is contained in:
parent
61264de620
commit
d404368b6c
|
|
@ -5,6 +5,7 @@ import 'dart:ui' as ui;
|
||||||
|
|
||||||
import 'package:camera/camera.dart';
|
import 'package:camera/camera.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import '../services/tts_service.dart';
|
import '../services/tts_service.dart';
|
||||||
import '../services/yolo_detection_service.dart';
|
import '../services/yolo_detection_service.dart';
|
||||||
|
|
@ -29,6 +30,8 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||||
List<YoloDetection> detectedObjects = const [];
|
List<YoloDetection> detectedObjects = const [];
|
||||||
Size? detectionFrameSize;
|
Size? detectionFrameSize;
|
||||||
Timer? _detectionTimer;
|
Timer? _detectionTimer;
|
||||||
|
Timer? _vibrationTimer;
|
||||||
|
bool _isVibrating = false;
|
||||||
String _lastSpokenMessage = "";
|
String _lastSpokenMessage = "";
|
||||||
DateTime? _lastSpokenAt;
|
DateTime? _lastSpokenAt;
|
||||||
|
|
||||||
|
|
@ -96,6 +99,9 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||||
Future<void> stopRecording() async {
|
Future<void> stopRecording() async {
|
||||||
_detectionTimer?.cancel();
|
_detectionTimer?.cancel();
|
||||||
_detectionTimer = null;
|
_detectionTimer = null;
|
||||||
|
_vibrationTimer?.cancel();
|
||||||
|
_vibrationTimer = null;
|
||||||
|
_isVibrating = false;
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
|
|
@ -190,6 +196,8 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_vibrateForTwoSeconds();
|
||||||
|
|
||||||
final Map<String, int> objectCounts = {};
|
final Map<String, int> objectCounts = {};
|
||||||
for (final YoloDetection object in response.objects) {
|
for (final YoloDetection object in response.objects) {
|
||||||
objectCounts[object.name] = (objectCounts[object.name] ?? 0) + 1;
|
objectCounts[object.name] = (objectCounts[object.name] ?? 0) + 1;
|
||||||
|
|
@ -204,6 +212,32 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||||
await _speakOnce("Terdeteksi $objectSummary");
|
await _speakOnce("Terdeteksi $objectSummary");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _vibrateForTwoSeconds() {
|
||||||
|
if (_isVibrating) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isVibrating = true;
|
||||||
|
int vibrationCount = 0;
|
||||||
|
const int maxVibrationCount = 8;
|
||||||
|
const Duration vibrationInterval = Duration(milliseconds: 250);
|
||||||
|
|
||||||
|
HapticFeedback.vibrate();
|
||||||
|
vibrationCount++;
|
||||||
|
|
||||||
|
_vibrationTimer?.cancel();
|
||||||
|
_vibrationTimer = Timer.periodic(vibrationInterval, (Timer timer) {
|
||||||
|
if (!mounted || !isRecording || vibrationCount >= maxVibrationCount) {
|
||||||
|
timer.cancel();
|
||||||
|
_isVibrating = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HapticFeedback.vibrate();
|
||||||
|
vibrationCount++;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _speakOnce(String message) async {
|
Future<void> _speakOnce(String message) async {
|
||||||
final DateTime now = DateTime.now();
|
final DateTime now = DateTime.now();
|
||||||
final bool canRepeat = _lastSpokenAt == null ||
|
final bool canRepeat = _lastSpokenAt == null ||
|
||||||
|
|
@ -221,6 +255,7 @@ class _CameraScreenState extends State<CameraScreen> {
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_detectionTimer?.cancel();
|
_detectionTimer?.cancel();
|
||||||
|
_vibrationTimer?.cancel();
|
||||||
controller?.dispose();
|
controller?.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue