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