Skip to content

Commit

Permalink
[sensor_plus] Implement set*SamplingPeriod method call
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA committed Nov 28, 2023
1 parent 7c3b965 commit 97da567
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 19 deletions.
7 changes: 7 additions & 0 deletions packages/sensors_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.1.4

* Implement set*SamplingPeriod method call.
* Update deprecated API in integration_test.
* Update sensors_plus to 4.0.1.
* Update sensors_plus_platform_interface to 1.2.0.

## 1.1.3

* Update sensors_plus to 3.0.2.
Expand Down
4 changes: 2 additions & 2 deletions packages/sensors_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ 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: ^4.0.1
sensors_plus_tizen: ^1.1.4
```
Then you can import `sensors_plus` in your Dart code:
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,7 +13,8 @@ void main() {
(WidgetTester tester) async {
final completer = Completer<AccelerometerEvent>();
late StreamSubscription<AccelerometerEvent> subscription;
subscription = accelerometerEvents.listen((AccelerometerEvent event) {
subscription =
accelerometerEventStream().listen((AccelerometerEvent event) {
completer.complete(event);
subscription.cancel();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/sensors_plus/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
sensors_plus: ^3.0.2
sensors_plus: ^4.0.1
sensors_plus_tizen:
path: ../

Expand Down
4 changes: 2 additions & 2 deletions packages/sensors_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,7 +18,7 @@ flutter:
dependencies:
flutter:
sdk: flutter
sensors_plus_platform_interface: ^1.1.3
sensors_plus_platform_interface: ^1.2.0

dev_dependencies:
flutter_lints: ^2.0.1
6 changes: 3 additions & 3 deletions packages/sensors_plus/tizen/src/device_sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand Down Expand Up @@ -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<DeviceSensor *>(user_data);
SensorEvent sensor_event;
Expand Down
3 changes: 2 additions & 1 deletion packages/sensors_plus/tizen/src/device_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef std::function<void(SensorEvent)> SensorEventCallback;

class DeviceSensor {
public:
DeviceSensor(SensorType sensor_type);
DeviceSensor(SensorType sensor_type, int interval);
~DeviceSensor();

int GetLastError() { return last_error_; }
Expand All @@ -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;
Expand Down
67 changes: 59 additions & 8 deletions packages/sensors_plus/tizen/src/sensors_plus_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
#include <flutter/event_channel.h>
#include <flutter/event_sink.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar.h>
#include <flutter/standard_method_codec.h>

#include <memory>

#include "device_sensor.h"
#include "log.h"

typedef flutter::EventChannel<flutter::EncodableValue> FlEventChannel;
typedef flutter::EventSink<flutter::EncodableValue> FlEventSink;
typedef flutter::MethodChannel<flutter::EncodableValue> FlMethodChannel;
typedef flutter::StreamHandler<flutter::EncodableValue> FlStreamHandler;
typedef flutter::StreamHandlerError<flutter::EncodableValue>
FlStreamHandlerError;

class DeviceSensorStreamHandler : public FlStreamHandler {
public:
DeviceSensorStreamHandler(SensorType type) : sensor_(type) {}
DeviceSensorStreamHandler(SensorType type, int32_t interval)
: sensor_(type, interval) {}

protected:
std::unique_ptr<FlStreamHandlerError> OnListenInternal(
Expand Down Expand Up @@ -58,7 +62,7 @@ class SensorsPlusPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
auto plugin = std::make_unique<SensorsPlusPlugin>();
plugin->SetupEventChannels(registrar);
plugin->SetupEventChannels(registrar, plugin.get());
registrar->AddPlugin(std::move(plugin));
}

Expand All @@ -67,40 +71,87 @@ class SensorsPlusPlugin : public flutter::Plugin {
virtual ~SensorsPlusPlugin() {}

private:
void SetupEventChannels(flutter::PluginRegistrar *registrar) {
void SetupEventChannels(flutter::PluginRegistrar *registrar,
SensorsPlusPlugin *plugin) {
std::unique_ptr<FlMethodChannel> method_channel =
std::make_unique<FlMethodChannel>(
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<FlEventChannel> accelerometer_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/accelerometer",
&flutter::StandardMethodCodec::GetInstance());
accelerometer_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(
SensorType::kAccelerometer));
std::make_unique<DeviceSensorStreamHandler>(SensorType::kAccelerometer,
interval_));

std::unique_ptr<FlEventChannel> gyroscope_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/gyroscope",
&flutter::StandardMethodCodec::GetInstance());
gyroscope_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kGyroscope));
std::make_unique<DeviceSensorStreamHandler>(SensorType::kGyroscope,
interval_));

std::unique_ptr<FlEventChannel> user_accel_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/user_accel",
&flutter::StandardMethodCodec::GetInstance());
user_accel_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kUserAccel));
std::make_unique<DeviceSensorStreamHandler>(SensorType::kUserAccel,
interval_));

std::unique_ptr<FlEventChannel> magnetometer_channel =
std::make_unique<FlEventChannel>(
registrar->messenger(),
"dev.fluttercommunity.plus/sensors/magnetometer",
&flutter::StandardMethodCodec::GetInstance());
magnetometer_channel->SetStreamHandler(
std::make_unique<DeviceSensorStreamHandler>(SensorType::kMagnetometer));
std::make_unique<DeviceSensorStreamHandler>(SensorType::kMagnetometer,
interval_));
}

void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> 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<int32_t>(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(
Expand Down

0 comments on commit 97da567

Please sign in to comment.