Skip to content

Commit

Permalink
Remove adc.py, add io.py, update modules as necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeetShetty committed Dec 13, 2016
1 parent 5ba5167 commit c451c42
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 168 deletions.
79 changes: 0 additions & 79 deletions greenpithumb/adc.py

This file was deleted.

55 changes: 55 additions & 0 deletions greenpithumb/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#import RPi.GPIO as GPIO

# TODO(mtlynch): Move these pin constants to a configuration file so they're not
# hardcoded in.

# Raspberry Pi pin to which each component is connected
PIN_PUMP = 0

# Signal to turn a Raspberry Pi pin off.
SIGNAL_OFF = 0

# Signal to turn a Raspberry Pi pin on.
SIGNAL_ON = 1


class Error(Exception):
pass


class WriteError(Error):
pass


class IO(object):
"""Wrapper for input and output on a Raspberry Pi board.
This wraps the board and allows the caller to read or send signals to
the Raspberry Pi's channels.
"""

def __init__(self, placeholder_gpio):
"""Creates a new input/output wrapper.
Args:
placeholder_gpio: Placeholder for RPi.GPIO.
"""
self.GPIO = placeholder_gpio

def write_pin(self, channel, value):
"""Writes a value to a Raspberry Pi channel.
Args:
channel: Index of Raspberry Pi channel to write to.
value: Value to write to the pin (must be either 0 or 1023).
Raises:
WriteError: The write value is invalid.
"""
if not (value == SIGNAL_OFF or value == SIGNAL_ON):
raise WriteError(
'Write value of %s is invalid. Must be either %s or %s' %
(value, SIGNAL_OFF, SIGNAL_ON))
self.GPIO.setup(channel, self.GPIO.OUT)
self.GPIO.output(channel, value)
self.GPIO.cleanup(channel)
10 changes: 5 additions & 5 deletions greenpithumb/light_sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import adc

_LIGHT_SENSOR_MIN_VALUE = 290
_LIGHT_SENSOR_MAX_VALUE = adc.PIN_MAX_VALUE
_LIGHT_SENSOR_MAX_VALUE = 1023


class Error(Exception):
Expand All @@ -15,14 +13,16 @@ class LightSensorLowError(Error):
class LightSensor(object):
"""Wrapper for light sensor."""

def __init__(self, adc):
def __init__(self, adc, pin):
"""Creates a new LightSensor wrapper.
Args:
adc: ADC(analog to digital) interface to receive
analog signals from light sensor.
pin: ADC pin to which the light sensor is connected.
"""
self._adc = adc
self._pin = pin

def ambient_light(self):
"""Returns light level as percentage.
Expand All @@ -31,7 +31,7 @@ def ambient_light(self):
LightSensorLowError: Ambient light level is lower than the minimum
expected value.
"""
ambient_light = self._adc.read_pin(adc.PIN_LIGHT_SENSOR)
ambient_light = self._adc.read_adc(self._pin)

if ambient_light < _LIGHT_SENSOR_MIN_VALUE:
raise LightSensorLowError(
Expand Down
9 changes: 4 additions & 5 deletions greenpithumb/moisture_sensor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import adc


class MoistureSensor(object):
"""Wrapper for DHT11 moisture sensor."""

def __init__(self, adc):
def __init__(self, adc, pin):
"""Creates a new MoistureSensor wrapper.
Args:
adc: ADC(analog to digital) interface to receive analog signals from
moisture sensor.
pin: ADC pin to which the moisture sensor is connected.
"""
self._adc = adc
self._pin = pin

def moisture(self):
"""Returns moisture level."""
return self._adc.read_pin(adc.PIN_MOISTURE_SENSOR)
return self._adc.read_adc(self._pin)
13 changes: 6 additions & 7 deletions greenpithumb/pump.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adc
import io

# Pump rate in mL/s (4.3 L/min)
_PUMP_RATE_ML_PER_SEC = 4300.0 / 60.0
Expand All @@ -7,15 +7,14 @@
class Pump(object):
"""Wrapper for a Seaflo 12V water pump."""

def __init__(self, adc, clock):
def __init__(self, io, clock):
"""Creates a new Pump wrapper.
Args:
adc: ADC (analog to digital) interface to send digital signals to
pump.
io: Raspberry Pi I/O interface.
clock: A clock interface.
"""
self._adc = adc
self._io = io
self._clock = clock

def pump_water(self, amount_ml):
Expand All @@ -32,11 +31,11 @@ def pump_water(self, amount_ml):
elif amount_ml < 0.0:
raise ValueError('Cannot pump a negative amount of water')
else:
self._adc.write_pin(adc.PIN_PUMP, adc.SIGNAL_ON)
self._io.write_pin(io.PIN_PUMP, io.SIGNAL_ON)

wait_time_seconds = amount_ml / _PUMP_RATE_ML_PER_SEC
self._clock.wait(wait_time_seconds)

self._adc.write_pin(adc.PIN_PUMP, adc.SIGNAL_OFF)
self._io.write_pin(io.PIN_PUMP, io.SIGNAL_OFF)

return
9 changes: 4 additions & 5 deletions greenpithumb/weight_sensor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import adc


class WeightSensor(object):
"""Wrapper for a weight sensor."""

def __init__(self, adc):
def __init__(self, adc, pin):
"""Creates a new WeightSensor wrapper.
Args:
adc: ADC(analog to digital) interface to receive analog signals
from weight sensor.
pin: ADC pin to which the weight sensor is connected.
"""
self._adc = adc
self._pin = pin

def weight(self):
"""Returns weight in grams."""
weight = self._adc.read_pin(adc.PIN_WEIGHT_SENSOR)
weight = self._adc.read_adc(self._pin)

# TODO(JeetShetty): Implement the actual conversion from weight to
# weight_grams
Expand Down
44 changes: 0 additions & 44 deletions tests/test_adc.py

This file was deleted.

24 changes: 24 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import absolute_import
import unittest

import mock

from greenpithumb import io


class IOTest(unittest.TestCase):

def setUp(self):
self.mock_GPIO = mock.Mock()
self.IO = io.IO(self.mock_GPIO)
self.channel = 1

def test_invalid_write_value_raises_write_error(self):
with self.assertRaises(io.WriteError):
write_value = 2
self.IO.write_pin(self.channel, write_value)

def test_write_value_in_range(self):
write_value = 1
self.IO.write_pin(self.channel, write_value)
self.mock_GPIO.output.assert_called_with(self.channel, write_value)
10 changes: 5 additions & 5 deletions tests/test_light_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

import mock

from greenpithumb import adc
from greenpithumb import light_sensor


class LightSensorTest(unittest.TestCase):

def setUp(self):
self.mock_adc = mock.Mock(spec=adc.Adc)
self.light_sensor = light_sensor.LightSensor(self.mock_adc)
self.mock_adc = mock.Mock()
pin = 1
self.light_sensor = light_sensor.LightSensor(self.mock_adc, pin)

def test_light_50_pct(self):
"""Near midpoint light sensor value should return near 50."""
self.mock_adc.read_pin.return_value = 656
self.mock_adc.read_adc.return_value = 656
ambient_light = self.light_sensor.ambient_light()
self.assertAlmostEqual(ambient_light, 49.93178718)

def test_ambient_light_too_low(self):
"""Light sensor value less than min should raise a ValueError."""
with self.assertRaises(light_sensor.LightSensorLowError):
self.mock_adc.read_pin.return_value = (
self.mock_adc.read_adc.return_value = (
light_sensor._LIGHT_SENSOR_MIN_VALUE - 1)
self.light_sensor.ambient_light()
9 changes: 5 additions & 4 deletions tests/test_moisture_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

import mock

from greenpithumb import adc
from greenpithumb import moisture_sensor


class MoistureSensorTest(unittest.TestCase):

def setUp(self):
self.mock_adc = mock.Mock(spec=adc.Adc)
self.moisture_sensor = moisture_sensor.MoistureSensor(self.mock_adc)
self.mock_adc = mock.Mock()
pin = 1
self.moisture_sensor = moisture_sensor.MoistureSensor(self.mock_adc,
pin)

def test_moisture_50(self):
"""Moisture level read should be moisture level returned."""
self.mock_adc.read_pin.return_value = 50
self.mock_adc.read_adc.return_value = 50
moisture_level = self.moisture_sensor.moisture()
self.assertEqual(moisture_level, 50)
Loading

0 comments on commit c451c42

Please sign in to comment.