Hauptprogramm

Dependencies:   ILI9340_Driver_Lib PM2_Libary Lib_DFPlayerMini

Committer:
ackerden
Date:
Wed May 05 15:25:02 2021 +0000
Revision:
29:91df2c5fb297
Parent:
27:bbcd157dcd63
audio;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ackerden 27:bbcd157dcd63 1 #include "mbed.h"
ackerden 27:bbcd157dcd63 2 #include "Chirp.h"
ackerden 27:bbcd157dcd63 3
ackerden 27:bbcd157dcd63 4 Chirp chirp(D14, D15);
ackerden 29:91df2c5fb297 5 const int addr7bit = 0x48; // 7 bit I2C address
ackerden 29:91df2c5fb297 6 const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90
ackerden 27:bbcd157dcd63 7
ackerden 27:bbcd157dcd63 8 //Chirp Docs are at https://github.com/Miceuz/i2c-moisture-sensor and https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/
ackerden 27:bbcd157dcd63 9 void getResults();
ackerden 27:bbcd157dcd63 10 Chirp::Chirp(PinName sda, PinName scl, char slave_adr)
ackerden 27:bbcd157dcd63 11 :i2c_p(new I2C(sda, scl)), i2c(*i2c_p), address(slave_adr)
ackerden 27:bbcd157dcd63 12 {
ackerden 27:bbcd157dcd63 13 i2c.frequency(10000);
ackerden 27:bbcd157dcd63 14
ackerden 27:bbcd157dcd63 15 }
ackerden 27:bbcd157dcd63 16
ackerden 27:bbcd157dcd63 17 float Chirp::getCapacitance(){
ackerden 27:bbcd157dcd63 18 i2c.frequency(10000);
ackerden 27:bbcd157dcd63 19 short capacitance = 0;
ackerden 29:91df2c5fb297 20 char i2cData[2];
ackerden 29:91df2c5fb297 21
ackerden 29:91df2c5fb297 22 char dataReg = 0; //Capacitance is stored in 2 bytes at address 0
ackerden 29:91df2c5fb297 23 char cmd[2];
ackerden 27:bbcd157dcd63 24
ackerden 29:91df2c5fb297 25 i2cData[0] = 0x01;
ackerden 29:91df2c5fb297 26 i2cData[1] = 0x00;
ackerden 29:91df2c5fb297 27 int test = i2c.write(addr8bit, cmd, 2);
ackerden 29:91df2c5fb297 28 printf("%i\n\n", test);
ackerden 29:91df2c5fb297 29 ThisThread::sleep_for(500);
ackerden 29:91df2c5fb297 30 cmd[0] = 0x00;
ackerden 29:91df2c5fb297 31 i2c.write(addr8bit, cmd, 1);
ackerden 29:91df2c5fb297 32 i2c.read(addr8bit, cmd, 2);
ackerden 29:91df2c5fb297 33 float tmp = (float((cmd[0] << 8) | cmd[1]) / 256.0);
ackerden 29:91df2c5fb297 34 printf("Temp = %.2f\n", tmp);
ackerden 27:bbcd157dcd63 35
ackerden 27:bbcd157dcd63 36
ackerden 29:91df2c5fb297 37 int master = i2c.write(address, &dataReg, 1);
ackerden 29:91df2c5fb297 38 printf("%i\n\n", master);
ackerden 29:91df2c5fb297 39 int success = i2c.read(address, i2cData, 2);
ackerden 29:91df2c5fb297 40 printf("%i\n\n", success);
ackerden 29:91df2c5fb297 41
ackerden 29:91df2c5fb297 42 if(success != 1)
ackerden 27:bbcd157dcd63 43 return -1000000;
ackerden 27:bbcd157dcd63 44 else
ackerden 27:bbcd157dcd63 45 {
ackerden 27:bbcd157dcd63 46 //I2C bytes are in different byte order, write into short bytes 0 and 1 in reverse order!
ackerden 27:bbcd157dcd63 47 *((char*)&capacitance) = i2cData[1];
ackerden 27:bbcd157dcd63 48 *(((char*)(&capacitance)) + 1) = i2cData[0];
ackerden 27:bbcd157dcd63 49 return capacitance;
ackerden 27:bbcd157dcd63 50 }}
ackerden 27:bbcd157dcd63 51
ackerden 27:bbcd157dcd63 52 float Chirp::getTemperature(){
ackerden 27:bbcd157dcd63 53 i2c.frequency(10000);
ackerden 27:bbcd157dcd63 54 short temperature = 0;
ackerden 27:bbcd157dcd63 55 char i2cData[2]= {0};
ackerden 27:bbcd157dcd63 56
ackerden 27:bbcd157dcd63 57
ackerden 27:bbcd157dcd63 58 char dataReg = 4; //Temperature is stored in 2 bytes at address 5
ackerden 29:91df2c5fb297 59 int master = i2c.write(address, &dataReg, 1);
ackerden 29:91df2c5fb297 60 printf("%i\n\n", master);
ackerden 27:bbcd157dcd63 61 int success = i2c.read(address, i2cData, 2);
ackerden 27:bbcd157dcd63 62 printf("%i\n\n", success);
ackerden 27:bbcd157dcd63 63
ackerden 27:bbcd157dcd63 64
ackerden 29:91df2c5fb297 65 if(success != 1)
ackerden 27:bbcd157dcd63 66 return -1000000;
ackerden 27:bbcd157dcd63 67 else
ackerden 27:bbcd157dcd63 68 {
ackerden 27:bbcd157dcd63 69 *((char*)&temperature) = i2cData[1];
ackerden 27:bbcd157dcd63 70 *(((char*)(&temperature)) + 1) = i2cData[0];
ackerden 27:bbcd157dcd63 71 return (float) temperature / 10.0;
ackerden 27:bbcd157dcd63 72 } }
ackerden 27:bbcd157dcd63 73
ackerden 27:bbcd157dcd63 74
ackerden 27:bbcd157dcd63 75 void getResults(){
ackerden 27:bbcd157dcd63 76 printf("test\n\n");
ackerden 27:bbcd157dcd63 77 printf("Capacitance = %.2f\n", chirp.getCapacitance());
ackerden 27:bbcd157dcd63 78 printf("Temperatur = %.2f\n", chirp.getTemperature());
ackerden 27:bbcd157dcd63 79 }