nunchuck: C port; FTI; C++ wraps C

Some API changes were made as well, see docs/apichanges.md.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-01-31 13:06:26 -07:00
parent 1bbb9386b7
commit 0749f130e1
15 changed files with 740 additions and 214 deletions

View File

@@ -1,6 +1,6 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
* Copyright (c) 2015-2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -33,117 +33,27 @@ using namespace upm;
using namespace std;
NUNCHUCK::NUNCHUCK(int bus, uint8_t addr)
NUNCHUCK::NUNCHUCK(int bus) :
m_nunchuck(nunchuck_init(bus))
{
stickX = 0;
stickY = 0;
accelX = 0;
accelY = 0;
accelZ = 0;
buttonC = false;
buttonZ = false;
// setup our i2c link
if ( !(m_i2c = mraa_i2c_init(bus)) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_init() failed");
return;
}
mraa_result_t rv;
if ( (rv = mraa_i2c_address(m_i2c, addr)) != MRAA_SUCCESS )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_address() failed");
}
if (!m_nunchuck)
throw std::runtime_error(string(__FUNCTION__)
+ ": nunchuck_init() failed");
}
NUNCHUCK::~NUNCHUCK()
{
mraa_i2c_stop(m_i2c);
}
bool NUNCHUCK::writeByte(uint8_t reg, uint8_t byte)
{
mraa_result_t rv;
if ( (rv = mraa_i2c_write_byte_data(m_i2c, byte, reg)) != MRAA_SUCCESS )
{
throw std::runtime_error(std::string(__FUNCTION__) +
": mraa_i2c_write_byte_data() failed");
return false;
}
return true;
}
int NUNCHUCK::readBytes(uint8_t reg, uint8_t *buffer, int len)
{
if (!len || !buffer)
return 0;
mraa_i2c_address(m_i2c, NUNCHUCK_I2C_ADDR);
mraa_i2c_write_byte(m_i2c, reg);
return mraa_i2c_read(m_i2c, buffer, len);
}
bool NUNCHUCK::init()
{
usleep(1000000);
// disable encryption
if (!writeByte(0xf0, 0x55))
return false;
if (!writeByte(0xfb, 0x00))
return false;
return true;
nunchuck_close(m_nunchuck);
}
void NUNCHUCK::update()
{
const int bufsize = 6;
uint8_t buf[bufsize];
int rv;
if (nunchuck_update(m_nunchuck))
throw std::runtime_error(string(__FUNCTION__)
+ ": nunchuck_update() failed");
rv = readBytes(0x00, buf, bufsize);
if (rv != bufsize)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": readBytes() failed");
return;
}
// analog stick X
stickX = buf[0];
// analog stick Y
stickY = buf[1];
// accelerometer X
accelX = ( (buf[2] << 2) | ((buf[5] & 0x0c) >> 2) );
// accelerometer Y
accelY = ( (buf[3] << 2) | ((buf[5] & 0x30) >> 4) );
// accelerometer Z
accelZ = ( (buf[4] << 2) | ((buf[5] & 0xc0) >> 6) );
// buttonC
if (buf[5] & 0x02)
buttonC = false;
else
buttonC = true;
// buttonZ
if (buf[5] & 0x01)
buttonZ = false;
else
buttonZ = true;
nunchuck_get_stick(m_nunchuck, &stickX, &stickY);
nunchuck_get_acceleration(m_nunchuck, &accelX, &accelY, &accelZ);
nunchuck_get_buttons(m_nunchuck, &buttonC, &buttonZ);
}