Skip to content

Commit

Permalink
Smooth Transistion From Torch to Off State
Browse files Browse the repository at this point in the history
In the case when the intensity of light goes above and below the threshhold value frequently the light also flashes frequently to prevent that we take the average of ten values and then compare average to threshold
  • Loading branch information
aadarshadhakalg committed Aug 3, 2021
1 parent 8ef979b commit d11cccd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/utils/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CameraAppState extends State<CameraApp> with WidgetsBindingObserver {
}

activateFlashModeToggler(event) {
print('called');
if (isCameraReady) {
_controller.setFlashMode(event);
}
Expand Down
19 changes: 19 additions & 0 deletions lib/src/utils/flash_mode_toggler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:light/light.dart';
enum Intensity { Light, Dark }

class FlashModeToggler {
static int count = 0;
static int sum = 0;
static FlashModeToggler? _instance;
FlashModeToggler._internal() {
flashModeStream.listen((event) {
Expand All @@ -17,6 +19,23 @@ class FlashModeToggler {
static final Light _light = Light();

static final Stream<FlashMode> flashModeStream = _light.lightSensorStream
.transform(
StreamTransformer.fromHandlers(
handleData: (int value, EventSink sink) {
print(value);
print(count);
if (count > 10) {
count = 0;
print("Average: ${sum / 10}");
sink.add(sum / 10);
sum = 0;
} else {
count++;
sum = sum + value;
}
},
),
)
.asyncMap<FlashMode>(
(event) => event < 10 ? FlashMode.torch : FlashMode.off)
.distinct()
Expand Down
2 changes: 2 additions & 0 deletions lib/src/utils/routes/route_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AppRouteObserver extends NavigatorObserver {
if (previousRoute?.settings.name == '/') {
print("Reopening Flash Stream");
FlashModeToggler.flashSubscription.resume();
FlashModeToggler.flashModeStream.single
.then((value) => FlashModeToggler.flashMode.add);
}
super.didPop(route, previousRoute);
}
Expand Down

0 comments on commit d11cccd

Please sign in to comment.