code

Dependencies:   MPU6050_SIM5320_TEST SDFileSystem WakeUp

Fork of Nucleo_L476RG_SDCard_WorkingSample by Mariwan J.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorBoards.cpp Source File

SensorBoards.cpp

00001 #include "SensorBoards.h"
00002 #include "mbed.h"
00003 
00004 
00005 uint8_t SensorBoards::getSensorNumbers()
00006 {
00007     I2C i2c(D14, D15);
00008     char cmd[1];
00009     for(int i=0; i<numberOfBoards; i++) {
00010         uint8_t write_adress=(boards[i].I2CAddress << 1) & 0xFE;
00011         uint8_t read_adress=(boards[i].I2CAddress << 1) | 0x01;
00012         i2c.start();
00013         i2c.write(write_adress);      //WRITE_ADRESS
00014         i2c.write(SENSOR_NUMBERS);   //CMD   0XA1
00015         wait(0.5);
00016         i2c.stop();
00017         i2c.start();
00018         i2c.read(read_adress,cmd,1);
00019         i2c.stop();
00020         boards[i].numberOfSensors=cmd[0];
00021         //printf("beoj senzora %d\n",boards[i].numberOfSensors);
00022     }
00023     return 1;
00024 
00025 
00026 
00027 
00028     //return boards[0].numberOfSensors;
00029 }
00030 uint8_t SensorBoards::getSensorIDs()
00031 {
00032     //wait(3);
00033     char cmd[2];
00034     //printf("a1=%02x",boards[0].I2CAddress);
00035     I2C i2c(D14, D15);
00036     printf("Get sensor id's\n");
00037   //  uint8_t length=2;//get_sensor_numbers(adress);
00038    // uint8_t i=0;
00039    // uint8_t value=0;
00040    // printf("adresa= %02x",boards[0].I2CAddress);
00041     for(int i=0; i<numberOfBoards; i++) {
00042         //for(int k=0; k<boards[i].numberOfSensors; k++) {
00043             //printf("__%02x",boards[0].I2CAddress);
00044            // uint8_t write_adress=(0x4a << 1) & 0xFE;
00045             //uint8_t read_adress=(0x4a << 1) | 0x01;
00046             uint8_t write_adress=(boards[i].I2CAddress << 1) & 0xFE;
00047             uint8_t read_adress=(boards[i].I2CAddress << 1) | 0x01;
00048             i2c.start();//SD_CARD_WRITE
00049             i2c.write(write_adress);
00050             i2c.write(SENSORS_IDS);  //A2
00051             wait(0.5);
00052             i2c.stop();
00053             wait(0.5);
00054             i2c.start();
00055             i2c.read(read_adress,cmd,2);
00056             i2c.stop();
00057           //  boards[i].sensorIDs[i]=
00058            // IDBuffer[0]=cmd[0];
00059            
00060            for(int k=0; k<boards[i].numberOfSensors; k++) {
00061                boards[i].sensorIDs[k]=cmd[k];
00062               // printf("ove %d\n",boards[i].sensorIDs[k]);
00063         }
00064     }
00065     
00066       // printf("prosoooo sammmmm\n");
00067         //printf("i=%02x  \n",IDBuffer[1]);
00068         return 1;
00069     }
00070     void SensorBoards::getSensorReadings(uint8_t address, uint8_t ID, uint8_t *IDMeasure) {
00071         I2C i2c(D14, D15);
00072         char cmd[4];
00073         float value;
00074         uint8_t write_adress=(address << 1) & 0xFE;
00075         uint8_t read_adress=(address << 1) | 0x01;
00076         i2c.start();//SD_CARD_WRITE
00077         i2c.write(write_adress);
00078         i2c.write(SENSOR_READINGS); //A3
00079         wait(0.2);
00080         i2c.write(ID);  //id
00081         i2c.stop();
00082         wait(2);
00083         i2c.start();
00084         i2c.read(read_adress,cmd,4);
00085         i2c.stop();
00086         memcpy(&value,&cmd,4);
00087         //*val=value;
00088         for(int i=0;i<4;i++)
00089         IDMeasure[i]=cmd[i];
00090         printf("value=%f \n",value);
00091 // return 1;
00092 
00093 
00094 
00095 
00096     }
00097     uint8_t SensorBoards::sensorBoardScanner() {
00098         char ucdata_write[2];
00099         short count=0;
00100         I2C i2c(D14, D15); // sda, scl
00101         for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2) { //Stepping in 2 Because Read/Write use LSB
00102             if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0)) { // Check for ACK from i2c Device NB I am 'ORing' the Write Bit
00103                 if(Device_Adress>>1!=0x68) {
00104                     //printf("ACK from the Device at Address %#4x\n\r",Device_Adress>>1);
00105                     //lcd.printf("ACK at:%#4x\n",Device_Adress);
00106                     boards[count].I2CAddress=Device_Adress>>1;
00107                     count++;
00108                     numberOfBoards++;
00109                 }
00110             }
00111         }
00112         //printf("\n\r %d Devices have been detected!\n\r",count);
00113         if (numberOfBoards!=0)
00114             return 1;
00115         else
00116             return 0;
00117     }
00118     uint8_t SensorBoards::returnBoardAdd() {
00119         return boards[0].I2CAddress;
00120     }