
Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.
Dependencies: C12832 MQTT LM75B MMA7660
Dependents: MFT_IoT_demo_USB400 IBM_RFID
MMA7660/MMA7660.cpp@6:37b6d0d56190, 2014-08-20 (annotated)
- Committer:
- samdanbury
- Date:
- Wed Aug 20 12:45:14 2014 +0000
- Revision:
- 6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | #include "MMA7660.h" |
samdanbury | 6:37b6d0d56190 | 2 | |
samdanbury | 6:37b6d0d56190 | 3 | MMA7660::MMA7660(PinName sda, PinName scl, bool active) : _i2c(sda, scl) |
samdanbury | 6:37b6d0d56190 | 4 | { |
samdanbury | 6:37b6d0d56190 | 5 | setActive(active); |
samdanbury | 6:37b6d0d56190 | 6 | samplerate = 64; |
samdanbury | 6:37b6d0d56190 | 7 | } |
samdanbury | 6:37b6d0d56190 | 8 | |
samdanbury | 6:37b6d0d56190 | 9 | //Since the MMA lacks a WHO_AM_I register, we can only check if there is a device that answers to the I2C address |
samdanbury | 6:37b6d0d56190 | 10 | bool MMA7660::testConnection( void ) |
samdanbury | 6:37b6d0d56190 | 11 | { |
samdanbury | 6:37b6d0d56190 | 12 | if (_i2c.write(MMA7660_ADDRESS, NULL, 0) == 0 ) |
samdanbury | 6:37b6d0d56190 | 13 | return true; |
samdanbury | 6:37b6d0d56190 | 14 | else |
samdanbury | 6:37b6d0d56190 | 15 | return false; |
samdanbury | 6:37b6d0d56190 | 16 | } |
samdanbury | 6:37b6d0d56190 | 17 | |
samdanbury | 6:37b6d0d56190 | 18 | void MMA7660::setActive(bool state) |
samdanbury | 6:37b6d0d56190 | 19 | { |
samdanbury | 6:37b6d0d56190 | 20 | active = state; |
samdanbury | 6:37b6d0d56190 | 21 | char modereg = read(MMA7660_MODE_R); |
samdanbury | 6:37b6d0d56190 | 22 | modereg &= ~(1<<0); |
samdanbury | 6:37b6d0d56190 | 23 | |
samdanbury | 6:37b6d0d56190 | 24 | //If it somehow was in testmode, disable that |
samdanbury | 6:37b6d0d56190 | 25 | if (modereg && (1<<2)) { |
samdanbury | 6:37b6d0d56190 | 26 | modereg &= ~(1<<2); |
samdanbury | 6:37b6d0d56190 | 27 | write(MMA7660_MODE_R, modereg); |
samdanbury | 6:37b6d0d56190 | 28 | } |
samdanbury | 6:37b6d0d56190 | 29 | |
samdanbury | 6:37b6d0d56190 | 30 | modereg += state; |
samdanbury | 6:37b6d0d56190 | 31 | write(MMA7660_MODE_R, modereg); |
samdanbury | 6:37b6d0d56190 | 32 | } |
samdanbury | 6:37b6d0d56190 | 33 | |
samdanbury | 6:37b6d0d56190 | 34 | void MMA7660::readData(int *data) |
samdanbury | 6:37b6d0d56190 | 35 | { |
samdanbury | 6:37b6d0d56190 | 36 | bool active_old = active; |
samdanbury | 6:37b6d0d56190 | 37 | if (!active) { |
samdanbury | 6:37b6d0d56190 | 38 | setActive(true); |
samdanbury | 6:37b6d0d56190 | 39 | wait(0.012 + 1/samplerate); //Wait until new sample is ready, my experience is that 1/samplerate isnt needed, but datasheet says so |
samdanbury | 6:37b6d0d56190 | 40 | } |
samdanbury | 6:37b6d0d56190 | 41 | |
samdanbury | 6:37b6d0d56190 | 42 | char temp[3]; |
samdanbury | 6:37b6d0d56190 | 43 | bool alert; |
samdanbury | 6:37b6d0d56190 | 44 | |
samdanbury | 6:37b6d0d56190 | 45 | do { |
samdanbury | 6:37b6d0d56190 | 46 | alert = false; |
samdanbury | 6:37b6d0d56190 | 47 | read(MMA7660_XOUT_R, temp, 3); |
samdanbury | 6:37b6d0d56190 | 48 | for (int i = 0; i<3; i++) { |
samdanbury | 6:37b6d0d56190 | 49 | if (temp[i] > 63) |
samdanbury | 6:37b6d0d56190 | 50 | alert = true; |
samdanbury | 6:37b6d0d56190 | 51 | if (temp[i] > 31) |
samdanbury | 6:37b6d0d56190 | 52 | temp[i] += 128+64; |
samdanbury | 6:37b6d0d56190 | 53 | data[i] = (signed char)temp[i]; |
samdanbury | 6:37b6d0d56190 | 54 | } |
samdanbury | 6:37b6d0d56190 | 55 | } while (alert); |
samdanbury | 6:37b6d0d56190 | 56 | |
samdanbury | 6:37b6d0d56190 | 57 | if (!active_old) |
samdanbury | 6:37b6d0d56190 | 58 | setActive(false); |
samdanbury | 6:37b6d0d56190 | 59 | } |
samdanbury | 6:37b6d0d56190 | 60 | |
samdanbury | 6:37b6d0d56190 | 61 | |
samdanbury | 6:37b6d0d56190 | 62 | void MMA7660::readData(float *data) |
samdanbury | 6:37b6d0d56190 | 63 | { |
samdanbury | 6:37b6d0d56190 | 64 | int intdata[3]; |
samdanbury | 6:37b6d0d56190 | 65 | readData(intdata); |
samdanbury | 6:37b6d0d56190 | 66 | for (int i = 0; i<3; i++) |
samdanbury | 6:37b6d0d56190 | 67 | data[i] = intdata[i]/MMA7660_SENSITIVITY; |
samdanbury | 6:37b6d0d56190 | 68 | } |
samdanbury | 6:37b6d0d56190 | 69 | |
samdanbury | 6:37b6d0d56190 | 70 | float MMA7660::x( void ) |
samdanbury | 6:37b6d0d56190 | 71 | { |
samdanbury | 6:37b6d0d56190 | 72 | return getSingle(0); |
samdanbury | 6:37b6d0d56190 | 73 | } |
samdanbury | 6:37b6d0d56190 | 74 | |
samdanbury | 6:37b6d0d56190 | 75 | float MMA7660::y( void ) |
samdanbury | 6:37b6d0d56190 | 76 | { |
samdanbury | 6:37b6d0d56190 | 77 | return getSingle(1); |
samdanbury | 6:37b6d0d56190 | 78 | } |
samdanbury | 6:37b6d0d56190 | 79 | |
samdanbury | 6:37b6d0d56190 | 80 | float MMA7660::z( void ) |
samdanbury | 6:37b6d0d56190 | 81 | { |
samdanbury | 6:37b6d0d56190 | 82 | return getSingle(2); |
samdanbury | 6:37b6d0d56190 | 83 | } |
samdanbury | 6:37b6d0d56190 | 84 | |
samdanbury | 6:37b6d0d56190 | 85 | |
samdanbury | 6:37b6d0d56190 | 86 | void MMA7660::setSampleRate(int samplerate) |
samdanbury | 6:37b6d0d56190 | 87 | { |
samdanbury | 6:37b6d0d56190 | 88 | bool active_old = active; |
samdanbury | 6:37b6d0d56190 | 89 | setActive(false); //Not allowed to be active to change anything |
samdanbury | 6:37b6d0d56190 | 90 | int rates[] = {120, 64, 32, 16, 8, 4, 2, 1}; //Alowed samplerates (and their number in array is also number required for MMA) |
samdanbury | 6:37b6d0d56190 | 91 | int sampleLoc = 0, sampleError = 10000, temp; |
samdanbury | 6:37b6d0d56190 | 92 | for (int i = 0; i<8; i++) { |
samdanbury | 6:37b6d0d56190 | 93 | temp = abs( rates[i] - samplerate ); |
samdanbury | 6:37b6d0d56190 | 94 | if (temp<sampleError) { |
samdanbury | 6:37b6d0d56190 | 95 | sampleLoc = i; |
samdanbury | 6:37b6d0d56190 | 96 | sampleError=temp; |
samdanbury | 6:37b6d0d56190 | 97 | } |
samdanbury | 6:37b6d0d56190 | 98 | } |
samdanbury | 6:37b6d0d56190 | 99 | |
samdanbury | 6:37b6d0d56190 | 100 | //Update the samplerate reg |
samdanbury | 6:37b6d0d56190 | 101 | temp = read(MMA7660_SR_R); |
samdanbury | 6:37b6d0d56190 | 102 | temp &= ~0x07; //Awake sample rate are lowest 3 bit |
samdanbury | 6:37b6d0d56190 | 103 | temp |= sampleLoc; |
samdanbury | 6:37b6d0d56190 | 104 | write(MMA7660_SR_R, temp); |
samdanbury | 6:37b6d0d56190 | 105 | this->samplerate = rates[sampleLoc]; |
samdanbury | 6:37b6d0d56190 | 106 | setActive(active_old); //Restore previous active state |
samdanbury | 6:37b6d0d56190 | 107 | } |
samdanbury | 6:37b6d0d56190 | 108 | |
samdanbury | 6:37b6d0d56190 | 109 | |
samdanbury | 6:37b6d0d56190 | 110 | MMA7660::Orientation MMA7660::getSide( void ) |
samdanbury | 6:37b6d0d56190 | 111 | { |
samdanbury | 6:37b6d0d56190 | 112 | char tiltreg = read(MMA7660_TILT_R); |
samdanbury | 6:37b6d0d56190 | 113 | //We care about 2 LSBs |
samdanbury | 6:37b6d0d56190 | 114 | tiltreg &= 0x03; |
samdanbury | 6:37b6d0d56190 | 115 | if (tiltreg == 0x01) |
samdanbury | 6:37b6d0d56190 | 116 | return MMA7660::Front; |
samdanbury | 6:37b6d0d56190 | 117 | if (tiltreg == 0x02) |
samdanbury | 6:37b6d0d56190 | 118 | return MMA7660::Back; |
samdanbury | 6:37b6d0d56190 | 119 | return MMA7660::Unknown; |
samdanbury | 6:37b6d0d56190 | 120 | } |
samdanbury | 6:37b6d0d56190 | 121 | |
samdanbury | 6:37b6d0d56190 | 122 | MMA7660::Orientation MMA7660::getOrientation( void ) |
samdanbury | 6:37b6d0d56190 | 123 | { |
samdanbury | 6:37b6d0d56190 | 124 | char tiltreg = read(MMA7660_TILT_R); |
samdanbury | 6:37b6d0d56190 | 125 | |
samdanbury | 6:37b6d0d56190 | 126 | //We care about bit 2, 3 and 4 (counting from zero) |
samdanbury | 6:37b6d0d56190 | 127 | tiltreg &= 0x07<<2; |
samdanbury | 6:37b6d0d56190 | 128 | tiltreg >>= 2; |
samdanbury | 6:37b6d0d56190 | 129 | if (tiltreg == 0x01) |
samdanbury | 6:37b6d0d56190 | 130 | return MMA7660::Left; |
samdanbury | 6:37b6d0d56190 | 131 | if (tiltreg == 0x02) |
samdanbury | 6:37b6d0d56190 | 132 | return MMA7660::Right; |
samdanbury | 6:37b6d0d56190 | 133 | if (tiltreg == 0x05) |
samdanbury | 6:37b6d0d56190 | 134 | return MMA7660::Down; |
samdanbury | 6:37b6d0d56190 | 135 | if (tiltreg == 0x06) |
samdanbury | 6:37b6d0d56190 | 136 | return MMA7660::Up; |
samdanbury | 6:37b6d0d56190 | 137 | return MMA7660::Unknown; |
samdanbury | 6:37b6d0d56190 | 138 | } |
samdanbury | 6:37b6d0d56190 | 139 | |
samdanbury | 6:37b6d0d56190 | 140 | |
samdanbury | 6:37b6d0d56190 | 141 | |
samdanbury | 6:37b6d0d56190 | 142 | ////////////////////////////////////////////// |
samdanbury | 6:37b6d0d56190 | 143 | ///////////////PRIVATE//////////////////////// |
samdanbury | 6:37b6d0d56190 | 144 | ////////////////////////////////////////////// |
samdanbury | 6:37b6d0d56190 | 145 | |
samdanbury | 6:37b6d0d56190 | 146 | |
samdanbury | 6:37b6d0d56190 | 147 | void MMA7660::write(char address, char data) |
samdanbury | 6:37b6d0d56190 | 148 | { |
samdanbury | 6:37b6d0d56190 | 149 | char temp[2]; |
samdanbury | 6:37b6d0d56190 | 150 | temp[0]=address; |
samdanbury | 6:37b6d0d56190 | 151 | temp[1]=data; |
samdanbury | 6:37b6d0d56190 | 152 | |
samdanbury | 6:37b6d0d56190 | 153 | _i2c.write(MMA7660_ADDRESS, temp, 2); |
samdanbury | 6:37b6d0d56190 | 154 | } |
samdanbury | 6:37b6d0d56190 | 155 | |
samdanbury | 6:37b6d0d56190 | 156 | char MMA7660::read(char address) |
samdanbury | 6:37b6d0d56190 | 157 | { |
samdanbury | 6:37b6d0d56190 | 158 | char retval; |
samdanbury | 6:37b6d0d56190 | 159 | _i2c.write(MMA7660_ADDRESS, &address, 1, true); |
samdanbury | 6:37b6d0d56190 | 160 | _i2c.read(MMA7660_ADDRESS, &retval, 1); |
samdanbury | 6:37b6d0d56190 | 161 | return retval; |
samdanbury | 6:37b6d0d56190 | 162 | } |
samdanbury | 6:37b6d0d56190 | 163 | |
samdanbury | 6:37b6d0d56190 | 164 | void MMA7660::read(char address, char *data, int length) |
samdanbury | 6:37b6d0d56190 | 165 | { |
samdanbury | 6:37b6d0d56190 | 166 | _i2c.write(MMA7660_ADDRESS, &address, 1, true); |
samdanbury | 6:37b6d0d56190 | 167 | _i2c.read(MMA7660_ADDRESS, data, length); |
samdanbury | 6:37b6d0d56190 | 168 | } |
samdanbury | 6:37b6d0d56190 | 169 | |
samdanbury | 6:37b6d0d56190 | 170 | float MMA7660::getSingle( int number ) |
samdanbury | 6:37b6d0d56190 | 171 | { |
samdanbury | 6:37b6d0d56190 | 172 | bool active_old = active; |
samdanbury | 6:37b6d0d56190 | 173 | if (!active) { |
samdanbury | 6:37b6d0d56190 | 174 | setActive(true); |
samdanbury | 6:37b6d0d56190 | 175 | wait(0.012 + 1/samplerate); //Wait until new sample is ready |
samdanbury | 6:37b6d0d56190 | 176 | } |
samdanbury | 6:37b6d0d56190 | 177 | |
samdanbury | 6:37b6d0d56190 | 178 | signed char temp; |
samdanbury | 6:37b6d0d56190 | 179 | bool alert; |
samdanbury | 6:37b6d0d56190 | 180 | |
samdanbury | 6:37b6d0d56190 | 181 | do { |
samdanbury | 6:37b6d0d56190 | 182 | alert = false; |
samdanbury | 6:37b6d0d56190 | 183 | temp = read(MMA7660_XOUT_R + number); |
samdanbury | 6:37b6d0d56190 | 184 | if (temp > 63) |
samdanbury | 6:37b6d0d56190 | 185 | alert = true; |
samdanbury | 6:37b6d0d56190 | 186 | if (temp > 31) |
samdanbury | 6:37b6d0d56190 | 187 | temp += 128+64; |
samdanbury | 6:37b6d0d56190 | 188 | } while (alert); |
samdanbury | 6:37b6d0d56190 | 189 | |
samdanbury | 6:37b6d0d56190 | 190 | if (!active_old) |
samdanbury | 6:37b6d0d56190 | 191 | setActive(false); |
samdanbury | 6:37b6d0d56190 | 192 | |
samdanbury | 6:37b6d0d56190 | 193 | return temp / MMA7660_SENSITIVITY; |
samdanbury | 6:37b6d0d56190 | 194 | } |