Skip to content

Commit

Permalink
Adding support for camera rotation as a command-line flag
Browse files Browse the repository at this point in the history
I just repositioned my camera and the only way to do it was upside down, so
I realized this is probably a useful feature.
  • Loading branch information
mtlynch committed Apr 20, 2017
1 parent e6e484e commit 81ef42d
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions greenpithumb/greenpithumb.py
Expand Up @@ -31,7 +31,7 @@

def make_sensor_pollers(poll_interval, wiring_config, moisture_threshold,
record_queue, sleep_windows, raspberry_pi_io,
photo_interval, image_path):
photo_interval, image_path, camera):
logger.info('creating sensor pollers (poll interval=%ds")',
poll_interval.total_seconds())
utc_clock = clock.Clock()
Expand Down Expand Up @@ -80,11 +80,9 @@ def make_sensor_pollers(poll_interval, wiring_config, moisture_threshold,
adc, wiring_config.adc_channels.light_sensor)),
camera_poller_factory.create_camera_poller(
camera_manager.CameraManager(
image_path,
utc_clock,
picamera.PiCamera(resolution=picamera.PiCamera.MAX_RESOLUTION)),
light_sensor.LightSensor(adc,
wiring_config.adc_channels.light_sensor))
image_path, utc_clock, camera,
light_sensor.LightSensor(
adc, wiring_config.adc_channels.light_sensor)))
]


Expand All @@ -104,6 +102,20 @@ def create_record_processor(db_connection, record_queue):
db_store.WateringEventStore(db_connection))


def create_camera(rotation):
"""Creates a new Camera instance with the given camera settings.
Args:
rotation: The amount (in whole degrees) to rotate the camera image.
Returns:
A Camera instance with the given camera settings.
"""
camera = picamera.PiCamera(resolution=picamera.PiCamera.MAX_RESOLUTION)
camera.rotation = rotation
return camera


def configure_logging(verbose):
"""Configure the root logger for log output."""
root_logger = logging.getLogger()
Expand All @@ -128,9 +140,11 @@ def main(args):
raspberry_pi_io = pi_io.IO(GPIO)
poll_interval = datetime.timedelta(minutes=args.poll_interval)
photo_interval = datetime.timedelta(minutes=args.photo_interval)
pollers = make_sensor_pollers(
poll_interval, wiring_config, args.moisture_threshold, record_queue,
parsed_windows, raspberry_pi_io, photo_interval, args.image_path)
camera = create_camera(args.camera_rotation)
pollers = make_sensor_pollers(poll_interval, wiring_config,
args.moisture_threshold, record_queue,
parsed_windows, raspberry_pi_io,
photo_interval, args.image_path, camera)
with contextlib.closing(db_store.open_or_create_db(
args.db_file)) as db_connection:
record_processor = create_record_processor(db_connection, record_queue)
Expand Down Expand Up @@ -196,6 +210,11 @@ def main(args):
help=('Moisture threshold to start pump. The pump will turn on if the '
'moisture level drops below this level'),
default=900)
parser.add_argument(
'--camera_rotation',
type=int,
choices=(0, 90, 180, 270),
help='Specifies the amount to rotate the camera\'s image.')
parser.add_argument(
'-v', '--verbose', action='store_true', help='Use verbose logging')
main(parser.parse_args())

0 comments on commit 81ef42d

Please sign in to comment.