2 using System.Collections.Generic;
7 /// The Haptic class provides properties and methods to control a vibrator.
9 public class Vibrator : IDisposable
11 private readonly int _vibratorId;
12 private static readonly Dictionary<int, Vibrator> s_vibrators = new Dictionary<int, Vibrator>();
13 private IntPtr _hapticHandle;
14 private bool _disposedValue = false;
15 private Vibrator(int id)
18 DeviceError res = (DeviceError) Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
19 if (res != DeviceError.None)
21 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
31 /// Get the Vibrator instance for the given vibrator index.
33 /// <param name="deviceId"> The index of the vibrator.
34 /// It can be greater than or equal to 0 and less than the number of vibrators. </param>
35 public static Vibrator GetVibrator(int deviceNumber)
37 if (deviceNumber < 0 || deviceNumber >= NumberOfVibrators)
39 throw DeviceExceptionFactory.CreateException(DeviceError.InvalidParameter, "invalid vibrator number");
41 if (!s_vibrators.ContainsKey(deviceNumber))
43 s_vibrators.Add(deviceNumber, new Vibrator(deviceNumber));
45 return s_vibrators[deviceNumber];
48 /// Gets the number of vibrators.
50 public static int NumberOfVibrators
55 DeviceError res = (DeviceError) Interop.Device.DeviceHapticGetCount(out count);
56 if (res != DeviceError.None)
58 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
64 /// Vibrates during the specified time with a constant intensity.
65 /// This function can be used to start monotonous vibration for the specified time.
67 /// <param name="duration">The play duration in milliseconds </param>
68 /// <param name="feedback">The amount of the intensity variation (0 ~ 100) </param>
69 public void Vibrate(int duration, int feedback)
72 DeviceError res = DeviceError.None;
73 if (_hapticHandle == IntPtr.Zero)
75 res = (DeviceError) Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
76 if (res != DeviceError.None)
78 throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
82 res = (DeviceError) Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
83 if (res != DeviceError.None)
85 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
89 /// Stops all vibration effects which are being played.
90 /// This function can be used to stop all effects started by Vibrate().
94 if (_hapticHandle != IntPtr.Zero)
96 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
97 if (res != DeviceError.None)
99 throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
104 /// Dispose API for closing the internal resources.
105 /// This function can be used to stop all effects started by Vibrate().
107 public void Dispose()
110 GC.SuppressFinalize(this);
113 protected virtual void Dispose(bool disposing)
117 if (_hapticHandle != IntPtr.Zero)
119 Interop.Device.DeviceHapticClose(_hapticHandle);
120 _hapticHandle = IntPtr.Zero;
122 _disposedValue = true;