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.

     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 */
    20 #include "vibration.h"
    22 void Vibration::vibrate(int, int, int mills) {
    23     QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create();
    24     vibrate->setIntensity(1.0);
    25     vibrate->setDuration(mills);
    27     vibrate->start();
    29     _effects.append(vibrate);
    30 }
    32 void Vibration::cancelVibration(int, int) {
    33     _timers.clear();
    34     _effects.clear();
    35 }
    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();
    41     QSharedPointer<QFeedbackHapticsEffect> vibrate = QSharedPointer<QFeedbackHapticsEffect>::create();
    42     vibrate->setIntensity(1.0);
    44     _effects.append(vibrate);
    45     _timers.append(timer);
    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 }

mercurial