A Stock Exchange Game using IO devices, the 128x128 uLCD, and other components

Dependencies:   4DGL-uLCD-SE SDFileSystem ShiftBrite mbed wave_player

Fork of WavePlayer_HelloWorld by jim hamblen

Committer:
RohanIyengar
Date:
Thu Nov 03 19:35:44 2016 +0000
Revision:
2:3585cd51c765
Created project

Who changed what in which revision?

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