Skip to content

Commit

Permalink
Merge pull request #48 from pimoroni/ads1015-library
Browse files Browse the repository at this point in the history
Migrate to ADS1015 library for #11
  • Loading branch information
Gadgetoid authored Jan 18, 2022
2 parents dec9c09 + a9d3fd7 commit d8c2df5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 186 deletions.
37 changes: 23 additions & 14 deletions library/automationhat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import atexit
import time
import warnings
from sys import version_info

try:
import RPi.GPIO as GPIO
except ImportError:
raise ImportError("This library requires the RPi.GPIO module\nInstall with: sudo pip install RPi.GPIO")
raise ImportError("This library requires the RPi.GPIO module\nInstall with: sudo python3 -m pip install RPi.GPIO")

try:
import smbus
except ImportError:
raise ImportError("This library requires python3-smbus\nInstall with: sudo apt install python3-smbus")

try:
import ads1015
except ImportError:
raise ImportError("This library requires ads1015\nInstall with: sudo python3 -m pip install ads1015")

from .ads1015 import ads1015
from .pins import ObjectCollection, AsyncWorker, StoppableThread

__version__ = '0.3.0'
Expand Down Expand Up @@ -38,6 +46,7 @@
_lights_need_updating = False
_is_setup = False
_t_update_lights = None
_ads1015 = None


class SNLight(object):
Expand Down Expand Up @@ -121,7 +130,7 @@ def read(self):

def _update(self):
self.setup()
self.value = _ads1015.read(self.channel)
self.value = _ads1015.get_voltage("in{}/gnd".format(self.channel)) / 3.3

if self._en_auto_lights:
adc = self.value
Expand Down Expand Up @@ -343,23 +352,23 @@ def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

_ads1015 = ads1015.ADS1015()
try:
import smbus
except ImportError:
if version_info[0] < 3:
raise ImportError("This library requires python-smbus\nInstall with: sudo apt install python-smbus")
elif version_info[0] == 3:
raise ImportError("This library requires python3-smbus\nInstall with: sudo apt install python3-smbus")
chip_type = _ads1015.detect_chip_type()
except IOError:
raise RuntimeError("No ADC detected, check your connections")

_ads1015 = ads1015(smbus.SMBus(1))
if chip_type == 'ADS1015':
_ads1015.set_sample_rate(1600)
else:
_ads1015.set_sample_rate(860)

if _ads1015.available() is False:
raise RuntimeError("No ADC detected, check your connections")
_ads1015.set_programmable_gain(4.096)

try:
import sn3218
except ImportError:
raise ImportError("This library requires sn3218\nInstall with: sudo pip install sn3218")
raise ImportError("This library requires sn3218\nInstall with: sudo python3 -m pip install sn3218")
except IOError:
pass

Expand Down
128 changes: 0 additions & 128 deletions library/automationhat/ads1015.py

This file was deleted.

3 changes: 2 additions & 1 deletion library/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ install_requires =
RPi.GPIO
sn3218
ST7735
ads1015>=0.0.8

[flake8]
exclude =
Expand All @@ -57,4 +58,4 @@ configtxt =
commands =
printf "Setting up i2c and SPI..\n"
raspi-config nonint do_spi 0
raspi-config nonint do_i2c 0
raspi-config nonint do_i2c 0
49 changes: 15 additions & 34 deletions library/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,30 @@
import pytest


class MockSMBus():
def __init__(self, bus):
self.regs = {
0x48: [0 for _ in range(255)], # ads1015,
0x54: [0 for _ in range(255)] # sn3218
}
self.regs[0x48][0] = (1 << 11) - 1 # 12-bit ADC, 12th bit is sign

# Set the uppermost bit of the ADS1015 CONFIG register
# to indicate an "inactive/start" status
self.regs[0x48][1] = 0b10000000

def read_i2c_block_data(self, address, register, length=2):
if (address, register) == (0x48, 0):
value = self.regs[address][register:register+(length // 2)]
return [(value[0] >> 4) & 0xff, (value[0] & 0x0f) << 4]
return self.regs[address][register:register+length]

def write_i2c_block_data(self, address, register, data):
if (address, register) == (0x48, 1):
return 0
self.regs[address][register:register+len(data)] = data
@pytest.fixture(scope='function')
def automationhat():
import automationhat
yield automationhat
del sys.modules['automationhat']


@pytest.fixture(scope='function')
def smbus_notimeout():
sys.modules['smbus'] = mock.Mock()
sys.modules['smbus'].SMBus = MockSMBus
yield
del sys.modules['smbus']
def ads1015():
sys.modules['ads1015'] = mock.MagicMock()
yield sys.modules['ads1015']
del sys.modules['ads1015']


@pytest.fixture(scope='function')
def smbus_timeout():
sys.modules['smbus'] = mock.Mock()
sys.modules['smbus'].SMBus = MockSMBus
yield
del sys.modules['smbus']
def sn3218():
sys.modules['sn3218'] = mock.MagicMock()
yield sys.modules['sn3218']
del sys.modules['sn3218']


@pytest.fixture(scope='function')
def mocksmbus():
sys.modules['smbus'] = mock.Mock()
def smbus():
sys.modules['smbus'] = mock.MagicMock()
yield sys.modules['smbus']
del sys.modules['smbus']

Expand Down
20 changes: 11 additions & 9 deletions library/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
import sys


def test_setup(gpio, smbus_notimeout):
import automationhat
def test_setup(gpio, smbus, sn3218, ads1015, automationhat):
automationhat.setup()


def test_analog(smbus_notimeout):
import automationhat
def test_analog(gpio, smbus, sn3218, ads1015, automationhat):
automationhat.setup()

ads1015.ADS1015().get_voltage.return_value = 3.3

# VCC = 3.3, GAIN = 4.096, FS = 2.027, Max Voltage = 25.85
# output ~= ((1 << 11) - 1) / 2047.0 * 2096.0 / 3300.0 * 25.85
assert round(automationhat.analog.one.read(), 1) == 32.1
assert round(automationhat.analog.two.read(), 1) == 32.1
assert round(automationhat.analog.three.read(), 1) == 32.1
assert round(automationhat.analog.one.read(), 2) == 25.85
assert round(automationhat.analog.two.read(), 2) == 25.85
assert round(automationhat.analog.three.read(), 2) == 25.85

values = automationhat.analog.read()
for k, v in values.items():
values[k] = round(v, 1)
assert values == {'one': 32.1, 'two': 32.1, 'three': 32.1, 'four': 4.1}
values[k] = round(v, 2)
assert values == {'one': 25.85, 'two': 25.85, 'three': 25.85, 'four': 3.3}

0 comments on commit d8c2df5

Please sign in to comment.