Seeed_QTouch.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. QTouch Library
  3. 2014 Copyright (c) Seeed Technology Inc. All right reserved.
  4. Author: ZhangKun & Loovee
  5. 2013-3-20
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <Wire.h>
  19. #include <Arduino.h>
  20. #include "Seeed_QTouch.h"
  21. SeeedQTouch::SeeedQTouch() {
  22. Wire.begin();
  23. }
  24. // Check chip ID
  25. int SeeedQTouch::chipPresent(void) {
  26. if (readReg(QTOUCH_REG_CHIPID) == 0x2e) {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. // if certain key touched, return 1 - touched, 0 untouched
  32. unsigned char SeeedQTouch::isTouch(int key) {
  33. if (key > 7) {
  34. return 0; // err input
  35. }
  36. unsigned char st = getState();
  37. return ((0x01 << key)&st); // if touched return 1, else return 0
  38. }
  39. // return all key state, bit0 for key0, bit1 for key1....
  40. unsigned char SeeedQTouch::getState() {
  41. return readReg(0x03);
  42. }
  43. int SeeedQTouch::touchNum() {
  44. unsigned char state = getState();
  45. if (0 == state) {
  46. return -1;
  47. }
  48. for (int i = 0; i < 7; i++) {
  49. if ((state >> i) & 0x01) {
  50. return i;
  51. }
  52. }
  53. return -1;
  54. }
  55. // Set group (0-3) for a key or disable the key (255)
  56. int SeeedQTouch::setGroup(uint8_t key, uint8_t group) {
  57. uint8_t value;
  58. if (key > 6) {
  59. return -1;
  60. }
  61. if (group < 4) {
  62. value = readReg(QTOUCH_REG_AVEASK0 + key) & 0xfc;
  63. if (value == 0) {
  64. value = 8 << 2; //AVE default
  65. }
  66. } else if (group == 0xff) {
  67. value = 0; //Key disabled
  68. group = 1; //AKS default
  69. } else {
  70. return -1;
  71. }
  72. writeReg(QTOUCH_REG_AVEASK0 + key, value | group);
  73. return 0;
  74. }
  75. // read register
  76. unsigned char SeeedQTouch::readReg(unsigned char addr_reg) {
  77. Wire.beginTransmission(ADDR_QTOUCH);
  78. Wire.write(addr_reg); // register to read
  79. Wire.endTransmission();
  80. Wire.requestFrom(ADDR_QTOUCH, 1); // read a byte
  81. while (Wire.available()) {
  82. return Wire.read();
  83. }
  84. }
  85. // write data to register
  86. unsigned char SeeedQTouch::writeReg(unsigned char addr_reg, unsigned char dta) { // write register
  87. Wire.beginTransmission(ADDR_QTOUCH);
  88. Wire.write(addr_reg); // register to read
  89. Wire.write(dta);
  90. Wire.endTransmission();
  91. }
  92. SeeedQTouch QTouch;