Library to read and write Azoteq IQS6xx device registers via I2C.
Dependents: IQS620_HelloWorld IQS622_HelloWorld IQS624_HelloWorld IQS621_HelloWorld ... more
Library: IQS62x
Library to read and write Azoteq IQS6xx device registers via I2C.
Supported Devices
Handy Table of ProxFusion Device Features
ALS = Ambient Light Sensor PIR = Passive Infrared
IQS62x.cpp@0:ce75ae1e8fc7, 2017-02-04 (annotated)
- Committer:
- AzqDev
- Date:
- Sat Feb 04 07:19:09 2017 +0000
- Revision:
- 0:ce75ae1e8fc7
- Child:
- 2:c16cb655d4a4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzqDev | 0:ce75ae1e8fc7 | 1 | // A class library for Azoteq IQS62x devices |
AzqDev | 0:ce75ae1e8fc7 | 2 | #include "IQS62x.h" |
AzqDev | 0:ce75ae1e8fc7 | 3 | |
AzqDev | 0:ce75ae1e8fc7 | 4 | // constructor |
AzqDev | 0:ce75ae1e8fc7 | 5 | IQS62xIO::IQS62xIO() : |
AzqDev | 0:ce75ae1e8fc7 | 6 | i2c( IQS_I2C_DATA_PIN, IQS_I2C_CLOCK_PIN ), // first run the constructor for mbed class I2C |
AzqDev | 0:ce75ae1e8fc7 | 7 | IQSready( IQS_READY_PIN ) // first run the constructor for mbed class DigitalIn |
AzqDev | 0:ce75ae1e8fc7 | 8 | { |
AzqDev | 0:ce75ae1e8fc7 | 9 | registers = I2CBuffer; // pointer to the receive buffer |
AzqDev | 0:ce75ae1e8fc7 | 10 | I2CErrorCount = 0; // reset I2C error count |
AzqDev | 0:ce75ae1e8fc7 | 11 | i2c.frequency( I2Cspeed ); // I2C clock frequency |
AzqDev | 0:ce75ae1e8fc7 | 12 | } |
AzqDev | 0:ce75ae1e8fc7 | 13 | |
AzqDev | 0:ce75ae1e8fc7 | 14 | // write some configuration data to the IQS62x |
AzqDev | 0:ce75ae1e8fc7 | 15 | void IQS62xIO::configure() { |
AzqDev | 0:ce75ae1e8fc7 | 16 | const static char system_register_setup_data [] = { 0xd0, 0x40 }; |
AzqDev | 0:ce75ae1e8fc7 | 17 | int numberOfBytes = sizeof( system_register_setup_data ); |
AzqDev | 0:ce75ae1e8fc7 | 18 | waitForReady(); |
AzqDev | 0:ce75ae1e8fc7 | 19 | // clear the reset bit in register 0xd0 |
AzqDev | 0:ce75ae1e8fc7 | 20 | if(0!=i2c.write(I2C_ADR,system_register_setup_data,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 21 | I2CErrorCount++; |
AzqDev | 0:ce75ae1e8fc7 | 22 | } |
AzqDev | 0:ce75ae1e8fc7 | 23 | |
AzqDev | 0:ce75ae1e8fc7 | 24 | // read all registers from the IQS62x |
AzqDev | 0:ce75ae1e8fc7 | 25 | void IQS62xIO::readAll() { |
AzqDev | 0:ce75ae1e8fc7 | 26 | waitForReady(); |
AzqDev | 0:ce75ae1e8fc7 | 27 | const static char i2c_start_address [] = { 0x0 }; |
AzqDev | 0:ce75ae1e8fc7 | 28 | int numberOfBytes = sizeof( i2c_start_address ); |
AzqDev | 0:ce75ae1e8fc7 | 29 | // write 0 to the IQS62x address register |
AzqDev | 0:ce75ae1e8fc7 | 30 | if(0!=i2c.write(I2C_ADR,i2c_start_address,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 31 | I2CErrorCount++; |
AzqDev | 0:ce75ae1e8fc7 | 32 | waitForReady(); |
AzqDev | 0:ce75ae1e8fc7 | 33 | memset(I2CBuffer,0x55,I2CBufferSize); // "clear" i2c receive buffer |
AzqDev | 0:ce75ae1e8fc7 | 34 | numberOfBytes = NUMBER_OF_REGISTERS; |
AzqDev | 0:ce75ae1e8fc7 | 35 | // read register values into a buffer |
AzqDev | 0:ce75ae1e8fc7 | 36 | if(0!=i2c.read(I2C_ADR,I2CBuffer,numberOfBytes,false)) |
AzqDev | 0:ce75ae1e8fc7 | 37 | I2CErrorCount++; |
AzqDev | 0:ce75ae1e8fc7 | 38 | } |
AzqDev | 0:ce75ae1e8fc7 | 39 | |
AzqDev | 0:ce75ae1e8fc7 | 40 | // wait for IQS62x to provide a ready signal (low) on IQS62x_ready pin |
AzqDev | 0:ce75ae1e8fc7 | 41 | void IQS62xIO::waitForReady() { |
AzqDev | 0:ce75ae1e8fc7 | 42 | int timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 43 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 44 | if(IQSready==1) break; |
AzqDev | 0:ce75ae1e8fc7 | 45 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 46 | } |
AzqDev | 0:ce75ae1e8fc7 | 47 | timeout=0; |
AzqDev | 0:ce75ae1e8fc7 | 48 | while (1) { |
AzqDev | 0:ce75ae1e8fc7 | 49 | if(IQSready==0) break; |
AzqDev | 0:ce75ae1e8fc7 | 50 | if (timeout++ > 1000000) goto fatal_error; |
AzqDev | 0:ce75ae1e8fc7 | 51 | } |
AzqDev | 0:ce75ae1e8fc7 | 52 | return; |
AzqDev | 0:ce75ae1e8fc7 | 53 | fatal_error: |
AzqDev | 0:ce75ae1e8fc7 | 54 | error ("Fatal Error: IQS62x ready pin is not toggling"); |
AzqDev | 0:ce75ae1e8fc7 | 55 | } |