GSMA version
Dependencies: FXOS8700CQ mbed sfh7779
Fork of StarterKit by
sensors.cpp@29:e6c8bd41caa6, 2016-07-16 (annotated)
- Committer:
- stefanrousseau
- Date:
- Sat Jul 16 17:35:15 2016 +0000
- Revision:
- 29:e6c8bd41caa6
- Parent:
- 11:e6602513730f
- Child:
- 30:48b43538e98b
Changed proximity to value instead of string
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefanrousseau | 4:f83bedd9cab4 | 1 | #include "mbed.h" |
stefanrousseau | 4:f83bedd9cab4 | 2 | #include "sensors.h" |
stefanrousseau | 4:f83bedd9cab4 | 3 | |
stefanrousseau | 4:f83bedd9cab4 | 4 | //I2C for pmod sensors: |
stefanrousseau | 4:f83bedd9cab4 | 5 | #define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020 |
stefanrousseau | 4:f83bedd9cab4 | 6 | #define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020 |
stefanrousseau | 4:f83bedd9cab4 | 7 | |
stefanrousseau | 11:e6602513730f | 8 | #include "hardware.h" |
stefanrousseau | 11:e6602513730f | 9 | //I2C i2c(PTC11, PTC10); //SDA, SCL |
stefanrousseau | 4:f83bedd9cab4 | 10 | |
stefanrousseau | 4:f83bedd9cab4 | 11 | #include "FXOS8700CQ.h" |
stefanrousseau | 4:f83bedd9cab4 | 12 | // Pin names for the motion sensor FRDM-K64F board: |
stefanrousseau | 4:f83bedd9cab4 | 13 | FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1) |
stefanrousseau | 4:f83bedd9cab4 | 14 | // Storage for the data from the sensor |
stefanrousseau | 4:f83bedd9cab4 | 15 | SRAWDATA accel_data; |
stefanrousseau | 4:f83bedd9cab4 | 16 | SRAWDATA magn_data; |
stefanrousseau | 4:f83bedd9cab4 | 17 | //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F |
stefanrousseau | 4:f83bedd9cab4 | 18 | InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt |
stefanrousseau | 4:f83bedd9cab4 | 19 | bool fxos_int2_triggered = false; |
stefanrousseau | 4:f83bedd9cab4 | 20 | void trigger_fxos_int2(void) |
stefanrousseau | 4:f83bedd9cab4 | 21 | { |
stefanrousseau | 4:f83bedd9cab4 | 22 | fxos_int2_triggered = true; |
stefanrousseau | 4:f83bedd9cab4 | 23 | //us_ellapsed = t.read_us(); |
stefanrousseau | 4:f83bedd9cab4 | 24 | } |
stefanrousseau | 4:f83bedd9cab4 | 25 | |
stefanrousseau | 4:f83bedd9cab4 | 26 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 27 | * Perform I2C single read. |
stefanrousseau | 4:f83bedd9cab4 | 28 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 29 | unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress) |
stefanrousseau | 4:f83bedd9cab4 | 30 | { |
stefanrousseau | 4:f83bedd9cab4 | 31 | char rxbuffer [1]; |
stefanrousseau | 11:e6602513730f | 32 | i2c.read(ucDeviceAddress, rxbuffer, 1 ); |
stefanrousseau | 4:f83bedd9cab4 | 33 | return (unsigned char)rxbuffer[0]; |
stefanrousseau | 4:f83bedd9cab4 | 34 | } //I2C_ReadSingleByte() |
stefanrousseau | 4:f83bedd9cab4 | 35 | |
stefanrousseau | 4:f83bedd9cab4 | 36 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 37 | * Perform I2C single read from address. |
stefanrousseau | 4:f83bedd9cab4 | 38 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 39 | unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr) |
stefanrousseau | 4:f83bedd9cab4 | 40 | { |
stefanrousseau | 4:f83bedd9cab4 | 41 | char txbuffer [1]; |
stefanrousseau | 4:f83bedd9cab4 | 42 | char rxbuffer [1]; |
stefanrousseau | 4:f83bedd9cab4 | 43 | txbuffer[0] = (char)Addr; |
stefanrousseau | 11:e6602513730f | 44 | i2c.write(ucDeviceAddress, txbuffer, 1 ); |
stefanrousseau | 11:e6602513730f | 45 | i2c.read(ucDeviceAddress, rxbuffer, 1 ); |
stefanrousseau | 4:f83bedd9cab4 | 46 | return (unsigned char)rxbuffer[0]; |
stefanrousseau | 4:f83bedd9cab4 | 47 | } //I2C_ReadSingleByteFromAddr() |
stefanrousseau | 4:f83bedd9cab4 | 48 | |
stefanrousseau | 4:f83bedd9cab4 | 49 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 50 | * Perform I2C read of more than 1 byte. |
stefanrousseau | 4:f83bedd9cab4 | 51 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 52 | int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength) |
stefanrousseau | 4:f83bedd9cab4 | 53 | { |
stefanrousseau | 4:f83bedd9cab4 | 54 | int status; |
stefanrousseau | 11:e6602513730f | 55 | status = i2c.read(ucDeviceAddress, ucData, ucLength); |
stefanrousseau | 4:f83bedd9cab4 | 56 | return status; |
stefanrousseau | 4:f83bedd9cab4 | 57 | } //I2C_ReadMultipleBytes() |
stefanrousseau | 4:f83bedd9cab4 | 58 | |
stefanrousseau | 4:f83bedd9cab4 | 59 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 60 | * Perform I2C write of a single byte. |
stefanrousseau | 4:f83bedd9cab4 | 61 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 62 | int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop) |
stefanrousseau | 4:f83bedd9cab4 | 63 | { |
stefanrousseau | 4:f83bedd9cab4 | 64 | int status; |
stefanrousseau | 4:f83bedd9cab4 | 65 | char txbuffer [1]; |
stefanrousseau | 4:f83bedd9cab4 | 66 | txbuffer[0] = (char)Data; //data |
stefanrousseau | 11:e6602513730f | 67 | status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop |
stefanrousseau | 4:f83bedd9cab4 | 68 | return status; |
stefanrousseau | 4:f83bedd9cab4 | 69 | } //I2C_WriteSingleByte() |
stefanrousseau | 4:f83bedd9cab4 | 70 | |
stefanrousseau | 4:f83bedd9cab4 | 71 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 72 | * Perform I2C write of 1 byte to an address. |
stefanrousseau | 4:f83bedd9cab4 | 73 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 74 | int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop) |
stefanrousseau | 4:f83bedd9cab4 | 75 | { |
stefanrousseau | 4:f83bedd9cab4 | 76 | int status; |
stefanrousseau | 4:f83bedd9cab4 | 77 | char txbuffer [2]; |
stefanrousseau | 4:f83bedd9cab4 | 78 | txbuffer[0] = (char)Addr; //address |
stefanrousseau | 4:f83bedd9cab4 | 79 | txbuffer[1] = (char)Data; //data |
stefanrousseau | 11:e6602513730f | 80 | //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end |
stefanrousseau | 11:e6602513730f | 81 | status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop |
stefanrousseau | 4:f83bedd9cab4 | 82 | return status; |
stefanrousseau | 4:f83bedd9cab4 | 83 | } //I2C_WriteSingleByteToAddr() |
stefanrousseau | 4:f83bedd9cab4 | 84 | |
stefanrousseau | 4:f83bedd9cab4 | 85 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 86 | * Perform I2C write of more than 1 byte. |
stefanrousseau | 4:f83bedd9cab4 | 87 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 88 | int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop) |
stefanrousseau | 4:f83bedd9cab4 | 89 | { |
stefanrousseau | 4:f83bedd9cab4 | 90 | int status; |
stefanrousseau | 11:e6602513730f | 91 | status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop |
stefanrousseau | 4:f83bedd9cab4 | 92 | return status; |
stefanrousseau | 4:f83bedd9cab4 | 93 | } //I2C_WriteMultipleBytes() |
stefanrousseau | 4:f83bedd9cab4 | 94 | |
stefanrousseau | 4:f83bedd9cab4 | 95 | bool bSi7020_present = false; |
stefanrousseau | 4:f83bedd9cab4 | 96 | void Init_Si7020(void) |
stefanrousseau | 4:f83bedd9cab4 | 97 | { |
stefanrousseau | 4:f83bedd9cab4 | 98 | char SN_7020 [8]; |
stefanrousseau | 4:f83bedd9cab4 | 99 | //SN part 1: |
stefanrousseau | 4:f83bedd9cab4 | 100 | I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false); |
stefanrousseau | 4:f83bedd9cab4 | 101 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4); |
stefanrousseau | 4:f83bedd9cab4 | 102 | |
stefanrousseau | 4:f83bedd9cab4 | 103 | //SN part 1: |
stefanrousseau | 4:f83bedd9cab4 | 104 | I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false); |
stefanrousseau | 4:f83bedd9cab4 | 105 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4); |
stefanrousseau | 4:f83bedd9cab4 | 106 | |
stefanrousseau | 4:f83bedd9cab4 | 107 | char Ver_7020 [2]; |
stefanrousseau | 4:f83bedd9cab4 | 108 | //FW version: |
stefanrousseau | 4:f83bedd9cab4 | 109 | I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false); |
stefanrousseau | 4:f83bedd9cab4 | 110 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2); |
stefanrousseau | 4:f83bedd9cab4 | 111 | |
stefanrousseau | 4:f83bedd9cab4 | 112 | if (SN_7020[4] != 0x14) |
stefanrousseau | 4:f83bedd9cab4 | 113 | { |
stefanrousseau | 4:f83bedd9cab4 | 114 | bSi7020_present = false; |
stefanrousseau | 4:f83bedd9cab4 | 115 | printf("Si7020 sensor not found\n"); |
stefanrousseau | 4:f83bedd9cab4 | 116 | } |
stefanrousseau | 4:f83bedd9cab4 | 117 | else |
stefanrousseau | 4:f83bedd9cab4 | 118 | { |
stefanrousseau | 4:f83bedd9cab4 | 119 | bSi7020_present = true; |
stefanrousseau | 4:f83bedd9cab4 | 120 | printf("Si7020 SN = 0x%02X%02X%02X%02X%02X%02X%02X%02X\n", SN_7020[0], SN_7020[1], SN_7020[2], SN_7020[3], SN_7020[4], SN_7020[5], SN_7020[6], SN_7020[7]); |
stefanrousseau | 4:f83bedd9cab4 | 121 | printf("Si7020 Version# = 0x%02X\n", Ver_7020[0]); |
stefanrousseau | 4:f83bedd9cab4 | 122 | } //bool bSi7020_present = true |
stefanrousseau | 4:f83bedd9cab4 | 123 | |
stefanrousseau | 4:f83bedd9cab4 | 124 | } //Init_Si7020() |
stefanrousseau | 4:f83bedd9cab4 | 125 | |
stefanrousseau | 4:f83bedd9cab4 | 126 | void Read_Si7020(void) |
stefanrousseau | 4:f83bedd9cab4 | 127 | { |
stefanrousseau | 4:f83bedd9cab4 | 128 | if (bSi7020_present) |
stefanrousseau | 4:f83bedd9cab4 | 129 | { |
stefanrousseau | 4:f83bedd9cab4 | 130 | char Humidity [2]; |
stefanrousseau | 4:f83bedd9cab4 | 131 | char Temperature [2]; |
stefanrousseau | 4:f83bedd9cab4 | 132 | //Command to measure humidity (temperature also gets measured): |
stefanrousseau | 4:f83bedd9cab4 | 133 | I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read |
stefanrousseau | 4:f83bedd9cab4 | 134 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done |
stefanrousseau | 4:f83bedd9cab4 | 135 | wait (0.05); //wait for measurement. Can also keep reading until no NACK is received |
stefanrousseau | 4:f83bedd9cab4 | 136 | //I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE5, false); //Hold mod, the device does a clock stretch on the read until it is done (crashes the I2C bus... |
stefanrousseau | 4:f83bedd9cab4 | 137 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity |
stefanrousseau | 4:f83bedd9cab4 | 138 | //printf("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]); |
stefanrousseau | 4:f83bedd9cab4 | 139 | int rh_code = (Humidity[0] << 8) + Humidity[1]; |
stefanrousseau | 4:f83bedd9cab4 | 140 | float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet |
stefanrousseau | 4:f83bedd9cab4 | 141 | //printf("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //printf("%*.*f\n", myFieldWidth, myPrecision, myFloatValue); |
stefanrousseau | 4:f83bedd9cab4 | 142 | sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh); |
stefanrousseau | 4:f83bedd9cab4 | 143 | |
stefanrousseau | 4:f83bedd9cab4 | 144 | //Command to read temperature when humidity is already done: |
stefanrousseau | 4:f83bedd9cab4 | 145 | I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false); |
stefanrousseau | 4:f83bedd9cab4 | 146 | I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature |
stefanrousseau | 4:f83bedd9cab4 | 147 | //printf("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]); |
stefanrousseau | 4:f83bedd9cab4 | 148 | int temp_code = (Temperature[0] << 8) + Temperature[1]; |
stefanrousseau | 4:f83bedd9cab4 | 149 | float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius |
stefanrousseau | 4:f83bedd9cab4 | 150 | //printf("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp); |
stefanrousseau | 4:f83bedd9cab4 | 151 | sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp); |
stefanrousseau | 4:f83bedd9cab4 | 152 | } //bool bSi7020_present = true |
stefanrousseau | 4:f83bedd9cab4 | 153 | |
stefanrousseau | 4:f83bedd9cab4 | 154 | } //Read_Si7020() |
stefanrousseau | 4:f83bedd9cab4 | 155 | |
stefanrousseau | 4:f83bedd9cab4 | 156 | /*------------------------------------------------------------------------------ |
stefanrousseau | 4:f83bedd9cab4 | 157 | * The following are aliases so that the Si1145 coding examples can be used as-is. |
stefanrousseau | 4:f83bedd9cab4 | 158 | *------------------------------------------------------------------------------*/ |
stefanrousseau | 4:f83bedd9cab4 | 159 | unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg' |
stefanrousseau | 4:f83bedd9cab4 | 160 | { |
stefanrousseau | 4:f83bedd9cab4 | 161 | unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg); |
stefanrousseau | 4:f83bedd9cab4 | 162 | return (result); |
stefanrousseau | 4:f83bedd9cab4 | 163 | } //ReadFrom_Si1145_Register() |
stefanrousseau | 4:f83bedd9cab4 | 164 | |
stefanrousseau | 4:f83bedd9cab4 | 165 | void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg' |
stefanrousseau | 4:f83bedd9cab4 | 166 | { |
stefanrousseau | 4:f83bedd9cab4 | 167 | I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true); |
stefanrousseau | 4:f83bedd9cab4 | 168 | } //WriteTo_Si1145_Register() |
stefanrousseau | 4:f83bedd9cab4 | 169 | |
stefanrousseau | 4:f83bedd9cab4 | 170 | #define REG_PARAM_WR 0x17 |
stefanrousseau | 4:f83bedd9cab4 | 171 | #define REG_PARAM_RD 0x2E |
stefanrousseau | 4:f83bedd9cab4 | 172 | #define REG_COMMAND 0x18 |
stefanrousseau | 4:f83bedd9cab4 | 173 | #define REG_RESPONSE 0x20 |
stefanrousseau | 4:f83bedd9cab4 | 174 | #define REG_HW_KEY 0x07 |
stefanrousseau | 4:f83bedd9cab4 | 175 | #define HW_KEY_VAL0 0x17 |
stefanrousseau | 4:f83bedd9cab4 | 176 | #define REG_MEAS_RATE_LSB 0x08 |
stefanrousseau | 4:f83bedd9cab4 | 177 | #define REG_MEAS_RATE_MSB 0x09 |
stefanrousseau | 4:f83bedd9cab4 | 178 | #define REG_PS_LED21 0x0F |
stefanrousseau | 4:f83bedd9cab4 | 179 | #define REG_PS_LED3 0x10 |
stefanrousseau | 4:f83bedd9cab4 | 180 | #define MAX_LED_CURRENT 0xF |
stefanrousseau | 4:f83bedd9cab4 | 181 | #define PARAM_CH_LIST 0x01 |
stefanrousseau | 4:f83bedd9cab4 | 182 | #define REG_ALS_VIS_DATA0 0x22 |
stefanrousseau | 4:f83bedd9cab4 | 183 | #define REG_ALS_VIS_DATA1 0x23 |
stefanrousseau | 4:f83bedd9cab4 | 184 | #define REG_ALS_IR_DATA0 0x24 |
stefanrousseau | 4:f83bedd9cab4 | 185 | #define REG_ALS_IR_DATA1 0x25 |
stefanrousseau | 4:f83bedd9cab4 | 186 | #define REG_PS1_DATA0 0x26 |
stefanrousseau | 4:f83bedd9cab4 | 187 | #define REG_PS1_DATA1 0x27 |
stefanrousseau | 4:f83bedd9cab4 | 188 | #define REG_PS2_DATA0 0x28 |
stefanrousseau | 4:f83bedd9cab4 | 189 | #define REG_PS2_DATA1 0x29 |
stefanrousseau | 4:f83bedd9cab4 | 190 | #define REG_PS3_DATA0 0x2A |
stefanrousseau | 4:f83bedd9cab4 | 191 | #define REG_PS3_DATA1 0x2B |
stefanrousseau | 4:f83bedd9cab4 | 192 | #define REG_UVINDEX0 0x2C |
stefanrousseau | 4:f83bedd9cab4 | 193 | #define REG_UVINDEX1 0x2D |
stefanrousseau | 4:f83bedd9cab4 | 194 | int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address' |
stefanrousseau | 4:f83bedd9cab4 | 195 | { |
stefanrousseau | 4:f83bedd9cab4 | 196 | char txbuffer [3]; |
stefanrousseau | 4:f83bedd9cab4 | 197 | txbuffer[0] = (char)REG_PARAM_WR; //destination |
stefanrousseau | 4:f83bedd9cab4 | 198 | txbuffer[1] = (char)value; |
stefanrousseau | 4:f83bedd9cab4 | 199 | txbuffer[2] = (char)(0xA0 + (address & 0x1F)); |
stefanrousseau | 4:f83bedd9cab4 | 200 | int retval; |
stefanrousseau | 4:f83bedd9cab4 | 201 | //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval; |
stefanrousseau | 4:f83bedd9cab4 | 202 | retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true); |
stefanrousseau | 4:f83bedd9cab4 | 203 | if(retval!=0) return retval; |
stefanrousseau | 4:f83bedd9cab4 | 204 | while(1) |
stefanrousseau | 4:f83bedd9cab4 | 205 | { |
stefanrousseau | 4:f83bedd9cab4 | 206 | retval=ReadFrom_Si1145_Register(REG_PARAM_RD); |
stefanrousseau | 4:f83bedd9cab4 | 207 | if (retval==value) break; |
stefanrousseau | 4:f83bedd9cab4 | 208 | } |
stefanrousseau | 4:f83bedd9cab4 | 209 | return (0); |
stefanrousseau | 4:f83bedd9cab4 | 210 | } //Si1145_ParamSet() |
stefanrousseau | 4:f83bedd9cab4 | 211 | |
stefanrousseau | 4:f83bedd9cab4 | 212 | void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements |
stefanrousseau | 4:f83bedd9cab4 | 213 | { |
stefanrousseau | 4:f83bedd9cab4 | 214 | WriteTo_Si1145_Register(REG_COMMAND,0x07); |
stefanrousseau | 4:f83bedd9cab4 | 215 | } //PsAlsForce() |
stefanrousseau | 4:f83bedd9cab4 | 216 | |
stefanrousseau | 4:f83bedd9cab4 | 217 | bool bSi1145_present = false; |
stefanrousseau | 4:f83bedd9cab4 | 218 | void Init_Si1145(void) |
stefanrousseau | 4:f83bedd9cab4 | 219 | { |
stefanrousseau | 4:f83bedd9cab4 | 220 | unsigned char readbyte; |
stefanrousseau | 4:f83bedd9cab4 | 221 | //Read Si1145 part ID: |
stefanrousseau | 4:f83bedd9cab4 | 222 | readbyte = ReadFrom_Si1145_Register(0x00); |
stefanrousseau | 4:f83bedd9cab4 | 223 | if (readbyte != 0x45) |
stefanrousseau | 4:f83bedd9cab4 | 224 | { |
stefanrousseau | 4:f83bedd9cab4 | 225 | bSi1145_present = false; |
stefanrousseau | 4:f83bedd9cab4 | 226 | printf("Si1145 sensor not found\n"); |
stefanrousseau | 4:f83bedd9cab4 | 227 | } |
stefanrousseau | 4:f83bedd9cab4 | 228 | else |
stefanrousseau | 4:f83bedd9cab4 | 229 | { |
stefanrousseau | 4:f83bedd9cab4 | 230 | bSi1145_present = true; |
stefanrousseau | 4:f83bedd9cab4 | 231 | printf("Si1145 Part ID : 0x%02X\n", readbyte); |
stefanrousseau | 4:f83bedd9cab4 | 232 | //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17) |
stefanrousseau | 4:f83bedd9cab4 | 233 | WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0); |
stefanrousseau | 4:f83bedd9cab4 | 234 | |
stefanrousseau | 4:f83bedd9cab4 | 235 | // Initialize LED Current |
stefanrousseau | 4:f83bedd9cab4 | 236 | // I2C Register 0x0F = 0xFF |
stefanrousseau | 4:f83bedd9cab4 | 237 | // I2C Register 0x10 = 0x0F |
stefanrousseau | 4:f83bedd9cab4 | 238 | WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT); |
stefanrousseau | 4:f83bedd9cab4 | 239 | WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT); |
stefanrousseau | 4:f83bedd9cab4 | 240 | |
stefanrousseau | 4:f83bedd9cab4 | 241 | // Parameter 0x01 = 0x37 |
stefanrousseau | 4:f83bedd9cab4 | 242 | //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK); |
stefanrousseau | 4:f83bedd9cab4 | 243 | //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some) |
stefanrousseau | 4:f83bedd9cab4 | 244 | Si1145_ParamSet(0x01, 0x7F); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, all but UV. One can only use AUX or UV but here we use AUX because UV does not work...) |
stefanrousseau | 4:f83bedd9cab4 | 245 | // I2C Register 0x18 = 0x0x07 //This is PSALS_FORCE to the Command register => Force a single PS (proximity sensor) and ALS (ambient light sensor) reading - The factory code has this as 0x05 which only does PS... |
stefanrousseau | 4:f83bedd9cab4 | 246 | PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07); |
stefanrousseau | 4:f83bedd9cab4 | 247 | WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode |
stefanrousseau | 4:f83bedd9cab4 | 248 | //Set MES_RATE to 0x1000. I.e. the device will automatically wake up every 16 * 256* 31.25 us = 0.128 seconds to measure |
stefanrousseau | 4:f83bedd9cab4 | 249 | WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00); |
stefanrousseau | 4:f83bedd9cab4 | 250 | WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10); |
stefanrousseau | 4:f83bedd9cab4 | 251 | } //bSi1145_present = true |
stefanrousseau | 4:f83bedd9cab4 | 252 | } //Init_Si1145() |
stefanrousseau | 4:f83bedd9cab4 | 253 | |
stefanrousseau | 4:f83bedd9cab4 | 254 | void Read_Si1145(void) |
stefanrousseau | 4:f83bedd9cab4 | 255 | { |
stefanrousseau | 4:f83bedd9cab4 | 256 | if (bSi1145_present) |
stefanrousseau | 4:f83bedd9cab4 | 257 | { |
stefanrousseau | 4:f83bedd9cab4 | 258 | // Once the measurements are completed, here is how to reconstruct them |
stefanrousseau | 4:f83bedd9cab4 | 259 | // Note very carefully that 16-bit registers are in the 'Little Endian' byte order |
stefanrousseau | 4:f83bedd9cab4 | 260 | // It may be more efficient to perform block I2C Reads, but this example shows |
stefanrousseau | 4:f83bedd9cab4 | 261 | // individual reads of registers |
stefanrousseau | 4:f83bedd9cab4 | 262 | |
stefanrousseau | 4:f83bedd9cab4 | 263 | int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1); |
stefanrousseau | 4:f83bedd9cab4 | 264 | int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1); |
stefanrousseau | 4:f83bedd9cab4 | 265 | int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1); |
stefanrousseau | 4:f83bedd9cab4 | 266 | //printf("PS1_Data = %d\n", PS1); |
stefanrousseau | 4:f83bedd9cab4 | 267 | //printf("PS2_Data = %d\n", PS2); |
stefanrousseau | 4:f83bedd9cab4 | 268 | //printf("PS3_Data = %d\n", PS3); |
stefanrousseau | 4:f83bedd9cab4 | 269 | //OBJECT PRESENT? |
stefanrousseau | 29:e6c8bd41caa6 | 270 | #if (0) |
stefanrousseau | 4:f83bedd9cab4 | 271 | if(PS1 < 22000){ |
stefanrousseau | 4:f83bedd9cab4 | 272 | //printf("Object Far\n"); |
stefanrousseau | 29:e6c8bd41caa6 | 273 | sprintf(SENSOR_DATA.Proximity, "Object Far\0"); |
stefanrousseau | 4:f83bedd9cab4 | 274 | } |
stefanrousseau | 4:f83bedd9cab4 | 275 | else if(PS1 < 24000) |
stefanrousseau | 4:f83bedd9cab4 | 276 | { |
stefanrousseau | 4:f83bedd9cab4 | 277 | //printf("Object in Vicinity\n"); |
stefanrousseau | 29:e6c8bd41caa6 | 278 | sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0"); |
stefanrousseau | 4:f83bedd9cab4 | 279 | } |
stefanrousseau | 4:f83bedd9cab4 | 280 | else if (PS1 < 30000) |
stefanrousseau | 4:f83bedd9cab4 | 281 | { |
stefanrousseau | 4:f83bedd9cab4 | 282 | //printf("Object Near\n"); |
stefanrousseau | 29:e6c8bd41caa6 | 283 | sprintf(SENSOR_DATA.Proximity, "Object Near\0"); |
stefanrousseau | 4:f83bedd9cab4 | 284 | } |
stefanrousseau | 4:f83bedd9cab4 | 285 | else |
stefanrousseau | 4:f83bedd9cab4 | 286 | { |
stefanrousseau | 4:f83bedd9cab4 | 287 | //printf("Object Very Near\n"); |
stefanrousseau | 29:e6c8bd41caa6 | 288 | sprintf(SENSOR_DATA.Proximity, "Object Very Near\0"); |
stefanrousseau | 4:f83bedd9cab4 | 289 | } |
stefanrousseau | 29:e6c8bd41caa6 | 290 | #else |
stefanrousseau | 29:e6c8bd41caa6 | 291 | sprintf(SENSOR_DATA.Proximity, "%d\0", PS1); |
stefanrousseau | 29:e6c8bd41caa6 | 292 | #endif |
stefanrousseau | 4:f83bedd9cab4 | 293 | |
stefanrousseau | 4:f83bedd9cab4 | 294 | //Force ALS read: |
stefanrousseau | 4:f83bedd9cab4 | 295 | //WriteTo_Si1145_Register(REG_COMMAND, 0x06); |
stefanrousseau | 4:f83bedd9cab4 | 296 | //wait (0.1); |
stefanrousseau | 4:f83bedd9cab4 | 297 | int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1); |
stefanrousseau | 4:f83bedd9cab4 | 298 | int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1); |
stefanrousseau | 4:f83bedd9cab4 | 299 | int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1); |
stefanrousseau | 4:f83bedd9cab4 | 300 | //printf("ALS_VIS_Data = %d\n", ALS_VIS); |
stefanrousseau | 4:f83bedd9cab4 | 301 | //printf("ALS_IR_Data = %d\n", ALS_IR); |
stefanrousseau | 4:f83bedd9cab4 | 302 | //printf("UV_INDEX_Data = %d\n", UV_INDEX); |
stefanrousseau | 4:f83bedd9cab4 | 303 | |
stefanrousseau | 4:f83bedd9cab4 | 304 | //printf("Ambient Light Visible Sensor = %d\n", ALS_VIS); |
stefanrousseau | 4:f83bedd9cab4 | 305 | sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS); |
stefanrousseau | 4:f83bedd9cab4 | 306 | //printf("Ambient Light Infrared Sensor = %d\n", ALS_IR); |
stefanrousseau | 4:f83bedd9cab4 | 307 | sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR); |
stefanrousseau | 4:f83bedd9cab4 | 308 | //float fUV_value = (UV_INDEX -50.0)/10000.0; |
stefanrousseau | 4:f83bedd9cab4 | 309 | float fUV_value = (UV_INDEX)/100.0; //this is the aux reading |
stefanrousseau | 4:f83bedd9cab4 | 310 | //printf("UV_Data = %0.2f\n", fUV_value); |
stefanrousseau | 4:f83bedd9cab4 | 311 | sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value); |
stefanrousseau | 4:f83bedd9cab4 | 312 | } //bSi1145_present = true |
stefanrousseau | 4:f83bedd9cab4 | 313 | } //Read_Si1145() |
stefanrousseau | 4:f83bedd9cab4 | 314 | |
stefanrousseau | 4:f83bedd9cab4 | 315 | //******************************************************************************************************************************************** |
stefanrousseau | 4:f83bedd9cab4 | 316 | //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer |
stefanrousseau | 4:f83bedd9cab4 | 317 | //******************************************************************************************************************************************** |
stefanrousseau | 11:e6602513730f | 318 | bool bMotionSensor_present = false; |
stefanrousseau | 4:f83bedd9cab4 | 319 | void init_motion_sensor() |
stefanrousseau | 4:f83bedd9cab4 | 320 | { |
stefanrousseau | 11:e6602513730f | 321 | int iWhoAmI = fxos.get_whoami(); |
stefanrousseau | 11:e6602513730f | 322 | |
stefanrousseau | 11:e6602513730f | 323 | printf("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI); |
stefanrousseau | 4:f83bedd9cab4 | 324 | // Iterrupt for active-low interrupt line from FXOS |
stefanrousseau | 4:f83bedd9cab4 | 325 | // Configured with only one interrupt on INT2 signaling Data-Ready |
stefanrousseau | 4:f83bedd9cab4 | 326 | //fxos_int2.fall(&trigger_fxos_int2); |
stefanrousseau | 11:e6602513730f | 327 | if (iWhoAmI != 0xC7) |
stefanrousseau | 11:e6602513730f | 328 | { |
stefanrousseau | 11:e6602513730f | 329 | bMotionSensor_present = false; |
stefanrousseau | 11:e6602513730f | 330 | printf("FXOS8700CQ motion sensor not found\n"); |
stefanrousseau | 11:e6602513730f | 331 | } |
stefanrousseau | 11:e6602513730f | 332 | else |
stefanrousseau | 11:e6602513730f | 333 | { |
stefanrousseau | 11:e6602513730f | 334 | bMotionSensor_present = true; |
stefanrousseau | 11:e6602513730f | 335 | fxos.enable(); |
stefanrousseau | 11:e6602513730f | 336 | } |
stefanrousseau | 4:f83bedd9cab4 | 337 | } //init_motion_sensor |
stefanrousseau | 4:f83bedd9cab4 | 338 | |
stefanrousseau | 11:e6602513730f | 339 | void read_motion_sensor() |
stefanrousseau | 11:e6602513730f | 340 | { |
stefanrousseau | 11:e6602513730f | 341 | if (bMotionSensor_present) |
stefanrousseau | 11:e6602513730f | 342 | { |
stefanrousseau | 11:e6602513730f | 343 | fxos.get_data(&accel_data, &magn_data); |
stefanrousseau | 11:e6602513730f | 344 | //printf("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z); |
stefanrousseau | 11:e6602513730f | 345 | sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x); |
stefanrousseau | 11:e6602513730f | 346 | sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y); |
stefanrousseau | 11:e6602513730f | 347 | sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z); |
stefanrousseau | 11:e6602513730f | 348 | |
stefanrousseau | 11:e6602513730f | 349 | //Try to normalize (/2048) the values so they will match the eCompass output: |
stefanrousseau | 11:e6602513730f | 350 | float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z; |
stefanrousseau | 11:e6602513730f | 351 | fAccelScaled_x = (accel_data.x/2048.0); |
stefanrousseau | 11:e6602513730f | 352 | fAccelScaled_y = (accel_data.y/2048.0); |
stefanrousseau | 11:e6602513730f | 353 | fAccelScaled_z = (accel_data.z/2048.0); |
stefanrousseau | 11:e6602513730f | 354 | //printf("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z); |
stefanrousseau | 11:e6602513730f | 355 | sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x); |
stefanrousseau | 11:e6602513730f | 356 | sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y); |
stefanrousseau | 11:e6602513730f | 357 | sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z); |
stefanrousseau | 11:e6602513730f | 358 | } //bMotionSensor_present |
stefanrousseau | 11:e6602513730f | 359 | } //read_motion_sensor |
stefanrousseau | 11:e6602513730f | 360 | |
stefanrousseau | 4:f83bedd9cab4 | 361 | void sensors_init(void) |
stefanrousseau | 4:f83bedd9cab4 | 362 | { |
stefanrousseau | 4:f83bedd9cab4 | 363 | Init_Si7020(); |
stefanrousseau | 4:f83bedd9cab4 | 364 | Init_Si1145(); |
stefanrousseau | 4:f83bedd9cab4 | 365 | init_motion_sensor(); |
stefanrousseau | 4:f83bedd9cab4 | 366 | } //sensors_init |
stefanrousseau | 4:f83bedd9cab4 | 367 | |
stefanrousseau | 4:f83bedd9cab4 | 368 | void read_sensors(void) |
stefanrousseau | 4:f83bedd9cab4 | 369 | { |
stefanrousseau | 4:f83bedd9cab4 | 370 | Read_Si7020(); |
stefanrousseau | 4:f83bedd9cab4 | 371 | Read_Si1145(); |
stefanrousseau | 4:f83bedd9cab4 | 372 | read_motion_sensor(); |
stefanrousseau | 4:f83bedd9cab4 | 373 | } //read_sensors |