Successful acro and level mode now! Relying on MPU9250 as base sensor. I'm working continuously on tuning and features :) NEWEST VERSION ON: https://github.com/MaEtUgR/FlyBed (CODE 100% compatible/copyable)

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C_Sensor.cpp Source File

I2C_Sensor.cpp

00001 #include "I2C_Sensor.h"
00002 
00003 // calculate the 8-Bit write/read I2C-Address from the 7-Bit adress of the device
00004 #define GET_I2C_WRITE_ADDRESS(ADR)  (ADR << 1&0xFE) // ADR & 1111 1110
00005 #define GET_I2C_READ_ADDRESS(ADR)   (ADR << 1|0x01) // ADR | 0000 0001
00006 
00007 I2C_Sensor::I2C_Sensor(PinName sda, PinName scl, char i2c_address) : i2c(sda, scl), local("local")
00008 {
00009     I2C_Sensor::i2c_address = i2c_address;
00010     i2c.frequency(400000); // standard speed
00011     //i2c.frequency(1000000); // ultrafast!
00012 }
00013 
00014 void I2C_Sensor::saveCalibrationValues(float values[], int size, char * filename)
00015 {
00016     FILE *fp = fopen(strcat("/local/", filename), "w");
00017     for(int i = 0; i < size; i++)
00018         fprintf(fp, "%f\r\n", values[i]);
00019     fclose(fp);
00020 }
00021 
00022 void I2C_Sensor::loadCalibrationValues(float values[], int size, char * filename)
00023 {
00024     FILE *fp = fopen(strcat("/local/", filename), "r");
00025     for(int i = 0; i < size; i++)
00026         fscanf(fp, "%f", &values[i]);
00027     fclose(fp);
00028 }
00029 
00030 // I2C functions --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00031 
00032 
00033 char I2C_Sensor::readRegister(char reg)
00034 {
00035     char value = 0;
00036     
00037     i2c.write(i2c_address, &reg, 1);
00038     i2c.read(i2c_address, &value, 1);
00039 
00040     return value;
00041 }
00042 
00043 void I2C_Sensor::writeRegister(char reg, char data)
00044 {
00045     char buffer[2] = {reg, data};
00046     i2c.write(i2c_address, buffer, 2);
00047 }
00048 
00049 int I2C_Sensor::readMultiRegister(char reg, char* output, int size)
00050 {
00051     if (0 != i2c.write (i2c_address, &reg, 1)) return 1; // tell register address of the MSB get the sensor to do slave-transmit subaddress updating.
00052     if (0 != i2c.read  (i2c_address, output, size)) return 1; // tell it where to store the data read
00053     return 0;
00054 }