SensorEvent.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef __SENSOR_EVENT_H
  2. #define __SENSOR_EVENT_H
  3. #pragma once
  4. #include "stdafx.h"
  5. #include <InitGuid.h>
  6. #include <SensorsApi.h>
  7. #include <Sensors.h>
  8. class CMyEvents : public ISensorEvents
  9. {
  10. public:
  11. STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  12. {
  13. if (ppv == NULL)
  14. {
  15. return E_POINTER;
  16. }
  17. if (iid == __uuidof(IUnknown))
  18. {
  19. *ppv = static_cast<IUnknown*>(this);
  20. }
  21. else if (iid == __uuidof(ISensorEvents))
  22. {
  23. *ppv = static_cast<ISensorEvents*>(this);
  24. }
  25. else
  26. {
  27. *ppv = NULL;
  28. return E_NOINTERFACE;
  29. }
  30. AddRef();
  31. m_count = 0;
  32. return S_OK;
  33. }
  34. STDMETHODIMP_(ULONG) AddRef()
  35. {
  36. return InterlockedIncrement(&m_cRef);
  37. }
  38. STDMETHODIMP_(ULONG) Release()
  39. {
  40. ULONG count = InterlockedDecrement(&m_cRef);
  41. if (count == 0)
  42. {
  43. delete this;
  44. return 0;
  45. }
  46. return count;
  47. }
  48. //
  49. // ISensorEvents methods.
  50. //
  51. STDMETHODIMP OnEvent(
  52. ISensor *pSensor,
  53. REFGUID eventID,
  54. IPortableDeviceValues *pEventData)
  55. {
  56. HRESULT hr = S_OK;
  57. // Handle custom events here.
  58. return hr;
  59. }
  60. STDMETHODIMP OnLeave(
  61. REFSENSOR_ID sensorID)
  62. {
  63. HRESULT hr = S_OK;
  64. // Peform any housekeeping tasks for the sensor that is leaving.
  65. // For example, if you have maintained a reference to the sensor,
  66. // release it now and set the pointer to NULL.
  67. return hr;
  68. }
  69. STDMETHODIMP OnDataUpdated(ISensor *pSensor,ISensorDataReport *pNewData);
  70. STDMETHODIMP OnStateChanged(
  71. ISensor* pSensor,
  72. SensorState state)
  73. {
  74. HRESULT hr = S_OK;
  75. if(NULL == pSensor)
  76. {
  77. return E_INVALIDARG;
  78. }
  79. if(state == SENSOR_STATE_READY)
  80. {
  81. wprintf_s(L"\nTime sensor is now ready.");
  82. }
  83. else if(state == SENSOR_STATE_ACCESS_DENIED)
  84. {
  85. wprintf_s(L"\nNo permission for the time sensor.\n");
  86. wprintf_s(L"Enable the sensor in the control panel.\n");
  87. }
  88. return hr;
  89. }
  90. private:
  91. long m_cRef;
  92. int m_count;
  93. };
  94. #endif //__SENSOR_EVENT_H