/* * Author: Jon Trulson * Copyright (c) 2014 Intel Corporation. * * 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. */ #include #include #include #include "mpr121.h" using namespace upm; using namespace std; MPR121::MPR121(int bus, uint8_t address) : m_i2c(bus) { m_addr = address; mraa::Result ret = m_i2c.address(m_addr); if (ret != mraa::SUCCESS) { throw std::invalid_argument(std::string(__FUNCTION__) + ": mraa_i2c_address() failed"); return; } m_buttonStates = 0; m_overCurrentFault = false; } mraa::Result MPR121::writeBytes(uint8_t reg, uint8_t *buffer, int len) { if (!len || !buffer) return mraa::SUCCESS; // create a buffer 1 byte larger than the supplied buffer, // store the register in the first byte uint8_t buf2[len + 1]; buf2[0] = reg; // copy in the buffer after the reg byte for (int i=1; i<(len + 1); i++) buf2[i] = buffer[i-1]; m_i2c.address(m_addr); return m_i2c.write(buf2, len + 1); } int MPR121::readBytes(uint8_t reg, uint8_t *buffer, int len) { if (!len || !buffer) return 0; // The usual m_i2c.read() does not work here, so we need to // read each byte individually. for (int i=0; i