def __init__(self, nysa, urn, debug=False): super(SFCamera, self).__init__(nysa, urn, debug) #print "Camera URN : %s" % urn i2c_urn = self.get_integration_references(self.urn)[0] #print "i2c: URN: %s" % i2c_urn self.i2c = i2c.I2C(nysa, i2c_urn, debug=debug) self.i2c.enable_i2c(True) self.i2c.enable_interrupt(True) self.i2c.get_status() self.i2c.set_speed_to_100khz() self.reset_camera() self.set_rgb_mode() self.reset_counts() time.sleep(.4) row_count = self.read_row_count() pixel_count = self.read_pixel_count() size = row_count * pixel_count self.width = pixel_count / 2 self.height = row_count self.status = 0 try: self.dma_reader = DMAReadController( device=self, mem_base0=0x00000000, mem_base1=0x00100000, size=size / 4, reg_status=STATUS, reg_base0=MEM_0_BASE, reg_size0=MEM_0_SIZE, reg_base1=MEM_1_BASE, reg_size1=MEM_1_SIZE, timeout=3, finished0=STATUS_MEMORY_0_FINISHED, finished1=STATUS_MEMORY_1_FINISHED, empty0=STATUS_MEMORY_0_EMPTY, empty1=STATUS_MEMORY_1_EMPTY) except NysaDMAException as ex: raise SFCameraError("Error initializing the DMA Reader: %s" % str(ex))
def __init__(self, nysa, urn, debug = False): super(SFCamera, self).__init__(nysa, urn, debug) #print "Camera URN : %s" % urn i2c_urn = self.get_integration_references(self.urn)[0] #print "i2c: URN: %s" % i2c_urn self.i2c = i2c.I2C(nysa, i2c_urn, debug = debug) self.i2c.enable_i2c(True) self.i2c.enable_interrupt(True) self.i2c.get_status() self.i2c.set_speed_to_100khz() self.reset_camera() self.set_rgb_mode() self.reset_counts() time.sleep(.4) row_count = self.read_row_count() pixel_count = self.read_pixel_count() size = row_count * pixel_count self.width = pixel_count / 2 self.height = row_count self.status = 0 try: self.dma_reader = DMAReadController(device = self, mem_base0 = 0x00000000, mem_base1 = 0x00100000, size = size / 4, reg_status = STATUS, reg_base0 = MEM_0_BASE, reg_size0 = MEM_0_SIZE, reg_base1 = MEM_1_BASE, reg_size1 = MEM_1_SIZE, timeout = 3, finished0 = STATUS_MEMORY_0_FINISHED, finished1 = STATUS_MEMORY_1_FINISHED, empty0 = STATUS_MEMORY_0_EMPTY, empty1 = STATUS_MEMORY_1_EMPTY) except NysaDMAException as ex: raise SFCameraError("Error initializing the DMA Reader: %s" % str(ex))
class SFCamera(driver.Driver): @staticmethod def get_abi_class(): return 0 @staticmethod def get_abi_major(): return driver.get_device_id_from_name("camera") @staticmethod def get_abi_minor(): return SPARKFUN_640_480_CAMERA_MODULE def __init__(self, nysa, urn, debug=False): super(SFCamera, self).__init__(nysa, urn, debug) #print "Camera URN : %s" % urn i2c_urn = self.get_integration_references(self.urn)[0] #print "i2c: URN: %s" % i2c_urn self.i2c = i2c.I2C(nysa, i2c_urn, debug=debug) self.i2c.enable_i2c(True) self.i2c.enable_interrupt(True) self.i2c.get_status() self.i2c.set_speed_to_100khz() self.reset_camera() self.set_rgb_mode() self.reset_counts() time.sleep(.4) row_count = self.read_row_count() pixel_count = self.read_pixel_count() size = row_count * pixel_count self.width = pixel_count / 2 self.height = row_count self.status = 0 try: self.dma_reader = DMAReadController( device=self, mem_base0=0x00000000, mem_base1=0x00100000, size=size / 4, reg_status=STATUS, reg_base0=MEM_0_BASE, reg_size0=MEM_0_SIZE, reg_base1=MEM_1_BASE, reg_size1=MEM_1_SIZE, timeout=3, finished0=STATUS_MEMORY_0_FINISHED, finished1=STATUS_MEMORY_1_FINISHED, empty0=STATUS_MEMORY_0_EMPTY, empty1=STATUS_MEMORY_1_EMPTY) except NysaDMAException as ex: raise SFCameraError("Error initializing the DMA Reader: %s" % str(ex)) def __del__(self): self.i2c.enable_i2c(False) def get_width(self): return self.width def get_height(self): return self.height def stop_async_reader(self): self.dma_reader.disable_asynchronous_read() def start_async_reader(self, callback): self.dma_reader.enable_asynchronous_read(callback) def get_control(self): """get_control Read the control register Args: Nothing Return: 32-bit control register value Raises: NysaCommError: Error in communication """ return self.read_register(CONTROL) def set_control(self, control): """set_control Write the control register Args: control: 32-bit control value Returns: Nothing Raises: NysaCommError: Error in communication """ self.write_register(CONTROL, control) def get_status(self): """get_status read the status register Args: Nothing Return: 32-bit status register value Raises: NysaCommError: Error in communication """ return self.read_register(STATUS) def enable_camera(self, enable): """ Enable the camera core Args: enable(boolean): True/False to enable the camera and core Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_ENABLE, enable) self.enable_register_bit(CONTROL, CONTROL_INTERRUPT_ENABLE, enable) def read_row_count(self): """ Read the number of rows from the camera Args: Nothing Returns (int) Number of rows in the image Raises: NysaCommError """ return self.read_register(ROW_COUNT) def read_pixel_count(self): """ Reads the number of pixels in a row Args: Nothing Returns (32-bit Integer): Number of pixels in a row Raises: NysaCommError """ return self.read_register(PIXEL_COUNT) def reset_counts(self): """ Resets the row and pixel counts so the core will re-read the values form an image Args: Nothing Returns: Nothing Raises: NysaCommError """ self.set_register_bit(CONTROL, 5) self.clear_register_bit(CONTROL, 5) def auto_led_mode(self, enable): """ Allow core to enable and disable the 'Flash' LED Args: enable(boolean): True: Allow core to control LED False: Do not allow core to control LED Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_AUTO_FLASH_EN, enable) def manual_flash_on(self, enable): """ When Auto mode is not enabled this will turn on the 'Flash' Args: enable(boolean): True: Turn on th flash False: Turn off the flash Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_MANUAL_FLASH_ON) def reset_camera(self): """ Resets the camera core (sends reset to the camera as well) Args: Nothing Return: Nothing Raises: NysaCommError """ self.i2c.reset_i2c_core() self.clear_register_bit(CONTROL, CONTROL_CAMERA_RESET) time.sleep(.2) self.set_register_bit(CONTROL, CONTROL_CAMERA_RESET) time.sleep(.5) def enable_image_debug(self, enable): """ Enable a debug image to be sent to the host from core (Exercise memroy controller) Args: enable (boolean): True: Send the debug image to the host False: Set the camera to normal mode Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_IMAGE_DEBUG, enable) def get_config_data(self): config_dict = {} img_config = self.i2c.read_from_i2c(CAMERA_I2C, Array('B', [0x02]), 2) return img_config def set_rgb_mode(self, mode=0x00): """ Set the camera to output RGB data instead of YCBCR data Args: mode (32-bit unsigned integer) default = 0x00 (RGB) Returns: Nothing Raises: NysaCommError """ mode = mode << 2 mode |= 0x02 self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x40])) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [I2C_IMG_CONFIG, mode])) self.i2c.is_interrupt_enabled() def set_start_pixel_count(self, start_pixel): """ Adjust the start of the pixel count within the camera Args: start_pixel (16-bit unsigned integer): Start pixel of the image Returns: Nothing Raises: NysaCommError """ sp = Array('B') sp.append(start_pixel & 0xFF) sp.append(((start_pixel) >> 8) & 0xFF) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x1E, sp[0]])) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x1F, sp[1]])) def software_reset(self): #self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x40])) time.sleep(.5) #self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x00])) def set_debug_mode(self): #print "Setting idle mode" self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x08])) def read_raw_image(self): """ Read an entire image from the memory. The size of the image is set during initialization of the DMA Reader controller. The data is outputted This data is not formatted Args: Nothing Returns: (Array of bytes): Size of the image (row * pixel count) """ return self.dma_reader.read(anticipate=True)
class SFCamera(driver.Driver): @staticmethod def get_abi_class(): return 0 @staticmethod def get_abi_major(): return driver.get_device_id_from_name("camera") @staticmethod def get_abi_minor(): return SPARKFUN_640_480_CAMERA_MODULE def __init__(self, nysa, urn, debug = False): super(SFCamera, self).__init__(nysa, urn, debug) #print "Camera URN : %s" % urn i2c_urn = self.get_integration_references(self.urn)[0] #print "i2c: URN: %s" % i2c_urn self.i2c = i2c.I2C(nysa, i2c_urn, debug = debug) self.i2c.enable_i2c(True) self.i2c.enable_interrupt(True) self.i2c.get_status() self.i2c.set_speed_to_100khz() self.reset_camera() self.set_rgb_mode() self.reset_counts() time.sleep(.4) row_count = self.read_row_count() pixel_count = self.read_pixel_count() size = row_count * pixel_count self.width = pixel_count / 2 self.height = row_count self.status = 0 try: self.dma_reader = DMAReadController(device = self, mem_base0 = 0x00000000, mem_base1 = 0x00100000, size = size / 4, reg_status = STATUS, reg_base0 = MEM_0_BASE, reg_size0 = MEM_0_SIZE, reg_base1 = MEM_1_BASE, reg_size1 = MEM_1_SIZE, timeout = 3, finished0 = STATUS_MEMORY_0_FINISHED, finished1 = STATUS_MEMORY_1_FINISHED, empty0 = STATUS_MEMORY_0_EMPTY, empty1 = STATUS_MEMORY_1_EMPTY) except NysaDMAException as ex: raise SFCameraError("Error initializing the DMA Reader: %s" % str(ex)) def __del__(self): self.i2c.enable_i2c(False) def get_width(self): return self.width def get_height(self): return self.height def stop_async_reader(self): self.dma_reader.disable_asynchronous_read() def start_async_reader(self, callback): self.dma_reader.enable_asynchronous_read(callback) def get_control(self): """get_control Read the control register Args: Nothing Return: 32-bit control register value Raises: NysaCommError: Error in communication """ return self.read_register(CONTROL) def set_control(self, control): """set_control Write the control register Args: control: 32-bit control value Returns: Nothing Raises: NysaCommError: Error in communication """ self.write_register(CONTROL, control) def get_status(self): """get_status read the status register Args: Nothing Return: 32-bit status register value Raises: NysaCommError: Error in communication """ return self.read_register(STATUS) def enable_camera(self, enable): """ Enable the camera core Args: enable(boolean): True/False to enable the camera and core Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_ENABLE, enable) self.enable_register_bit(CONTROL, CONTROL_INTERRUPT_ENABLE, enable) def read_row_count(self): """ Read the number of rows from the camera Args: Nothing Returns (int) Number of rows in the image Raises: NysaCommError """ return self.read_register(ROW_COUNT) def read_pixel_count(self): """ Reads the number of pixels in a row Args: Nothing Returns (32-bit Integer): Number of pixels in a row Raises: NysaCommError """ return self.read_register(PIXEL_COUNT) def reset_counts(self): """ Resets the row and pixel counts so the core will re-read the values form an image Args: Nothing Returns: Nothing Raises: NysaCommError """ self.set_register_bit(CONTROL, 5) self.clear_register_bit(CONTROL, 5) def auto_led_mode(self, enable): """ Allow core to enable and disable the 'Flash' LED Args: enable(boolean): True: Allow core to control LED False: Do not allow core to control LED Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_AUTO_FLASH_EN, enable) def manual_flash_on(self, enable): """ When Auto mode is not enabled this will turn on the 'Flash' Args: enable(boolean): True: Turn on th flash False: Turn off the flash Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_MANUAL_FLASH_ON) def reset_camera(self): """ Resets the camera core (sends reset to the camera as well) Args: Nothing Return: Nothing Raises: NysaCommError """ self.i2c.reset_i2c_core() self.clear_register_bit(CONTROL, CONTROL_CAMERA_RESET) time.sleep(.2) self.set_register_bit(CONTROL, CONTROL_CAMERA_RESET) time.sleep(.5) def enable_image_debug(self, enable): """ Enable a debug image to be sent to the host from core (Exercise memroy controller) Args: enable (boolean): True: Send the debug image to the host False: Set the camera to normal mode Returns: Nothing Raises: NysaCommError """ self.enable_register_bit(CONTROL, CONTROL_IMAGE_DEBUG, enable) def get_config_data(self): config_dict = {} img_config = self.i2c.read_from_i2c(CAMERA_I2C, Array('B', [0x02]), 2) return img_config def set_rgb_mode(self, mode = 0x00): """ Set the camera to output RGB data instead of YCBCR data Args: mode (32-bit unsigned integer) default = 0x00 (RGB) Returns: Nothing Raises: NysaCommError """ mode = mode << 2 mode |= 0x02 self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x40])) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [I2C_IMG_CONFIG, mode])) self.i2c.is_interrupt_enabled() def set_start_pixel_count(self, start_pixel): """ Adjust the start of the pixel count within the camera Args: start_pixel (16-bit unsigned integer): Start pixel of the image Returns: Nothing Raises: NysaCommError """ sp = Array('B') sp.append(start_pixel & 0xFF) sp.append(((start_pixel) >> 8) & 0xFF) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x1E, sp[0]])) self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x1F, sp[1]])) def software_reset(self): #self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x40])) time.sleep(.5) #self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x00])) def set_debug_mode(self): #print "Setting idle mode" self.i2c.write_to_i2c(CAMERA_I2C, Array('B', [0x02, 0x08])) def read_raw_image(self): """ Read an entire image from the memory. The size of the image is set during initialization of the DMA Reader controller. The data is outputted This data is not formatted Args: Nothing Returns: (Array of bytes): Size of the image (row * pixel count) """ return self.dma_reader.read(anticipate = True)