Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Committer:
owenbrotherwood
Date:
Mon Jan 16 19:19:50 2017 +0000
Revision:
4:f63476855239
Parent:
3:a91b1bb396ca
Child:
5:4a8384331ca7
Complete rewrite; used MicroBitCompas as template

Who changed what in which revision?

UserRevisionLine numberNew contents of line
owenbrotherwood 4:f63476855239 1 /*
owenbrotherwood 4:f63476855239 2 The MIT License (MIT)
owenbrotherwood 4:f63476855239 3
owenbrotherwood 4:f63476855239 4 Copyright (c) 2016 British Broadcasting Corporation.
owenbrotherwood 4:f63476855239 5 This software is provided by Lancaster University by arrangement with the BBC.
owenbrotherwood 0:fb4572fc4901 6
owenbrotherwood 4:f63476855239 7 Permission is hereby granted, free of charge, to any person obtaining a
owenbrotherwood 4:f63476855239 8 copy of this software and associated documentation files (the "Software"),
owenbrotherwood 4:f63476855239 9 to deal in the Software without restriction, including without limitation
owenbrotherwood 4:f63476855239 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
owenbrotherwood 4:f63476855239 11 and/or sell copies of the Software, and to permit persons to whom the
owenbrotherwood 4:f63476855239 12 Software is furnished to do so, subject to the following conditions:
owenbrotherwood 4:f63476855239 13
owenbrotherwood 4:f63476855239 14 The above copyright notice and this permission notice shall be included in
owenbrotherwood 4:f63476855239 15 all copies or substantial portions of the Software.
owenbrotherwood 4:f63476855239 16
owenbrotherwood 4:f63476855239 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
owenbrotherwood 4:f63476855239 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
owenbrotherwood 4:f63476855239 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
owenbrotherwood 4:f63476855239 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
owenbrotherwood 4:f63476855239 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
owenbrotherwood 4:f63476855239 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
owenbrotherwood 4:f63476855239 23 DEALINGS IN THE SOFTWARE.
owenbrotherwood 4:f63476855239 24 */
owenbrotherwood 1:f6fed00a3ff2 25
owenbrotherwood 4:f63476855239 26 /**
owenbrotherwood 4:f63476855239 27 * Class definition for MicroBit Mpr121.
owenbrotherwood 4:f63476855239 28 *
owenbrotherwood 4:f63476855239 29 * Represents an implementation of the Mpr121.
owenbrotherwood 4:f63476855239 30 * Also includes ...
owenbrotherwood 4:f63476855239 31 */
owenbrotherwood 4:f63476855239 32 #include "MicroBitConfig.h"
owenbrotherwood 4:f63476855239 33 #include "MicroBitPin.h"
owenbrotherwood 4:f63476855239 34 #include "MicroBitMpr121.h"
owenbrotherwood 4:f63476855239 35 #include "MicroBitFiber.h"
owenbrotherwood 4:f63476855239 36 #include "ErrorNo.h"
owenbrotherwood 4:f63476855239 37
owenbrotherwood 4:f63476855239 38 /**
owenbrotherwood 4:f63476855239 39 * An initialisation member function.
owenbrotherwood 4:f63476855239 40 *
owenbrotherwood 4:f63476855239 41 * @param id the unique identifier for this instance.
owenbrotherwood 4:f63476855239 42 *
owenbrotherwood 4:f63476855239 43 * @param address the base address on the i2c bus.
owenbrotherwood 4:f63476855239 44 */
owenbrotherwood 4:f63476855239 45 void MicroBitMpr121::init(uint16_t id, uint16_t address)
owenbrotherwood 0:fb4572fc4901 46 {
owenbrotherwood 4:f63476855239 47 this->id = id;
owenbrotherwood 4:f63476855239 48 this->address = address;
owenbrotherwood 4:f63476855239 49
owenbrotherwood 4:f63476855239 50 // Indicate that we're up and running.
owenbrotherwood 4:f63476855239 51 status |= MICROBIT_COMPONENT_RUNNING;
owenbrotherwood 0:fb4572fc4901 52 }
owenbrotherwood 0:fb4572fc4901 53
owenbrotherwood 4:f63476855239 54 /**
owenbrotherwood 4:f63476855239 55 * Constructor.
owenbrotherwood 4:f63476855239 56 * Create a software representation.
owenbrotherwood 4:f63476855239 57 *
owenbrotherwood 4:f63476855239 58 * @param _i2c an instance of i2c
owenbrotherwood 4:f63476855239 59 *
owenbrotherwood 4:f63476855239 60 * @param address the address register on the i2c bus. Defaults to MPR121_DEFAULT_ADDR.
owenbrotherwood 4:f63476855239 61 *
owenbrotherwood 4:f63476855239 62 * @param id the ID of the new object. Defaults to MPR121_DEFAULT_ADDR.
owenbrotherwood 4:f63476855239 63 *
owenbrotherwood 4:f63476855239 64 * @code
owenbrotherwood 4:f63476855239 65 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
owenbrotherwood 4:f63476855239 66 *
owenbrotherwood 4:f63476855239 67 * MicroBitMpr121 mpr121(i2c);
owenbrotherwood 4:f63476855239 68 * @endcode
owenbrotherwood 4:f63476855239 69 */
owenbrotherwood 4:f63476855239 70 MicroBitMpr121::MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address, uint16_t id) :
owenbrotherwood 4:f63476855239 71 int1(MICROBIT_PIN_P16),
owenbrotherwood 4:f63476855239 72 i2c(_i2c)
owenbrotherwood 4:f63476855239 73
owenbrotherwood 0:fb4572fc4901 74 {
owenbrotherwood 4:f63476855239 75 init(id, address);
owenbrotherwood 0:fb4572fc4901 76 }
owenbrotherwood 0:fb4572fc4901 77
owenbrotherwood 4:f63476855239 78 /**
owenbrotherwood 4:f63476855239 79 * Issues a standard, 2 byte I2C command write.
owenbrotherwood 4:f63476855239 80 *
owenbrotherwood 4:f63476855239 81 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 82 *
owenbrotherwood 4:f63476855239 83 * @param reg The address of the register to write to.
owenbrotherwood 4:f63476855239 84 *
owenbrotherwood 4:f63476855239 85 * @param value The value to write.
owenbrotherwood 4:f63476855239 86 *
owenbrotherwood 4:f63476855239 87 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
owenbrotherwood 4:f63476855239 88 */
owenbrotherwood 4:f63476855239 89 int MicroBitMpr121::writeCommand(uint8_t reg, uint8_t value)
owenbrotherwood 0:fb4572fc4901 90 {
owenbrotherwood 4:f63476855239 91 uint8_t command[2];
owenbrotherwood 4:f63476855239 92 command[0] = reg;
owenbrotherwood 4:f63476855239 93 command[1] = value;
owenbrotherwood 4:f63476855239 94
owenbrotherwood 4:f63476855239 95 return i2c.write(address, (const char *)command, 2);
owenbrotherwood 0:fb4572fc4901 96 }
owenbrotherwood 0:fb4572fc4901 97
owenbrotherwood 4:f63476855239 98 /**
owenbrotherwood 4:f63476855239 99 * Issues a read command, copying data into the specified buffer.
owenbrotherwood 4:f63476855239 100 *
owenbrotherwood 4:f63476855239 101 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 102 *
owenbrotherwood 4:f63476855239 103 * @param reg The address of the register to access.
owenbrotherwood 4:f63476855239 104 *
owenbrotherwood 4:f63476855239 105 * @param buffer Memory area to read the data into.
owenbrotherwood 4:f63476855239 106 *
owenbrotherwood 4:f63476855239 107 * @param length The number of bytes to read.
owenbrotherwood 4:f63476855239 108 *
owenbrotherwood 4:f63476855239 109 * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
owenbrotherwood 4:f63476855239 110 */
owenbrotherwood 4:f63476855239 111 int MicroBitMpr121::readCommand(uint8_t reg, uint8_t* buffer, int length)
owenbrotherwood 0:fb4572fc4901 112 {
owenbrotherwood 4:f63476855239 113 int result;
owenbrotherwood 4:f63476855239 114
owenbrotherwood 4:f63476855239 115 if (buffer == NULL || length <= 0)
owenbrotherwood 4:f63476855239 116 return MICROBIT_INVALID_PARAMETER;
owenbrotherwood 0:fb4572fc4901 117
owenbrotherwood 4:f63476855239 118 result = i2c.write(address, (const char *)&reg, 1, true);
owenbrotherwood 4:f63476855239 119 if (result !=0)
owenbrotherwood 4:f63476855239 120 return MICROBIT_I2C_ERROR;
owenbrotherwood 4:f63476855239 121
owenbrotherwood 4:f63476855239 122 result = i2c.read(address, (char *)buffer, length);
owenbrotherwood 4:f63476855239 123 if (result !=0)
owenbrotherwood 4:f63476855239 124 return MICROBIT_I2C_ERROR;
owenbrotherwood 4:f63476855239 125
owenbrotherwood 4:f63476855239 126 return MICROBIT_OK;
owenbrotherwood 0:fb4572fc4901 127 }
owenbrotherwood 0:fb4572fc4901 128
owenbrotherwood 4:f63476855239 129 /**
owenbrotherwood 4:f63476855239 130 * Issues a read of a given address, and returns the value.
owenbrotherwood 4:f63476855239 131 *
owenbrotherwood 4:f63476855239 132 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 133 *
owenbrotherwood 4:f63476855239 134 * @param reg The address of the 16 bit register to access.
owenbrotherwood 4:f63476855239 135 *
owenbrotherwood 4:f63476855239 136 * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the read request failed.
owenbrotherwood 4:f63476855239 137 */
owenbrotherwood 4:f63476855239 138 int MicroBitMpr121::read16(uint8_t reg)
owenbrotherwood 0:fb4572fc4901 139 {
owenbrotherwood 4:f63476855239 140 uint8_t cmd[2];
owenbrotherwood 4:f63476855239 141 int result;
owenbrotherwood 4:f63476855239 142
owenbrotherwood 4:f63476855239 143 cmd[0] = reg;
owenbrotherwood 4:f63476855239 144 result = i2c.write(address, (const char *)cmd, 1);
owenbrotherwood 4:f63476855239 145 if (result !=0)
owenbrotherwood 4:f63476855239 146 return MICROBIT_I2C_ERROR;
owenbrotherwood 1:f6fed00a3ff2 147
owenbrotherwood 4:f63476855239 148 cmd[0] = 0x00;
owenbrotherwood 4:f63476855239 149 cmd[1] = 0x00;
owenbrotherwood 1:f6fed00a3ff2 150
owenbrotherwood 4:f63476855239 151 result = i2c.read(address, (char *)cmd, 2);
owenbrotherwood 4:f63476855239 152 if (result !=0)
owenbrotherwood 4:f63476855239 153 return MICROBIT_I2C_ERROR;
owenbrotherwood 4:f63476855239 154
owenbrotherwood 4:f63476855239 155 return (int16_t) ((cmd[1] | (cmd[0] << 8))); //concatenate the MSB and LSB
owenbrotherwood 0:fb4572fc4901 156 }
owenbrotherwood 0:fb4572fc4901 157
owenbrotherwood 4:f63476855239 158 /**
owenbrotherwood 4:f63476855239 159 * Issues a read of a given address, and returns the value.
owenbrotherwood 4:f63476855239 160 *
owenbrotherwood 4:f63476855239 161 * Blocks the calling thread until complete.
owenbrotherwood 4:f63476855239 162 *
owenbrotherwood 4:f63476855239 163 * @param reg The address of the 16 bit register to access.
owenbrotherwood 4:f63476855239 164 *
owenbrotherwood 4:f63476855239 165 * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
owenbrotherwood 4:f63476855239 166 */
owenbrotherwood 4:f63476855239 167 int MicroBitMpr121::read8(uint8_t reg)
owenbrotherwood 0:fb4572fc4901 168 {
owenbrotherwood 4:f63476855239 169 uint8_t data;
owenbrotherwood 4:f63476855239 170 int result;
owenbrotherwood 4:f63476855239 171
owenbrotherwood 4:f63476855239 172 data = 0;
owenbrotherwood 4:f63476855239 173 result = readCommand(reg, (uint8_t*) &data, 1);
owenbrotherwood 4:f63476855239 174 if (result != MICROBIT_OK)
owenbrotherwood 4:f63476855239 175 return MICROBIT_I2C_ERROR;
owenbrotherwood 4:f63476855239 176
owenbrotherwood 4:f63476855239 177 return data;
owenbrotherwood 0:fb4572fc4901 178 }
owenbrotherwood 0:fb4572fc4901 179
owenbrotherwood 4:f63476855239 180 /**
owenbrotherwood 4:f63476855239 181 * Periodic callback from MicroBit idle thread.
owenbrotherwood 4:f63476855239 182 *
owenbrotherwood 4:f63476855239 183 * Calls ...
owenbrotherwood 4:f63476855239 184 */
owenbrotherwood 4:f63476855239 185 void MicroBitMpr121::idleTick()
owenbrotherwood 0:fb4572fc4901 186 {
owenbrotherwood 4:f63476855239 187
owenbrotherwood 0:fb4572fc4901 188 }
owenbrotherwood 0:fb4572fc4901 189
owenbrotherwood 4:f63476855239 190 /**
owenbrotherwood 4:f63476855239 191 * Attempts to read the 8 bit ID from the mpr121, this can be used for
owenbrotherwood 4:f63476855239 192 * validation purposes. TODO?
owenbrotherwood 4:f63476855239 193 *
owenbrotherwood 4:f63476855239 194 * @return the 8 bit ID returned by the mpr121, or MICROBIT_I2C_ERROR if the request fails.
owenbrotherwood 4:f63476855239 195 *
owenbrotherwood 4:f63476855239 196 * @code
owenbrotherwood 4:f63476855239 197 * compass.whoAmI();
owenbrotherwood 4:f63476855239 198 * @endcode
owenbrotherwood 4:f63476855239 199 */
owenbrotherwood 4:f63476855239 200 int MicroBitMpr121::whoAmI()
owenbrotherwood 0:fb4572fc4901 201 {
owenbrotherwood 4:f63476855239 202 uint8_t data;
owenbrotherwood 4:f63476855239 203 int result;
owenbrotherwood 4:f63476855239 204
owenbrotherwood 4:f63476855239 205 // result = readCommand(MAG_WHOAMI, &data, 1);
owenbrotherwood 4:f63476855239 206 if (result != MICROBIT_OK)
owenbrotherwood 4:f63476855239 207 return MICROBIT_I2C_ERROR;
owenbrotherwood 4:f63476855239 208
owenbrotherwood 4:f63476855239 209 return (int)data;
owenbrotherwood 0:fb4572fc4901 210 }
owenbrotherwood 4:f63476855239 211
owenbrotherwood 4:f63476855239 212 /**
owenbrotherwood 4:f63476855239 213 * Destructor, where we deregister this instance from the array of fiber components.
owenbrotherwood 4:f63476855239 214 */
owenbrotherwood 4:f63476855239 215 MicroBitMpr121::~MicroBitMpr121()
owenbrotherwood 4:f63476855239 216 {
owenbrotherwood 4:f63476855239 217 fiber_remove_idle_component(this);
owenbrotherwood 4:f63476855239 218 }