1"""! This module defines all necessary functions to
4 - create a new project or open an existing project
5 - set target on which the created model is executed
6 - create a model with blocks as per desired logic
7 - add blocks to the model
8 - adjust each block parameters as required
10 - build and compile the model
11 - run the model on the target hardware
12 - and finally when every thing is done, close the CASP process
14 All functions in this module uses global object \ref _casp_process.casp_proc from \ref _casp_process.py module.
20if sys.version_info[:3] < (3,9):
21 from typing
import List
as list
23from _casp_process
import casp_proc, casp_bv_proc
24import casp_utils
as utils
25import casp_defs
as defs
26import casp_targets
as targets
27import casp_wtypes
as wtypes
28import casp_pins
as pins
31def Init(make_visible: bool=
False, casp_file: str=
"CASP-Eval"):
32 """! Initializes CASP subprocess. This is the first function to be called before using any other functions in this module.
36 visible: Set True to make CASP window visible.
38 casp_file: Set CASP executable file to be invoked. Set this argument only if the executable file version is different from 'CASP-Eval'.
40 casp_proc.init(make_visible, casp_file)
43 """! Terminates CASP subprocess and releases the resources.
48 """! Invokes BlockViewer process for user to browse CASP blocks, view block parameters, block documentation etc.
55 """! Displays available serial ports on the host. User can use this function before using the \ref ProgrammerPortSet() function.
57 response = casp_proc.write(
"casp.sys.serial.ports")
58 print(f
"CASP: {response.strip()}")
61 """! Refreshes CASP project. For further details about refreshing CASP project, please refer CASP main documentation \ref Help().
63 response = casp_proc.write(
"casp.sys.refresh")
66 """! Invokes CASP Serial Terminal window.
68 response = casp_proc.write(
"casp.sys.terminal")
71 """! Opens CASP Remote Terminal Viewer (RTV) process. For further details please refer to CASP main documentation \ref Help().
73 response = casp_proc.write(
"casp.sys.rtv")
76 """! Opens CASP Bluetooth Low Energy (BLE) process. For further details please refer to CASP main documentation \ref Help().
78 response = casp_proc.write(
"casp.sys.ble.client")
81 """! Opens CASP main documentation.
83 response = casp_proc.write(
"casp.sys.help")
86 """! Opens CASP board support package (BSP) documentation.
88 response = casp_proc.write(
"casp.sys.help.bsp")
91 """! Opens CASP Python Libraries documentation.
93 response = casp_proc.write(
"casp.sys.help.python")
96 """! Opens 'CASP User Data' directory from user's Documents folder.
98 response = casp_proc.write(
"casp.sys.dir.user_data")
99 print(f
"CASP: {response.strip()}")
102 """! Opens current CASP target support folder.
104 response = casp_proc.write(
"casp.sys.dir.target_support")
105 print(f
"CASP: {response.strip()}")
108 """! Opens current CASP project directory.
110 response = casp_proc.write(
"casp.sys.dir.project")
111 print(f
"CASP: {response.strip()}")
114 """! Opens CASP tutorials directory.
116 response = casp_proc.write(
"casp.sys.dir.tutorials")
117 print(f
"CASP: {response.strip()}")
120 """! Opens CASP examples directory.
122 response = casp_proc.write(
"casp.sys.dir.examples")
123 print(f
"CASP: {response.strip()}")
126 """! Creates new CASP project and returns the project path.
130 proj_name: Project name to be created.
134 Returns project path where the project is created.
137 response = casp_proc.write(
"casp.proj.create", [proj_name])
139 response = casp_proc.write(
"casp.proj.create")
140 strs = utils.SplitString(response.strip())
141 print(f
"CASP: {strs[0]}")
144 pro_path = str(strs[1])
148 """! Opens existing CASP project.
152 proj_full_path: Project full path (along with project file extension .prj) should be provided as argument. If no argument is provided, CASP opens most recent project created or closed.
156 Returns project path that was opened.
159 response = casp_proc.write(
"casp.proj.open", [proj_full_path])
161 response = casp_proc.write(
"casp.proj.open")
162 strs = utils.SplitString(response.strip())
163 print(f
"CASP: {strs[0]}")
166 pro_path = str(strs[1])
170 """! This function does a couple of things.
172 - It creates a temporary project along with a default workspace (.wsp) file.
173 - Loads/open the temporary project.
174 - Creates two simulation panel windows of type '2D Plotter' and 'Controls/Meters'
176 Please note that the temporary project files created using this function and further modified using other functions will be available till next CASP process start. Once CASP restarts these files will be deleted.
178 User is recommended to use this function (in place of above two functions) to quickly create a project and start working on it.
182 Returns project path that was opened.
184 response = casp_proc.write(
"casp.proj.create")
185 strs = utils.SplitString(response.strip())
186 print(f
"CASP: {strs[0]}")
187 response = casp_proc.write(
"casp.proj.open")
188 strs = utils.SplitString(response.strip())
189 print(f
"CASP: {strs[0]}")
192 pro_path = str(strs[1])
196 """! Clears default project workspace i.e. deletes all blocks that were created in the workspace. This function is rarely required to be used by the user.
198 response = casp_proc.write(
"casp.proj.wsp.clear")
199 print(f
"CASP: {response.strip()}")
202 """! Opens project's documentation.
204 response = casp_proc.write(
"casp.sys.help.proj")
207 """! Saves current project.
209 response = casp_proc.write(
"casp.proj.save")
210 print(f
"CASP: {response.strip()}")
213 """! Closes current project.
215 response = casp_proc.write(
"casp.proj.close")
216 print(f
"CASP: {response.strip()}")
219 """! Set target hardware on which the current model has to execute. It also updates \ref casp_pins.py with the selected target pin variables for later use, and reloads it.
223 target_name: Target name string from \ref casp_targets.py file.
225 response = casp_proc.write(
"casp.proj.set.target", [target_name])
226 importlib.reload(pins)
227 print(f
"CASP: {response.strip()}")
234 target_id: Target ID string to be set.
236 response = casp_proc.write(
"casp.proj.set.target_id", [target_id])
237 print(f
"CASP: {response.strip()}")
240 """! Set programmer port. In most cases leave it to default.
244 programmer: Use either \ref casp_defs.PROGRAMMER0 or \ref casp_defs.PROGRAMMER1.
246 response = casp_proc.write(
"casp.proj.set.programmer", [str(programmer)])
247 print(f
"CASP: {response.strip()}")
250 """! Set serial port to which the device or development board to be programmed is connected. Use function \ref SerialPorts() to display available serial ports on the host.
254 serial_port: Set serial port to which the device to be programmed is connected.
256 response = casp_proc.write(
"casp.proj.set.programmer.port", [serial_port])
257 print(f
"CASP: {response.strip()}")
260 """! Set IP address of remote device supporting CASP Remote Build protocol. Please refer CASP main documentation for details on CASP Remote Build feature.
264 ip: Set remote device IP.
266 response = casp_proc.write(
"casp.proj.set.rbuild.ip", [ip])
267 print(f
"CASP: {response.strip()}")
270 """! Set enable or disable Remote Terminal Viewer feature on remote build device. Please refer CASP main documentation for details on Remote Terminal Viewer.
274 enable: Set True to enable and False to disable.
276 response = casp_proc.write(
"casp.proj.set.rbuild.rtv", [str(int(enable))])
277 print(f
"CASP: {response.strip()}")
280 """! Generates Python code for the current project model. The generated file (.py) will be located in projects directory.
284 skip_disabled_blocks: Skips disabled blocks in the generated code.
286 response = casp_proc.write(
"casp.proj.gen.py_code", [str(int(skip_disabled_blocks))])
287 print(f
"CASP: {response.strip()}")
290 """! Adds new sub-window to an existing group. Creates new group if group name is not found.
294 grp_name: Existing group name to which new sub-window is added.
296 wnd_name: New sub-window name to be added. It should be unique to existing sub-window names in the group.
298 wnd_type: Sub-window type. It shall be one of the constants defined in \ref casp_wtypes.py module.
300 response = casp_proc.write(
"casp.sim_panel.subwnd.add", [grp_name, wnd_name, str(wnd_type)])
301 print(f
"CASP: {response.strip()}")
304 """! Sets simulation execution to finite time.
308 set: Set True to set simulation execution to finite time or False to set simulation execution time to infinite.
310 response = casp_proc.write(
"casp.sim_setup.finite_time", [str(int(set))])
311 print(f
"CASP: {response.strip()}")
314 """! Sets simulation end time in milli seconds.
318 time_msec: Set end time in milli seconds.
320 response = casp_proc.write(
"casp.sim_setup.end_time", [str(time_msec)])
321 print(f
"CASP: {response.strip()}")
324 """! Sets simulation time step in micro seconds.
328 time_usec: Set time step in micro seconds.
330 response = casp_proc.write(
"casp.sim_setup.time_step", [str(time_usec)])
331 print(f
"CASP: {response.strip()}")
334 """! Sets simulation plot step.
338 plot_step: Set plot step in multiples of simulation time step.
340 response = casp_proc.write(
"casp.sim_setup.plot_step", [str(plot_step)])
341 print(f
"CASP: {response.strip()}")
344 """! Sets simulation plot samples. It sets specified fixed plot samples buffer (FIFO buffer) during simulation. Plot samples are pushed into the buffer at each simulation plot step. If the buffer is full the first in sample will be discarded. Higher buffer values demand more memory and CPU resources.
348 plot_samples: Set number of plot samples.
352 For a simulation time step=1msec with plot step=1 and plot samples set to 10000, the FIFO buffer size can hold samples for 10 secs (0.001*1*10000).
354 response = casp_proc.write(
"casp.sim_setup.plot_samples", [str(plot_samples)])
355 print(f
"CASP: {response.strip()}")
358 """! Sets simulation speed factor. This value should be non negative.
360 - Speed factor 0 will run the simulation at full speed.
361 - Speed factor 1 will run the simulation in real time.
362 - Speed factor between 0 and 1 will run the simulation slower than real time.
363 - Speed factor >1 will run the simulation faster than real time.
365 In case 3 and 4 above the simulation speed is with respective to real time.
369 sf: Set simulation speed factor.
371 response = casp_proc.write(
"casp.sim_setup.speed_factor", [str(sf)])
372 print(f
"CASP: {response.strip()}")
375 """! Sets simulation integer and floating point precision to 32-bit, 48-bit (applicable to FPGA) and 64-bit.
379 precision: Set simulation precision constants as define in module \ref casp_defs.py. Typical values are \ref casp_defs.PRECISION_32, \ref casp_defs.PRECISION_48, \ref casp_defs.PRECISION_64.
381 response = casp_proc.write(
"casp.sim_setup.precision", [str(precision)])
382 print(f
"CASP: {response.strip()}")
386 """! Sets compiler optimization level. Typical values are from 0 to 5. Default value is 1.
389 - 1 to 3: Increasing optimization level from 1 to 3
390 - 4: Optimize for size
391 - 5: Optimize for speed
395 opt_level: Set compiler optimization level from above values.
397 response = casp_proc.write(
"casp.sim_setup.cmp_opt_level", [str(opt_level)])
398 print(f
"CASP: {response.strip()}")
401 """! Sets maximum threads limit that can run in parallel during execution.
405 max_threads: Set maximum threads limit value. 0 value indicate that the maximum threads are set based on the available CPU cores on the target. The set value will be limited to available CPU cores on the target.
407 response = casp_proc.write(
"casp.sim_setup.limit_model_threads", [str(max_threads)])
408 print(f
"CASP: {response.strip()}")
411 """! Sets maximum block threads limit that can run in parallel within a block during execution.
415 max_threads: Set maximum block threads limit value. 0 value indicate that the maximum threads are set based on the available CPU cores on the target. The set value will be limited to available CPU cores on the target.
417 response = casp_proc.write(
"casp.sim_setup.limit_block_threads", [str(max_threads)])
418 print(f
"CASP: {response.strip()}")
421 """! Enables or disables python support for the current target. Here, python supprt means blocks having embedded python code will be executed during simulation if this feature is enabled otherwise compilation will generate and error. User can enable it only if the target supports embedded python.
425 enable_disable: Set True to enable or False to disable this feature.
427 response = casp_proc.write(
"casp.sim_setup.enable_target_python", [str(int(enable_disable))])
428 print(f
"CASP: {response.strip()}")
431 """! Sets maximum OpenMP threads that can be created for the current target (if this feature is supported by the target).
435 max_threads: Set maximum threads (1,2,3...). Value 1 means this feature is disabled. 0 means the maximum value is decided based on number of CPU cores available on the target.
437 response = casp_proc.write(
"casp.sim_setup.openmp_threads", [str(max_threads)])
438 print(f
"CASP: {response.strip()}")
441 """! Configures OpenCL support for current target.
445 platform_index: Set OpenCL platform index (-1,0,1,2...)
447 device_index: Set OpenCL device index (-1,0,1,2...)
449 If both 'platform_index' and 'device_index' arguments are -1 then this feature is disabled.
451 text = str(platform_index)+
","+str(device_index)
452 response = casp_proc.write(
"casp.sim_setup.opencl", [text])
453 print(f
"CASP: {response.strip()}")
456 """! Configures CUDA support for current target.
460 device_index: Set CUDA device index (-1,0,1,2...). -1 disables CUDA support.
462 response = casp_proc.write(
"casp.sim_setup.cuda", [str(device_index)])
463 print(f
"CASP: {response.strip()}")
466 """! Quickly validates current model for any errors and displays the result of validation in brief. If the validation fails, user can check the log files in project directory for details.
468 print(
"CASP: Validating model...")
469 response = casp_proc.write(
"casp.sim.validate")
470 print(f
"CASP: {response.strip()}")
473 """! Builds current model and displays the result of the build process. User should check the log files in the project directory for details if the build fails.
475 print(
"CASP: Build started...")
476 response = casp_proc.write(
"casp.sim.build")
477 print(f
"CASP: {response.strip()}")
480 """! Generally, \ref casp.Build() function skips un-modified code files during build to speed up the build process. However, the BuildFresh() function deletes all the intermediate build files from previous build and freshly rebuilds the model. After the build process is completed, it displays the result of the build process in brief. User should check the log files in the project directory for details if the build process fails.
482 print(
"CASP: Re-Build started...")
483 response = casp_proc.write(
"casp.sim.rebuild")
484 print(f
"CASP: {response.strip()}")
487 """! Cancels current build process.
489 print(
"CASP: Build cancel started...")
490 response = casp_proc.write(
"casp.sim.cancel_build")
491 print(f
"CASP: {response.strip()}")
494 """! Clears previous build data including all the intermediate build files.
496 print(
"CASP: Build clear started...")
497 response = casp_proc.write(
"casp.sim.build.clear")
498 print(f
"CASP: {response.strip()}")
501 """! Starts simulation if the target supports simulation panel. Else it starts to program the target board with generated binary and resets the target board so that the uploaded binary will get executed. This function also builds the model if needed.
503 print(
"CASP: Starting simulation (or target programming)...")
504 response = casp_proc.write(
"casp.sim.start")
505 print(f
"CASP: {response.strip()}")
508 """! Starts simulation (or target programming) with previous build data. This function does not re-build the model.
510 print(
"CASP: Starting simulation (or target programming)...")
511 response = casp_proc.write(
"casp.sim.start_prev")
512 print(f
"CASP: {response.strip()}")
515 """! Stops current simulation (or target programming) process.
517 print(
"CASP: Stopping simulation (or target programming)...")
518 response = casp_proc.write(
"casp.sim.stop")
519 print(f
"CASP: {response.strip()}")
521def BlockAdd(blk_path: str, blk_name: str=
" ") -> int:
522 """! Adds new block to the current model. The module corresponding to the block to be added shall be first imported. These block modules are available in pycasp/blks directory.
526 blk_path: block path constant from the corresponding block module. Generally, it is in the form of <block module>.path.
528 blk_name: any meaning full string.
532 Block Id (of type integer) if the block instance is created and added to the model or else 0. It is better to store the return value as it will be referred in functions related to the block.
534 response = casp_proc.write(
"casp.blk.add", [blk_path, blk_name])
535 strs = utils.SplitString(response.strip())
536 print(f
"CASP: {strs[0]}")
539 blk_id = int(strs[1])
543 """! Displays block documentation of specified block Id. The module corresponding to the block Id shall be first imported from pycasp/blks directory.
547 blk_id: It should be the reference block Id (and not the block Id of the created block instance). Generally, it is in the form of <block module>.id.
549 response = casp_proc.write(
"casp.blk.help", [str(blk_id)])
550 print(f
"CASP: {response.strip()}")
553 """! Sets block name of the specified block instance Id.
557 blk_id: block instance Id for which the block name shall be set.
559 blk_name: a user friendly name to be set.
561 response = casp_proc.write(
"casp.blk.set_name", [str(blk_id), blk_name])
562 print(f
"CASP: {response.strip()}")
565 """! Enable or disable a block in the current model.
569 blk_id: block instance Id
571 enable: True to enable the block or False to disable it
573 response = casp_proc.write(
"casp.blk.enable", [str(blk_id), str(int(enable))])
574 print(f
"CASP: {response.strip()}")
577 """! Auto relocates the block instance in the workspace. This function is useful only when CASP window is visible and user wants to re-locate the block in the workspace.
581 blk_id: block instance Id to re-locate
583 response = casp_proc.write(
"casp.blk.re_locate", [str(blk_id)])
584 print(f
"CASP: {response.strip()}")
587 """! Sets block parameter value of the specified block instance Id.
591 blk_id: block instance Id
593 grp_para_name: parameter name string of the block parameter. Generally, it is in the form of <block_module_name>.Para.<block_group_constant>.<block_parameter_constant>.name.
595 para_val: new value of the block parameter. Parameter value can be of absolute value or one of the value from the parameter list. Refer block module help for parameter value list.
597 response = casp_proc.write(
"casp.blk.set_para", [str(blk_id), grp_para_name, para_val])
598 print(f
"CASP: {response.strip()}")
601 """! Sets block initial condition value of the specified block instance Id. The function is similar to \ref casp.BlockParaSet() except it sets block initial condition values instead block parameter values. Refer \ref casp.BlockParaSet() for details.
603 response = casp_proc.write(
"casp.blk.set_init", [str(blk_id), grp_para_name, para_val])
604 print(f
"CASP: {response.strip()}")
606def BlockPortsConnect(blk_id1: int, port_name1: str, blk_id2: int, port_name2: str, auto_update_input_port_para: bool=
True):
607 """! Connects two block ports.
613 port_name1: port name of block1
617 port_name2: port name of block2
619 auto_update_input_port_para: auto updates input port data type and size based on connected output port parameters.
621 response = casp_proc.write(
"casp.blk.connect_ports", [str(blk_id1), port_name1, str(blk_id2), port_name2, str(int(auto_update_input_port_para))])
622 print(f
"CASP: {response.strip()}")
625 """! Deletes specified block Id instance from the model.
629 blk_id: block Id to delete
633 True if successful else False
635 response = casp_proc.write(
"casp.blk.del", [str(blk_id)])
636 print(f
"CASP: {response.strip()}")
637 if "Error" in response.strip():
WspClear()
Clears default project workspace i.e.
ProgrammerPortSet(str serial_port)
Set serial port to which the device or development board to be programmed is connected.
SimSetupFiniteTime(bool set=True)
Sets simulation execution to finite time.
BlockRelocate(int blk_id)
Auto relocates the block instance in the workspace.
bool BlockDelete(int blk_id)
Deletes specified block Id instance from the model.
int BlockAdd(str blk_path, str blk_name=" ")
Adds new block to the current model.
SimSetupMaximumModelThreads(int max_threads=1)
Sets maximum threads limit that can run in parallel during execution.
SimSetupPrecision(int precision=defs.PRECISION_32)
Sets simulation integer and floating point precision to 32-bit, 48-bit (applicable to FPGA) and 64-bi...
ModelValidate()
Quickly validates current model for any errors and displays the result of validation in brief.
ProjectSave()
Saves current project.
HelpBsp()
Opens CASP board support package (BSP) documentation.
FolderTargetSupport()
Opens current CASP target support folder.
SimSetupOpenClSupport(int platform_index=-1, int device_index=-1)
Configures OpenCL support for current target.
BlockEnable(int blk_id, bool enable=True)
Enable or disable a block in the current model.
Build()
Builds current model and displays the result of the build process.
str ProjectOpen(str proj_full_path=None)
Opens existing CASP project.
ProgrammerSet(int programmer=defs.PROGRAMMER0)
Set programmer port.
str ProjectOpenDefault()
This function does a couple of things.
SerialTerminal()
Invokes CASP Serial Terminal window.
GenPyCode(bool skip_disabled_blocks=True)
Generates Python code for the current project model.
BlockInitConditionSet(int blk_id, str grp_para_name, str para_val)
Sets block initial condition value of the specified block instance Id.
Help()
Opens CASP main documentation.
BuildCancel()
Cancels current build process.
SimSetupMaximumBlockThreads(int max_threads=1)
Sets maximum block threads limit that can run in parallel within a block during execution.
TargetSet(str target_name)
Set target hardware on which the current model has to execute.
SimStop()
Stops current simulation (or target programming) process.
Close()
Terminates CASP subprocess and releases the resources.
SimPanelWindowAdd(str grp_name, str wnd_name, int wnd_type)
Adds new sub-window to an existing group.
SimStartPrev()
Starts simulation (or target programming) with previous build data.
TargetIdSet(str target_id)
Set target ID.
SimSetupPlotSamples(int plot_samples=10000)
Sets simulation plot samples.
FolderProject()
Opens current CASP project directory.
FolderExamples()
Opens CASP examples directory.
str ProjectCreate(str proj_name=None)
Creates new CASP project and returns the project path.
RemoteBuildIPSet(str ip)
Set IP address of remote device supporting CASP Remote Build protocol.
BlockViewer()
Invokes BlockViewer process for user to browse CASP blocks, view block parameters,...
BleClient()
Opens CASP Bluetooth Low Energy (BLE) process.
HelpPython()
Opens CASP Python Libraries documentation.
BlockParaSet(int blk_id, str grp_para_name, str para_val)
Sets block parameter value of the specified block instance Id.
BlockNameSet(int blk_id, str blk_name)
Sets block name of the specified block instance Id.
Init(bool make_visible=False, str casp_file="CASP-Eval")
Initializes CASP subprocess.
Rtv()
Opens CASP Remote Terminal Viewer (RTV) process.
BlockHelp(int blk_id)
Displays block documentation of specified block Id.
BlockPortsConnect(int blk_id1, str port_name1, int blk_id2, str port_name2, bool auto_update_input_port_para=True)
Connects two block ports.
SimSetupEndTime(int time_msec=1000)
Sets simulation end time in milli seconds.
ProjectClose()
Closes current project.
SimSetupPlotStep(int plot_step=1)
Sets simulation plot step.
BuildFresh()
Generally, casp.Build() function skips un-modified code files during build to speed up the build proc...
FolderUserData()
Opens 'CASP User Data' directory from user's Documents folder.
Refresh()
Refreshes CASP project.
FolderTutorials()
Opens CASP tutorials directory.
SerialPorts()
Displays available serial ports on the host.
SimStart()
Starts simulation if the target supports simulation panel.
SimSetupSpeedFactor(float sf=1)
Sets simulation speed factor.
ProjectHelp()
Opens project's documentation.
SimSetupTargetPythonSupport(bool enable_disable=True)
Enables or disables python support for the current target.
SimSetupTimeStep(int time_usec=1000)
Sets simulation time step in micro seconds.
SimSetupCudaSupport(int device_index=-1)
Configures CUDA support for current target.
SimSetupOpenMpThreads(int max_threads=1)
Sets maximum OpenMP threads that can be created for the current target (if this feature is supported ...
BuildClear()
Clears previous build data including all the intermediate build files.
SimSetupOptLevel(int opt_level=1)
set compiler optimization level (0 to 5: 4-opt for size, 5-opt for speed)
RemoteBuildRtvEnable(bool enable=True)
Set enable or disable Remote Terminal Viewer feature on remote build device.