Ini File Equivalents

This section lists ini file commands with the Python code that does the same or similar thing. It will illustrate how to do common operations.
On the left are example ini file commands, and the equivalent Python is on the right. Keep in mind that Python is much more flexible than the ini commands. Where an ini command required a numerical constant, Python will allow any expression. Python has much more flow control, functions with parameters, user-defined classes and so on.

FIELD_WR= OUTPUT_FORMAT_TEST, 256

FIELD_WR= SEQ_CAP_MODE, VIDEO, 1

reg.OUTPUT_FORMAT_TEST = 256

reg.SEQ_CAP_MODE.VIDEO = 1

This is the recommended way to write a register, variable or bitfield.

 


FIELD_WR=CAM1_AWB_CCM_L_0, 0x0180
FIELD_WR=CAM1_AWB_CCM_L_1, 0xFF7A
FIELD_WR=CAM1_AWB_CCM_L_2, 0x0018

reg. CAM1_AWB_CCM_L_0 = 1.5
reg. CAM1_AWB_CCM_L_1 = -0.523
reg. CAM1_AWB_CCM_L_2 = 0.094

If the data type is defined, you can use floating point values and Python will translate to the right value accordingly. This example has fixed8 (signed with 8 fraction bits) CCM settings.

 


REG= 0x3084, 0x2409 (no register page)

REG= 1, 0x08, 0x0158 (with register page)

reg.reg(0x3084).value = 0x2409

reg.reg(1, 0x08).value = 0x0158

On older sensors that have multiple pages of 8-bit addressed registers and an ADDR_SPACE_SEL register, the reg.reg() method takes two parameters. On newer sensors with a 16-bit register address space reg.reg() takes one parameter. It is an error to use the wrong number of parameters.

 


BITFIELD= 0x001A, 0x0200, 1

BITFIELD= 0, 0x23, 0x100, 1

reg.reg(0x001A).bitfield(0x0200).value = 1

reg.reg(0, 0x23).bitfield(0x100).value = 1

Same as REG= but use the bitfield() method to construct a Bitfield object.

 


VAR= 18, 0x126, 1

reg.var(18, 0x126).value = 1

Note that the size of the variable is determined by the sensor data file. If the variable is not defined then an optional size in bits can be passed as the third argument. It defaults to 16 bits wide.

 


VAR8=15, 0x0C, 0

reg.var(15, 0x0C, 8).value = 0

VAR8 forces an 8-bit data transaction. The Python is not quite equivalent because if the variable is defined in the sensor data file, Python will use the data size in the sensor data file and ignore the data size parameter.

 


SFR= 0x1078, 0xFFFF

reg.sfr(0x1078).value = 0xFFFF

This writes to MCU memory using the physical, rather than logical, addressing. SFR8 is analogous to VAR8. If the SOC supports multiple physical regions, then the physical region must be the first parameter. After the address you can specify an optional size in bits. The default is 16 bits.

 


REG_BURST= 0x990, 0x3C3C, 0x3C3C, 0x3C5F, 0x4F30, 0xED08, 0xBD61, 0xD5CE, 0x4CD

reg.reg(0x990).value = [0x3C3C, 0x3C3C, 0x3C5F, 0x4F30, 0xED08, 0xBD61, 0xD5CE, 0x4CD]

or (if 0x990 is MCU_DATA_0)

reg.MCU_DATA_0 = [0x3C3C, 0x3C3C, 0x3C5F, 0x4F30, 0xED08, 0xBD61, 0xD5CE, 0x4CD]

If you set a register to a list of values Python does a burst write. This only works for simple registers and SFR (physically addressed) memory.

 


SERIAL_REG= 0x64, 0x25, 0x18, 8:16

midlib.Register(ship_addr=0x64, addr=0x25, addr_size=8, data_size=16).value = 0x18

or (since this example is on the DEMO2)

demo2.SP_CONTROL = 0x18

The SERIAL_REG command is for I2C write to any arbitrary device address with specified register address size and data size. There is equivalent Python syntax by explicitly calling the midlib.Register constructor, but there may be a better way.
If there is a chip data file loaded for the chip, you can access the register by its name through the RegisterSet object for the chip

chipreg = midlib.Camera(0).chip(1).reg
chipreg.CONTROL_REG = 0x123

If the register is on the DEMO2 or MIDES FPGA you can use the predefined demo2 or mides RegisterSet variables. If there is no chip data file (or even if there is), you can create a Python variable for the Register object and use that.

ctrl_reg = midlib.Register(ship_addr=0x38, addr=0x04, addr_size=8, data_size=16)
ctrl_reg.value = 0x18

You can also do bitfield writes in Python using the bitfield() method of the Register class, or by named bitfield.

 


DELAY= 50

midlib.delay(50)

Delay in milliseconds.

 


STATE= Display Zoom Percent, 50

devware.setstate('Display Zoom Percent', 50)

Set a DevWare internal variable. You can also set so-called DevWare Option variables with devware.setoption().

 


LOAD= AWB Settings
LOAD= mysettings.ini, New CCM

devware.load_preset('AWB Settings')
devware.load_preset('mysettings.ini', 'New CCM')

If no filename is given, the devware.load_preset() method will load from the currently executing ini file, or if that can't be determined, the main ini file. Also consider defining a function entirely in Python instead of using an ini preset.

 


POLL_FIELD= SEQ_CMD, !=0, DELAY=10, TIMEOUT=50

timeout = 0
while (reg.SEQ_CMD.uncached_value != 0 and timeout < 50):
midlib.delay(10)
timeout += 1

Python, of course, has looping and flow control constructs far beyond what you can do in an ini file. If you do a lot of similar POLLs, you could write a function to make your code more compact.

 


POLL_REG= 0x3F, 0xFFFF, >8, DELAY=50, TIMEOUT=5

timeout = 0
while (reg.reg(0x3F).uncached_value > 8 and timeout < 5):
midlib.delay(50)
timeout += 1

POLL_VAR similarly, but with reg.var().

 


IF_FIELD= SHUTTER_WIDTH_LIM_AE, >0x100, LOAD=Night mode, ELSELOAD=Day mode

if (reg.SHUTTER_WIDTH_LIM_AE.value > 0x100):
night_mode()
else:
day_mode()

The ini file syntax required the if and else blocks to be in presets. In Python they can be inline, or in functions. Also there is much more flexibility in expressing the condition in Python.

 


IF_REG= 2, 0x37, 0xFFFF,== 0x100, LOAD=Night mode, ELSELOAD=Day mode

if (reg.reg(2, 0x37).value == 0x100):
night_mode()
else:
day_mode()

 

 


ERROR_IF= COMMAND_REGISTER, HOST_COMMAND, !=0, "Command failed"

assert reg.COMMAND_REGISTER. HOST_COMMAND.value == 0, 'Command failed'

The error message will go to the Python Console window.

 


FAR1= 0, 0x3044, 0x0020, 1

midlib.Register(addr_type='FAR1', addr=0x3044).bitfield(0x0020).value = 1

Similarly for FAR2.
To access a FAR register by name use
midlib.Register(symbol='FAR1.DARK_CONTROL').value
Unfortunately, the pre-existing practice of using a dot in the names of FAR registers currently precludes using the reg.REGNAME syntax.

 


DATATYPE= HUE_ANGLE, signed, RANGE=-22:22

reg.HUE_ANGLE.datatype = signed
reg.HUE_ANGLE.minimum = -22
reg.HUE_ANGLE.maximum = 22

Datatypes are defined in the sensor data file now, but you can also set or change datatype, minimum, and maximum within Python if needed.

 


LOG= "// White balance settings"

devware.log('// White balance settings')

This works exactly the same as the ini command.

 


ICON= blue star

#icon = blue star

This is only interpreted by the User Toolbar to set a button icon. In Python use the same syntax but in a comment. There must be no space between # and icon.

 


IMAGE= 640, 480, YCBCR

devware.image(640, 480, 'YCBCR')

This works exactly the same as the ini command.

 


LOAD_PROM= 0xA8, PGA

midlib.Camera(0).load_prom(0xA8, 'PGA')

Midlib load_prom() cannot load software settings (SWCCM, SWSTEREO, etc.).

 


SAVE_IMAGE= capture.raw

ret, img = midlib.Camera(0).grab_frame()
savedir = devware.getoption_str('Capture File')
savedir = os.path.dirname(savedir)
savename = os.path.join(savedir,'capture.raw')
f = open(savename, 'wb')
f.write(img)
f.close()

SAVE_IMAGE by default saves in the DevWare image capture directory. The example code above shows how to get the directory using getoption_str().

 


SAVE_REGS= regdump.txt

savedir = devware.getoption_str('Capture File')
savedir = os.path.dirname(savedir)
savename = os.path.join(savedir,'regdump.txt')
f = open(savename, 'wt')
print('[Saved Registers]', file=f)
for rr in reg:
print('FIELD_WR=",rr.symbol,',',rr.value, file=f)
f.close()

SAVE_REGS by default saves in the DevWare image capture directory. The example code above shows how to get the directory using getoption_str(). SAVE_REGS also writes the data as ini file commands. In Python you could of course use any format you want.

 


SAVE_REGS= regdump.txt, AWB

savename = os.path.join(savedir,'regdump.txt')
f = open(savename, 'wt')
for rr in reg:
if (re.match('.*AWB', rr.symbol)):
print('FIELD_WR=',rr.symbol,',',rr.value, file=f)
f.close()

This example uses a wildcard to save only registers with AWB in the name somewhere. Any criterion can be used.

 


XMCLK= 24000000

midlib.Camera(0).MI_OUTPUT_CLOCK_FREQ = 24000000

All midlib mi_modes defined in midlib2.h are exposed as attributes on a Camera object. Some others are MI_ALLOW_FAR_ACCESS, MI_PIXCLK_POLARITY, MI_SENSOR_POWER, MI_SENSOR_RESET, MI_SHIP_SPEED, MI_DIRECT_VAR_ACCESS.

Â