A compilation of some hardware sensors and their shared programming interfaces.
I2CSensor.h@1:15396cab58d1, 2014-03-19 (annotated)
- Committer:
- mgottscho
- Date:
- Wed Mar 19 00:35:31 2014 +0000
- Revision:
- 1:15396cab58d1
- Parent:
- 0:8d34cc2ff388
Updated for most recent UtilityLib.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mgottscho | 0:8d34cc2ff388 | 1 | /* I2CSensor.h |
mgottscho | 0:8d34cc2ff388 | 2 | * Tested with mbed board: FRDM-KL46Z |
mgottscho | 0:8d34cc2ff388 | 3 | * Author: Mark Gottscho |
mgottscho | 0:8d34cc2ff388 | 4 | * mgottscho@ucla.edu |
mgottscho | 0:8d34cc2ff388 | 5 | */ |
mgottscho | 0:8d34cc2ff388 | 6 | |
mgottscho | 0:8d34cc2ff388 | 7 | #ifndef SENSOR_H |
mgottscho | 0:8d34cc2ff388 | 8 | #define SENSOR_H |
mgottscho | 0:8d34cc2ff388 | 9 | |
mgottscho | 0:8d34cc2ff388 | 10 | #include "mbed.h" |
mgottscho | 0:8d34cc2ff388 | 11 | |
mgottscho | 0:8d34cc2ff388 | 12 | /** |
mgottscho | 0:8d34cc2ff388 | 13 | * Base class from which digital sensors using I2C should be derived to simplify low-level communications with the device. |
mgottscho | 0:8d34cc2ff388 | 14 | */ |
mgottscho | 0:8d34cc2ff388 | 15 | class I2CSensor { |
mgottscho | 0:8d34cc2ff388 | 16 | public: |
mgottscho | 0:8d34cc2ff388 | 17 | /** |
mgottscho | 0:8d34cc2ff388 | 18 | * @param sda I2C SDA pin ID |
mgottscho | 0:8d34cc2ff388 | 19 | * @param scl I2C SCL pin ID |
mgottscho | 0:8d34cc2ff388 | 20 | * @param i2c_addr I2C 8-bit address (LSB is actually don't care) |
mgottscho | 0:8d34cc2ff388 | 21 | */ |
mgottscho | 0:8d34cc2ff388 | 22 | I2CSensor(PinName sda, PinName scl, int i2c_addr); |
mgottscho | 0:8d34cc2ff388 | 23 | |
mgottscho | 0:8d34cc2ff388 | 24 | /** |
mgottscho | 0:8d34cc2ff388 | 25 | */ |
mgottscho | 0:8d34cc2ff388 | 26 | ~I2CSensor(); |
mgottscho | 0:8d34cc2ff388 | 27 | |
mgottscho | 0:8d34cc2ff388 | 28 | /** |
mgottscho | 0:8d34cc2ff388 | 29 | * @returns I2C SDA pin ID |
mgottscho | 0:8d34cc2ff388 | 30 | */ |
mgottscho | 0:8d34cc2ff388 | 31 | PinName getSDAPin(); |
mgottscho | 0:8d34cc2ff388 | 32 | |
mgottscho | 0:8d34cc2ff388 | 33 | /** |
mgottscho | 0:8d34cc2ff388 | 34 | * @returns I2C SCL pin ID |
mgottscho | 0:8d34cc2ff388 | 35 | */ |
mgottscho | 0:8d34cc2ff388 | 36 | PinName getSCLPin(); |
mgottscho | 0:8d34cc2ff388 | 37 | |
mgottscho | 0:8d34cc2ff388 | 38 | /** |
mgottscho | 0:8d34cc2ff388 | 39 | * @returns Device I2C address (LSB always 0 in this case, it is don't care) |
mgottscho | 0:8d34cc2ff388 | 40 | */ |
mgottscho | 0:8d34cc2ff388 | 41 | uint8_t getDeviceI2CAddress(); |
mgottscho | 0:8d34cc2ff388 | 42 | |
mgottscho | 0:8d34cc2ff388 | 43 | /** |
mgottscho | 0:8d34cc2ff388 | 44 | * Read an 8-bit register. |
mgottscho | 0:8d34cc2ff388 | 45 | * @param reg_addr the register in the device |
mgottscho | 0:8d34cc2ff388 | 46 | * @returns The raw value from the register specified by reg_addr. |
mgottscho | 0:8d34cc2ff388 | 47 | */ |
mgottscho | 0:8d34cc2ff388 | 48 | uint8_t getRegister(const uint8_t reg_addr); |
mgottscho | 0:8d34cc2ff388 | 49 | |
mgottscho | 0:8d34cc2ff388 | 50 | /** |
mgottscho | 0:8d34cc2ff388 | 51 | * Read a 16-bit register. |
mgottscho | 0:8d34cc2ff388 | 52 | * @param reg_addr the register in the device |
mgottscho | 0:8d34cc2ff388 | 53 | * @returns The raw value from the register specified by reg_addr. |
mgottscho | 0:8d34cc2ff388 | 54 | */ |
mgottscho | 0:8d34cc2ff388 | 55 | uint16_t getRegister16b(const uint8_t reg_addr); |
mgottscho | 0:8d34cc2ff388 | 56 | |
mgottscho | 0:8d34cc2ff388 | 57 | /** |
mgottscho | 0:8d34cc2ff388 | 58 | * Set an 8-bit register. |
mgottscho | 0:8d34cc2ff388 | 59 | * @param reg_addr the register in the device |
mgottscho | 0:8d34cc2ff388 | 60 | * @param data the byte to write to the register |
mgottscho | 0:8d34cc2ff388 | 61 | */ |
mgottscho | 0:8d34cc2ff388 | 62 | void setRegister(const uint8_t reg_addr, const uint8_t data); |
mgottscho | 0:8d34cc2ff388 | 63 | |
mgottscho | 0:8d34cc2ff388 | 64 | /** |
mgottscho | 0:8d34cc2ff388 | 65 | * Set a 16-bit register. |
mgottscho | 0:8d34cc2ff388 | 66 | * @param reg_addr the register in the device |
mgottscho | 0:8d34cc2ff388 | 67 | * @param data the byte to write to the register |
mgottscho | 0:8d34cc2ff388 | 68 | */ |
mgottscho | 0:8d34cc2ff388 | 69 | void setRegister16b(const uint8_t reg_addr, const uint16_t data); |
mgottscho | 0:8d34cc2ff388 | 70 | |
mgottscho | 0:8d34cc2ff388 | 71 | protected: |
mgottscho | 0:8d34cc2ff388 | 72 | /** |
mgottscho | 0:8d34cc2ff388 | 73 | * @param reg_addr 8-bit register address inside the device |
mgottscho | 0:8d34cc2ff388 | 74 | * @param data 8-bit data that will be read from the register. This pointer MUST be valid. This array MUST be at least as long as len. |
mgottscho | 0:8d34cc2ff388 | 75 | * @param len total number of bytes to read. len must be >= 1. |
mgottscho | 0:8d34cc2ff388 | 76 | * @returns 0 on success, otherwise error code from I2C |
mgottscho | 0:8d34cc2ff388 | 77 | */ |
mgottscho | 0:8d34cc2ff388 | 78 | int __readReg(const uint8_t reg_addr, uint8_t *data, int len); |
mgottscho | 0:8d34cc2ff388 | 79 | |
mgottscho | 0:8d34cc2ff388 | 80 | /** |
mgottscho | 0:8d34cc2ff388 | 81 | * @param data 8-bit data that will be written to the register. This pointer MUST be valid. This array MUST be at least as long as total_len. |
mgottscho | 0:8d34cc2ff388 | 82 | * data[0] should be set to the 8-bit register address. The data payload should start at index 1. |
mgottscho | 0:8d34cc2ff388 | 83 | * @param total_len total length of the data array, which is the length of the payload + 1 for the register address. total_len must be >= 2. |
mgottscho | 0:8d34cc2ff388 | 84 | * @returns 0 on success, otherwise error code from I2C |
mgottscho | 0:8d34cc2ff388 | 85 | */ |
mgottscho | 0:8d34cc2ff388 | 86 | int __writeReg(const uint8_t *data, int total_len); |
mgottscho | 0:8d34cc2ff388 | 87 | |
mgottscho | 0:8d34cc2ff388 | 88 | PinName __sda_pin; |
mgottscho | 0:8d34cc2ff388 | 89 | PinName __scl_pin; |
mgottscho | 0:8d34cc2ff388 | 90 | int __i2c_addr; |
mgottscho | 0:8d34cc2ff388 | 91 | uint8_t __who_am_i; |
mgottscho | 0:8d34cc2ff388 | 92 | I2C __i2c; //We wrap the mbed i2c SDK functionality |
mgottscho | 0:8d34cc2ff388 | 93 | }; |
mgottscho | 0:8d34cc2ff388 | 94 | |
mgottscho | 0:8d34cc2ff388 | 95 | #endif |