|
1 /* |
|
2 * |
|
3 * Copyright 2013 Canonical Ltd. |
|
4 * |
|
5 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 * you may not use this file except in compliance with the License. |
|
7 * You may obtain a copy of the License at |
|
8 * |
|
9 * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 * |
|
11 * Unless required by applicable law or agreed to in writing, |
|
12 * software distributed under the License is distributed on an |
|
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
14 * KIND, either express or implied. See the License for the |
|
15 * specific language governing permissions and limitations |
|
16 * under the License. |
|
17 * |
|
18 */ |
|
19 |
|
20 #include "vibration.h" |
|
21 |
|
22 void Vibration::vibrate(int, int, int mills) { |
|
23 QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create(); |
|
24 vibrate->setIntensity(1.0); |
|
25 vibrate->setDuration(mills); |
|
26 |
|
27 vibrate->start(); |
|
28 |
|
29 _effects.append(vibrate); |
|
30 } |
|
31 |
|
32 void Vibration::cancelVibration(int, int) { |
|
33 _timers.clear(); |
|
34 _effects.clear(); |
|
35 } |
|
36 |
|
37 void Vibration::vibrateWithPattern(int, int, const QList<int> &pattern, int repeat) { |
|
38 QSharedPointer<QTimer> timer = QSharedPointer<QTimer>::create(); |
|
39 QSharedPointer<int> k = QSharedPointer<int>::create(); |
|
40 |
|
41 QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create(); |
|
42 vibrate->setIntensity(1.0); |
|
43 |
|
44 _effects.append(vibrate); |
|
45 _timers.append(timer); |
|
46 |
|
47 timer->connect(timer.data(), &QTimer::timeout, [=, timer = timer.data()] () { |
|
48 if (*k >= pattern.size()) { |
|
49 if (repeat < 0 || repeat >= pattern.size()) { |
|
50 timer->stop(); |
|
51 return; |
|
52 } |
|
53 *k = repeat; |
|
54 } |
|
55 bool idle = (*k % 2 == 0); |
|
56 if (!idle) { |
|
57 vibrate->setDuration(pattern[*k]); |
|
58 vibrate->start(); |
|
59 } |
|
60 timer->start(pattern[*k]); |
|
61 (*k)++; |
|
62 }); |
|
63 timer->start(1); |
|
64 } |