class pah8011 for mbed

pixart_pah8011.cpp

Committer:
bell_huang
Date:
2017-10-27
Revision:
5:37451de228e4
Parent:
4:cbc49829af39
Child:
6:d196b612b14a

File content as of revision 5:37451de228e4:

#include "pixart_pah8011.h"


extern "C" {
    
    
    #include "pah_driver.h"
    #include "pah_platform_functions.h"
    
    
    static I2C      *g_i2c = NULL;
    static uint8_t  g_slave_id = 0x15;
    
    void disable_debug_printf(const char *fmt, ...)
    {
        // do nothing
    }

    DEBUG_PRINT_HANDLE debug_printf = disable_debug_printf;    
    
    
    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();
    }
    
    void pah8011::enable_debug_print(DEBUG_PRINT_HANDLE handler)
    {
        if (handler)
            debug_printf = handler;
        else
            debug_printf = disable_debug_printf;
    }
    
    bool pah8011::init(I2C &i2c, uint8_t slave_id)
    {
        g_i2c = &i2c;
        
        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);
    }
    
}