Devices
Any electrical item, active or passive, that is not a harness.
A device may have terminating channel endpoints (one connector per channel) and passthroughs (the same channel_id on exactly two connectors, each with a repeat_channel_id). Both are devices; place them with Place device.
How device data is stored
The primary data structure of a device is a TSV called a “signals_list”. Signals lists can be written manually or generated from a python script that can help automate the generation of lists for complicated devices.
The definition of a device lives in a TSV file called a "Signals List".
What a signals list holds and the rules it must follow: Signals lists.
Building a device
When a Device is built in Harnice, here's what happens:
- Harnice runs the build instructions if it's found in the device directory.
- The signals list is validated and verified.
- A block diagram symbol SVG that can represent this device in a system block diagram is generated or updated based on the signals list.
- Every signals-list
connector_idgets a node with that same id. The build adds any that are missing, at the origin.
The default device build instructions ship with Harnice as harnice/defaults/device_default_build_instructions.py. The first build copies them into build.py.
Default device build instructions
import os
# configuration is a dict keyed by field id from config.yaml.
# Read one answer with configuration["field_id"].
# A standalone build uses the setup defaults. A system rebuild passes that instance's answers.
# json, yaml, and toml fields are already parsed.
# print(configuration["field_id"])
# ---------------------------------------------------------------------------
# Device example: four XLR channels (two in, two out).
# Each channel has one instance, so repeat_channel_id stays blank.
# ---------------------------------------------------------------------------
ch_type_ids = {
"in": (1, "https://github.com/harnice/harnice-general-library"),
"out": (4, "https://github.com/harnice/harnice-general-library"),
"chassis": (5, "https://github.com/harnice/harnice-general-library")
}
xlr_pinout = {
"pos": 2,
"neg": 3,
"chassis": 1
}
connector_mpns = {
"XLR3F": ["in1", "in2"],
"XLR3M": ["out1", "out2"]
}
def mpn_for_connector(connector_name):
for mpn, conn_list in connector_mpns.items():
if connector_name in conn_list:
return mpn
return None
signals_list.new()
for connector_name in ["in1", "in2", "out1", "out2"]:
if connector_name.startswith("in"):
channel_type = ch_type_ids["in"]
elif connector_name.startswith("out"):
channel_type = ch_type_ids["out"]
else:
continue
channel_name = connector_name
connector_mpn = mpn_for_connector(connector_name)
for signal in chtype.signals(channel_type):
signals_list.append(
channel_id=channel_name,
signal=signal,
connector_id=connector_name,
cavity=xlr_pinout.get(signal),
channel_type=channel_type,
device_connector_mpn=connector_mpn
)
signals_list.append(
channel_id=f"{channel_name}-shield",
signal="chassis",
connector_id=connector_name,
cavity=xlr_pinout.get("chassis"),
channel_type=ch_type_ids["chassis"],
device_connector_mpn=connector_mpn
)
# A passthrough is the same channel_id on exactly two connectors. Every row
# needs a repeat_channel_id, and the two repeats must differ. Use
# harness_connector_mpn when the harness owns that face.
#
#
# How to use the relevant signals list functions:
# signals_list.append(
# channel_id="ch0",
# repeat_channel_id="A",
# signal="pos",
# connector_id="A",
# cavity=2,
# channel_type=ch_type_ids["in"],
# harness_connector_mpn="XLR3F",
# )
# signals_list.append(
# channel_id="ch0",
# repeat_channel_id="B",
# signal="pos",
# connector_id="B",
# cavity=2,
# channel_type=ch_type_ids["out"],
# harness_connector_mpn="XLR3M",
# )
#
# Define the physical model of your device in this section. Choose from
# default macros or import your own STEP file.
# Each macro saves with fileio.path("body"), the device's body.step.
# A paste on the physical model page writes body_images/<face>.png and a
# comment in body.step. The next macro run rewrites body.step. Rectangular
# and rack-chassis macros link again only the faces named in images=.
# The enclosure and STEP-import macros take no images argument. Comment
# that call out after the body is right, then paste. Leaving it in build.py
# drops those face links on the next run. The PNG files stay either way.
# Rectangular sketch surface "front" is the face at y = 0.
# images = {
# "front": os.path.join(fileio.dirpath("body_images"), "front.png"),
# }
# build_utils.run_macro(
# "build_rectangular_physical_model",
# "macros/device_artifacts",
# "https://github.com/harnice/harnice-general-library",
# artifact_id="body-1",
# params={"width": 4, "height": 2, "depth": 6},
# images=images,
# )
# Rack sketch surface "bottom" is the face at z = 0. The front ears are "front".
# images = {
# "bottom": os.path.join(fileio.dirpath("body_images"), "bottom.png"),
# }
# build_utils.run_macro(
# "build_rackmount_physical_model",
# "macros/device_artifacts",
# "https://github.com/harnice/harnice-general-library",
# artifact_id="body-1",
# params={"u": 2, "depth": 10},
# images=images,
# )
# Rack enclosure. depth and units are the macro arguments. The front panel is
# 19 inches wide and the body is 19 inches wide. 1U is 1.75in tall.
# No images argument. Comment this call out before pasting face pictures.
# build_utils.run_macro(
# "build_rackmount_enclosure",
# "macros/device_artifacts",
# "https://github.com/harnice/harnice-general-library",
# artifact_id="body-1",
# depth=10,
# units=2,
# )
# Imported STEP. No images argument. Comment this call out after the copy
# looks right, then paste face pictures. The next run recopies the vendor
# file and drops those links.
# build_utils.run_macro(
# "build_physical_model_from_step",
# "macros/device_artifacts",
# "https://github.com/harnice/harnice-general-library",
# artifact_id="body-1",
# path_to_step="vendor/chassis.step",
# )
# Drop instance_data folders whose refdes is no longer in this project.
build_utils.remove_unused_instance_data()
Physical model
A device body is fileio.path("body") (body.step). Its nodes live in attributes.json under nodes. How to write the solid, place nodes, paste a face picture, and arrange devices in a system is CAD.
Open attributes to read and write attributes.json. Changes autosave. Open physical model to move nodes on the solid the build wrote, and to paste a picture on a face. A pasted picture stays through the next build when images names that file, or when the macro that rewrites body.step is commented out. The PNG stays in body_images/ either way. Every signals-list connector_id has a node with that same id. The build adds any that are missing, at the origin, and that writes nodes when the key is absent. The physical model page reads those nodes. It does not add them.
How to define a new device
-
Ensure every channel going into or out of a device has a type defined in a repo somewhere.
Channel Types
Channel Types
A channel type is electrical information only. It records the signals on one electrical channel, the direction (
in,out, or blank), optional electrical limits (voltage, current, power, level), and which other channel types that channel may connect to.A channel type must not specify a connector type. Connector family, gender, shell, insert, and part number are not channel-type fields. They belong on the device signals list (
connector_id,device_connector_mpn,harness_connector_mpn) and on the connector part.The same electrical channel on two connectors is one channel type. Balanced mic level on an XLR and balanced mic level on a terminal block share a type. Do not add a channel type because the connector changed. Leave connector names out of
descriptionandsignals.balanced audio mic levelis a description.XLR mic inputis a connector, so it is not a channel type.
How channels are mapped is on Systems.
How to define a new channel type
- In a repository of your choice (or start with harnice-general-library on your own branch), navigate to
channel_types.tsvat the library root - If you want channel definitions to be private and are therefore working in a private repository, ensure the repo's path is listed in file
repository_locations.csv(located at root of your harnice source code repo). The first column is the URL or traceable path, and the second column is your local path. - If you find the channel_type you're looking for, temporarily note it as a touple in a notepad somewhere with format
(ch_type_id, universal_library_repository). - If you don't find it, make a new one. It's important to try and reduce the number of channel_types in here to reduce complexity, but it's also important that you adhere to strict and true rules about what is allowed to be mapped to what. A new connector is not a new channel type. The row records electrical information only and must not name a connector type. Modifications and additions to this document should be taken and reviewed very seriously.
Two optional columns set the default appearance. Both cells are Python dict literals, parsed with
ast.literal_eval. Leave a cell blank to leave that default unset. Harnice reads thechannel_types.tsvat the root of the library named by the channel type'slib_repo.channel_default_appearance— one engineering appearance dict. A channel trace (channelorharness-channel) and the channel card in the connectors view use it. Example:{'base_color': '#D59A10'}.signal_default_appearances— signal name to appearance dict. A circuit trace uses the entry for itssignal_of_channel_type, and the connectors view uses that entry for the signal bar. Names match thesignalscolumn. Example:{'pos': {'base_color': '#E24B4A'}, 'neg': {'base_color': '#2E6B9F'}}.
An instance
appearancethat already includesbase_coloris the appearance drawn for that trace. Traces are drawn when the project is built, so a change to these columns shows up on overlays after the next build. The connectors view reads the columns when the page opens. A trace whose resolved appearance has nobase_coloris the rainbow placeholder. A card or bar with nobase_colorkeeps the console palette.The dict is the engineering appearance from the Appearance guide:
base_color, and optionallyoutline_color,parallelstripe,perpstripe, andtwisted. Single quotes match the rows already in the file. The separator is a tab, so a comma inside the literal stays in the cell. - In a repository of your choice (or start with harnice-general-library on your own branch), navigate to
-
Make a folder for the part number of your device somewhere on your computer. Click Build in Harnice, which will generate an example device that you can then edit.
Building a project
-
In Harnice, click New project…. You do not need a rev folder yet.
-
Fill in part number, revision (
1is fine), project type, and a description, then click OK. Harnice creates the folders and an example you can edit. -
Click Build to generate the rest of the project files.
-
-
Edit the attributes of your new device.
Editing the Attributes of a Project
-
In the Harnice console, open attributes. That page reads and writes
attributes.json(fileio.path("attributes")). Changes autosave. Projects without that page keep the file in the revision folder. -
Change the default reference designator here, as well as any other attributes you may want to record.
- Click Build again.
-
-
Edit the signals list of your new device. To generate
build.pyfrom a pinout, ICD, or datasheet, paste the Device signals list prompt on Building with AI; put the source file next tobuild.pyin the revision folder. A catalog of device part numbers uses the Part family prompt on that page. Each SKU is its own device project.Updating a Signals List
Harnice defines a signals list to be a table that keeps track of every single signal going into and out of a device, which connector or channel it's part of, which signal names it has, which connector contact number or part number it has.
To generate
build.pyfrom a pinout, ICD, datasheet, or spreadsheet, paste the Device signals list prompt on Building with AI. Put the source file in the device revision folder (next tobuild.py) or give the agent that path.- If your device is very simple (has only a small number of signals), you are free to edit
fileio.path("signals list")manually. If you choose to do this, deletefileio.path("build instructions")before clicking Build again, because the script will overwrite the signals list. However, it is recommended that you edit the Python build instructions instead to produce a validated, reproduceable, and portable result. - To edit the build instructions Python file, this quick-start guide should give you an idea of how the default script is set up, and how you might be able to change it to suit your needs. The goal of this file is to make a signals list. Do not be concerned with writing this efficiently, as it will only be ran while building the device.
- List, utility, and channel-type modules (
signals_list,chtype, and the rest) are already available in the script — you do not need to import them from harnice. - Copy your channel_type touples in from earlier notepad step. Again, these are relevant only to the device you're trying to make. The dictionary is used to store these here so that you can reference the touples in a human-readable format in this script only.
ch_type_ids = {"in": (1, "https://github.com/harnice/harnice-general-library"),"out": (4, "https://github.com/harnice/harnice-general-library"),"chassis": (5, "https://github.com/harnice/harnice-general-library")} -
- Define your connector part numbers. Default convention is this dictionary, where you can resolve the part number by finding the connector name.
mpn_for_connector(connector_name)will search your definition dictionary for a connector name and return its part number.connector_mpns = {"XLR3F": ["in1", "in2"],"XLR3M": ["out1", "out2"]}
Define your pinouts. Default convention is this dictionary, where if you reference something like
xlr_pinout.pos, this dictionary should return the cavity name. A channel type is electrical information only and must not specify a connector type, a pinout, or a part number. Put the cavity map and the connector part number on the device. The same electrical channel on two connectors still uses one channel type. If you use the same pinout across multiple devices, you may consider importing that definition from a python library elsewhere.xlr_pinout = {"pos": 2,"neg": 3,"chassis": 1}signals_list.new()will overwrite the existing signals list with a blank one, which will be repopulated while the rest of this script is executed.- Bespoke logic should be written in python for the remainder of this script, making use of patterns and naming conventions of this specific device.
- I'd recommend you use a python editor on your screen at the same time as a csv editor that automatically refreshes (vscode, easycsveditor on the mac store) so that you can update your python, run builds frequently, and watch the updates in realtime on your csv
- Define your connector part numbers. Default convention is this dictionary, where you can resolve the part number by finding the connector name.
- List, utility, and channel-type modules (
- If your device is very simple (has only a small number of signals), you are free to edit
-
Give the device a body when it should show up in a system assembly. Paste the CAD prompt on Building with AI, or follow CAD.
-
Work on your block diagram symbol.
Working on a Block Diagram Symbol
the first time you build after updating your signals list, you may get an error like
The following pin(s) exist in SVG symbol but not in Signals List: in1, in2, out1, out2. Harnice keeps the block diagram symbol in sync with your signals list. Delete or edit the SVG so its pins match, then rebuild—missing pins are appended automatically when possible.Before you polish the symbol, name your connectors in the signals list and build once more so the pins that appear in the SVG match what you intend.
- Open the Harnice console on your device and open the block diagram symbol editor.
- Arrange the symbol the way you want it to look. Do not rename pin identifiers that must match the signals list; you can change placement and appearance.
- Text on the symbol is a Python f-string.
{refdes}becomes the instance name, and{configuration.get("field_id")}reads a configuration field, when a system builds this device. The result is written into that system's diagram.\nstarts a new line. Text with no{...}and no\nstays as written. Harnice never runs this text as Python: it understands data, math, and a short list of functions and methods, listed under "Showing values on the symbol" in Device Configurations. - Save the symbol and click Build again to confirm the SVG still matches the signals list.
When you build a device from its Signals List, Harnice creates or updates a block diagram symbol SVG in the revision directory. That symbol contains ports that match the connectors in your Signals List.
Device modeling for simulation of behavior in a system (future work)
It is often useful to model how an entire electrical system will behave by aggregating up behaviors of many contained devices and how they interact with each other.
Eventually, Harnice will allow you to do this automatically within the same source-of-truth system definition that defines your harnesses.
When this feature is implemented, devices will contain an automatically generated .kicad_esch file that will allow the user to define a schematic that represents the lump behavior of your device. Harnice will ensure that every signal on your signals list is accounted for in the simulation esch, and the user may choose to connect any simulation device between those symbols. This way, when the device is used in a system block diagram, this device esch can referenced by the system simulator and its behavior can be considered while running an entire system simulation profile.