Dali 3D User Interface Engine
dali-feedback.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 #define LOG_TAG "DALI_FEEDBACK"
19 
20 // CLASS HEADER
21 #include "dali-feedback.h"
22 
23 // EXTERNAL INCLUDES
24 #include <iostream>
25 #include <fstream>
26 #include <feedback.h>
27 #include <mm_sound.h>
28 #include <mm_sound_private.h>
29 
30 #include <dlog.h>
31 
32 #define DEBUG_PRINTF(fmt, arg...) LOGD(" "fmt, ##arg)
33 
34 using std::string;
35 using namespace Dali;
36 
37 // The plugin factories
38 extern "C" DALI_EXPORT_API Dali::FeedbackPlugin* CreateFeedbackPlugin(void)
39 {
40  return new Dali::Plugin::DaliFeedback;
41 }
42 
43 namespace Dali
44 {
45 
46 namespace Plugin
47 {
48 
49 DaliFeedback::DaliFeedback()
50 : mHapticInitialized( false )
51 {
52  feedback_initialize();
53 
54  if( 0 == haptic_open( HAPTIC_DEVICE_0, &mDeviceHandle ) )
55  {
56  mHapticInitialized = true;
57  }
58  else
59  {
60  DEBUG_PRINTF( "Haptic feedback controller failed to initialize\n" );
61  }
62 }
63 
65 {
66  if( mHapticInitialized )
67  {
68  int errorCode = haptic_close( mDeviceHandle );
69  if( errorCode < 0 )
70  {
71  DEBUG_PRINTF( "device_haptic_close() failed with error code: %d\n", errorCode );
72  }
73  }
74 
75  feedback_deinitialize();
76 }
77 
78 void DaliFeedback::PlayHaptic( const std::string& filePath )
79 {
80  if( mHapticInitialized )
81  {
82  if ( filePath.size() == 0 )
83  {
84  DEBUG_PRINTF( "File Path can't be NULL!\n" );
85  return;
86  }
87 
88  int errorCode = haptic_vibrate_file_with_detail( mDeviceHandle, filePath.c_str(), HAPTIC_ITERATION_ONCE, HAPTIC_FEEDBACK_AUTO, HAPTIC_PRIORITY_MIN, NULL );
89  if( errorCode != 0 )
90  {
91  DEBUG_PRINTF( "PlayHaptic() failed with error code: %d\n", errorCode );
92  }
93  }
94  else
95  {
96  DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
97  }
98 }
99 
100 void DaliFeedback::PlayHapticMonotone( unsigned int duration )
101 {
102  if( mHapticInitialized )
103  {
104  int errorCode = haptic_vibrate_monotone_with_detail( mDeviceHandle, duration, HAPTIC_FEEDBACK_AUTO, HAPTIC_PRIORITY_MIN, NULL );
105  if( errorCode != 0 )
106  {
107  DEBUG_PRINTF( "PlayHapticMonotone() failed with error code: %d\n", errorCode );
108  }
109  }
110  else
111  {
112  DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
113  }
114 }
115 
117 {
118  if( mHapticInitialized )
119  {
120  int errorCode = haptic_stop_all_effects( mDeviceHandle );
121  if( errorCode != 0 )
122  {
123  DEBUG_PRINTF( "StopHaptic() failed with error code: %d\n", errorCode );
124  }
125  }
126  else
127  {
128  DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
129  }
130 }
131 
132 int DaliFeedback::PlaySound( const std::string& fileName )
133 {
134  int handle = -1;
135  int errorCode = mm_sound_play_keysound( fileName.c_str(), VOLUME_TYPE_SYSTEM & VOLUME_GAIN_TOUCH );
136  if( errorCode < 0 )
137  {
138  DEBUG_PRINTF( "PlaySound() %s failed with error code = %d\n", fileName.c_str(), errorCode );
139  }
140 
141  return handle;
142 }
143 
144 void DaliFeedback::StopSound( int handle )
145 {
146  int errorCode = mm_sound_stop_sound( handle );
147  if( errorCode < 0 )
148  {
149  DEBUG_PRINTF( "StopSound() handle = %d failed with error code = %d\n", handle, errorCode);
150  }
151  else
152  {
153  DEBUG_PRINTF( "stop handle %d success\n", handle );
154  }
155 }
156 
157 void DaliFeedback::PlayFeedbackPattern( int type, int pattern )
158 {
159  int errorCode = feedback_play_type( static_cast<feedback_type_e>(type), static_cast<feedback_pattern_e>(pattern) );
160  if( errorCode != 0 )
161  {
162  DEBUG_PRINTF( "DaliFeedback::PlayFeedbackPattern() with type = %d, pattern = %d returned with error = %d\n", (int)type, (int)pattern, errorCode );
163  }
164 }
165 
166 } // namespace Plugin
167 
168 } // namespace Dali
169 
Dali Docs Home
Read more about Dali