class pah8011 for mbed

Committer:
bell_huang
Date:
Fri Oct 27 08:21:32 2017 +0000
Revision:
5:37451de228e4
Parent:
4:cbc49829af39
Child:
6:d196b612b14a
Change debug printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bell_huang 0:242cf8f28bf2 1 #include "pixart_pah8011.h"
bell_huang 0:242cf8f28bf2 2
bell_huang 5:37451de228e4 3
bell_huang 0:242cf8f28bf2 4 extern "C" {
bell_huang 0:242cf8f28bf2 5
bell_huang 5:37451de228e4 6
bell_huang 0:242cf8f28bf2 7 #include "pah_driver.h"
bell_huang 5:37451de228e4 8 #include "pah_platform_functions.h"
bell_huang 5:37451de228e4 9
bell_huang 5:37451de228e4 10
bell_huang 0:242cf8f28bf2 11 static I2C *g_i2c = NULL;
bell_huang 0:242cf8f28bf2 12 static uint8_t g_slave_id = 0x15;
bell_huang 5:37451de228e4 13
bell_huang 5:37451de228e4 14 void disable_debug_printf(const char *fmt, ...)
bell_huang 0:242cf8f28bf2 15 {
bell_huang 5:37451de228e4 16 // do nothing
bell_huang 0:242cf8f28bf2 17 }
bell_huang 5:37451de228e4 18
bell_huang 5:37451de228e4 19 DEBUG_PRINT_HANDLE debug_printf = disable_debug_printf;
bell_huang 5:37451de228e4 20
bell_huang 0:242cf8f28bf2 21
bell_huang 0:242cf8f28bf2 22 bool i2c_write_reg(uint8_t addr, uint8_t data)
bell_huang 0:242cf8f28bf2 23 {
bell_huang 0:242cf8f28bf2 24 char data_write[2];
bell_huang 0:242cf8f28bf2 25
bell_huang 0:242cf8f28bf2 26 data_write[0] = addr;
bell_huang 0:242cf8f28bf2 27 data_write[1] = data;
bell_huang 0:242cf8f28bf2 28
bell_huang 0:242cf8f28bf2 29 return 0 == g_i2c->write((g_slave_id << 1), data_write, 2, 0);
bell_huang 0:242cf8f28bf2 30 }
bell_huang 0:242cf8f28bf2 31
bell_huang 0:242cf8f28bf2 32 bool i2c_burst_read_reg(uint8_t addr, uint8_t *data, uint32_t rx_size)
bell_huang 0:242cf8f28bf2 33 {
bell_huang 0:242cf8f28bf2 34 if (0 != g_i2c->write((g_slave_id << 1), (const char*)&addr, 1, 1))
bell_huang 0:242cf8f28bf2 35 return false;
bell_huang 0:242cf8f28bf2 36
bell_huang 0:242cf8f28bf2 37 if (0 != g_i2c->read((g_slave_id << 1), (char*)data, rx_size, 0))
bell_huang 0:242cf8f28bf2 38 return false;
bell_huang 0:242cf8f28bf2 39
bell_huang 0:242cf8f28bf2 40 return true;
bell_huang 0:242cf8f28bf2 41 }
bell_huang 0:242cf8f28bf2 42
bell_huang 0:242cf8f28bf2 43 bool i2c_read_reg(uint8_t addr, uint8_t *data)
bell_huang 0:242cf8f28bf2 44 {
bell_huang 0:242cf8f28bf2 45 return i2c_burst_read_reg(addr, data, 1);
bell_huang 0:242cf8f28bf2 46 }
bell_huang 0:242cf8f28bf2 47
bell_huang 0:242cf8f28bf2 48 void delay_ms(uint64_t ms)
bell_huang 0:242cf8f28bf2 49 {
bell_huang 0:242cf8f28bf2 50 wait_ms(ms);
bell_huang 0:242cf8f28bf2 51 }
bell_huang 0:242cf8f28bf2 52
bell_huang 0:242cf8f28bf2 53 } // extern "C"
bell_huang 0:242cf8f28bf2 54
bell_huang 0:242cf8f28bf2 55
bell_huang 0:242cf8f28bf2 56 namespace pixart {
bell_huang 0:242cf8f28bf2 57
bell_huang 0:242cf8f28bf2 58
bell_huang 0:242cf8f28bf2 59 pah8011::pah8011()
bell_huang 0:242cf8f28bf2 60 : m_is_ppg_enabled(false)
bell_huang 0:242cf8f28bf2 61 , m_is_touch_enabled(false)
bell_huang 0:242cf8f28bf2 62 {
bell_huang 0:242cf8f28bf2 63 }
bell_huang 0:242cf8f28bf2 64
bell_huang 0:242cf8f28bf2 65 pah8011::~pah8011()
bell_huang 0:242cf8f28bf2 66 {
bell_huang 0:242cf8f28bf2 67 pah_deinit();
bell_huang 0:242cf8f28bf2 68 }
bell_huang 0:242cf8f28bf2 69
bell_huang 5:37451de228e4 70 void pah8011::enable_debug_print(DEBUG_PRINT_HANDLE handler)
bell_huang 5:37451de228e4 71 {
bell_huang 5:37451de228e4 72 if (handler)
bell_huang 5:37451de228e4 73 debug_printf = handler;
bell_huang 5:37451de228e4 74 else
bell_huang 5:37451de228e4 75 debug_printf = disable_debug_printf;
bell_huang 5:37451de228e4 76 }
bell_huang 5:37451de228e4 77
bell_huang 5:37451de228e4 78 bool pah8011::init(I2C &i2c, uint8_t slave_id)
bell_huang 0:242cf8f28bf2 79 {
bell_huang 0:242cf8f28bf2 80 g_i2c = &i2c;
bell_huang 0:242cf8f28bf2 81
bell_huang 0:242cf8f28bf2 82 pah_flags_s flags;
bell_huang 0:242cf8f28bf2 83 memset(&flags, 0, sizeof(flags));
bell_huang 0:242cf8f28bf2 84
bell_huang 0:242cf8f28bf2 85 flags.intshape_pulse_type = pah_intshape_pulse_type_level;
bell_huang 0:242cf8f28bf2 86
bell_huang 0:242cf8f28bf2 87 if (!pah_init_with_flags(&flags))
bell_huang 0:242cf8f28bf2 88 return false;
bell_huang 0:242cf8f28bf2 89
bell_huang 0:242cf8f28bf2 90 return true;
bell_huang 0:242cf8f28bf2 91 }
bell_huang 0:242cf8f28bf2 92
bell_huang 0:242cf8f28bf2 93 bool pah8011::enable_ppg()
bell_huang 0:242cf8f28bf2 94 {
bell_huang 0:242cf8f28bf2 95 m_is_ppg_enabled = true;
bell_huang 0:242cf8f28bf2 96
bell_huang 0:242cf8f28bf2 97 return select_mode();
bell_huang 0:242cf8f28bf2 98 }
bell_huang 0:242cf8f28bf2 99
bell_huang 0:242cf8f28bf2 100 bool pah8011::disable_ppg()
bell_huang 0:242cf8f28bf2 101 {
bell_huang 0:242cf8f28bf2 102 m_is_ppg_enabled = false;
bell_huang 0:242cf8f28bf2 103
bell_huang 0:242cf8f28bf2 104 return select_mode();
bell_huang 0:242cf8f28bf2 105 }
bell_huang 0:242cf8f28bf2 106
bell_huang 0:242cf8f28bf2 107 bool pah8011::enable_touch()
bell_huang 0:242cf8f28bf2 108 {
bell_huang 0:242cf8f28bf2 109 m_is_touch_enabled = true;
bell_huang 0:242cf8f28bf2 110
bell_huang 0:242cf8f28bf2 111 return select_mode();
bell_huang 0:242cf8f28bf2 112 }
bell_huang 0:242cf8f28bf2 113
bell_huang 0:242cf8f28bf2 114 bool pah8011::disable_touch()
bell_huang 0:242cf8f28bf2 115 {
bell_huang 0:242cf8f28bf2 116 m_is_touch_enabled = false;
bell_huang 0:242cf8f28bf2 117
bell_huang 0:242cf8f28bf2 118 return select_mode();
bell_huang 0:242cf8f28bf2 119 }
bell_huang 0:242cf8f28bf2 120
bell_huang 0:242cf8f28bf2 121 bool pah8011::task()
bell_huang 0:242cf8f28bf2 122 {
bell_huang 0:242cf8f28bf2 123 pah_ret ret = pah_task();
bell_huang 0:242cf8f28bf2 124
bell_huang 0:242cf8f28bf2 125 return ret == pah_success
bell_huang 0:242cf8f28bf2 126 || ret == pah_no_interrupt;
bell_huang 0:242cf8f28bf2 127 }
bell_huang 0:242cf8f28bf2 128
bell_huang 0:242cf8f28bf2 129 bool pah8011::get_result(task_result &result)
bell_huang 0:242cf8f28bf2 130 {
bell_huang 0:242cf8f28bf2 131 if (!pah_has_fifo_data())
bell_huang 0:242cf8f28bf2 132 return false;
bell_huang 0:242cf8f28bf2 133
bell_huang 4:cbc49829af39 134 result.data = (int32_t*)pah_get_fifo_data();
bell_huang 0:242cf8f28bf2 135 result.num_per_ch = pah_fifo_data_num_per_ch();
bell_huang 0:242cf8f28bf2 136 result.ch_num = pah_get_fifo_ch_num();
bell_huang 0:242cf8f28bf2 137 result.is_touched = pah_is_touched();
bell_huang 0:242cf8f28bf2 138
bell_huang 0:242cf8f28bf2 139 return true;
bell_huang 0:242cf8f28bf2 140 }
bell_huang 0:242cf8f28bf2 141
bell_huang 0:242cf8f28bf2 142 bool pah8011::select_mode()
bell_huang 0:242cf8f28bf2 143 {
bell_huang 0:242cf8f28bf2 144 pah_mode mode = pah_stop_mode;
bell_huang 0:242cf8f28bf2 145
bell_huang 0:242cf8f28bf2 146 if (m_is_ppg_enabled)
bell_huang 0:242cf8f28bf2 147 {
bell_huang 0:242cf8f28bf2 148 if (m_is_touch_enabled)
bell_huang 0:242cf8f28bf2 149 mode = pah_ppg_touch_mode;
bell_huang 0:242cf8f28bf2 150 else
bell_huang 0:242cf8f28bf2 151 mode = pah_ppg_mode;
bell_huang 0:242cf8f28bf2 152 }
bell_huang 0:242cf8f28bf2 153 else if (m_is_touch_enabled)
bell_huang 0:242cf8f28bf2 154 {
bell_huang 0:242cf8f28bf2 155 mode = pah_touch_mode;
bell_huang 0:242cf8f28bf2 156 }
bell_huang 0:242cf8f28bf2 157 else
bell_huang 0:242cf8f28bf2 158 {
bell_huang 0:242cf8f28bf2 159 mode = pah_stop_mode;
bell_huang 0:242cf8f28bf2 160 }
bell_huang 0:242cf8f28bf2 161
bell_huang 0:242cf8f28bf2 162 return pah_enter_mode(mode);
bell_huang 0:242cf8f28bf2 163 }
bell_huang 0:242cf8f28bf2 164
bell_huang 0:242cf8f28bf2 165 }
bell_huang 0:242cf8f28bf2 166