touch.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #define JG106C_ADDRESS 0x2c
  2. #define JG106C_INT_PIN 3
  3. #define JG106C_ISR_NUM 1
  4. #if defined TOUCH_ENABLED && TOUCH_ENABLED == 1
  5. #include <Wire.h>
  6. #if (ARDUINO >= 100)
  7. #include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
  8. // Macro to deal with the difference in I2C write functions from old and new Arduino versions.
  9. #define _I2C_WRITE write
  10. #define _I2C_READ read
  11. #else
  12. #include <WProgram.h>
  13. #define _I2C_WRITE send
  14. #define _I2C_READ receive
  15. #endif
  16. #define JG106C_SENS_REGISTER 0x10
  17. #define JG106C_SLP_SENS_REGISTER 0x20
  18. #define TOUCH_NUM_KEYS 4
  19. #define TOUCH_KEY0 0
  20. #define TOUCH_KEY1 1
  21. #define TOUCH_KEY2 2
  22. #define TOUCH_KEY3 3
  23. static void read_i2c_bytes(uint8_t addr, uint8_t numBytes, uint8_t *values) {
  24. uint8_t count = 0;
  25. // Wire.beginTransmission(addr);
  26. // Wire._I2C_WRITE((byte)reg);
  27. // Wire.endTransmission();
  28. Wire.requestFrom(addr, (byte)2, (byte)true);
  29. while (Wire.available()) {
  30. if (count < numBytes) {
  31. values[count] = Wire.read();
  32. count++;
  33. }
  34. else {
  35. // read to clear the buffer
  36. Wire.read();
  37. }
  38. }
  39. }
  40. static void write_i2c_register(uint8_t addr, uint8_t reg, uint8_t val) {
  41. Wire.beginTransmission(addr);
  42. Wire._I2C_WRITE((byte)reg);
  43. Wire._I2C_WRITE((byte)val);
  44. Wire.endTransmission();
  45. }
  46. void setButtonSensitivity (uint8_t button, uint8_t sensitivity) {
  47. uint8_t regVal;
  48. // set sensitivity
  49. regVal = JG106C_SENS_REGISTER | button;
  50. write_i2c_register(JG106C_ADDRESS, regVal, sensitivity);
  51. // set sleep sensitivity to the same
  52. regVal = JG106C_SLP_SENS_REGISTER | button;
  53. write_i2c_register(JG106C_ADDRESS, regVal, sensitivity);
  54. }
  55. void touchInit() {
  56. delay(300);
  57. Wire.begin();
  58. // initialize the IC
  59. write_i2c_register(JG106C_ADDRESS, (uint8_t)0x05, (uint8_t)TOUCH_NUM_KEYS);
  60. // byte0:
  61. // Bit2 - KOM (Key Output Mode)
  62. // 0 = single touch; 1 = multi-touch
  63. // Bit1 - RT (Reset Enable)
  64. // 0 = disabled; 1 = enabled
  65. // Bit0 - SLP (Power Saving mode - enter after 4 seconds of inactivity)
  66. // 0 = disabled; 1 = enabled
  67. // byte1:
  68. // defines how many keys are required
  69. // set the sensitivity
  70. setButtonSensitivity(TOUCH_KEY0, 0x0a);
  71. setButtonSensitivity(TOUCH_KEY1, 0x0a);
  72. setButtonSensitivity(TOUCH_KEY2, 0x0a);
  73. setButtonSensitivity(TOUCH_KEY3, 0x0a);
  74. }
  75. uint8_t handleTouch() {
  76. uint8_t buttons[2];
  77. // read the button status
  78. read_i2c_bytes(JG106C_ADDRESS, 2, buttons);
  79. return buttons[0];
  80. }
  81. void sendButtonCommand(uint8_t buttons, uint32_t timePressed) {
  82. char buf[64];
  83. // send button status via serial
  84. sprintf(buf, "%02x%lu", buttons, timePressed);
  85. sendCommand(CMD_SEND_TOUCH, buf);
  86. }
  87. #endif // TOUCH_ENABLED