Hover!( Microchip MGC3130 ) library. Now, it is development version. http://www.hoverlabs.co/#hover https://www.switch-science.com/catalog/2124/

Dependents:   MjHover_Hello

MjHover.cpp

Committer:
matsujirushi
Date:
2015-04-03
Revision:
4:11a94d34ab58
Parent:
3:b03009537d66
Child:
5:bd26aba87b9b

File content as of revision 4:11a94d34ab58:

#include "MjHover.h"

namespace matsujirushi {

MjHover::MjHover(I2C* i2c, uint8_t address, DigitalInOut* ts, DigitalInOut* reset_n)
{
    this->i2c = i2c;
    this->address = address & 0xfe;
    this->ts = ts;
    this->reset_n = reset_n;
}

void MjHover::begin()
{
    this->ts->input();
    this->reset_n->output();
    this->reset_n->write(0);
    wait_us(100);
    this->reset_n->input();
    wait_ms(1000);
}

void MjHover::setRelease()
{
    this->ts->input();
    wait_us(100);
}

bool MjHover::getStatus()
{
    if (this->ts->read() == 0)
    {
        this->ts->output();
        this->ts->write(0);
        return false;
    }
    return true;
}

uint8_t MjHover::getEvent()
{
    this->i2c->start();
    this->i2c->write(this->address | 1);
    uint8_t size = this->i2c->read(1);
    uint8_t buffer[255];
    buffer[0] = size;
    for (int i = 1; i < size; i++)
    {
        buffer[i] = this->i2c->read(i < size - 1 ? 1 : 0);
    }
    this->i2c->stop();
    
    uint8_t data;
    uint8_t event;
    for (int c = 0; c < 18; c++)
    {
        data = buffer[c];
        
        if (c == 10 && data > 1)
        {
            event = (0x01 << (data - 1)) | 0x20;
            return event;
        }
        if (c == 14 && data > 0x1f)
        {
            event = ((data & 0xe0) >> 5) | 0x40;
            return event;
        }
        if (c == 15 && data > 0)
        {
            event = ((data & 0x03) << 3) | 0x40;
            return event;
        }
    }
    
    return 0x00;
}

const char* MjHover::getEventString(uint8_t eventByte)
{
    switch (eventByte)
    {
    case 0x22:
        return "Right Swipe";
    case 0x24:
        return "Left Swipe";
    case 0x28:
        return "Up Swipe";
    case 0x30:
        return "Down Swipe";
    case 0x41:
        return "Tap South";
    case 0x42:
        return "Tap West";
    case 0x50:
        return "Tap Center";
    case 0x48:
        return "Tap East";
    case 0x44:
        return "Tap North";
    default:
        return "";
    }
}

} // namespace matsujirushi