Touchgui/plugins/org.apache.cordova.vibration/src/ubuntu/vibration.cpp

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

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

mercurial