Start of a microbit mpr121 library
Dependents: microbitmpr121-example
MicroBitMpr121.cpp
- Committer:
- owenbrotherwood
- Date:
- 2017-01-16
- Revision:
- 5:4a8384331ca7
- Parent:
- 4:f63476855239
- Child:
- 6:103a5a2ca571
File content as of revision 5:4a8384331ca7:
/* The MIT License (MIT) Copyright (c) 2016 British Broadcasting Corporation. This software is provided by Lancaster University by arrangement with the BBC. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Class definition for MicroBit Mpr121. * * Represents an implementation of the Mpr121. * Also includes ... */ #include "MicroBitConfig.h" #include "MicroBitPin.h" #include "MicroBitMpr121.h" #include "MicroBitFiber.h" #include "ErrorNo.h" /** * An initialisation member function. * * @param id the unique identifier for this instance. * * @param address the base address on the i2c bus. */ void MicroBitMpr121::init(uint16_t id, uint16_t address) { this->id = id; this->address = address; // Indicate that we're up and running. status |= MICROBIT_COMPONENT_RUNNING; } /** * Constructor. * Create a software representation. * * @param _i2c an instance of i2c * * @param address the address register on the i2c bus. Defaults to MPR121_DEFAULT_ADDR. * * @param id the ID of the new object. Defaults to MPR121_DEFAULT_ADDR. * * @code * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0); * * MicroBitMpr121 mpr121(i2c); * @endcode */ MicroBitMpr121::MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address, DigitalIn interrupt, uint16_t id) : int1(interrupt), i2c(_i2c) { init(id, address); } /** * Issues a standard, 2 byte I2C command write. * * Blocks the calling thread until complete. * * @param reg The address of the register to write to. * * @param value The value to write. * * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed. */ int MicroBitMpr121::writeCommand(uint8_t reg, uint8_t value) { uint8_t command[2]; command[0] = reg; command[1] = value; return i2c.write(address, (const char *)command, 2); } /** * Issues a read command, copying data into the specified buffer. * * Blocks the calling thread until complete. * * @param reg The address of the register to access. * * @param buffer Memory area to read the data into. * * @param length The number of bytes to read. * * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed. */ int MicroBitMpr121::readCommand(uint8_t reg, uint8_t* buffer, int length) { int result; if (buffer == NULL || length <= 0) return MICROBIT_INVALID_PARAMETER; result = i2c.write(address, (const char *)®, 1, true); if (result !=0) return MICROBIT_I2C_ERROR; result = i2c.read(address, (char *)buffer, length); if (result !=0) return MICROBIT_I2C_ERROR; return MICROBIT_OK; } /** * Issues a read of a given address, and returns the value. * * Blocks the calling thread until complete. * * @param reg The address of the 16 bit register to access. * * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the read request failed. */ int MicroBitMpr121::read16(uint8_t reg) { uint8_t cmd[2]; int result; cmd[0] = reg; result = i2c.write(address, (const char *)cmd, 1); if (result !=0) return MICROBIT_I2C_ERROR; cmd[0] = 0x00; cmd[1] = 0x00; result = i2c.read(address, (char *)cmd, 2); if (result !=0) return MICROBIT_I2C_ERROR; return (int16_t) ((cmd[1] | (cmd[0] << 8))); //concatenate the MSB and LSB } /** * Issues a read of a given address, and returns the value. * * Blocks the calling thread until complete. * * @param reg The address of the 16 bit register to access. * * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed. */ int MicroBitMpr121::read8(uint8_t reg) { uint8_t data; int result; data = 0; result = readCommand(reg, (uint8_t*) &data, 1); if (result != MICROBIT_OK) return MICROBIT_I2C_ERROR; return data; } /** * Periodic callback from MicroBit idle thread. * * Calls ... */ void MicroBitMpr121::idleTick() { } /** * Attempts to read the 8 bit ID from the mpr121, this can be used for * validation purposes. TODO? * * @return the 8 bit ID returned by the mpr121, or MICROBIT_I2C_ERROR if the request fails. * * @code * mpr121.whoAmI(); * @endcode */ int MicroBitMpr121::whoAmI() { uint8_t data; int result; // result = readCommand(MPR121_WHOAMI, &data, 1); if (result != MICROBIT_OK) return MICROBIT_I2C_ERROR; return (int)data; } /** * Destructor, where we deregister this instance from the array of fiber components. */ MicroBitMpr121::~MicroBitMpr121() { fiber_remove_idle_component(this); }