Tetris game for mbed.

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

Fork of HelloWorld by Simon Ford

Committer:
greiner218
Date:
Mon Nov 07 07:18:59 2016 +0000
Revision:
3:803d3e67513f
Parent:
2:36cda8980746
Added .wav file

Who changed what in which revision?

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