Skip to content

Commit

Permalink
initial module
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellee1019 committed May 19, 2024
1 parent 3d4a380 commit 6cdb24e
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE_DIR=$(dirname $0)
VIRTUAL_ENV=$MODULE_DIR/.venv
PYTHON=$VIRTUAL_ENV/bin/python
19 changes: 19 additions & 0 deletions .github/workflows/build-action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# see https://github.com/viamrobotics/build-action for help
on:
push:
tags:
- "*" # the build-action will trigger on all tags including RC versions

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: viamrobotics/build-action@v1
with:
# note: you can replace this line with 'version: ""' if
# you want to test the build process without deploying
version: ${{ github.ref_name }}
ref: ${{ github.sha }}
key-id: ${{ secrets.viam_key_id }}
key-value: ${{ secrets.viam_key_value }}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Michael Lee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
apt-get install -y python3.11-venv
python3 -m venv .venv
. .venv/bin/activate
pip3 install -r requirements.txt
python3 -m PyInstaller --onefile --hidden-import="googleapiclient" --hidden-import="viam-wrap" models.py
tar -czvf dist/archive.tar.gz dist/models
12 changes: 12 additions & 0 deletions exec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

# bash safe mode. look at `set --help` to see what these are doing
set -euxo pipefail

cd $(dirname $0)
source .env
./setup.sh

# Be sure to use `exec` so that termination signals reach the python process,
# or handle forwarding termination signals manually
exec $PYTHON models.py $@
18 changes: 18 additions & 0 deletions meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"module_id": "michaellee1019:i2cdetect",
"visibility": "private",
"url": "https://github.com/michaellee1019/i2cdetect",
"description": "A Viam module that returns all active i2c addresses as sensor values. Useful for troubleshooting wiring and connection issues without having to install software or SSH into the device.",
"models": [
{
"api": "rdk:component:sensor",
"model": "michaellee1019:i2cdetect:i2cdetect"
}
],
"build": {
"build": "sh build.sh",
"path": "dist/archive.tar.gz",
"arch" : ["linux/arm64", "linux/amd64"]
},
"entrypoint": "dist/models"
}
42 changes: 42 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import viam_wrap
from viam.components.sensor import Sensor
from viam.proto.app.robot import ComponentConfig
from typing import Mapping, Self
from viam.proto.common import ResourceName
from viam.resource.base import ResourceBase
import sys
import smbus2

class i2cdetect(Sensor):
MODEL = "michaellee1019:i2cdetect:i2cdetect"
i2c_bus = None

@classmethod
def new(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]) -> Self:
output = self(config.name)
output.reconfigure(config, dependencies)
return output

def reconfigure(self,
config: ComponentConfig,
dependencies: Mapping[ResourceName, ResourceBase]):
self.i2c_bus = int(config.attributes.fields["i2c_bus"].number_value)

async def get_readings(self, **kwargs):
bus = smbus2.SMBus(self.i2c_bus)
active = []
for device in range(128):
try:
bus.read_byte(device)
active.append(hex(device))
except: # exception if read_byte fails
pass
return {"active": active}

if __name__ == '__main__':
# necessary for pyinstaller to see it
# build this with:
# pyinstaller --onefile --hidden-import viam-wrap --paths $VIRTUAL_ENV/lib/python3.10/site-packages installable.py
# `--paths` arg may no longer be necessary once viam-wrap is published somewhere
# todo: utility to append this stanza automatically at build time
viam_wrap.main(sys.modules.get(__name__))
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
viam-sdk
pyinstaller
git+https://github.com/viam-labs/1liner
smbus2
37 changes: 37 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# setup.sh -- environment bootstrapper for python virtualenv

set -euo pipefail

SUDO=sudo
if ! command -v $SUDO; then
echo no sudo on this system, proceeding as current user
SUDO=""
fi

if command -v apt-get; then
if dpkg --status python3-venv > /dev/null; then
echo "python3-venv is installed, skipping setup"
else
if ! apt info python3-venv; then
echo package info not found, trying apt update
$SUDO apt-get -qq update
fi
$SUDO apt-get install -qqy python3-venv
fi
else
echo Skipping tool installation because your platform is missing apt-get.
echo If you see failures below, install the equivalent of python3-venv for your system.
fi

source .env
if [ -f $VIRTUAL_ENV/.install_complete ]; then
echo "completion marker is present, skipping virtualenv setup"
else
sudo apt install -y git
echo creating virtualenv at $VIRTUAL_ENV
python3 -m venv $VIRTUAL_ENV
echo installing dependencies from requirements.txt
$VIRTUAL_ENV/bin/pip install -r requirements.txt
touch $VIRTUAL_ENV/.install_complete
fi

0 comments on commit 6cdb24e

Please sign in to comment.