IS31SE5000 IR SENSOR FOR TOUCHLESS MOTION AND PROXIMITY
Dependents: test_IS31SE5000 testSensor
IS31SE5000.cpp@0:b92d4b745118, 2015-12-24 (annotated)
- Committer:
- Rhyme
- Date:
- Thu Dec 24 00:40:19 2015 +0000
- Revision:
- 0:b92d4b745118
- Child:
- 2:215d002d2bbf
first commit before publishing;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:b92d4b745118 | 1 | /** |
Rhyme | 0:b92d4b745118 | 2 | * IS31SE5000 : IR sensor for touchless motion and proximity |
Rhyme | 0:b92d4b745118 | 3 | * |
Rhyme | 0:b92d4b745118 | 4 | */ |
Rhyme | 0:b92d4b745118 | 5 | #include "mbed.h" |
Rhyme | 0:b92d4b745118 | 6 | #include "IS31SE5000.h" |
Rhyme | 0:b92d4b745118 | 7 | |
Rhyme | 0:b92d4b745118 | 8 | #define REG_STATUS 0x00 |
Rhyme | 0:b92d4b745118 | 9 | #define REG_SHUTDOWN 0x01 |
Rhyme | 0:b92d4b745118 | 10 | #define REG_CONFIG 0x11 |
Rhyme | 0:b92d4b745118 | 11 | |
Rhyme | 0:b92d4b745118 | 12 | #define SSD_SHUTDOWN 0x00 |
Rhyme | 0:b92d4b745118 | 13 | #define SSD_NORMAL 0x01 |
Rhyme | 0:b92d4b745118 | 14 | |
Rhyme | 0:b92d4b745118 | 15 | #define EC_400MA 0x00 |
Rhyme | 0:b92d4b745118 | 16 | #define EC_280MA 0x01 |
Rhyme | 0:b92d4b745118 | 17 | #define EC_210MA 0x02 |
Rhyme | 0:b92d4b745118 | 18 | #define EC_70MA 0x04 |
Rhyme | 0:b92d4b745118 | 19 | |
Rhyme | 0:b92d4b745118 | 20 | #define PD_BE_OFF 0x01 |
Rhyme | 0:b92d4b745118 | 21 | #define PD_BE_CLOSE 0x02 |
Rhyme | 0:b92d4b745118 | 22 | |
Rhyme | 0:b92d4b745118 | 23 | #define MD_PD2_PD1 0x01 |
Rhyme | 0:b92d4b745118 | 24 | #define MD_PD1_PD2 0x02 |
Rhyme | 0:b92d4b745118 | 25 | |
Rhyme | 0:b92d4b745118 | 26 | #define MODE_MOTION 0x00 |
Rhyme | 0:b92d4b745118 | 27 | #define MODE_PROXIMITY 0x01 |
Rhyme | 0:b92d4b745118 | 28 | |
Rhyme | 0:b92d4b745118 | 29 | /** |
Rhyme | 0:b92d4b745118 | 30 | * Register Definitions |
Rhyme | 0:b92d4b745118 | 31 | * |
Rhyme | 0:b92d4b745118 | 32 | * Status Register 0x00 (Read Only) |
Rhyme | 0:b92d4b745118 | 33 | * Bit [7:4] reserved |
Rhyme | 0:b92d4b745118 | 34 | * Bit [3:2] PD |
Rhyme | 0:b92d4b745118 | 35 | * Bit [1:0] MD |
Rhyme | 0:b92d4b745118 | 36 | * The Status Register stores the motion |
Rhyme | 0:b92d4b745118 | 37 | * information which detected by IS31SE5000 |
Rhyme | 0:b92d4b745118 | 38 | * |
Rhyme | 0:b92d4b745118 | 39 | * PD : Proximity Detection |
Rhyme | 0:b92d4b745118 | 40 | * 01 : Be off |
Rhyme | 0:b92d4b745118 | 41 | * 10 : Be close |
Rhyme | 0:b92d4b745118 | 42 | * Others : No motion |
Rhyme | 0:b92d4b745118 | 43 | * |
Rhyme | 0:b92d4b745118 | 44 | * MD : Motion Detection |
Rhyme | 0:b92d4b745118 | 45 | * 01 : From PD2 to PD1 |
Rhyme | 0:b92d4b745118 | 46 | * 10 : From PD1 to PD2 |
Rhyme | 0:b92d4b745118 | 47 | * Others : No motion |
Rhyme | 0:b92d4b745118 | 48 | * |
Rhyme | 0:b92d4b745118 | 49 | * Shutdown Register 0x01 |
Rhyme | 0:b92d4b745118 | 50 | * Bit [7:1] reserved |
Rhyme | 0:b92d4b745118 | 51 | * Bit [0] SSD (default = 0) |
Rhyme | 0:b92d4b745118 | 52 | * The Shutdown Register sets software shutdown |
Rhyme | 0:b92d4b745118 | 53 | * mode of IS31SE5000. |
Rhyme | 0:b92d4b745118 | 54 | * |
Rhyme | 0:b92d4b745118 | 55 | * SSD : Software Shutdown Enable |
Rhyme | 0:b92d4b745118 | 56 | * 0 : Software shutdown mode (default) |
Rhyme | 0:b92d4b745118 | 57 | * 1 : Normal operation |
Rhyme | 0:b92d4b745118 | 58 | * |
Rhyme | 0:b92d4b745118 | 59 | * Configuration Register |
Rhyme | 0:b92d4b745118 | 60 | * Bit [7:4] Reserved |
Rhyme | 0:b92d4b745118 | 61 | * Bit [3:1] EC (default = 000) |
Rhyme | 0:b92d4b745118 | 62 | * Bit [0] MODE (default = 0) |
Rhyme | 0:b92d4b745118 | 63 | * The Configuration Register sets the operating function. |
Rhyme | 0:b92d4b745118 | 64 | * |
Rhyme | 0:b92d4b745118 | 65 | * EC : Emitting Current Setting |
Rhyme | 0:b92d4b745118 | 66 | * (Adjusting detection distance, |
Rhyme | 0:b92d4b745118 | 67 | * refer to detail information in Data Sheet Page 10) |
Rhyme | 0:b92d4b745118 | 68 | * 000 : 400mA (default) |
Rhyme | 0:b92d4b745118 | 69 | * 001 : 280mA |
Rhyme | 0:b92d4b745118 | 70 | * 01x : 210mA |
Rhyme | 0:b92d4b745118 | 71 | * 1xx : 70mA |
Rhyme | 0:b92d4b745118 | 72 | * |
Rhyme | 0:b92d4b745118 | 73 | * MODE : Mode Selection |
Rhyme | 0:b92d4b745118 | 74 | * 0 : Motion mode (default) |
Rhyme | 0:b92d4b745118 | 75 | * 1 : Proximity mode |
Rhyme | 0:b92d4b745118 | 76 | */ |
Rhyme | 0:b92d4b745118 | 77 | |
Rhyme | 0:b92d4b745118 | 78 | IS31SE5000::IS31SE5000(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { |
Rhyme | 0:b92d4b745118 | 79 | // activate the peripheral |
Rhyme | 0:b92d4b745118 | 80 | shutdown_reg = 0x00 ; |
Rhyme | 0:b92d4b745118 | 81 | config_reg = 0x00 ; |
Rhyme | 0:b92d4b745118 | 82 | } |
Rhyme | 0:b92d4b745118 | 83 | |
Rhyme | 0:b92d4b745118 | 84 | IS31SE5000::~IS31SE5000() { } |
Rhyme | 0:b92d4b745118 | 85 | |
Rhyme | 0:b92d4b745118 | 86 | /* for Status Register */ |
Rhyme | 0:b92d4b745118 | 87 | uint8_t IS31SE5000::getStatus(void) /* returns status register */ |
Rhyme | 0:b92d4b745118 | 88 | { |
Rhyme | 0:b92d4b745118 | 89 | uint8_t status_reg = 0x00 ; |
Rhyme | 0:b92d4b745118 | 90 | readRegs(REG_STATUS, &status_reg, 1) ; |
Rhyme | 0:b92d4b745118 | 91 | return( status_reg ) ; |
Rhyme | 0:b92d4b745118 | 92 | } |
Rhyme | 0:b92d4b745118 | 93 | |
Rhyme | 0:b92d4b745118 | 94 | uint8_t IS31SE5000::getPD(void) /* get Proximity Detection Value */ |
Rhyme | 0:b92d4b745118 | 95 | { |
Rhyme | 0:b92d4b745118 | 96 | uint8_t data ; |
Rhyme | 0:b92d4b745118 | 97 | data = getStatus() ; |
Rhyme | 0:b92d4b745118 | 98 | data = (data >> 2) & 0x03 ; |
Rhyme | 0:b92d4b745118 | 99 | return(data) ; |
Rhyme | 0:b92d4b745118 | 100 | } |
Rhyme | 0:b92d4b745118 | 101 | |
Rhyme | 0:b92d4b745118 | 102 | uint8_t IS31SE5000::getMD(void) /* get Motion Detection Value */ |
Rhyme | 0:b92d4b745118 | 103 | { |
Rhyme | 0:b92d4b745118 | 104 | uint8_t data ; |
Rhyme | 0:b92d4b745118 | 105 | data = getStatus() ; |
Rhyme | 0:b92d4b745118 | 106 | data &= 0x03 ; |
Rhyme | 0:b92d4b745118 | 107 | return(data) ; |
Rhyme | 0:b92d4b745118 | 108 | } |
Rhyme | 0:b92d4b745118 | 109 | |
Rhyme | 0:b92d4b745118 | 110 | /* for Shutdown Register */ |
Rhyme | 0:b92d4b745118 | 111 | void IS31SE5000::activate(void) |
Rhyme | 0:b92d4b745118 | 112 | { |
Rhyme | 0:b92d4b745118 | 113 | uint8_t data[2] = { REG_SHUTDOWN, SSD_NORMAL } ; |
Rhyme | 0:b92d4b745118 | 114 | writeRegs(data, 2) ; |
Rhyme | 0:b92d4b745118 | 115 | } |
Rhyme | 0:b92d4b745118 | 116 | |
Rhyme | 0:b92d4b745118 | 117 | void IS31SE5000::shutdown(void) |
Rhyme | 0:b92d4b745118 | 118 | { |
Rhyme | 0:b92d4b745118 | 119 | uint8_t data[2] = { REG_SHUTDOWN, SSD_SHUTDOWN } ; |
Rhyme | 0:b92d4b745118 | 120 | writeRegs(data, 2) ; |
Rhyme | 0:b92d4b745118 | 121 | } |
Rhyme | 0:b92d4b745118 | 122 | |
Rhyme | 0:b92d4b745118 | 123 | /* for Configuration Register */ |
Rhyme | 0:b92d4b745118 | 124 | void IS31SE5000::setEC(uint8_t ec_value) |
Rhyme | 0:b92d4b745118 | 125 | { |
Rhyme | 0:b92d4b745118 | 126 | uint8_t data[2] ; |
Rhyme | 0:b92d4b745118 | 127 | data[0] = REG_CONFIG ; |
Rhyme | 0:b92d4b745118 | 128 | config_reg = ((ec_value << 1)&0x07) | (config_reg & 0x01) ; |
Rhyme | 0:b92d4b745118 | 129 | data[1] = config_reg ; |
Rhyme | 0:b92d4b745118 | 130 | writeRegs(data, 2) ; |
Rhyme | 0:b92d4b745118 | 131 | |
Rhyme | 0:b92d4b745118 | 132 | } |
Rhyme | 0:b92d4b745118 | 133 | |
Rhyme | 0:b92d4b745118 | 134 | void IS31SE5000::motionMode(void) |
Rhyme | 0:b92d4b745118 | 135 | { |
Rhyme | 0:b92d4b745118 | 136 | uint8_t data[2] ; |
Rhyme | 0:b92d4b745118 | 137 | data[0] = REG_CONFIG ; |
Rhyme | 0:b92d4b745118 | 138 | config_reg &= ~(MODE_PROXIMITY) ; |
Rhyme | 0:b92d4b745118 | 139 | data[1] = config_reg ; |
Rhyme | 0:b92d4b745118 | 140 | writeRegs(data, 2) ; |
Rhyme | 0:b92d4b745118 | 141 | } |
Rhyme | 0:b92d4b745118 | 142 | |
Rhyme | 0:b92d4b745118 | 143 | void IS31SE5000::proximityMode(void) |
Rhyme | 0:b92d4b745118 | 144 | { |
Rhyme | 0:b92d4b745118 | 145 | uint8_t data[2] ; |
Rhyme | 0:b92d4b745118 | 146 | data[0] = REG_CONFIG ; |
Rhyme | 0:b92d4b745118 | 147 | config_reg |= MODE_PROXIMITY ; |
Rhyme | 0:b92d4b745118 | 148 | data[1] = config_reg ; |
Rhyme | 0:b92d4b745118 | 149 | writeRegs(data, 2) ; |
Rhyme | 0:b92d4b745118 | 150 | } |
Rhyme | 0:b92d4b745118 | 151 | |
Rhyme | 0:b92d4b745118 | 152 | uint8_t IS31SE5000::getEC(void) |
Rhyme | 0:b92d4b745118 | 153 | { |
Rhyme | 0:b92d4b745118 | 154 | return( (config_reg >> 1) & 0x07) ; |
Rhyme | 0:b92d4b745118 | 155 | } |
Rhyme | 0:b92d4b745118 | 156 | |
Rhyme | 0:b92d4b745118 | 157 | uint8_t IS31SE5000::getMode(void) |
Rhyme | 0:b92d4b745118 | 158 | { |
Rhyme | 0:b92d4b745118 | 159 | return( config_reg & 0x01 ) ; |
Rhyme | 0:b92d4b745118 | 160 | } |
Rhyme | 0:b92d4b745118 | 161 | |
Rhyme | 0:b92d4b745118 | 162 | /* */ |
Rhyme | 0:b92d4b745118 | 163 | uint8_t IS31SE5000::get_shutdown_reg(void) |
Rhyme | 0:b92d4b745118 | 164 | { |
Rhyme | 0:b92d4b745118 | 165 | return(shutdown_reg) ; |
Rhyme | 0:b92d4b745118 | 166 | } |
Rhyme | 0:b92d4b745118 | 167 | |
Rhyme | 0:b92d4b745118 | 168 | uint8_t IS31SE5000::get_config_reg(void) |
Rhyme | 0:b92d4b745118 | 169 | { |
Rhyme | 0:b92d4b745118 | 170 | return( config_reg ) ; |
Rhyme | 0:b92d4b745118 | 171 | } |
Rhyme | 0:b92d4b745118 | 172 | |
Rhyme | 0:b92d4b745118 | 173 | void IS31SE5000::readRegs(int addr, uint8_t * data, int len) { |
Rhyme | 0:b92d4b745118 | 174 | char t[1] = {addr}; |
Rhyme | 0:b92d4b745118 | 175 | m_i2c.write(m_addr, t, 1, true); |
Rhyme | 0:b92d4b745118 | 176 | m_i2c.read(m_addr, (char *)data, len); |
Rhyme | 0:b92d4b745118 | 177 | } |
Rhyme | 0:b92d4b745118 | 178 | |
Rhyme | 0:b92d4b745118 | 179 | void IS31SE5000::writeRegs(uint8_t * data, int len) { |
Rhyme | 0:b92d4b745118 | 180 | m_i2c.write(m_addr, (char *)data, len); |
Rhyme | 0:b92d4b745118 | 181 | } |