Skip to content

Commit

Permalink
initial module copied from viam-modular-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellee1019 committed May 20, 2024
1 parent a78662d commit ac54d0e
Show file tree
Hide file tree
Showing 9 changed files with 250 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:ht16k33",
"visibility": "public",
"url": "https://github.com/michaellee1019/ht16k33",
"description": "",
"models": [
{
"api": "rdk:component:generic",
"model": "michaellee1019:ht16k33:seg_14_x_4"
}
],
"build": {
"build": "sh build.sh",
"path": "dist/archive.tar.gz",
"arch" : ["linux/arm64", "linux/amd64"]
},
"entrypoint": "dist/models"
}
129 changes: 129 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import viam_wrap
from viam.components.generic import Generic
from viam.proto.app.robot import ComponentConfig
from typing import Mapping, Optional, Self
from viam.utils import ValueTypes
from viam.proto.common import ResourceName
from viam.resource.base import ResourceBase
import sys

# Import all board pins and bus interface.
import board
import busio

# Import the HT16K33 LED matrix module.
from adafruit_ht16k33 import segments, ht16k33

class Ht16k33_Seg14x4(Generic):
MODEL = "michaellee1019:ht16k33:seg_14_x_4"
i2c = None
segs = None

async def do_command(
self,
command: Mapping[str, ValueTypes],
*,
timeout: Optional[float] = None,
**kwargs
) -> Mapping[str, ValueTypes]:
result = {key: False for key in command.keys()}
for (name, args) in command.items():
if name == 'marquee':
if 'text' in args:
#TODO: NoneType is not converted to None
self.marquee(args['text'], args.get('delay'))
result[name] = True
else:
result[name] = 'missing text parameter'
if name == 'print':
if 'value' in args:
# TODO: decimal results in Error: TypeError - slice indices must be integers or None or have an __index__ method
self.print(args['value'], args.get('decimal'))
result[name] = True
else:
result[name] = 'missing value parameter'
if name == 'print_hex':
if 'value' in args:
self.print_hex(args['value'])
result[name] = True
else:
result[name] = 'missing value parameter'
if name == 'scroll':
if 'count' in args:
self.scroll(args['count'])
result[name] = True
else:
result[name] = 'missing count parameter'
if name == 'set_digit_raw':
if all(k in args for k in ('index','bitmask')):
self.set_digit_raw(args['index'], args['bitmask'])
result[name] = True
else:
result[name] = 'missing index and/or bitmask parameters'
return result


def marquee(self, text: str, delay: float) -> None:
# TODO try to support loop
self.segs.marquee(text, loop = False, delay= 0 if delay is None else delay)

def print(self, value, decimal: int) -> None:
self.segs.print(value, decimal= 0 if decimal is None else decimal)

def print_hex(self, value: int) -> None:
self.segs.print_hex(value)

def scroll(self, count: int) -> None:
# TODO Error: IndexError - bytearray index out of range
self.segs.scroll(2)

def set_digit_raw(self, index: int, bitmask: int) -> None:
# TODO Error: TypeError - unsupported operand type(s) for &=: 'float' and 'int'
self.segs.set_digit_raw(1, bitmask)

@classmethod
def new(self, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]) -> Self:
self.i2c = busio.I2C(board.SCL, board.SDA)

brightness = None
auto_write = None
if 'brightness' in config.attributes.fields:
brightness = config.attributes.fields["brightness"].number_value
if 'auto_write' in config.attributes.fields:
auto_write = config.attributes.fields["auto_write"].bool_value

addresses = config.attributes.fields["addresses"].list_value
hex_addresses=[]
for address in addresses:
hex_addresses.append(int(address,16))
# set brightness through base class

self.segs = segments.Seg14x4(
i2c=self.i2c,
address=hex_addresses,
auto_write= True if auto_write is None else auto_write,
chars_per_display=4)

if brightness is not None:
ht16k33.HT16K33(self.i2c, hex_addresses, brightness=brightness)

output = self(config.name)
return output

@classmethod
def validate_config(self, config: ComponentConfig) -> None:
addresses = config.attributes.fields["addresses"].list_value
if addresses is None:
raise Exception('A address attribute is required for seg_14_x_4 component. Must be a string array of 1 or more addresses in hexidecimal format such as "0x00".')

# TODO: assert len()>1, parse addresses here

return None

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
adafruit-circuitpython-ht16k33
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 ac54d0e

Please sign in to comment.