Skip to content

Code blocks

Radim Hošák edited this page Jul 21, 2021 · 3 revisions

Putting together a .ino pulsebox source code, 'block by block'

The codeblocks module attempts to make generating the .ino files easier. Most importantly, generating the .ino file can be scripted.

Example: A single-channel, single-pulse sequence

import pulsebox.codeblocks as pcb
source_code = ""

# Let's start with a header comment (optional):
source_code += pcb.header("This is an example .ino pulsebox source code!")

# Setup the pulsebox to run in triggered mode on trigger pin 52:
source_code += "\n"
source_code += pcb.setup(triggered=True, parameter=52)

# Now we are ready to fill the sequence() with instructions.
# First comes a state change.
# The states of the sixteen pulsebox channels are stored
# in an array of length 16.
# Let's change the first channel to HIGH (1)
states = [0] * 16
states[0] = 1
source_code += "\n"
source_code += pcb.state_change(states)

# And a delay
loop_iters = 10
loop_label_suffix = 0  # must be unique across all ASM loops!
source_code += "\n"
source_code += pcb.loop(loop_iters, loop_label_suffix)

# Finally, set the first channel back to LOW (0)
states[0] = 0
source_code += "\n"
source_code += pcb.state_change(states)

# End the code
source_code += "\n"
source_code += pcb.end()

The entire code can now be saved to a file or simply printed with.

print(source_code)