gesture.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #define APDS9960_INT_PIN 2
  2. #define APDS9960_ISR_NUM 0
  3. // gesture sensor object
  4. #if GESTURE_ENABLED && GESTURE_SPARKFUN_ENABLED
  5. #include "SparkFun_APDS9960.h"
  6. #define GESTURE_INTERRUPT 1
  7. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  8. #elif GESTURE_ENABLED && GESTURE_ADAFRUIT_ENABLED
  9. #include "APDS9960.h"
  10. APDS9960 apds;
  11. #endif // GESTURE_ENABLED
  12. // brightness value
  13. uint16_t ambientLightReading = 0;
  14. uint16_t ambientLightMaxDelta = 1000;
  15. // setup function
  16. void setupGesture () {
  17. // APDS9960 //
  18. #if GESTURE_ENABLED && GESTURE_SPARKFUN_ENABLED
  19. apds.init();
  20. apds.enableGestureSensor(true);
  21. #elif GESTURE_ENABLED && GESTURE_ADAFRUIT_ENABLED
  22. apds.begin();
  23. //for gesture to work we have to have proximity enabled and to manually set GMODE to 1
  24. apds.enableProximity(true);
  25. apds.enableGesture(true);
  26. // TODO: enable light sensor
  27. // customization
  28. // onion.io
  29. // Note: LED current will affect touch buttons !
  30. /*Set LED current and Boost
  31. LED drive current 100 mA LDRIVE = 0
  32. 50 LDRIVE = 1
  33. 25 LDRIVE = 2
  34. 12.5 LDRIVE = 3
  35. LED boost 100 % LED_BOOST = 0
  36. 150 LED_BOOST = 1
  37. 200 LED_BOOST = 2
  38. 300 LED_BOOST = 3
  39. */
  40. apds.setLED((apds9960LedDrive_t)0x3, APDS9960_LEDBOOST_100PCNT); //onion.io Aug 16 2018: lower LED power improves touch performance, interface in the library is fucked up, if using the enum it'll be too large to fit
  41. #endif // GESTURE_ENABLED
  42. }
  43. void setupAmbientLight () {
  44. // initialize - if not already initialized
  45. #if GESTURE_ENABLED && GESTURE_SPARKFUN_ENABLED
  46. uint8_t mode = apds.getMode();
  47. if (mode == 0x00 || mode == 0xff) {
  48. apds.init();
  49. }
  50. apds.enableLightSensor(false);
  51. #endif // GESTURE_ENABLED
  52. }
  53. // helper functions
  54. void sendAmbientLightReading() {
  55. char buf[64];
  56. sprintf(buf, "%d", ambientLightReading);
  57. sendCommand(CMD_SEND_AMBIENT_LIGHT, buf);
  58. }
  59. // handle gesture data
  60. void handleGesture() {
  61. #if GESTURE_ENABLED && GESTURE_SPARKFUN_ENABLED
  62. if ( apds.isGestureAvailable() ) {
  63. switch ( apds.readGesture() ) {
  64. case DIR_UP:
  65. sendCommand(CMD_SEND_GESTURE, "U");
  66. break;
  67. case DIR_DOWN:
  68. sendCommand(CMD_SEND_GESTURE, "D");
  69. break;
  70. case DIR_LEFT:
  71. sendCommand(CMD_SEND_GESTURE, "L");
  72. break;
  73. case DIR_RIGHT:
  74. sendCommand(CMD_SEND_GESTURE, "R");
  75. break;
  76. case DIR_NEAR:
  77. sendCommand(CMD_SEND_GESTURE, "N");
  78. break;
  79. case DIR_FAR:
  80. sendCommand(CMD_SEND_GESTURE, "F");
  81. break;
  82. default:
  83. sendCommand(CMD_SEND_GESTURE, "X");
  84. break;
  85. }
  86. }
  87. #elif GESTURE_ENABLED && GESTURE_ADAFRUIT_ENABLED
  88. switch ( apds.readGesture() ) {
  89. case APDS9960_UP:
  90. sendCommand(CMD_SEND_GESTURE, "U");
  91. break;
  92. case APDS9960_DOWN:
  93. sendCommand(CMD_SEND_GESTURE, "D");
  94. break;
  95. case APDS9960_LEFT:
  96. sendCommand(CMD_SEND_GESTURE, "L");
  97. break;
  98. case APDS9960_RIGHT:
  99. sendCommand(CMD_SEND_GESTURE, "R");
  100. break;
  101. default:
  102. // do nothing
  103. break;
  104. }
  105. #endif // GESTURE_ENABLED
  106. }
  107. void handleAmbientLight() {
  108. uint16_t val = 0;
  109. uint16_t delta;
  110. bool status = false;
  111. #if GESTURE_ENABLED && GESTURE_SPARKFUN_ENABLED
  112. status = apds.readAmbientLight(val);
  113. #endif // GESTURE_ENABLED
  114. if (status) {
  115. // find the delta (abs function doesn't work for uint)
  116. if (ambientLightReading > val) {
  117. delta = ambientLightReading - val;
  118. } else {
  119. delta = val - ambientLightReading;
  120. }
  121. // update the reading
  122. ambientLightReading = val;
  123. // communicate immediately if change is larger than delta
  124. if (delta >= ambientLightMaxDelta) {
  125. // Serial.print("Large delta detected, delta = ");
  126. // Serial.println(delta, DEC);
  127. sendAmbientLightReading();
  128. }
  129. }
  130. }