Ye Qiu / Mbed 2 deprecated Tic_Tac_Toe_with_Touchpad

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player

Fork of app-board-RTOS-Threads by jim hamblen

Committer:
ethan_wireless
Date:
Tue Oct 20 17:24:08 2015 +0000
Revision:
5:36b6ce2faf88
NA

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ethan_wireless 5:36b6ce2faf88 1 /*
ethan_wireless 5:36b6ce2faf88 2 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
ethan_wireless 5:36b6ce2faf88 3
ethan_wireless 5:36b6ce2faf88 4 Permission is hereby granted, free of charge, to any person obtaining a copy
ethan_wireless 5:36b6ce2faf88 5 of this software and associated documentation files (the "Software"), to deal
ethan_wireless 5:36b6ce2faf88 6 in the Software without restriction, including without limitation the rights
ethan_wireless 5:36b6ce2faf88 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ethan_wireless 5:36b6ce2faf88 8 copies of the Software, and to permit persons to whom the Software is
ethan_wireless 5:36b6ce2faf88 9 furnished to do so, subject to the following conditions:
ethan_wireless 5:36b6ce2faf88 10
ethan_wireless 5:36b6ce2faf88 11 The above copyright notice and this permission notice shall be included in
ethan_wireless 5:36b6ce2faf88 12 all copies or substantial portions of the Software.
ethan_wireless 5:36b6ce2faf88 13
ethan_wireless 5:36b6ce2faf88 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ethan_wireless 5:36b6ce2faf88 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ethan_wireless 5:36b6ce2faf88 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ethan_wireless 5:36b6ce2faf88 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ethan_wireless 5:36b6ce2faf88 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ethan_wireless 5:36b6ce2faf88 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ethan_wireless 5:36b6ce2faf88 20 THE SOFTWARE.
ethan_wireless 5:36b6ce2faf88 21 */
ethan_wireless 5:36b6ce2faf88 22
ethan_wireless 5:36b6ce2faf88 23 #include <mbed.h>
ethan_wireless 5:36b6ce2faf88 24 #include <sstream>
ethan_wireless 5:36b6ce2faf88 25 #include <string>
ethan_wireless 5:36b6ce2faf88 26 #include <list>
ethan_wireless 5:36b6ce2faf88 27
ethan_wireless 5:36b6ce2faf88 28 #include <mpr121.h>
ethan_wireless 5:36b6ce2faf88 29
ethan_wireless 5:36b6ce2faf88 30 Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
ethan_wireless 5:36b6ce2faf88 31 {
ethan_wireless 5:36b6ce2faf88 32 this->i2c = i2c;
ethan_wireless 5:36b6ce2faf88 33
ethan_wireless 5:36b6ce2faf88 34 address = i2cAddress;
ethan_wireless 5:36b6ce2faf88 35
ethan_wireless 5:36b6ce2faf88 36 // Configure the MPR121 settings to default
ethan_wireless 5:36b6ce2faf88 37 this->configureSettings();
ethan_wireless 5:36b6ce2faf88 38 }
ethan_wireless 5:36b6ce2faf88 39
ethan_wireless 5:36b6ce2faf88 40
ethan_wireless 5:36b6ce2faf88 41 void Mpr121::configureSettings()
ethan_wireless 5:36b6ce2faf88 42 {
ethan_wireless 5:36b6ce2faf88 43 // Put the MPR into setup mode
ethan_wireless 5:36b6ce2faf88 44 this->write(ELE_CFG,0x00);
ethan_wireless 5:36b6ce2faf88 45
ethan_wireless 5:36b6ce2faf88 46 // Electrode filters for when data is > baseline
ethan_wireless 5:36b6ce2faf88 47 unsigned char gtBaseline[] = {
ethan_wireless 5:36b6ce2faf88 48 0x01, //MHD_R
ethan_wireless 5:36b6ce2faf88 49 0x01, //NHD_R
ethan_wireless 5:36b6ce2faf88 50 0x00, //NCL_R
ethan_wireless 5:36b6ce2faf88 51 0x00 //FDL_R
ethan_wireless 5:36b6ce2faf88 52 };
ethan_wireless 5:36b6ce2faf88 53
ethan_wireless 5:36b6ce2faf88 54 writeMany(MHD_R,gtBaseline,4);
ethan_wireless 5:36b6ce2faf88 55
ethan_wireless 5:36b6ce2faf88 56 // Electrode filters for when data is < baseline
ethan_wireless 5:36b6ce2faf88 57 unsigned char ltBaseline[] = {
ethan_wireless 5:36b6ce2faf88 58 0x01, //MHD_F
ethan_wireless 5:36b6ce2faf88 59 0x01, //NHD_F
ethan_wireless 5:36b6ce2faf88 60 0xFF, //NCL_F
ethan_wireless 5:36b6ce2faf88 61 0x02 //FDL_F
ethan_wireless 5:36b6ce2faf88 62 };
ethan_wireless 5:36b6ce2faf88 63
ethan_wireless 5:36b6ce2faf88 64 writeMany(MHD_F,ltBaseline,4);
ethan_wireless 5:36b6ce2faf88 65
ethan_wireless 5:36b6ce2faf88 66 // Electrode touch and release thresholds
ethan_wireless 5:36b6ce2faf88 67 unsigned char electrodeThresholds[] = {
ethan_wireless 5:36b6ce2faf88 68 E_THR_T, // Touch Threshhold
ethan_wireless 5:36b6ce2faf88 69 E_THR_R // Release Threshold
ethan_wireless 5:36b6ce2faf88 70 };
ethan_wireless 5:36b6ce2faf88 71
ethan_wireless 5:36b6ce2faf88 72 for(int i=0; i<12; i++){
ethan_wireless 5:36b6ce2faf88 73 int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
ethan_wireless 5:36b6ce2faf88 74 }
ethan_wireless 5:36b6ce2faf88 75
ethan_wireless 5:36b6ce2faf88 76 // Proximity Settings
ethan_wireless 5:36b6ce2faf88 77 unsigned char proximitySettings[] = {
ethan_wireless 5:36b6ce2faf88 78 0xff, //MHD_Prox_R
ethan_wireless 5:36b6ce2faf88 79 0xff, //NHD_Prox_R
ethan_wireless 5:36b6ce2faf88 80 0x00, //NCL_Prox_R
ethan_wireless 5:36b6ce2faf88 81 0x00, //FDL_Prox_R
ethan_wireless 5:36b6ce2faf88 82 0x01, //MHD_Prox_F
ethan_wireless 5:36b6ce2faf88 83 0x01, //NHD_Prox_F
ethan_wireless 5:36b6ce2faf88 84 0xFF, //NCL_Prox_F
ethan_wireless 5:36b6ce2faf88 85 0xff, //FDL_Prox_F
ethan_wireless 5:36b6ce2faf88 86 0x00, //NHD_Prox_T
ethan_wireless 5:36b6ce2faf88 87 0x00, //NCL_Prox_T
ethan_wireless 5:36b6ce2faf88 88 0x00 //NFD_Prox_T
ethan_wireless 5:36b6ce2faf88 89 };
ethan_wireless 5:36b6ce2faf88 90 writeMany(MHDPROXR,proximitySettings,11);
ethan_wireless 5:36b6ce2faf88 91
ethan_wireless 5:36b6ce2faf88 92 unsigned char proxThresh[] = {
ethan_wireless 5:36b6ce2faf88 93 PROX_THR_T, // Touch Threshold
ethan_wireless 5:36b6ce2faf88 94 PROX_THR_R // Release Threshold
ethan_wireless 5:36b6ce2faf88 95 };
ethan_wireless 5:36b6ce2faf88 96 writeMany(EPROXTTH,proxThresh,2);
ethan_wireless 5:36b6ce2faf88 97
ethan_wireless 5:36b6ce2faf88 98 this->write(FIL_CFG,0x04);
ethan_wireless 5:36b6ce2faf88 99
ethan_wireless 5:36b6ce2faf88 100 // Set the electrode config to transition to active mode
ethan_wireless 5:36b6ce2faf88 101 this->write(ELE_CFG,0x0c);
ethan_wireless 5:36b6ce2faf88 102 }
ethan_wireless 5:36b6ce2faf88 103
ethan_wireless 5:36b6ce2faf88 104 void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
ethan_wireless 5:36b6ce2faf88 105
ethan_wireless 5:36b6ce2faf88 106 if(electrode > 11) return;
ethan_wireless 5:36b6ce2faf88 107
ethan_wireless 5:36b6ce2faf88 108 // Get the current mode
ethan_wireless 5:36b6ce2faf88 109 unsigned char mode = this->read(ELE_CFG);
ethan_wireless 5:36b6ce2faf88 110
ethan_wireless 5:36b6ce2faf88 111 // Put the MPR into setup mode
ethan_wireless 5:36b6ce2faf88 112 this->write(ELE_CFG,0x00);
ethan_wireless 5:36b6ce2faf88 113
ethan_wireless 5:36b6ce2faf88 114 // Write the new threshold
ethan_wireless 5:36b6ce2faf88 115 this->write((ELE0_T+(electrode*2)), touch);
ethan_wireless 5:36b6ce2faf88 116 this->write((ELE0_T+(electrode*2)+1), release);
ethan_wireless 5:36b6ce2faf88 117
ethan_wireless 5:36b6ce2faf88 118 //Restore the operating mode
ethan_wireless 5:36b6ce2faf88 119 this->write(ELE_CFG, mode);
ethan_wireless 5:36b6ce2faf88 120 }
ethan_wireless 5:36b6ce2faf88 121
ethan_wireless 5:36b6ce2faf88 122
ethan_wireless 5:36b6ce2faf88 123 unsigned char Mpr121::read(int key){
ethan_wireless 5:36b6ce2faf88 124
ethan_wireless 5:36b6ce2faf88 125 unsigned char data[2];
ethan_wireless 5:36b6ce2faf88 126
ethan_wireless 5:36b6ce2faf88 127 //Start the command
ethan_wireless 5:36b6ce2faf88 128 i2c->start();
ethan_wireless 5:36b6ce2faf88 129
ethan_wireless 5:36b6ce2faf88 130 // Address the target (Write mode)
ethan_wireless 5:36b6ce2faf88 131 int ack1= i2c->write(address);
ethan_wireless 5:36b6ce2faf88 132
ethan_wireless 5:36b6ce2faf88 133 // Set the register key to read
ethan_wireless 5:36b6ce2faf88 134 int ack2 = i2c->write(key);
ethan_wireless 5:36b6ce2faf88 135
ethan_wireless 5:36b6ce2faf88 136 // Re-start for read of data
ethan_wireless 5:36b6ce2faf88 137 i2c->start();
ethan_wireless 5:36b6ce2faf88 138
ethan_wireless 5:36b6ce2faf88 139 // Re-send the target address in read mode
ethan_wireless 5:36b6ce2faf88 140 int ack3 = i2c->write(address+1);
ethan_wireless 5:36b6ce2faf88 141
ethan_wireless 5:36b6ce2faf88 142 // Read in the result
ethan_wireless 5:36b6ce2faf88 143 data[0] = i2c->read(0);
ethan_wireless 5:36b6ce2faf88 144
ethan_wireless 5:36b6ce2faf88 145 // Reset the bus
ethan_wireless 5:36b6ce2faf88 146 i2c->stop();
ethan_wireless 5:36b6ce2faf88 147
ethan_wireless 5:36b6ce2faf88 148 return data[0];
ethan_wireless 5:36b6ce2faf88 149 }
ethan_wireless 5:36b6ce2faf88 150
ethan_wireless 5:36b6ce2faf88 151
ethan_wireless 5:36b6ce2faf88 152 int Mpr121::write(int key, unsigned char value){
ethan_wireless 5:36b6ce2faf88 153
ethan_wireless 5:36b6ce2faf88 154 //Start the command
ethan_wireless 5:36b6ce2faf88 155 i2c->start();
ethan_wireless 5:36b6ce2faf88 156
ethan_wireless 5:36b6ce2faf88 157 // Address the target (Write mode)
ethan_wireless 5:36b6ce2faf88 158 int ack1= i2c->write(address);
ethan_wireless 5:36b6ce2faf88 159
ethan_wireless 5:36b6ce2faf88 160 // Set the register key to write
ethan_wireless 5:36b6ce2faf88 161 int ack2 = i2c->write(key);
ethan_wireless 5:36b6ce2faf88 162
ethan_wireless 5:36b6ce2faf88 163 // Read in the result
ethan_wireless 5:36b6ce2faf88 164 int ack3 = i2c->write(value);
ethan_wireless 5:36b6ce2faf88 165
ethan_wireless 5:36b6ce2faf88 166 // Reset the bus
ethan_wireless 5:36b6ce2faf88 167 i2c->stop();
ethan_wireless 5:36b6ce2faf88 168
ethan_wireless 5:36b6ce2faf88 169 return (ack1+ack2+ack3)-3;
ethan_wireless 5:36b6ce2faf88 170 }
ethan_wireless 5:36b6ce2faf88 171
ethan_wireless 5:36b6ce2faf88 172
ethan_wireless 5:36b6ce2faf88 173 int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
ethan_wireless 5:36b6ce2faf88 174 //Start the command
ethan_wireless 5:36b6ce2faf88 175 i2c->start();
ethan_wireless 5:36b6ce2faf88 176
ethan_wireless 5:36b6ce2faf88 177 // Address the target (Write mode)
ethan_wireless 5:36b6ce2faf88 178 int ack= i2c->write(address);
ethan_wireless 5:36b6ce2faf88 179 if(ack!=1){
ethan_wireless 5:36b6ce2faf88 180 return -1;
ethan_wireless 5:36b6ce2faf88 181 }
ethan_wireless 5:36b6ce2faf88 182
ethan_wireless 5:36b6ce2faf88 183 // Set the register key to write
ethan_wireless 5:36b6ce2faf88 184 ack = i2c->write(start);
ethan_wireless 5:36b6ce2faf88 185 if(ack!=1){
ethan_wireless 5:36b6ce2faf88 186 return -1;
ethan_wireless 5:36b6ce2faf88 187 }
ethan_wireless 5:36b6ce2faf88 188
ethan_wireless 5:36b6ce2faf88 189 // Write the date set
ethan_wireless 5:36b6ce2faf88 190 int count = 0;
ethan_wireless 5:36b6ce2faf88 191 while(ack==1 && (count < length)){
ethan_wireless 5:36b6ce2faf88 192 ack = i2c->write(dataSet[count]);
ethan_wireless 5:36b6ce2faf88 193 count++;
ethan_wireless 5:36b6ce2faf88 194 }
ethan_wireless 5:36b6ce2faf88 195 // Stop the cmd
ethan_wireless 5:36b6ce2faf88 196 i2c->stop();
ethan_wireless 5:36b6ce2faf88 197
ethan_wireless 5:36b6ce2faf88 198 return count;
ethan_wireless 5:36b6ce2faf88 199 }
ethan_wireless 5:36b6ce2faf88 200
ethan_wireless 5:36b6ce2faf88 201
ethan_wireless 5:36b6ce2faf88 202 bool Mpr121::getProximityMode(){
ethan_wireless 5:36b6ce2faf88 203 if(this->read(ELE_CFG) > 0x0c)
ethan_wireless 5:36b6ce2faf88 204 return true;
ethan_wireless 5:36b6ce2faf88 205 else
ethan_wireless 5:36b6ce2faf88 206 return false;
ethan_wireless 5:36b6ce2faf88 207 }
ethan_wireless 5:36b6ce2faf88 208
ethan_wireless 5:36b6ce2faf88 209 void Mpr121::setProximityMode(bool mode){
ethan_wireless 5:36b6ce2faf88 210 this->write(ELE_CFG,0x00);
ethan_wireless 5:36b6ce2faf88 211 if(mode){
ethan_wireless 5:36b6ce2faf88 212 this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
ethan_wireless 5:36b6ce2faf88 213 } else {
ethan_wireless 5:36b6ce2faf88 214 this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
ethan_wireless 5:36b6ce2faf88 215 }
ethan_wireless 5:36b6ce2faf88 216 }
ethan_wireless 5:36b6ce2faf88 217
ethan_wireless 5:36b6ce2faf88 218
ethan_wireless 5:36b6ce2faf88 219 int Mpr121::readTouchData(){
ethan_wireless 5:36b6ce2faf88 220 return this->read(0x00);
ethan_wireless 5:36b6ce2faf88 221 }