begin of main function

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Tap.cpp Source File

Tap.cpp

00001 #include "Tap.h"
00002 
00003 I2C tapI2C(p9, p10);
00004 InterruptIn tapInterrupt(p5);
00005 char buff[2];
00006 const int accelAddr = 0x53 << 1;
00007 volatile int i;
00008 
00009 void tapsConfig(void) {  //configures the settings for the accelerometer (same settings as mini project 6 converted to I2C)
00010     buff[0] = 0x1D;
00011     buff[1] = 80;
00012     tapI2C.write(accelAddr, buff, 2);
00013     wait(0.02);
00014     buff[0] = 0x21;  
00015     buff[1] = 0x10;
00016     tapI2C.write(accelAddr, buff, 2);
00017     wait(0.02);
00018     buff[0] = 0x22;
00019     buff[1] = 0x05;
00020     tapI2C.write(accelAddr, buff, 2);
00021     wait(0.02);
00022     buff[0] = 0x23;
00023     buff[1] = 0xFF;
00024     tapI2C.write(accelAddr, buff, 2);
00025     wait(0.02);
00026     buff[0] = 0x2A;
00027     buff[1] = 0x07;
00028     tapI2C.write(accelAddr, buff, 2);
00029     wait(0.02);
00030     buff[0] = 0x2E;
00031     buff[1] = 0x60;
00032     tapI2C.write(accelAddr, buff, 2);
00033     wait(0.02);
00034     buff[1] = 0x2F;
00035     buff[0] = 0x60;
00036     tapI2C.write(accelAddr, buff, 2);
00037     wait(0.02);
00038     buff[0] = 0x2D;
00039     buff[1] = 0x08;
00040     tapI2C.write(accelAddr, buff, 2);
00041     wait(0.02);
00042     tapInterrupt.rise(&tapsHappened);
00043     tapsHappened(); // resets the interrupt, same as mini project 6
00044 }
00045 
00046 void tapsHappened(void) {
00047     char tapByte;
00048     bool absol = true;
00049     bool unit = true;
00050     buff[0]=0x30;
00051     tapI2C.write(accelAddr,buff,1);
00052     tapI2C.read(accelAddr,buff,1);
00053     tapByte = buff[0]; // gets a value for tapByte
00054     if (tapByte & 0x40) { //based on the value of tapByte, determines if there was a single or double tap
00055         unit = !unit; // changes the value of unit and absol based on the number of taps
00056     }
00057     if (tapByte & 0x20) { 
00058         absol = !absol;
00059     }
00060     if (unit == 1 && absol == 1) { /* based on the values of unit and absol, determines the value of i,
00061     which is used to determine the units of the temperature displayed */
00062         i = 0;
00063     }
00064     if (unit == 1 && absol == 0) {
00065         i = 1;
00066     }
00067     if (unit == 0 && absol == 1) {
00068         i = 2;
00069     }
00070     if (unit == 0 && absol == 0) {
00071         i = 3;
00072     }
00073 }
00074 int getSetting(int i){ /* the value of i is set in a void function (which must be void because it is an interrupt)
00075 This function is an int function that returns the value of i, allowing us to call it for the value of i from main.cpp */
00076     return i;
00077 }