Michal Kravcik
/
touchboard_mpr121
aa
main.cpp@1:b61353e34ace, 2022-02-14 (annotated)
- Committer:
- Kiskovce
- Date:
- Mon Feb 14 11:56:39 2022 +0000
- Revision:
- 1:b61353e34ace
- Parent:
- 0:e09703934ff4
ee
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 | |
Kiskovce | 1:b61353e34ace | 29 | DigitalOut led1(PB_3); |
Kiskovce | 1:b61353e34ace | 30 | |
4180_1 | 0:e09703934ff4 | 31 | |
4180_1 | 0:e09703934ff4 | 32 | // Create the interrupt receiver object on pin 26 |
4180_1 | 0:e09703934ff4 | 33 | InterruptIn interrupt(p26); |
4180_1 | 0:e09703934ff4 | 34 | |
4180_1 | 0:e09703934ff4 | 35 | // Setup the Serial to the PC for debugging |
4180_1 | 0:e09703934ff4 | 36 | Serial pc(USBTX, USBRX); |
4180_1 | 0:e09703934ff4 | 37 | |
4180_1 | 0:e09703934ff4 | 38 | // Setup the i2c bus on pins 28 and 27 |
Kiskovce | 1:b61353e34ace | 39 | I2C i2c(PB_6, PB_7); |
4180_1 | 0:e09703934ff4 | 40 | |
4180_1 | 0:e09703934ff4 | 41 | // Setup the Mpr121: |
4180_1 | 0:e09703934ff4 | 42 | // constructor(i2c object, i2c address of the mpr121) |
4180_1 | 0:e09703934ff4 | 43 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
4180_1 | 0:e09703934ff4 | 44 | |
4180_1 | 0:e09703934ff4 | 45 | void fallInterrupt() { |
4180_1 | 0:e09703934ff4 | 46 | int key_code=0; |
4180_1 | 0:e09703934ff4 | 47 | int i=0; |
4180_1 | 0:e09703934ff4 | 48 | int value=mpr121.read(0x00); |
4180_1 | 0:e09703934ff4 | 49 | value +=mpr121.read(0x01)<<8; |
4180_1 | 0:e09703934ff4 | 50 | // LED demo mod by J. Hamblen |
4180_1 | 0:e09703934ff4 | 51 | //pc.printf("MPR value: %x \r\n", value); |
4180_1 | 0:e09703934ff4 | 52 | i=0; |
4180_1 | 0:e09703934ff4 | 53 | // puts key number out to LEDs for demo |
4180_1 | 0:e09703934ff4 | 54 | for (i=0; i<12; i++) { |
4180_1 | 0:e09703934ff4 | 55 | if (((value>>i)&0x01)==1) key_code=i+1; |
4180_1 | 0:e09703934ff4 | 56 | } |
4180_1 | 0:e09703934ff4 | 57 | led4=key_code & 0x01; |
4180_1 | 0:e09703934ff4 | 58 | led3=(key_code>>1) & 0x01; |
4180_1 | 0:e09703934ff4 | 59 | led2=(key_code>>2) & 0x01; |
4180_1 | 0:e09703934ff4 | 60 | led1=(key_code>>3) & 0x01; |
4180_1 | 0:e09703934ff4 | 61 | } |
4180_1 | 0:e09703934ff4 | 62 | int main() { |
4180_1 | 0:e09703934ff4 | 63 | |
4180_1 | 0:e09703934ff4 | 64 | pc.printf("\nHello from the mbed & mpr121\n\r"); |
4180_1 | 0:e09703934ff4 | 65 | |
4180_1 | 0:e09703934ff4 | 66 | unsigned char dataArray[2]; |
4180_1 | 0:e09703934ff4 | 67 | int key; |
4180_1 | 0:e09703934ff4 | 68 | int count = 0; |
4180_1 | 0:e09703934ff4 | 69 | |
4180_1 | 0:e09703934ff4 | 70 | pc.printf("Test 1: read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 71 | dataArray[0] = mpr121.read(AFE_CFG); |
4180_1 | 0:e09703934ff4 | 72 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 73 | |
4180_1 | 0:e09703934ff4 | 74 | pc.printf("Test 2: read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 75 | dataArray[0] = mpr121.read(0x5d); |
4180_1 | 0:e09703934ff4 | 76 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 77 | |
4180_1 | 0:e09703934ff4 | 78 | pc.printf("Test 3: write & read a value: \r\n"); |
4180_1 | 0:e09703934ff4 | 79 | mpr121.read(ELE0_T); |
4180_1 | 0:e09703934ff4 | 80 | mpr121.write(ELE0_T,0x22); |
4180_1 | 0:e09703934ff4 | 81 | dataArray[0] = mpr121.read(ELE0_T); |
4180_1 | 0:e09703934ff4 | 82 | pc.printf("Read value=%x\r\n\n",dataArray[0]); |
4180_1 | 0:e09703934ff4 | 83 | |
4180_1 | 0:e09703934ff4 | 84 | pc.printf("Test 4: Write many values: \r\n"); |
4180_1 | 0:e09703934ff4 | 85 | unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; |
4180_1 | 0:e09703934ff4 | 86 | mpr121.writeMany(0x42,data,7); |
4180_1 | 0:e09703934ff4 | 87 | |
4180_1 | 0:e09703934ff4 | 88 | // Now read them back .. |
4180_1 | 0:e09703934ff4 | 89 | key = 0x42; |
4180_1 | 0:e09703934ff4 | 90 | count = 0; |
4180_1 | 0:e09703934ff4 | 91 | while (count < 7) { |
4180_1 | 0:e09703934ff4 | 92 | char result = mpr121.read(key); |
4180_1 | 0:e09703934ff4 | 93 | key++; |
4180_1 | 0:e09703934ff4 | 94 | count++; |
4180_1 | 0:e09703934ff4 | 95 | pc.printf("Read value: '%x'=%x\n\r",key,result); |
4180_1 | 0:e09703934ff4 | 96 | } |
4180_1 | 0:e09703934ff4 | 97 | |
4180_1 | 0:e09703934ff4 | 98 | pc.printf("Test 5: Read Electrodes:\r\n"); |
4180_1 | 0:e09703934ff4 | 99 | key = ELE0_T; |
4180_1 | 0:e09703934ff4 | 100 | count = 0; |
4180_1 | 0:e09703934ff4 | 101 | while (count < 24) { |
4180_1 | 0:e09703934ff4 | 102 | char result = mpr121.read(key); |
4180_1 | 0:e09703934ff4 | 103 | pc.printf("Read key:%x value:%x\n\r",key,result); |
4180_1 | 0:e09703934ff4 | 104 | key++; |
4180_1 | 0:e09703934ff4 | 105 | count++; |
4180_1 | 0:e09703934ff4 | 106 | } |
4180_1 | 0:e09703934ff4 | 107 | pc.printf("--------- \r\n\n"); |
4180_1 | 0:e09703934ff4 | 108 | |
4180_1 | 0:e09703934ff4 | 109 | // mpr121.setProximityMode(true); |
4180_1 | 0:e09703934ff4 | 110 | |
4180_1 | 0:e09703934ff4 | 111 | pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); |
4180_1 | 0:e09703934ff4 | 112 | |
4180_1 | 0:e09703934ff4 | 113 | interrupt.fall(&fallInterrupt); |
4180_1 | 0:e09703934ff4 | 114 | interrupt.mode(PullUp); |
4180_1 | 0:e09703934ff4 | 115 | |
4180_1 | 0:e09703934ff4 | 116 | while (1) { |
4180_1 | 0:e09703934ff4 | 117 | wait(5); |
4180_1 | 0:e09703934ff4 | 118 | pc.printf("."); |
4180_1 | 0:e09703934ff4 | 119 | } |
4180_1 | 0:e09703934ff4 | 120 | } |
4180_1 | 0:e09703934ff4 | 121 | |
4180_1 | 0:e09703934ff4 | 122 | |
4180_1 | 0:e09703934ff4 | 123 |