Skip to content

Commit

Permalink
add tests and format
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Sep 9, 2024
1 parent 0da029d commit d100bb5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/krux/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Camera:
"""Camera is a singleton interface for interacting with the device's camera"""

def __init__(self):
self.initialized = False
self.cam_id = None
self.antiglare_enabled = False
self.mode = None
Expand All @@ -51,9 +50,7 @@ def __init__(self):

def initialize_sensor(self, grayscale=False):
"""Initializes the camera"""
self.initialized = False
self.antiglare_enabled = False
self.mode = COLOR_MODE
if self.cam_id in (OV7740_ID, GC2145_ID):
sensor.reset(freq=18200000)
if board.config["type"] == "cube":
Expand All @@ -63,6 +60,7 @@ def initialize_sensor(self, grayscale=False):
else:
sensor.reset()
self.cam_id = sensor.get_id()
self.mode = COLOR_MODE
if grayscale and self.cam_id != GC2145_ID:
# GC2145 does not support grayscale
sensor.set_pixformat(sensor.GRAYSCALE)
Expand Down
2 changes: 1 addition & 1 deletion src/krux/pages/qr_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def anti_glare_control(self):
(self.ctx.display.height() - FONT_HEIGHT) // 2,
self.ctx.display.width(),
FONT_HEIGHT,
theme.bg_color
theme.bg_color,
)
self.ctx.display.to_landscape()
self.ctx.input.reset_ios_state()
Expand Down
61 changes: 61 additions & 0 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,67 @@ def test_initialize_sensors(mocker, m5stickv):
)


def test_fail_to_initialize_sensor(mocker, m5stickv):
from krux.camera import Camera

# Mock sensor.reset to raise an exception
mocker.patch("sensor.reset", side_effect=Exception)
c = Camera()
with pytest.raises(Exception):
c.initialize_sensor()
assert c.mode == None


def test_initialize_run_no_sensor(mocker, m5stickv):
from krux.camera import Camera

mocker.patch("sensor.reset", side_effect=Exception)
c = Camera()
try:
# Fails to initialize at boot
c.initialize_sensor()
except:
pass
with pytest.raises(ValueError, match="No camera found"):
c.initialize_run()
assert c.mode == None


def test_initialize_run(mocker, m5stickv):
from krux.camera import Camera, COLOR_MODE

c = Camera()
c.initialize_sensor()
c.initialize_run()
assert c.mode == COLOR_MODE
assert c.cam_id is not None
assert c.antiglare_enabled == False


def test_initialize_run_from_grayscale(mocker, m5stickv):
from krux.camera import Camera, COLOR_MODE, GRAYSCALE_MODE

c = Camera()
c.initialize_sensor()
c.mode = GRAYSCALE_MODE
c.initialize_run()
assert c.mode == COLOR_MODE
assert c.cam_id is not None
assert c.antiglare_enabled == False


def test_initialize_run_with_anti_glair_enabled(mocker, m5stickv):
from krux.camera import Camera, COLOR_MODE, GRAYSCALE_MODE

c = Camera()
c.initialize_sensor()
c.antiglare_enabled = True
c.initialize_run()
assert c.mode == COLOR_MODE
assert c.cam_id is not None
assert c.antiglare_enabled == False


def test_toggle_antiglare(mocker, m5stickv):
import krux
from krux.camera import Camera, OV7740_ID, OV2640_ID, GC0328_ID, GC2145_ID
Expand Down

0 comments on commit d100bb5

Please sign in to comment.