Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellee1019 authored Jan 23, 2024
0 parents commit 692856b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
sudo 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" src/main.py
tar -czvf dist/archive.tar.gz dist/main
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:tm1637",
"visibility": "private",
"url": "https://github.com/michaellee1019/tm1637",
"description": "This module implements a driver for tm1637. Currently only one method exists to show the current system time for a configurable amount of seconds",
"models": [
{
"api": "rdk:component:generic",
"model": "michaellee1019:tm1637:time"
}
],
"build": {
"build": "sh build.sh",
"path": "dist/archive.tar.gz",
"arch" : ["linux/arm64", "linux/amd64"]
},
"entrypoint": "dist/main"
}
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
viam-sdk
rpi-gpio==0.7.1
raspberrypi-tm1637
25 changes: 25 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import asyncio

from viam.components.generic import Generic
from viam.module.module import Module
from viam.resource.registry import Registry, ResourceCreatorRegistration
from models import TM1637


async def main():
"""
This function creates and starts a new module, after adding all desired
resource models. Resource creators must be registered to the resource
registry before the module adds the resource model.
"""
Registry.register_resource_creator(
Generic.SUBTYPE,
TM1637.MODEL,
ResourceCreatorRegistration(TM1637.new, TM1637.validate_config))
module = Module.from_args()

module.add_model_from_registry(Generic.SUBTYPE, TM1637.MODEL)
await module.start()

if __name__ == "__main__":
asyncio.run(main())
73 changes: 73 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from typing import Final

import tm1637
from datetime import datetime
import time

from typing import Mapping, Optional

from typing_extensions import Self

from viam.proto.app.robot import ComponentConfig
from viam.proto.common import ResourceName
from viam.utils import ValueTypes
from viam.resource.base import ResourceBase
from viam.resource.types import Model, ModelFamily
from viam.components.generic import Generic

class TM1637(Generic):
tm = None
MODEL: Final = Model(ModelFamily("michaellee1019", "tm1637"), "time")

@classmethod
def new(cls, config: ComponentConfig, dependencies: Mapping[ResourceName, ResourceBase]) -> Self:
new_tm = cls(config.name)

if 'clk_pin' in config.attributes.fields:
clk = config.attributes.fields["clk_pin"].number_value
if 'dio_pin' in config.attributes.fields:
dio = config.attributes.fields["dio_pin"].number_value
new_tm.tm = tm1637.TM1637(clk=int(clk), dio=int(dio)) # Using GPIO pins 18 and 17
return new_tm

@classmethod
def validate_config(self, config: ComponentConfig) -> None:
if 'clk_pin' not in config.attributes.fields or 'dio_pin' not in config.attributes.fields:
raise Exception('clk_pin and dio_pin are required attributes of the TM1637 model')

return None

# Attributes example
# {
# "clk_pin": 18,
# "dio_pin": 17
# }

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 == 'flash_time':
self.flash_time(args['duration'])
result[name] = True
return result

# Attributes example
# {
# "flash_time": {"duration": 5},
# }

def flash_time(self, duration: int):
clear = [0, 0, 0, 0] # Defining values used to clear the display
self.tm.write(clear)
now = datetime.now()
hh = int(datetime.strftime(now,'%H'))
mm = int(datetime.strftime(now,'%M'))
self.tm.numbers(hh, mm, colon=True)
time.sleep(duration)
self.tm.write(clear)

0 comments on commit 692856b

Please sign in to comment.