![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Compass Working
Tap.cpp@5:f6762034b4c0, 2016-01-19 (annotated)
- Committer:
- pstephens18
- Date:
- Tue Jan 19 18:51:56 2016 +0000
- Revision:
- 5:f6762034b4c0
- Parent:
- 4:92024cb66c9c
Second
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pstephens18 | 1:ef74b3428716 | 1 | #include "Tap.h" |
pstephens18 | 1:ef74b3428716 | 2 | I2C acc(p9,p10); |
pstephens18 | 2:ef6778c2438e | 3 | DigitalIn int2(p20); |
pstephens18 | 5:f6762034b4c0 | 4 | InterruptIn check(p12); |
pstephens18 | 1:ef74b3428716 | 5 | BusOut led(LED1,LED2,LED3,LED4); |
pstephens18 | 2:ef6778c2438e | 6 | char acc_config[2]; |
pstephens18 | 2:ef6778c2438e | 7 | const int addr=(0x53<<1); |
pstephens18 | 2:ef6778c2438e | 8 | char data1[1]; |
pstephens18 | 2:ef6778c2438e | 9 | char ack; |
pstephens18 | 1:ef74b3428716 | 10 | |
asloop18 | 4:92024cb66c9c | 11 | void Tap_init() |
pstephens18 | 2:ef6778c2438e | 12 | { |
pstephens18 | 2:ef6778c2438e | 13 | acc.frequency(100000); |
pstephens18 | 2:ef6778c2438e | 14 | acc_config[0]=0x31; acc_config[1]=0x0B; //data format |
pstephens18 | 2:ef6778c2438e | 15 | ack = acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 16 | wait(0.1); |
pstephens18 | 2:ef6778c2438e | 17 | |
pstephens18 | 2:ef6778c2438e | 18 | acc_config[0]=0x2D; acc_config[1]=0x08; //power control->measure mode |
pstephens18 | 2:ef6778c2438e | 19 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 20 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 21 | |
pstephens18 | 2:ef6778c2438e | 22 | acc_config[0]=0x1D; acc_config[1]=0x50; //Tap Threshold ->5g |
pstephens18 | 2:ef6778c2438e | 23 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 24 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 25 | |
pstephens18 | 2:ef6778c2438e | 26 | acc_config[0]=0x21; acc_config[1]=0x10; //Tap duration -> 0.01 |
pstephens18 | 2:ef6778c2438e | 27 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 28 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 29 | |
pstephens18 | 2:ef6778c2438e | 30 | acc_config[0]=0x22; acc_config[1]=0x05; //Tap Gap ~0.006 |
pstephens18 | 2:ef6778c2438e | 31 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 32 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 33 | |
pstephens18 | 2:ef6778c2438e | 34 | acc_config[0]=0x23; acc_config[1]=0xFF; //Tap window ->~0.38175 |
pstephens18 | 2:ef6778c2438e | 35 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 36 | wait(0.1); |
pstephens18 | 2:ef6778c2438e | 37 | |
pstephens18 | 2:ef6778c2438e | 38 | acc_config[0]=0x2A; acc_config[1]=0x07; //Axis -> x&y&z |
pstephens18 | 2:ef6778c2438e | 39 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 40 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 41 | |
pstephens18 | 2:ef6778c2438e | 42 | acc_config[0]=0x2E; acc_config[1]=0x60; //enable interupt -> single and double tap |
pstephens18 | 2:ef6778c2438e | 43 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 44 | wait(0.1); |
pstephens18 | 1:ef74b3428716 | 45 | |
pstephens18 | 2:ef6778c2438e | 46 | acc_config[0]=0x2F; acc_config[1]=0x60; //map interupt ->int2 |
pstephens18 | 2:ef6778c2438e | 47 | acc.write(addr,acc_config, 2); |
pstephens18 | 2:ef6778c2438e | 48 | wait(0.1); |
asloop18 | 4:92024cb66c9c | 49 | } |
asloop18 | 4:92024cb66c9c | 50 | float Tap() |
asloop18 | 4:92024cb66c9c | 51 | { |
pstephens18 | 5:f6762034b4c0 | 52 | int2.rise(&Tap_return) |
pstephens18 | 5:f6762034b4c0 | 53 | while(1) |
pstephens18 | 5:f6762034b4c0 | 54 | { |
pstephens18 | 5:f6762034b4c0 | 55 | wait(0.01); |
pstephens18 | 2:ef6778c2438e | 56 | |
pstephens18 | 5:f6762034b4c0 | 57 | if(int2.read()==1) // Check for interupt signal |
pstephens18 | 1:ef74b3428716 | 58 | { |
pstephens18 | 5:f6762034b4c0 | 59 | |
pstephens18 | 5:f6762034b4c0 | 60 | acc_config[0]=0x30; |
pstephens18 | 5:f6762034b4c0 | 61 | acc.write(addr,acc_config,1); |
pstephens18 | 5:f6762034b4c0 | 62 | acc.read(addr,data1,1); // Get interupt information |
pstephens18 | 5:f6762034b4c0 | 63 | |
pstephens18 | 5:f6762034b4c0 | 64 | if(data1[0] & 0x40) // Turn on one LED for one tap |
pstephens18 | 5:f6762034b4c0 | 65 | { |
pstephens18 | 5:f6762034b4c0 | 66 | led =1; |
pstephens18 | 5:f6762034b4c0 | 67 | } |
pstephens18 | 5:f6762034b4c0 | 68 | if(data1[0] & 0x20) // Turn on two LEDs for two taps |
pstephens18 | 5:f6762034b4c0 | 69 | { |
pstephens18 | 5:f6762034b4c0 | 70 | led =2; |
pstephens18 | 5:f6762034b4c0 | 71 | } |
pstephens18 | 5:f6762034b4c0 | 72 | wait(1); |
pstephens18 | 5:f6762034b4c0 | 73 | // Turn off LED |
pstephens18 | 5:f6762034b4c0 | 74 | if(led==1){return 1;} |
pstephens18 | 5:f6762034b4c0 | 75 | if(led ==2){return 2;} |
pstephens18 | 5:f6762034b4c0 | 76 | |
pstephens18 | 5:f6762034b4c0 | 77 | } |
pstephens18 | 5:f6762034b4c0 | 78 | } |
pstephens18 | 5:f6762034b4c0 | 79 | } |
pstephens18 | 5:f6762034b4c0 | 80 | |
pstephens18 | 5:f6762034b4c0 | 81 | void Tap_return() |
pstephens18 | 5:f6762034b4c0 | 82 | { |
pstephens18 | 2:ef6778c2438e | 83 | |
pstephens18 | 5:f6762034b4c0 | 84 | } |