-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove adc.py, add io.py, update modules as necessary.
- Loading branch information
1 parent
5ba5167
commit c451c42
Showing
12 changed files
with
122 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.