Bryce Williams / Mbed 2 deprecated DRV2605L_Haptic_Driver_Demo

Dependencies:   DRV2605 MPR121 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Bryce Williams 
00003     10/19/2015
00004     
00005     Basic Demo of the TI DRV2605L Haptics Driver Breakout. 
00006     This code uses the MPR121 Library and modified version 
00007     of Dr. Jim Hamblen's Hello World Example for the MPR121
00008     for Capacitive Touch Keypad for user input. 
00009     
00010     The user and touch keys 3,7,11,2,6,10, 1,5,9,and 0. 
00011     Each Key will toggle through about 10 effects each,
00012     (key 11 toggles between 12 or 13 of the effects). 
00013     Effects are the ROM Library Haptics effects internal to 
00014     the  DRV2605L Haptics Driver. 
00015 */
00016 
00017 #include "mbed.h"
00018 #include "DRV2605.h"
00019 #include "mpr121.h"
00020 
00021 DRV2605 haptics(p9, p10);
00022 DigitalOut led1(LED1);
00023 DigitalOut led2(LED2);
00024 DigitalOut led3(LED3);
00025 DigitalOut led4(LED4);
00026 // Setup the i2c bus on pins 28 and 27
00027 I2C i2c(p28, p27);
00028 // Create the interrupt receiver object on pin 26
00029 InterruptIn interrupt(p26);
00030 
00031 // Setup the Mpr121 Cap Touch Driver:
00032 // constructor(i2c object, i2c address of the mpr121)
00033 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00034 
00035 void fallInterrupt() {
00036     int key_code=0;
00037     int i=0;
00038     int value=mpr121.read(0x00);
00039     value +=mpr121.read(0x01)<<8;
00040     // LED demo mod by J. Hamblen
00041     printf("MPR value: %x \r\n", value);
00042     i=0;
00043     // puts key number out to LEDs for demo
00044     for (i=0; i<12; i++) {
00045         if (((value>>i)&0x01)==1) key_code=i+1;
00046         }
00047     led4=key_code & 0x01;
00048     led3=(key_code>>1) & 0x01;
00049     led2=(key_code>>2) & 0x01;
00050     led1=(key_code>>3) & 0x01;
00051 
00052     // Evaluate Keypad from Left-to-Right Top-to-Bottom like reading
00053     // Each Key sequences through decimal indexes ending with 
00054     // corresponding positions... i.e. 
00055     // Key 3 (value = 800) sequences 3, 13, 23, ... 123
00056     // Key 5 (value = 40) sequences 5, 15, 25,... 115
00057     // Key 11(value = 10) and 12 (value = 100) are free for custom 
00058     // implementation
00059     switch(value){
00060         static int e1 = 1, e2 = 2, e3 = 3, e4 = 4, e5 = 5, e6 = 6,
00061                    e7 = 7, e8 = 8, e9 = 9, e10 = 10;
00062         case 0x8:
00063             printf("Playing %i\n", e1);
00064             haptics.play_waveform(e1);
00065             e1+=10; 
00066             if(e1 > 121) e1 = 1;
00067             break;
00068         case 0x80:
00069             printf("Playing %i\n", e2);
00070             haptics.play_waveform(e2);
00071             e2+=10; 
00072             if(e2 > 122) e2 = 2;
00073             break;
00074         case 0x800:
00075             printf("Playing %i\n", e3);
00076             haptics.play_waveform(e3);
00077             e3+=10; 
00078             if(e3 > 123) e3 = 3;
00079             break;
00080         case 0x4:
00081             printf("Playing %i\n", e4);
00082             haptics.play_waveform(e4);
00083             e4+=10; 
00084             if(e4 > 114) e4 = 4;
00085             break;
00086         case 0x40:
00087             printf("Playing %i\n", e5);
00088             haptics.play_waveform(e5);
00089             e5+=10; 
00090             if(e5 > 115) e5 = 5;
00091             break;
00092         case 0x400:
00093             printf("Playing %i\n", e6);
00094             haptics.play_waveform(e6);
00095             e6+=10; 
00096             if(e6 > 116) e6 = 6;
00097             break;
00098         case 0x2:
00099             printf("Playing %i\n", e7);
00100             haptics.play_waveform(e7);
00101             e7+=10; 
00102             if(e7 > 117) e7 = 7;
00103             break;
00104         case 0x20:
00105             printf("Playing %i\n", e8);
00106             haptics.play_waveform(e8);
00107             e8+=10; 
00108             if(e8 > 118) e8 = 8;
00109             break;
00110         case 0x200:
00111             printf("Playing %i\n", e9);
00112             haptics.play_waveform(e9);
00113             e9+=10; 
00114             if(e9 > 119) e9 = 9;
00115             break;
00116         case 0x1:
00117             printf("Playing %i\n", e10);
00118             haptics.play_waveform(e10);
00119             e10+=10; 
00120             if(e10 > 120) e10 = 10;
00121             break;
00122         case 0x10:
00123             break;
00124         case 0x100:
00125             break;
00126     }
00127 }
00128 
00129 int main() {
00130     // Set up Interrupts for MPR121 Cap Touch Driver/ Keypad
00131     interrupt.fall(&fallInterrupt);
00132     interrupt.mode(PullUp);
00133     
00134     // Daignostics Routine
00135     printf("Diagnostics Result: %X\n", haptics.diagnostics());
00136         
00137     // Initialization Procedure as outlined in Section 9.3 of Device Datasheet
00138     printf("Calibration Result: %X\n",haptics.init(3.3));
00139     
00140     // Daignostics Routine
00141     printf("Diagnostics Result: %X\n", haptics.diagnostics());
00142     
00143     // Play sequence of library waveforms as outlined in Section 9.3.2.1 of Device Datasheet
00144     haptics.load_waveform_sequence(123,21,43,18,94,48,112,36);
00145     haptics.play();
00146     while(haptics.i2cReadByte(GO));         // Wait for playback to complete
00147     
00148     wait(2);
00149     
00150     haptics.load_waveform_sequence(55,65);
00151     haptics.play();
00152     while(haptics.i2cReadByte(GO));         // Wait for playback to complete
00153     
00154     wait(2);
00155     
00156     // Play a waveform corresponding to touch-keypad press
00157     while(1);
00158 }