// If a measurement was successfully started during the last execution ... if (state.i2cStatus == 0x0000) {
// Select the result register i2cStart(); i2cTx(I2C_OP_WRITE | ALS_I2C_ADDR); i2cTx(ALS_REG_RESULT);
// If successful if (state.i2cStatus == 0x0000) { U16 resultRegH; U16 resultRegL; // Read the result i2cRepeatedStart(); i2cTx(I2C_OP_READ | ALS_I2C_ADDR); i2cRxAck(resultRegH); i2cRxNack(resultRegL); i2cStop();
// Convert the result (4-bit exponent + 12-bit mantissa) into 16-bit fixed-point U16 exp = resultRegH >> 4; U16 mant = (resultRegH << 12) | (resultRegL << 4); // The exponent is in range 0 to 11 U16 value = mant >> (11 - exp); output.value = value;
// Notify the application if (value < cfg.lowThreshold) { fwGenAlertInterrupt(); } if (value > cfg.highThreshold) { fwGenAlertInterrupt(); } } else { i2cStop(); } } |