Skip to content

Commit 18bc193

Browse files
initial coding of module
1 parent d3db40a commit 18bc193

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Michael Lee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

meta.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"module_id": "michaellee1019:mpr121",
3+
"visibility": "private",
4+
"url": "https://github.com/michaellee1019/mpr121",
5+
"description": "A Viam module that returns sensor values from a capacitive touch sensor with the MPR121 chip.",
6+
"models": [
7+
{
8+
"api": "rdk:builtin:sensor",
9+
"model": "michaellee1019:mpr121:mpr121"
10+
}
11+
],
12+
"entrypoint": "run.sh",
13+
"build": {
14+
"build": "sh run.sh",
15+
"path": "dist/archive.tar.gz",
16+
"arch" : ["linux/arm64", "linux/amd64"]
17+
}
18+
}

models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import viam_wrap
2+
from viam.components.sensor import Sensor
3+
from viam.proto.app.robot import ComponentConfig
4+
from typing import Mapping, Self
5+
from viam.proto.common import ResourceName
6+
from viam.resource.base import ResourceBase
7+
import time
8+
import board
9+
import busio
10+
import adafruit_mpr121
11+
12+
class MPR121(Sensor):
13+
MODEL = "michaellee1019:mpr121:mpr121"
14+
# Create MPR121 object.
15+
mpr121: adafruit_mpr121.MPR121 = None
16+
17+
@classmethod
18+
def new(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]) -> Self:
19+
output = self(config.name)
20+
output.reconfigure(config, dependencies)
21+
return output
22+
23+
def reconfigure(self,
24+
config: ComponentConfig,
25+
dependencies: Mapping[ResourceName, ResourceBase]):
26+
i2c = busio.I2C(board.SCL, board.SDA)
27+
self.mpr121 = adafruit_mpr121.MPR121(i2c)
28+
29+
async def get_readings(self, **kwargs):
30+
return self.mpr121
31+

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
viam-sdk
2+
pyinstaller
3+
git+https://github.com/viam-labs/1liner
4+
adafruit-circuitpython-mpr121

run.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
cd `dirname $0`
3+
set -eux
4+
5+
sudo apt-get update
6+
sudo apt-get install -y python3-pip python3-venv i2c-tools
7+
sudo pip install -r requirements.txt
8+
sudo raspi-config nonint do_i2c 0
9+
10+
# Be sure to use `exec` so that termination signals reach the python process,
11+
# or handle forwarding termination signals manually
12+
exec python3 src/main.py $@

test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# be sure to run first:
2+
# sudo pip3 install adafruit-circuitpython-mpr121 --break-system-packages
3+
# sudo raspi-config nonint do_i2c 0
4+
5+
# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
6+
# SPDX-License-Identifier: MIT
7+
8+
# Simple test of the MPR121 capacitive touch sensor library.
9+
# Will print out a message when any of the 12 capacitive touch inputs of the
10+
# board are touched. Open the serial REPL after running to see the output.
11+
# Author: Tony DiCola
12+
import time
13+
import board
14+
import busio
15+
16+
# Import MPR121 module.
17+
import adafruit_mpr121
18+
19+
# Create I2C bus.
20+
i2c = busio.I2C(board.SCL, board.SDA)
21+
22+
# Create MPR121 object.
23+
mpr121 = adafruit_mpr121.MPR121(i2c)
24+
25+
# Note you can optionally change the address of the device:
26+
# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
27+
28+
# Loop forever testing each input and printing when they're touched.
29+
while True:
30+
# Loop through all 12 inputs (0-11).
31+
for i in range(12):
32+
# Call is_touched and pass it then number of the input. If it's touched
33+
# it will return True, otherwise it will return False.
34+
if mpr121[i].value:
35+
print("Input {} touched!".format(i))
36+
time.sleep(0.25) # Small delay to keep from spamming output messages.

0 commit comments

Comments
 (0)