reeedstudio 11 лет назад
Родитель
Сommit
18db672cbb
6 измененных файлов с 264 добавлено и 0 удалено
  1. 21 0
      License.txt
  2. 88 0
      Seeed_QTouch.cpp
  3. 77 0
      Seeed_QTouch.h
  4. 28 0
      examples/getTouchNumber/getTouchNumber.ino
  5. 28 0
      examples/isTouch/isTouch.ino
  6. 22 0
      keywords.txt

+ 21 - 0
License.txt

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2013 Seeed Technology Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 88 - 0
Seeed_QTouch.cpp

@@ -0,0 +1,88 @@
+/*
+  QTouch Library
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author: ZhangKun & Loovee
+  2013-3-20
+  
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include <Wire.h>
+#include <Arduino.h>
+#include "Seeed_QTouch.h"
+
+
+SeeedQTouch::SeeedQTouch()
+{
+    Wire.begin();
+}
+
+// if certain key touched, return 1 - touched, 0 untouched
+unsigned char SeeedQTouch::isTouch(int key)
+{
+    if(key>7)return 0;                  // err input
+    unsigned char st = getState();
+    return ((0x01<<key)&st);            // if touched return 1, else return 0
+}
+
+// return all key state, bit0 for key0, bit1 for key1....
+unsigned char SeeedQTouch::getState()
+{
+    return readReg(0x03);
+}
+
+int SeeedQTouch::touchNum()
+{
+    unsigned char state = getState();
+    if(0 == state)return -1;
+    
+    for(int i=0; i<7; i++)
+    {
+        if((state>>i) & 0x01)
+        {
+            return i;
+        }
+    }
+    
+    return -1;
+}
+
+// read register
+unsigned char SeeedQTouch::readReg(unsigned char addr_reg)
+{
+
+    Wire.beginTransmission(ADDR_QTOUCH);
+    Wire.write(addr_reg); // register to read
+    Wire.endTransmission();
+
+    Wire.requestFrom(ADDR_QTOUCH, 1); // read a byte
+
+    while(Wire.available()) 
+    {
+        return Wire.read();
+    }
+}
+
+// write data to register 
+unsigned char SeeedQTouch::writeReg(unsigned char addr_reg, unsigned char dta)      // write register
+{
+    Wire.beginTransmission(ADDR_QTOUCH);
+    Wire.write(addr_reg); // register to read
+    Wire.write(dta);
+    Wire.endTransmission();
+}
+
+SeeedQTouch QTouch;

+ 77 - 0
Seeed_QTouch.h

@@ -0,0 +1,77 @@
+/*
+  QTouch Library
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author: ZhangKun & Loovee
+  2013-3-20
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef __SEEED_QTOUCH_H__
+#define __SEEED_QTOUCH_H__
+
+#define ADDR_QTOUCH     0x1B                    // address of QTouch
+
+
+#define QTOUCH_REG_CHIPID       0x00            // read chip id
+#define QTOUCH_REG_VERSION      0x01            // Firmware Version
+#define QTOUCH_REG_DETSTATUS    0X02            // detection status
+#define QTOUCH_REG_KEYSTATUS    0X03            // key status
+#define QTOUCH_REG_KEYSIGM      0X04            // MSByte of KEY0
+// .... LSByte ....
+
+#define QTOUCH_REG_LPMODE       0X36            // low power mode
+#define QTOUCH_REG_MAXDURA      0X37            // max on duration
+#define QTOUCH_REG_CALIBRATE    0X38            // calibrate
+#define QTOUCH_REG_RESET        0X39            // reset
+
+
+class SeeedQTouch{
+
+public:
+    
+    SeeedQTouch();
+    unsigned char isTouch(int key);                     // if certain key touched, return 1 - touched, 0 untouched
+    unsigned char getState();                           // return all key state, bit0 for key0, bit1 for key1....
+    int touchNum();                                     // if no touch return -1, else return key number 
+
+    void setLowPowerMode(int val)                       // set low power mode
+    {
+        writeReg(QTOUCH_REG_LPMODE, val);
+    }
+    
+    void reset()                                        // reset
+    {
+        writeReg(QTOUCH_REG_RESET, 0x55);               // write a non-zero value to reset 
+    }
+    
+    void setMaxDuration(int val)                        // set maxim duration
+    {
+        writeReg(QTOUCH_REG_MAXDURA, val);
+    }
+    
+    void calibrate()                                    // calibrate
+    {
+        writeReg(QTOUCH_REG_RESET, 0x55);               // write a non-zero value to re-calibrate the device
+    }
+    
+private:
+
+    unsigned char readReg(unsigned char addr_reg);                          // read register
+    unsigned char writeReg(unsigned char addr_reg, unsigned char dta);      // write register
+};
+
+extern SeeedQTouch QTouch;
+#endif

+ 28 - 0
examples/getTouchNumber/getTouchNumber.ino

@@ -0,0 +1,28 @@
+// demo of Qtouch - getTouchNumber
+// this demo will use touchNum function
+// this function can return the touched state of the device
+// when there's no touched, return -1
+// or return the touch number
+
+#include <Wire.h>
+#include "Seeed_QTouch.h"
+
+
+void setup()
+{
+    Serial.begin(9600);
+}
+
+void loop()
+{
+    int tn = QTouch.touchNum();
+    
+    if(tn>=0)
+    {
+        Serial.print("KEY");
+        Serial.print(tn);
+        Serial.println(" touched");
+    }
+    
+    delay(10);
+}

+ 28 - 0
examples/isTouch/isTouch.ino

@@ -0,0 +1,28 @@
+// demo of Qtouch - isTouch
+// this demo will use isTouch function
+// this function can the state of certain key
+// such as you can get state of KEY0 with this function:
+// isTouch(0), if return 1 meas KEY0 got touched while
+// return 0 no touch.
+// besides, this demo also print the touch time
+
+#include <Wire.h>
+#include "Seeed_QTouch.h"
+
+void setup()
+{   
+    Serial.begin(9600);
+}
+
+void loop()
+{
+    if(QTouch.isTouch(0))
+    {
+        long time1 = millis();                          // get origin time
+        Serial.print("key 0 touched: ");
+        while(QTouch.isTouch(0));                       // unless key released
+        Serial.print(millis() - time1);                 
+        Serial.println(" ms");
+    }
+    delay(10);
+}

+ 22 - 0
keywords.txt

@@ -0,0 +1,22 @@
+LED_Bar	KEYWORD1
+Seeed_QTouch	KEYWORD1
+QTouch	KEYWORD1
+
+isTouch	KEYWORD2
+getState	KEYWORD2
+setLowPowerMode	KEYWORD2
+reset	KEYWORD2
+setMaxDuration	KEYWORD2
+calibrate	KEYWORD2
+
+
+ADDR_QTOUCH	KEYWORD3
+QTOUCH_REG_CHIPID	KEYWORD3
+QTOUCH_REG_VERSION	KEYWORD3
+QTOUCH_REG_DETSTATUS	KEYWORD3
+QTOUCH_REG_KEYSTATUS	KEYWORD3
+QTOUCH_REG_KEYSIGM	KEYWORD3
+QTOUCH_REG_LPMODE	KEYWORD3
+QTOUCH_REG_MAXDURA	KEYWORD3
+QTOUCH_REG_CALIBRATE	KEYWORD3
+QTOUCH_REG_RESET	KEYWORD3