|
| 1 | +import unittest |
| 2 | + |
| 3 | +from mpos import App, PackageManager |
| 4 | + |
| 5 | +from camera import Camera, GrabMode, PixelFormat, FrameSize, GainCeiling |
| 6 | + |
| 7 | +class TestCompareVersions(unittest.TestCase): |
| 8 | + |
| 9 | + def init_cam(self): |
| 10 | + try: |
| 11 | + cam = Camera(data_pins=[12,13,15,11,14,10,7,2],vsync_pin=6,href_pin=4,sda_pin=21,scl_pin=16,pclk_pin=9,xclk_pin=8,xclk_freq=20000000,pixel_format=PixelFormat.RGB565,powerdown_pin=-1,reset_pin=-1,frame_size=FrameSize.R240X240,grab_mode=GrabMode.LATEST) |
| 12 | + return cam |
| 13 | + except Exception as e: |
| 14 | + #self.assertTrue(False, f"camera init received exception: {e}") |
| 15 | + print(f"camera init received exception: {e}") |
| 16 | + return None |
| 17 | + |
| 18 | + def test_init_capture_deinit(self): |
| 19 | + cam = self.init_cam() |
| 20 | + self.assertTrue(cam is not None, "camera failed to initialize") |
| 21 | + self.assertEqual(cam.get_pixel_height(), 240, "wrong pixel height") |
| 22 | + self.assertEqual(cam.get_pixel_width(), 240, "wrong pixel width") |
| 23 | + memview = cam.capture() |
| 24 | + self.assertEqual(len(memview), 2 * 240 * 240, "capture size does not match expectations") |
| 25 | + cam.deinit() |
| 26 | + |
| 27 | + def disabled_test_multiple_runs(self): |
| 28 | + for _ in range(10): |
| 29 | + self.test_init_capture_deinit() |
| 30 | + |
| 31 | + def disabled_test_init_capture_deinit_poweroff(self): |
| 32 | + self.test_init_capture_deinit() |
| 33 | + from machine import Pin, I2C |
| 34 | + i2c = I2C(1, scl=Pin(16), sda=Pin(21), freq=100000) # Adjust pins and frequency |
| 35 | + devices = i2c.scan() |
| 36 | + print([hex(addr) for addr in devices]) # finds it on 60 = 0x3C after init |
| 37 | + camera_addr = 0x3C # for OV5640 |
| 38 | + reg_addr = 0x3008 |
| 39 | + reg_high = (reg_addr >> 8) & 0xFF # 0x30 |
| 40 | + reg_low = reg_addr & 0xFF # 0x08 |
| 41 | + power_off_command = 0x40 # Power off command Bit[6]: Software power down |
| 42 | + #i2c.writeto(camera_addr, bytes([reg_high, reg_low, power_off_command])) |
| 43 | + print("\nSecond capture will fail!") |
| 44 | + self.test_init_capture_deinit() |
| 45 | + |
| 46 | + def test_init_twice_capture_deinit_poweroff(self): |
| 47 | + self.test_init_capture_deinit() |
| 48 | + from machine import Pin, I2C |
| 49 | + i2c = I2C(1, scl=Pin(16), sda=Pin(21), freq=100000) # Adjust pins and frequency |
| 50 | + devices = i2c.scan() |
| 51 | + print([hex(addr) for addr in devices]) # finds it on 60 = 0x3C after init |
| 52 | + camera_addr = 0x3C # for OV5640 |
| 53 | + reg_addr = 0x3008 |
| 54 | + reg_high = (reg_addr >> 8) & 0xFF # 0x30 |
| 55 | + reg_low = reg_addr & 0xFF # 0x08 |
| 56 | + power_off_command = 0x40 # Power off command Bit[6]: Software power down |
| 57 | + #i2c.writeto(camera_addr, bytes([reg_high, reg_low, power_off_command])) |
| 58 | + cam = self.init_cam() |
| 59 | + self.assertTrue(cam is None, "expected camera to fail after i2c") |
| 60 | + print("\nSecond capture should now work!") |
| 61 | + self.test_init_capture_deinit() |
| 62 | + |
0 commit comments