Skip to content

Commit

Permalink
Merge pull request #6 from chris-kck/develop
Browse files Browse the repository at this point in the history
Develop -> Master Updated docs to automatically add docstrings' documentation and implemented 9 methods. Stable build.
  • Loading branch information
chris-kck authored Oct 12, 2020
2 parents c959c7a + ae287a0 commit e928f69
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 29 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ python:
- 3.6

# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install: pip install -U tox-travis
install:
- pip install -U tox-travis
- pip install . #execute setup.py with requirements
- pip install -r requirements_dev.txt



# Command to run tests, e.g. python setup.py test
script: tox
Expand Down
7 changes: 4 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import sys
sys.path.insert(0, os.path.abspath('..'))

import smartagro
#import smartagro
import sphinx_rtd_theme

# -- General configuration ---------------------------------------------
Expand All @@ -33,6 +33,7 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx_rtd_theme']
autodoc_mock_imports = ["RPi", "paho", "mcp3008", "socket"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand All @@ -56,9 +57,9 @@
# the built documents.
#
# The short X.Y version.
version = smartagro.__version__
#version = smartagro.__version__
# The full version, including alpha/beta/rc tags.
release = smartagro.__version__
#release = smartagro.__version__

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
16 changes: 16 additions & 0 deletions docs/modules.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Package Modules
======================================

.. automodule:: smartagro.smart
:members:

.. toctree::
:maxdepth: 3
:caption: Contents:

.. automodule:: smartagro.utils
:members:

.. toctree::
:maxdepth: 3
:caption: Contents:
4 changes: 2 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pip==19.2.3
pip>=19.2.3
bump2version==0.5.11
wheel==0.33.6
watchdog==0.9.0
Expand All @@ -9,4 +9,4 @@ Sphinx==1.8.5
twine==1.14.0
Click==7.0
pytest==4.6.5
pytest-runner==5.1
pytest-runner==5.1
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
with open('HISTORY.rst') as history_file:
history = history_file.read()

requirements = ['Click>=7.0', 'paho-mqtt==1.5.1','mcp3008','sockets', ] #packages what the person using the
requirements = ['Click>=7.0', 'paho-mqtt==1.5.1','spidev', 'sockets', 'RPi.GPIO', ] #packages what the person using the
# program needs. eg ADC libraries, etc that i will use.
#requirements has debug n dev tools etc what u will include in venv

Expand All @@ -28,7 +28,6 @@
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
Expand Down
125 changes: 125 additions & 0 deletions smartagro/mcp3008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
RPi_mcp3008 is a library to listen to the MCP3008 A/D converter chip,
as described in the datasheet.
https://www.adafruit.com/datasheets/MCP3008.pdf
Copyright (C) 2015 Luiz Eduardo Amaral <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import spidev

# Modes Single
CH0 = 8 # single-ended CH0
CH1 = 9 # single-ended CH1
CH2 = 10 # single-ended CH2
CH3 = 11 # single-ended CH3
CH4 = 12 # single-ended CH4
CH5 = 13 # single-ended CH5
CH6 = 14 # single-ended CH6
CH7 = 15 # single-ended CH7
# Modes Diff
DF0 = 0 # differential CH0 = IN+ CH1 = IN-
DF1 = 1 # differential CH0 = IN- CH1 = IN+
DF2 = 2 # differential CH2 = IN+ CH3 = IN-
DF3 = 3 # differential CH2 = IN- CH3 = IN+
DF4 = 4 # differential CH4 = IN+ CH5 = IN-
DF5 = 5 # differential CH4 = IN- CH5 = IN+
DF6 = 6 # differential CH6 = IN+ CH7 = IN-
DF7 = 7 # differential CH6 = IN- CH7 = IN+

RESOLUTION = 1 << 10 # 10 bits resolution

class MCP3008(spidev.SpiDev):
'''
Object that listens the MCP3008 in the SPI port of the RPi.
Connects the object to the specified SPI device.
The initialization arguments are MCP3008(bus=0, device=0) where:
MCP3008(X, Y) will open /dev/spidev-X.Y, same as spidev.SpiDev.open(X, Y).
'''
def __init__(self, bus=0, device=0):
self.bus = bus
self.device = device
self.open(self.bus, self.device)
self.modes = False

def __del__(self):
self.close()

def __enter__(self):
return self

def __exit__(self, type, value, tb):
self.close()

def __repr__(self):
return 'MCP3008 object at bus {0}, device {1}'.format(self.bus, self.device)

def __call__(self, norm=False):
return self.read(self.modes, norm)

@classmethod
def fixed(cls, modes, bus=0, device=0):
'''
Initializes the class with fixed modes, which turns the instance callable.
The modes argument is a list with the modes of operation to be read (e.g.
[mcp3008.CH0,mcp3008.Df0]).
When calling the instance the object will execute a reading of and return the
values (e.g. print instance()).
When calling the instance, you can pass the optional argument norm to
normalize
the data (e.g. print instance(5.2)).
'''
instance = cls(bus, device)
instance.modes = modes
return instance

def _read_single(self, mode):
'''
Returns the value of a single mode reading
'''
if not 0 <= mode <= 15:
raise IndexError('Outside the channels scope, please use: 0, 1 ..., 7')
request = [0x1, mode << 4, 0x0] # [start bit, configuration, listen space]
_, byte1, byte2 = self.xfer2(request)
value = (byte1%4 << 8) + byte2
return value

def read(self, modes, norm=False):
'''
Returns the raw value (0 ... 1024) of the reading.
The modes argument is a list with the modes of operation to be read (e.g.
[mcp3008.CH0,mcp3008.Df0]).
norm is a normalization factor, usually Vref.
'''
reading = []
for mode in modes:
reading.append(self._read_single(mode))
if norm:
return [float(norm)*value/RESOLUTION for value in reading]
else:
return reading

def read_all(self, norm=False):
'''
Returns a list with the readings of all the modes
Data Order:
[DF0, DF1, DF2, DF3, DF4, DF5, DF6, DF7,
CH0, CH1, CH2, CH3, CH4, CH5, CH6, CH7]
norm is a normalization factor, usually Vref.
'''
return self.read(range(16), norm)
4 changes: 2 additions & 2 deletions smartagro/smart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Main module."""
from smartagro.utils import *
#from smartagro.utils import *


def foo():
Expand All @@ -14,7 +14,7 @@ def __init__(self, broker_ip=None, broker_port=None, qos=0, devices=4):
self.qos = qos
self.devices = devices
#if none, scan network for brokers and connect to identified broker.
config_broker(broker_ip, qos, broker_port, stream_schema=None)
#config_broker()

def devices_init(self):
pass
Loading

0 comments on commit e928f69

Please sign in to comment.