John Magyar / QT2100
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers QT2100.cpp Source File

QT2100.cpp

00001 
00002 /*
00003 Copyright (c) 2015 John Magyar
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "QT2100.h"
00025 #include "mbed.h"
00026 
00027 /*
00028 
00029 Mbed library for the Atmel QT2100 Capacative Touch sensor device.
00030 
00031 The QT2100 is a capacitive touch controller device with 10 configurable QTouch channels,
00032 supporting up to seven touch buttons, and either a single slider (or wheel).
00033 
00034 Datasheet: http://www.atmel.com/images/at42qt2100_e_level0%2013ww04.pdf
00035 
00036 */
00037 
00038 QT2100::QT2100(PinName mosi,
00039                PinName miso,
00040                PinName sck,
00041                PinName cs,
00042                PinName tx,
00043                PinName rx):
00044     _spi(mosi, miso, sck),
00045     _cs(cs),
00046     _pc(tx, rx)
00047 {
00048 }
00049 
00050 QT2100::~QT2100()
00051 {
00052 }
00053 
00054 int qt2100[3];
00055 
00056 void QT2100::init()     // SPI interface initialization
00057 {
00058     _cs = 1;
00059     _spi.format(8,3);
00060     _spi.frequency(750000);
00061     wait_ms(600);
00062 }
00063 
00064 void QT2100::xfer3bytes(int byte0, int byte1, int byte2)     //  3-byte SPI transfer
00065 {
00066     int xfer[3] = {byte0, byte1, byte2};
00067 
00068     for (int i=0; i<3; i++) {
00069         _cs = 0;
00070         qt2100[i] = _spi.write(xfer[i]);
00071         _cs = 1;
00072         wait_us(100);
00073     }
00074 }
00075 
00076 void QT2100::terminalDisplay()     //  print register values for debugging
00077 {
00078     _pc.printf("\n");
00079     for (int i=0; i<3; i++) {
00080         _pc.printf("QT2100 Byte %d: 0x%x\n",i, qt2100[i]);
00081     }
00082 }
00083 
00084 void QT2100::devId()     //  query Device ID and Firmware version
00085 {
00086     xfer3bytes(0x40, 0x00, 0x00);     //  flush registers
00087     xfer3bytes(0x40, 0x00, 0x00);     //  get id and version
00088     terminalDisplay();
00089 }
00090 
00091 void QT2100::verifyChannels()     //  query sensors B1 through B7 and A1 through A3
00092 {
00093     xfer3bytes(0x20, 0x00, 0x00);     //  flush registers
00094     for (int i=0; i<10; i++) {
00095         xfer3bytes(0x20, i, 0x00);     // get each channel's status
00096         terminalDisplay();
00097     }
00098 }
00099 
00100 int8_t QT2100::keys()     // get data from each key channel
00101 {
00102     int8_t activeKey;
00103 
00104     xfer3bytes(0x08, 0x78, 0x41);
00105 
00106     switch(qt2100[1]) {
00107         case (0x01):
00108             activeKey = 0x01;
00109             break;
00110         case (0x02):
00111             activeKey = 0x02;
00112             break;
00113         case (0x04):
00114             activeKey = 0x04;
00115             break;
00116         case (0x08):
00117             activeKey = 0x08;
00118             break;
00119         case (0x10):
00120             activeKey = 0x10;
00121             break;
00122         case (0x20):
00123             activeKey = 0x20;
00124             break;
00125         case (0x40):
00126             activeKey = 0x40;
00127             break;
00128         case (0x80):
00129             activeKey = 0x80;
00130             break;
00131         default:
00132             activeKey = 0x00;
00133     }
00134     return activeKey;
00135 }
00136 
00137 int8_t QT2100::slider()     // get data from the slider channels
00138 {
00139     int8_t sliderPosition;
00140 
00141     xfer3bytes(0x08, 0x78, 0x40);     // 3 bits resolution, 8 positions (expandable to 8 bits, 256 positions)
00142 
00143     switch(qt2100[2]) {
00144         case (0x0):
00145             sliderPosition = 0x01;
00146             break;
00147         case (0x01):
00148             sliderPosition = 0x03;
00149             break;
00150         case (0x02):
00151             sliderPosition = 0x07;
00152             break;
00153         case (0x03):
00154             sliderPosition = 0x0f;
00155             break;
00156         case (0x04):
00157             sliderPosition = 0x1f;
00158             break;
00159         case (0x05):
00160             sliderPosition = 0x3f;
00161             break;
00162         case (0x06):
00163             sliderPosition = 0x7f;
00164             break;
00165         case (0x07):
00166             sliderPosition = 0xff;
00167             break;
00168         default:
00169             sliderPosition = 0x00;
00170     }
00171     return sliderPosition;
00172 }
00173 
00174 int8_t QT2100::wheel()     // get data from the wheel channels
00175 {
00176     int8_t wheelPosition;
00177 
00178     xfer3bytes(0x00, 0x78, 0x40);     // 3 bits resolution, 8 positions (expandable to 8 bits, 256 positions)
00179 
00180     switch(qt2100[2]) {
00181         case (0x0):
00182             wheelPosition = 0x01;
00183             break;
00184         case (0x01):
00185             wheelPosition = 0x03;
00186             break;
00187         case (0x02):
00188             wheelPosition = 0x07;
00189             break;
00190         case (0x03):
00191             wheelPosition = 0x0f;
00192             break;
00193         case (0x04):
00194             wheelPosition = 0x1f;
00195             break;
00196         case (0x05):
00197             wheelPosition = 0x3f;
00198             break;
00199         case (0x06):
00200             wheelPosition = 0x7f;
00201             break;
00202         case (0x07):
00203             wheelPosition = 0xff;
00204             break;
00205         default:
00206             wheelPosition = 0x00;
00207     }
00208     return wheelPosition;
00209 }