A compilation of some hardware sensors and their shared programming interfaces.
Embed:
(wiki syntax)
Show/hide line numbers
I2CSensor.h
00001 /* I2CSensor.h 00002 * Tested with mbed board: FRDM-KL46Z 00003 * Author: Mark Gottscho 00004 * mgottscho@ucla.edu 00005 */ 00006 00007 #ifndef SENSOR_H 00008 #define SENSOR_H 00009 00010 #include "mbed.h" 00011 00012 /** 00013 * Base class from which digital sensors using I2C should be derived to simplify low-level communications with the device. 00014 */ 00015 class I2CSensor { 00016 public: 00017 /** 00018 * @param sda I2C SDA pin ID 00019 * @param scl I2C SCL pin ID 00020 * @param i2c_addr I2C 8-bit address (LSB is actually don't care) 00021 */ 00022 I2CSensor (PinName sda, PinName scl, int i2c_addr); 00023 00024 /** 00025 */ 00026 ~I2CSensor(); 00027 00028 /** 00029 * @returns I2C SDA pin ID 00030 */ 00031 PinName getSDAPin (); 00032 00033 /** 00034 * @returns I2C SCL pin ID 00035 */ 00036 PinName getSCLPin (); 00037 00038 /** 00039 * @returns Device I2C address (LSB always 0 in this case, it is don't care) 00040 */ 00041 uint8_t getDeviceI2CAddress (); 00042 00043 /** 00044 * Read an 8-bit register. 00045 * @param reg_addr the register in the device 00046 * @returns The raw value from the register specified by reg_addr. 00047 */ 00048 uint8_t getRegister(const uint8_t reg_addr); 00049 00050 /** 00051 * Read a 16-bit register. 00052 * @param reg_addr the register in the device 00053 * @returns The raw value from the register specified by reg_addr. 00054 */ 00055 uint16_t getRegister16b(const uint8_t reg_addr); 00056 00057 /** 00058 * Set an 8-bit register. 00059 * @param reg_addr the register in the device 00060 * @param data the byte to write to the register 00061 */ 00062 void setRegister(const uint8_t reg_addr, const uint8_t data); 00063 00064 /** 00065 * Set a 16-bit register. 00066 * @param reg_addr the register in the device 00067 * @param data the byte to write to the register 00068 */ 00069 void setRegister16b(const uint8_t reg_addr, const uint16_t data); 00070 00071 protected: 00072 /** 00073 * @param reg_addr 8-bit register address inside the device 00074 * @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. 00075 * @param len total number of bytes to read. len must be >= 1. 00076 * @returns 0 on success, otherwise error code from I2C 00077 */ 00078 int __readReg (const uint8_t reg_addr, uint8_t *data, int len); 00079 00080 /** 00081 * @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. 00082 * data[0] should be set to the 8-bit register address. The data payload should start at index 1. 00083 * @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. 00084 * @returns 0 on success, otherwise error code from I2C 00085 */ 00086 int __writeReg (const uint8_t *data, int total_len); 00087 00088 PinName __sda_pin; 00089 PinName __scl_pin; 00090 int __i2c_addr; 00091 uint8_t __who_am_i; 00092 I2C __i2c; //We wrap the mbed i2c SDK functionality 00093 }; 00094 00095 #endif
Generated on Wed Jul 13 2022 19:24:40 by 1.7.2