Skip to content

Commit

Permalink
[sensors] Add sensors_tizen package (#4)
Browse files Browse the repository at this point in the history
* [sensors] Add sensors_tizen package

* Update copyright notice

* Replace integration_test.dart

Co-authored-by: Boram Bae <boram21.bae@samsung.com>
  • Loading branch information
swift-kim and bbrto21 committed Dec 9, 2020
1 parent b107f18 commit 2e11a38
Show file tree
Hide file tree
Showing 23 changed files with 841 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/sensors/.gitignore
@@ -0,0 +1,7 @@
.DS_Store
.dart_tool/

.packages
.pub/

build/
3 changes: 3 additions & 0 deletions packages/sensors/CHANGELOG.md
@@ -0,0 +1,3 @@
## 1.0.0

* Initial release.
26 changes: 26 additions & 0 deletions packages/sensors/LICENSE
@@ -0,0 +1,26 @@
Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
Copyright (c) 2017 The Chromium Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the names of the copyright holders nor the names of the
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 27 additions & 0 deletions packages/sensors/README.md
@@ -0,0 +1,27 @@
# sensors_tizen

The Tizen implementation of [`sensors`](https://github.com/flutter/plugins/tree/master/packages/sensors).

## Usage

This package is not an _endorsed_ implementation of `sensors`. Therefore, you have to include `sensors_tizen` alongside `sensors` as dependencies in your `pubspec.yaml` file.

```yaml
dependencies:
sensors: ^0.4.0
sensors_tizen: ^1.0.0
```

Then you can import `sensors` in your Dart code:

```dart
import 'package:sensors/sensors.dart';
```

For detailed usage, see https://github.com/flutter/plugins/tree/master/packages/sensors#usage.

## Supported devices

This plugin is supported on these types of devices:

- Galaxy Watch (running Tizen 5.5 or later)
41 changes: 41 additions & 0 deletions packages/sensors/example/.gitignore
@@ -0,0 +1,41 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json
7 changes: 7 additions & 0 deletions packages/sensors/example/README.md
@@ -0,0 +1,7 @@
# sensors_example

Demonstrates how to use the sensors plugin.

## Getting Started

To run this app on your Tizen device, use [flutter-tizen](https://github.com/flutter-tizen/flutter-tizen).
144 changes: 144 additions & 0 deletions packages/sensors/example/lib/main.dart
@@ -0,0 +1,144 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:sensors/sensors.dart';

import 'snake.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sensors Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
static const int _snakeRows = 20;
static const int _snakeColumns = 20;
static const double _snakeCellSize = 10.0;

List<double> _accelerometerValues;
List<double> _userAccelerometerValues;
List<double> _gyroscopeValues;
List<StreamSubscription<dynamic>> _streamSubscriptions =
<StreamSubscription<dynamic>>[];

@override
Widget build(BuildContext context) {
final List<String> accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1))?.toList();
final List<String> gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1))?.toList();
final List<String> userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
?.toList();

return Scaffold(
appBar: AppBar(
title: const Text('Sensor Example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Center(
child: DecoratedBox(
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.black38),
),
child: SizedBox(
height: _snakeRows * _snakeCellSize,
width: _snakeColumns * _snakeCellSize,
child: Snake(
rows: _snakeRows,
columns: _snakeColumns,
cellSize: _snakeCellSize,
),
),
),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Accelerometer: $accelerometer'),
],
),
padding: const EdgeInsets.all(16.0),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('UserAccelerometer: $userAccelerometer'),
],
),
padding: const EdgeInsets.all(16.0),
),
Padding(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Gyroscope: $gyroscope'),
],
),
padding: const EdgeInsets.all(16.0),
),
],
),
);
}

@override
void dispose() {
super.dispose();
for (StreamSubscription<dynamic> subscription in _streamSubscriptions) {
subscription.cancel();
}
}

@override
void initState() {
super.initState();
_streamSubscriptions
.add(accelerometerEvents.listen((AccelerometerEvent event) {
setState(() {
_accelerometerValues = <double>[event.x, event.y, event.z];
});
}));
_streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) {
setState(() {
_gyroscopeValues = <double>[event.x, event.y, event.z];
});
}));
_streamSubscriptions
.add(userAccelerometerEvents.listen((UserAccelerometerEvent event) {
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
});
}));
}
}
130 changes: 130 additions & 0 deletions packages/sensors/example/lib/snake.dart
@@ -0,0 +1,130 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:sensors/sensors.dart';

class Snake extends StatefulWidget {
Snake({this.rows = 20, this.columns = 20, this.cellSize = 10.0}) {
assert(10 <= rows);
assert(10 <= columns);
assert(5.0 <= cellSize);
}

final int rows;
final int columns;
final double cellSize;

@override
State<StatefulWidget> createState() => SnakeState(rows, columns, cellSize);
}

class SnakeBoardPainter extends CustomPainter {
SnakeBoardPainter(this.state, this.cellSize);

GameState state;
double cellSize;

@override
void paint(Canvas canvas, Size size) {
final Paint blackLine = Paint()..color = Colors.black;
final Paint blackFilled = Paint()
..color = Colors.black
..style = PaintingStyle.fill;
canvas.drawRect(
Rect.fromPoints(Offset.zero, size.bottomLeft(Offset.zero)),
blackLine,
);
for (math.Point<int> p in state.body) {
final Offset a = Offset(cellSize * p.x, cellSize * p.y);
final Offset b = Offset(cellSize * (p.x + 1), cellSize * (p.y + 1));

canvas.drawRect(Rect.fromPoints(a, b), blackFilled);
}
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

class SnakeState extends State<Snake> {
SnakeState(int rows, int columns, this.cellSize) {
state = GameState(rows, columns);
}

double cellSize;
GameState state;
AccelerometerEvent acceleration;
StreamSubscription<AccelerometerEvent> _streamSubscription;
Timer _timer;

@override
Widget build(BuildContext context) {
return CustomPaint(painter: SnakeBoardPainter(state, cellSize));
}

@override
void dispose() {
super.dispose();
_streamSubscription.cancel();
_timer.cancel();
}

@override
void initState() {
super.initState();
_streamSubscription =
accelerometerEvents.listen((AccelerometerEvent event) {
setState(() {
acceleration = event;
});
});

_timer = Timer.periodic(const Duration(milliseconds: 200), (_) {
setState(() {
_step();
});
});
}

void _step() {
final math.Point<int> newDirection = acceleration == null
? null
: acceleration.x.abs() < 1.0 && acceleration.y.abs() < 1.0
? null
: (acceleration.x.abs() < acceleration.y.abs())
? math.Point<int>(0, acceleration.y.sign.toInt())
: math.Point<int>(-acceleration.x.sign.toInt(), 0);
state.step(newDirection);
}
}

class GameState {
GameState(this.rows, this.columns) {
snakeLength = math.min(rows, columns) - 5;
}

int rows;
int columns;
int snakeLength;

List<math.Point<int>> body = <math.Point<int>>[const math.Point<int>(0, 0)];
math.Point<int> direction = const math.Point<int>(1, 0);

void step(math.Point<int> newDirection) {
math.Point<int> next = body.last + direction;
next = math.Point<int>(next.x % columns, next.y % rows);

body.add(next);
if (body.length > snakeLength) body.removeAt(0);
direction = newDirection ?? direction;
}
}

0 comments on commit 2e11a38

Please sign in to comment.