tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / API / Haptics / HapticPattern.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  *
18  *
19  * @file       HapticPattern.cpp
20  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24
25 #include "HapticPattern.h"
26 #include <Commons/Exception.h>
27
28 namespace WrtDeviceApis {
29 namespace Haptics {
30 namespace Api {
31 void HapticPattern::addInterval(unsigned long duration,
32         bool vibrate)
33 {
34     if (0 == duration) {
35         return;
36     }
37
38     // empty sequence
39     if (0 == m_sequence.size()) {
40         m_startsActive = vibrate;
41         m_sequence.push_back(duration);
42         return;
43     }
44
45     // check if last interval is action or pause
46     bool lastVibrates = m_startsActive ^ ((m_sequence.size() & 0x1) == 0);
47
48     if (lastVibrates == vibrate) {
49         *m_sequence.rbegin() += duration; // extend last interval
50     } else {
51         m_sequence.push_back(duration); // add new interval
52     }
53 }
54
55 bool HapticPattern::isActive(size_t interval) const
56 {
57     if (interval >= m_sequence.size()) {
58         ThrowMsg(Commons::OutOfRangeException,
59                  "Interval index is out of haptic sequence range");
60     }
61
62     return m_startsActive ^ ((interval & 0x1) != 0);
63 }
64
65 unsigned long HapticPattern::duration(size_t interval) const
66 {
67     if (interval >= m_sequence.size()) {
68         return 0;
69     }
70
71     return m_sequence[interval];
72 }
73 } // Api
74 } // Haptics
75 } // WrtDeviceApis