Skip to content

Commit 679f36a

Browse files
added example program for the MPR121 capacitive touch sensor. (#689)
1 parent 5ad4c36 commit 679f36a

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_subdirectory(GPIO_Input)
1717
add_subdirectory(GPIO_Output)
1818
add_subdirectory(MIDI_UART_Input)
1919
add_subdirectory(MIDI_USBH_Input)
20+
add_subdirectory(Mpr121)
2021
add_subdirectory(OLED_SSD130x4WireSPI)
2122
add_subdirectory(SDMMC_HelloWorld)
2223
add_subdirectory(SerialPrint)

examples/Mpr121/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(FIRMWARE_NAME Mpr121)
2+
set(FIRMWARE_SOURCES main.cpp)
3+
include(DaisyProject)

examples/Mpr121/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Project Name
2+
TARGET = Mpr121
3+
4+
# Sources
5+
CPP_SOURCES = main.cpp
6+
7+
# Library Locations
8+
LIBDAISY_DIR = ../..
9+
10+
# Core location, and generic Makefile.
11+
SYSTEM_FILES_DIR = $(LIBDAISY_DIR)/core
12+
include $(SYSTEM_FILES_DIR)/Makefile

examples/Mpr121/main.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/** Reads data from an MPR121 capacitive touch sensor
2+
*
3+
* The results are printed over serial.
4+
* Each channel will output either "H" or "L" for whether it's touched,
5+
* and then display in paranthesis the "filtered value" / "baseline value"
6+
*/
7+
#include "daisy_seed.h"
8+
#include <cmath>
9+
10+
using namespace daisy;
11+
12+
DaisySeed hw;
13+
Mpr121I2C sensor;
14+
15+
int main(void)
16+
{
17+
hw.Init(true);
18+
19+
System::Delay(100);
20+
21+
// Initialize MPR121 w/ default settings
22+
Mpr121I2C::Config sensor_cfg;
23+
sensor.Init(sensor_cfg);
24+
25+
hw.StartLog(false);
26+
uint32_t print_time = System::GetNow();
27+
28+
while(1) {
29+
auto now = System::GetNow();
30+
31+
if (now - print_time > 100) {
32+
// Acquire new data
33+
uint16_t touched = sensor.Touched();
34+
uint16_t data_base[12];
35+
uint16_t data_filt[12];
36+
for (int i = 0; i < 12; i++) {
37+
data_base[i] = sensor.BaselineData(i);
38+
data_filt[i] = sensor.FilteredData(i);
39+
}
40+
41+
// Print the data
42+
hw.PrintLine("\nStates:");
43+
for (int i = 0; i < 12; i++) {
44+
if (i % 4 == 0)
45+
hw.Print("\n");
46+
bool sta = (touched & (1 << i)) > 0;
47+
hw.Print("%d: %s (%d / %d)\t", i, sta ? "H" : "L", data_filt[i], data_base[i]);
48+
}
49+
hw.Print("\n");
50+
print_time = System::GetNow();
51+
}
52+
53+
// Blink
54+
hw.SetLed((now & 511) < 255);
55+
}
56+
}

0 commit comments

Comments
 (0)