class pah8011 for mbed

pixart_pah8011.cpp

Committer:
bell_huang
Date:
2017-10-27
Revision:
4:cbc49829af39
Parent:
0:242cf8f28bf2
Child:
5:37451de228e4

File content as of revision 4:cbc49829af39:

#include "pixart_pah8011.h"

extern "C" {
    
    #include "pah_driver.h"

    typedef void (*debug_printf_handle)(const char *fmt, ...);

    static Serial   *g_serial = NULL;
    static I2C      *g_i2c = NULL;
    static uint8_t  g_slave_id = 0x15;

    void debug_printf(const char *fmt, ...)
    {
        if (!g_serial)
            return;
            
        char msg[128];
        va_list marker;
        va_start(marker, fmt);
        vsprintf(msg, fmt, marker);
        va_end(marker);
        g_serial->printf(msg);
    }
    
    bool i2c_write_reg(uint8_t addr, uint8_t data)
    {
        char data_write[2];
    
        data_write[0] = addr;
        data_write[1] = data;
        
        return 0 == g_i2c->write((g_slave_id << 1), data_write, 2, 0);
    }
    
    bool i2c_burst_read_reg(uint8_t addr, uint8_t *data, uint32_t rx_size)
    {
        if (0 != g_i2c->write((g_slave_id << 1), (const char*)&addr, 1, 1))
            return false;
        
        if (0 != g_i2c->read((g_slave_id << 1), (char*)data, rx_size, 0))
            return false;
        
        return true;
    }
    
    bool i2c_read_reg(uint8_t addr, uint8_t *data)
    {
        return i2c_burst_read_reg(addr, data, 1);
    }
    
    void delay_ms(uint64_t ms)
    {
        wait_ms(ms);
    }

} // extern "C"


namespace pixart {
    
    
    pah8011::pah8011()
        : m_is_ppg_enabled(false)
        , m_is_touch_enabled(false)
    {
    }    
    
    pah8011::~pah8011()
    {
        pah_deinit();
    }
    
    bool pah8011::init(I2C &i2c, Serial *serial, uint8_t slave_id)
    {
        g_i2c = &i2c;
        g_serial = serial;
        
        pah_flags_s flags;
        memset(&flags, 0, sizeof(flags));
        
        flags.intshape_pulse_type = pah_intshape_pulse_type_level;
        
        if (!pah_init_with_flags(&flags))
            return false;
        
        return true;
    }
    
    bool pah8011::enable_ppg()
    {
        m_is_ppg_enabled = true;
        
        return select_mode();
    }
    
    bool pah8011::disable_ppg()
    {
        m_is_ppg_enabled = false;
        
        return select_mode();
    }
    
    bool pah8011::enable_touch()
    {
        m_is_touch_enabled = true;
        
        return select_mode();
    }
    
    bool pah8011::disable_touch()
    {
        m_is_touch_enabled = false;
        
        return select_mode();
    }
    
    bool pah8011::task()
    {
        pah_ret ret = pah_task();
        
        return ret == pah_success
            || ret == pah_no_interrupt;
    }
    
    bool pah8011::get_result(task_result &result)
    {
        if (!pah_has_fifo_data())
            return false;
            
        result.data         = (int32_t*)pah_get_fifo_data();
        result.num_per_ch   = pah_fifo_data_num_per_ch();
        result.ch_num       = pah_get_fifo_ch_num();
        result.is_touched   = pah_is_touched();
            
        return true;
    }
 
    bool pah8011::select_mode()
    {
        pah_mode mode = pah_stop_mode;
        
        if (m_is_ppg_enabled)
        {
            if (m_is_touch_enabled)
                mode = pah_ppg_touch_mode;
            else
                mode = pah_ppg_mode;
        }
        else if (m_is_touch_enabled)
        {
            mode = pah_touch_mode;
        }
        else
        {
            mode = pah_stop_mode;
        }
        
        return pah_enter_mode(mode);
    }
    
}