DEMO
Dependencies: mbed
Fork of MPR121_Demo by
main.cpp@1:4e809692213a, 2017-06-12 (annotated)
- Committer:
- niklasjakob
- Date:
- Mon Jun 12 17:10:20 2017 +0000
- Revision:
- 1:4e809692213a
- Parent:
- 0:e09703934ff4
MPR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 0:e09703934ff4 | 1 | /* |
4180_1 | 0:e09703934ff4 | 2 | Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) |
4180_1 | 0:e09703934ff4 | 3 | |
4180_1 | 0:e09703934ff4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
4180_1 | 0:e09703934ff4 | 5 | of this software and associated documentation files (the "Software"), to deal |
4180_1 | 0:e09703934ff4 | 6 | in the Software without restriction, including without limitation the rights |
4180_1 | 0:e09703934ff4 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
4180_1 | 0:e09703934ff4 | 8 | copies of the Software, and to permit persons to whom the Software is |
4180_1 | 0:e09703934ff4 | 9 | furnished to do so, subject to the following conditions: |
4180_1 | 0:e09703934ff4 | 10 | |
4180_1 | 0:e09703934ff4 | 11 | The above copyright notice and this permission notice shall be included in |
4180_1 | 0:e09703934ff4 | 12 | all copies or substantial portions of the Software. |
4180_1 | 0:e09703934ff4 | 13 | |
4180_1 | 0:e09703934ff4 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4180_1 | 0:e09703934ff4 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
4180_1 | 0:e09703934ff4 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
4180_1 | 0:e09703934ff4 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
4180_1 | 0:e09703934ff4 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
4180_1 | 0:e09703934ff4 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
4180_1 | 0:e09703934ff4 | 20 | THE SOFTWARE. |
4180_1 | 0:e09703934ff4 | 21 | */ |
4180_1 | 0:e09703934ff4 | 22 | |
4180_1 | 0:e09703934ff4 | 23 | #include <mbed.h> |
4180_1 | 0:e09703934ff4 | 24 | #include <string> |
4180_1 | 0:e09703934ff4 | 25 | #include <list> |
4180_1 | 0:e09703934ff4 | 26 | |
4180_1 | 0:e09703934ff4 | 27 | #include <mpr121.h> |
4180_1 | 0:e09703934ff4 | 28 | |
4180_1 | 0:e09703934ff4 | 29 | DigitalOut led1(LED1); |
4180_1 | 0:e09703934ff4 | 30 | DigitalOut led2(LED2); |
4180_1 | 0:e09703934ff4 | 31 | DigitalOut led3(LED3); |
4180_1 | 0:e09703934ff4 | 32 | DigitalOut led4(LED4); |
4180_1 | 0:e09703934ff4 | 33 | |
4180_1 | 0:e09703934ff4 | 34 | // Create the interrupt receiver object on pin 26 |
4180_1 | 0:e09703934ff4 | 35 | InterruptIn interrupt(p26); |
4180_1 | 0:e09703934ff4 | 36 | |
4180_1 | 0:e09703934ff4 | 37 | // Setup the Serial to the PC for debugging |
4180_1 | 0:e09703934ff4 | 38 | Serial pc(USBTX, USBRX); |
4180_1 | 0:e09703934ff4 | 39 | |
4180_1 | 0:e09703934ff4 | 40 | // Setup the i2c bus on pins 28 and 27 |
niklasjakob | 1:4e809692213a | 41 | I2C i2c(P0_29, P0_28); |
4180_1 | 0:e09703934ff4 | 42 | |
4180_1 | 0:e09703934ff4 | 43 | // Setup the Mpr121: |
4180_1 | 0:e09703934ff4 | 44 | // constructor(i2c object, i2c address of the mpr121) |
4180_1 | 0:e09703934ff4 | 45 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
4180_1 | 0:e09703934ff4 | 46 | |
4180_1 | 0:e09703934ff4 | 47 | void fallInterrupt() { |
4180_1 | 0:e09703934ff4 | 48 | int key_code=0; |
4180_1 | 0:e09703934ff4 | 49 | int i=0; |
4180_1 | 0:e09703934ff4 | 50 | int value=mpr121.read(0x00); |
4180_1 | 0:e09703934ff4 | 51 | value +=mpr121.read(0x01)<<8; |
niklasjakob | 1:4e809692213a | 52 | // LED demo mod by J. Hamble |
4180_1 | 0:e09703934ff4 | 53 | i=0; |
4180_1 | 0:e09703934ff4 | 54 | // puts key number out to LEDs for demo |
4180_1 | 0:e09703934ff4 | 55 | for (i=0; i<12; i++) { |
4180_1 | 0:e09703934ff4 | 56 | if (((value>>i)&0x01)==1) key_code=i+1; |
4180_1 | 0:e09703934ff4 | 57 | } |
4180_1 | 0:e09703934ff4 | 58 | led4=key_code & 0x01; |
4180_1 | 0:e09703934ff4 | 59 | led3=(key_code>>1) & 0x01; |
4180_1 | 0:e09703934ff4 | 60 | led2=(key_code>>2) & 0x01; |
4180_1 | 0:e09703934ff4 | 61 | led1=(key_code>>3) & 0x01; |
4180_1 | 0:e09703934ff4 | 62 | } |
4180_1 | 0:e09703934ff4 | 63 | int main() { |
4180_1 | 0:e09703934ff4 | 64 | |
4180_1 | 0:e09703934ff4 | 65 | pc.printf("\nHello from the mbed & mpr121\n\r"); |
4180_1 | 0:e09703934ff4 | 66 | |
4180_1 | 0:e09703934ff4 | 67 | unsigned char dataArray[2]; |
4180_1 | 0:e09703934ff4 | 68 | int key; |
4180_1 | 0:e09703934ff4 | 69 | int count = 0; |
4180_1 | 0:e09703934ff4 | 70 | |
4180_1 | 0:e09703934ff4 | 71 | pc.printf("Test 1: read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 72 | dataArray[0] = mpr121.read(AFE_CFG); |
4180_1 | 0:e09703934ff4 | 73 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 74 | |
4180_1 | 0:e09703934ff4 | 75 | pc.printf("Test 2: read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 76 | dataArray[0] = mpr121.read(0x5d); |
4180_1 | 0:e09703934ff4 | 77 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 78 | |
4180_1 | 0:e09703934ff4 | 79 | pc.printf("Test 3: write & read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 80 | mpr121.read(ELE0_T); |
4180_1 | 0:e09703934ff4 | 81 | mpr121.write(ELE0_T,0x22); |
4180_1 | 0:e09703934ff4 | 82 | dataArray[0] = mpr121.read(ELE0_T); |
4180_1 | 0:e09703934ff4 | 83 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 84 | |
4180_1 | 0:e09703934ff4 | 85 | pc.printf("Test 4: Write many values: \r\n"); |
4180_1 | 0:e09703934ff4 | 86 | unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; |
4180_1 | 0:e09703934ff4 | 87 | mpr121.writeMany(0x42,data,7); |
4180_1 | 0:e09703934ff4 | 88 | |
4180_1 | 0:e09703934ff4 | 89 | // Now read them back .. |
4180_1 | 0:e09703934ff4 | 90 | key = 0x42; |
4180_1 | 0:e09703934ff4 | 91 | count = 0; |
4180_1 | 0:e09703934ff4 | 92 | while (count < 7) { |
4180_1 | 0:e09703934ff4 | 93 | char result = mpr121.read(key); |
4180_1 | 0:e09703934ff4 | 94 | key++; |
4180_1 | 0:e09703934ff4 | 95 | count++; |
4180_1 | 0:e09703934ff4 | 96 | pc.printf("Read value: '%x'=%x\n\r",key,result); |
4180_1 | 0:e09703934ff4 | 97 | } |
4180_1 | 0:e09703934ff4 | 98 | |
4180_1 | 0:e09703934ff4 | 99 | pc.printf("Test 5: Read Electrodes:\r\n"); |
4180_1 | 0:e09703934ff4 | 100 | key = ELE0_T; |
4180_1 | 0:e09703934ff4 | 101 | count = 0; |
4180_1 | 0:e09703934ff4 | 102 | while (count < 24) { |
4180_1 | 0:e09703934ff4 | 103 | char result = mpr121.read(key); |
4180_1 | 0:e09703934ff4 | 104 | pc.printf("Read key:%x value:%x\n\r",key,result); |
4180_1 | 0:e09703934ff4 | 105 | key++; |
4180_1 | 0:e09703934ff4 | 106 | count++; |
4180_1 | 0:e09703934ff4 | 107 | } |
4180_1 | 0:e09703934ff4 | 108 | pc.printf("--------- \r\n\n"); |
4180_1 | 0:e09703934ff4 | 109 | |
4180_1 | 0:e09703934ff4 | 110 | // mpr121.setProximityMode(true); |
4180_1 | 0:e09703934ff4 | 111 | |
4180_1 | 0:e09703934ff4 | 112 | pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); |
4180_1 | 0:e09703934ff4 | 113 | |
4180_1 | 0:e09703934ff4 | 114 | interrupt.fall(&fallInterrupt); |
4180_1 | 0:e09703934ff4 | 115 | interrupt.mode(PullUp); |
4180_1 | 0:e09703934ff4 | 116 | |
4180_1 | 0:e09703934ff4 | 117 | while (1) { |
4180_1 | 0:e09703934ff4 | 118 | wait(5); |
4180_1 | 0:e09703934ff4 | 119 | pc.printf("."); |
4180_1 | 0:e09703934ff4 | 120 | } |
4180_1 | 0:e09703934ff4 | 121 | } |
4180_1 | 0:e09703934ff4 | 122 | |
4180_1 | 0:e09703934ff4 | 123 | |
4180_1 | 0:e09703934ff4 | 124 |