Jason Schilling / Mbed 2 deprecated miniProject7

Dependencies:   mbed TextLCD

Revision:
1:eccb59d79d6a
Parent:
0:0e0c93f4cd44
Child:
2:e32b4313502d
diff -r 0e0c93f4cd44 -r eccb59d79d6a main.cpp
--- a/main.cpp	Wed Jan 16 23:03:13 2019 +0000
+++ b/main.cpp	Wed Jan 16 23:46:13 2019 +0000
@@ -0,0 +1,108 @@
+#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() {
+    tapI2C.frequency(2000000); // 2 MHz clock
+    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);
+    while(1){
+    tapsHappened(); 
+    }
+    // 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;
+}
\ No newline at end of file