From 1ef2c54390319788a315cca76cc3c4a5bb672878 Mon Sep 17 00:00:00 2001 From: JunsuChoi Date: Tue, 28 Nov 2023 12:32:58 +0900 Subject: [PATCH] [sensor_plus] Implement set*SamplingPeriod method call --- packages/sensors_plus/CHANGELOG.md | 5 ++ packages/sensors_plus/README.md | 2 +- .../integration_test/sensors_plus_test.dart | 4 +- packages/sensors_plus/pubspec.yaml | 2 +- .../sensors_plus/tizen/src/device_sensor.cc | 6 +- .../sensors_plus/tizen/src/device_sensor.h | 3 +- .../tizen/src/sensors_plus_plugin.cc | 67 ++++++++++++++++--- 7 files changed, 73 insertions(+), 16 deletions(-) diff --git a/packages/sensors_plus/CHANGELOG.md b/packages/sensors_plus/CHANGELOG.md index 9df081147..ce93a1aa7 100644 --- a/packages/sensors_plus/CHANGELOG.md +++ b/packages/sensors_plus/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.4 + +* Implement set*SamplingPeriod method call. +* Update deprecated API in integration_test. + ## 1.1.3 * Update sensors_plus to 3.0.2. diff --git a/packages/sensors_plus/README.md b/packages/sensors_plus/README.md index ca4143861..2b433ceaa 100644 --- a/packages/sensors_plus/README.md +++ b/packages/sensors_plus/README.md @@ -11,7 +11,7 @@ This package is not an _endorsed_ implementation of 'sensors_plus'. Therefore, y ```yaml dependencies: sensors_plus: ^3.0.2 - sensors_plus_tizen: ^1.1.3 + sensors_plus_tizen: ^1.1.4 ``` Then you can import `sensors_plus` in your Dart code: diff --git a/packages/sensors_plus/example/integration_test/sensors_plus_test.dart b/packages/sensors_plus/example/integration_test/sensors_plus_test.dart index 9981a2834..03e6ca732 100644 --- a/packages/sensors_plus/example/integration_test/sensors_plus_test.dart +++ b/packages/sensors_plus/example/integration_test/sensors_plus_test.dart @@ -1,7 +1,6 @@ // Copyright 2019, the Chromium project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. - import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:sensors_plus/sensors_plus.dart'; @@ -14,7 +13,8 @@ void main() { (WidgetTester tester) async { final completer = Completer(); late StreamSubscription subscription; - subscription = accelerometerEvents.listen((AccelerometerEvent event) { + subscription = + accelerometerEventStream().listen((AccelerometerEvent event) { completer.complete(event); subscription.cancel(); }); diff --git a/packages/sensors_plus/pubspec.yaml b/packages/sensors_plus/pubspec.yaml index c7bc88fce..a942877bb 100644 --- a/packages/sensors_plus/pubspec.yaml +++ b/packages/sensors_plus/pubspec.yaml @@ -2,7 +2,7 @@ name: sensors_plus_tizen description: Tizen implementation of the sensors plugin. homepage: https://github.com/flutter-tizen/plugins repository: https://github.com/flutter-tizen/plugins/tree/master/packages/sensors_plus -version: 1.1.3 +version: 1.1.4 environment: sdk: ">=2.18.0 <4.0.0" diff --git a/packages/sensors_plus/tizen/src/device_sensor.cc b/packages/sensors_plus/tizen/src/device_sensor.cc index a840c510c..a23873d78 100644 --- a/packages/sensors_plus/tizen/src/device_sensor.cc +++ b/packages/sensors_plus/tizen/src/device_sensor.cc @@ -24,8 +24,8 @@ sensor_type_e ToTizenSensorType(const SensorType &sensor_type) { } // namespace -DeviceSensor::DeviceSensor(SensorType sensor_type) - : sensor_type_(sensor_type) {} +DeviceSensor::DeviceSensor(SensorType sensor_type, int interval) + : sensor_type_(sensor_type), interval_(interval) {} DeviceSensor::~DeviceSensor() { if (is_listening_) { @@ -67,7 +67,7 @@ bool DeviceSensor::StartListen(SensorEventCallback callback) { } ret = sensor_listener_set_event_cb( - listener_, 60, + listener_, interval_, [](sensor_h sensor, sensor_event_s *event, void *user_data) { auto *self = static_cast(user_data); SensorEvent sensor_event; diff --git a/packages/sensors_plus/tizen/src/device_sensor.h b/packages/sensors_plus/tizen/src/device_sensor.h index ed55b148d..2d3857ccc 100644 --- a/packages/sensors_plus/tizen/src/device_sensor.h +++ b/packages/sensors_plus/tizen/src/device_sensor.h @@ -19,7 +19,7 @@ typedef std::function SensorEventCallback; class DeviceSensor { public: - DeviceSensor(SensorType sensor_type); + DeviceSensor(SensorType sensor_type, int interval); ~DeviceSensor(); int GetLastError() { return last_error_; } @@ -32,6 +32,7 @@ class DeviceSensor { private: SensorType sensor_type_; + int interval_ = 0; sensor_listener_h listener_ = nullptr; bool is_listening_ = false; int last_error_ = TIZEN_ERROR_NONE; diff --git a/packages/sensors_plus/tizen/src/sensors_plus_plugin.cc b/packages/sensors_plus/tizen/src/sensors_plus_plugin.cc index e36cb0c39..bcb45b508 100644 --- a/packages/sensors_plus/tizen/src/sensors_plus_plugin.cc +++ b/packages/sensors_plus/tizen/src/sensors_plus_plugin.cc @@ -7,22 +7,26 @@ #include #include #include +#include #include #include #include #include "device_sensor.h" +#include "log.h" typedef flutter::EventChannel FlEventChannel; typedef flutter::EventSink FlEventSink; +typedef flutter::MethodChannel FlMethodChannel; typedef flutter::StreamHandler FlStreamHandler; typedef flutter::StreamHandlerError FlStreamHandlerError; class DeviceSensorStreamHandler : public FlStreamHandler { public: - DeviceSensorStreamHandler(SensorType type) : sensor_(type) {} + DeviceSensorStreamHandler(SensorType type, int32_t interval) + : sensor_(type, interval) {} protected: std::unique_ptr OnListenInternal( @@ -58,7 +62,7 @@ class SensorsPlusPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { auto plugin = std::make_unique(); - plugin->SetupEventChannels(registrar); + plugin->SetupEventChannels(registrar, plugin.get()); registrar->AddPlugin(std::move(plugin)); } @@ -67,15 +71,26 @@ class SensorsPlusPlugin : public flutter::Plugin { virtual ~SensorsPlusPlugin() {} private: - void SetupEventChannels(flutter::PluginRegistrar *registrar) { + void SetupEventChannels(flutter::PluginRegistrar *registrar, + SensorsPlusPlugin *plugin) { + std::unique_ptr method_channel = + std::make_unique( + registrar->messenger(), "dev.fluttercommunity.plus/sensors/method", + &flutter::StandardMethodCodec::GetInstance()); + + method_channel->SetMethodCallHandler( + [plugin_pointer = plugin](const auto &call, auto result) { + plugin_pointer->HandleMethodCall(call, std::move(result)); + }); + std::unique_ptr accelerometer_channel = std::make_unique( registrar->messenger(), "dev.fluttercommunity.plus/sensors/accelerometer", &flutter::StandardMethodCodec::GetInstance()); accelerometer_channel->SetStreamHandler( - std::make_unique( - SensorType::kAccelerometer)); + std::make_unique(SensorType::kAccelerometer, + interval_)); std::unique_ptr gyroscope_channel = std::make_unique( @@ -83,7 +98,8 @@ class SensorsPlusPlugin : public flutter::Plugin { "dev.fluttercommunity.plus/sensors/gyroscope", &flutter::StandardMethodCodec::GetInstance()); gyroscope_channel->SetStreamHandler( - std::make_unique(SensorType::kGyroscope)); + std::make_unique(SensorType::kGyroscope, + interval_)); std::unique_ptr user_accel_channel = std::make_unique( @@ -91,7 +107,8 @@ class SensorsPlusPlugin : public flutter::Plugin { "dev.fluttercommunity.plus/sensors/user_accel", &flutter::StandardMethodCodec::GetInstance()); user_accel_channel->SetStreamHandler( - std::make_unique(SensorType::kUserAccel)); + std::make_unique(SensorType::kUserAccel, + interval_)); std::unique_ptr magnetometer_channel = std::make_unique( @@ -99,8 +116,42 @@ class SensorsPlusPlugin : public flutter::Plugin { "dev.fluttercommunity.plus/sensors/magnetometer", &flutter::StandardMethodCodec::GetInstance()); magnetometer_channel->SetStreamHandler( - std::make_unique(SensorType::kMagnetometer)); + std::make_unique(SensorType::kMagnetometer, + interval_)); } + + void HandleMethodCall( + const flutter::MethodCall &method_call, + std::unique_ptr> result) { + const std::string &method_name = method_call.method_name(); + const flutter::EncodableValue *arguments = method_call.arguments(); + + if (method_name == "setAccelerationSamplingPeriod" || + method_name == "setGyroscopeSamplingPeriod" || + method_name == "setUserAccelerometerSamplingPeriod" || + method_name == "setMagnetometerSamplingPeriod") { + const auto *sampling_period = std::get_if(arguments); + if (sampling_period) { + // TODO(jsuya): In the sensor_plus_platform_interface, the + // set***SamplingPeriod method call is called only before creating an + // Event channel. Tizen has sensor_listener_set_interval, but it does + // not support setting the sampling period after creating an event + // channel because there is no use case. This may be supported in the + // future. + interval_ = *sampling_period / 1000; // sampling_period is microsecond + // and interval_ is millisecond. + } else { + result->Error("Invalid argument", "No sampling period provided."); + return; + } + } else { + result->NotImplemented(); + return; + } + result->Success(); + } + + int interval_ = 0; }; void SensorsPlusPluginRegisterWithRegistrar(