Jason Schilling / Mbed 2 deprecated miniProject7

Dependencies:   mbed TextLCD

main.cpp

Committer:
jschilling22
Date:
2019-01-17
Revision:
2:e32b4313502d
Parent:
1:eccb59d79d6a

File content as of revision 2:e32b4313502d:

#include "mbed.h"

I2C tapI2C(p9, p10);  //SDA, SCL
Serial pc(USBTX, USBRX);
void tapsHappened(void);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led4(LED4);
InterruptIn tapInterrupt(p5);
Timeout singleTimeout;
Timeout doubleTimeout;
void singleOff(void);
void doubleOff(void);
char buff[3];
const int accelAddr = 0x53 << 1;
const int tempAddr = 0x90;


int main() {
    buff[0] = 0x1D;
    buff[1] = 80;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x21;   
    buff[1] = 0x10;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x22;
    buff[1] = 0x05;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x23;
    buff[1] = 0xFF;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x2A;
    buff[1] = 0x07;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x2E;
    buff[1] = 0x60;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[1] = 0x2F;
    buff[0] = 0x60;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    buff[0] = 0x2D;
    buff[1] = 0x08;
    tapI2C.write(accelAddr, buff, 2);
    wait(0.02);
    
    tapInterrupt.rise(&tapsHappened);
    tapsHappened();
    while(1){
     pc.printf("%d\r\n", tapInterrupt.read());
     tapsHappened();
     wait(0.5); 
    }
    // Holds bytes for I2C reads/writes
    short rawTemp; // Holder for temperature bits
    float temp; // temperature, in deg C
    // Load buffer with configuration info
    buff[0] = 0x01; // Address of config register
    buff[1] = 0x60; // Config Byte 1 
    buff[2] = 0xA0; // Config Byte 2 
    tapI2C.write(tempAddr, buff, 3);
    buff[0] = 0x00; // Address of temperature reading register
    tapI2C.write(tempAddr, buff, 1); // Only one byte (byte 0) is sent
    while (true) {
    wait(1);
    tapI2C.read(tempAddr, buff, 2); // Read two-byte temperature data
    // Buff[0] holds bits 4-11 of temperature
    // Buff[1] holds bits 0-3 of temperature, followed by 0's as placeholders
    rawTemp = (buff[0] <<8) + buff[1]; // Place the bits in 16-bit holder
    rawTemp = rawTemp >> 4; // Shift right to drop placeholders, returning it to 12 bit
    temp = 0.0625 * rawTemp;
    pc.printf("%.2f deg C\r\n", temp);
    }
}
    // the ADXL345 is turned on for each transmission (and off after each transmission) to adjust its settings
    /* for all of the setting adjustments, the first line is the address of the register.
    The second line adjusts the setting to the desired value */
void tapsHappened(void) {
    char tapByte;
    buff[0]=0x30;
    tapI2C.write(accelAddr,buff,1); // 0x30 is the address, 0x80 means we are reading it
    tapI2C.read(accelAddr,buff,1); // write zero to get a read response (could send anything). tapByte is set equal to this response
    tapByte = buff[0];
    if (tapByte & 0x40) { 
        /* 0x40 converted to binary is a single tap. If there is a single tap, 
        it turns on LED 1 and activates the singleOff function one second later */
        led1 = 1;
        singleTimeout.attach(&singleOff, 1.0);
    }
    if (tapByte & 0x20) { /* 0x20 converted to binary is a double tap. If there is a double tap, 
        it turns on LED 2 and activates the doubleOff function one second later. 
        This will turn on LED 1 and 2 because every double tap starts with a single tap */
        led2 = 1;
        doubleTimeout.attach(&doubleOff, 1.0);
    }
}
void singleOff(void) {
    // first LED off when this function is called by the timeout
    led1 = 0;
}
void doubleOff(void) {
    // first LED off when this function is called by the timeout
    led2 = 0;
}