Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

100 Commits

Repository files navigation

PicaxePreprocess.py

A PICAXE preprocessor to implement most preprocessor directives found in Picaxe Programming Editor 6 for use with axepad, blockly, and other non-PE6 editors.

This tool can also be used as a starting point for include/macro implementations in other software tools.

Table of contents

Setup

If you don't already have it, install python3.

Copy picaxepreprocess.py (and optionally the Makefile for advanced usage) to the directory your .bas files are in or to a known location. For ease of use this location may be in the system path such as /usr/local/bin so that the script can be accessible from anywhere on the system by entering picaxepreprocess.py in the terminal without needing to know the actual location.

You might also want to edit the compile_path variable to point to the Picaxe compilers. If you don't already have them, they can be downloaded from here. Alternatively, the compiler path can be specified with the -P option.

That's it!

Usage

Run ./picaxepreprocess.py.

If your starting file is named "main.bas", and you're ok with outputing to "compiled.bas", that's it!

You can also specify an input file with -i filename.bas or without the -i if it is the last option, such as picaxepreprocess.py input.bas Output files can be specified similarly with -o outputfilename.bas

The preprocessor will automatically look for included files in the working directory, and in a subdirectory called /include if it exists. (note: for optimal compatibility with PE6 includes, using this include directory on shared projects with PE6 users is not reccomended.) You can also specifiy absolute file paths.

Options and usage

In general usage, the script should be called and any required options and flags given after it on the same line as follows:

picaxepreprocess.py [OPTIONS] [INPUTFILE]

Optional switches

ShortExtendedValue expectedDescription
-i--file=yesInput file (default main.bas). Flag not required if it is the last argument given.
-o--ofile=yesOutput file (default compiled.bas)
-u--uploadnoSend the file to the compiler if this option is included.
-s--syntaxnoSend the file to the compiler for a syntax check only (no download)
--nocolornoDisable terminal colour for systems that do not support it (Windows).
--noifsnoDisable evaluation of #if and #ifdef - this will be left to the compiler if this flag is present.
--tablesertxdnoEnable a non standard extension that will evaluate a ;#sertxd directive. See the Table Sertxd Extension section below for more details.
--tableseroutnoEnable a non standard extension that will evaluate a ;#serout directive. Also automatically enables --tablesertxd. See the Table Sertxd Extension section below for more details.
--verbosenoPrint preproccessor debugging info
-h--helpnoDisplay a text version of this help message

Optional switches only used if sending to the compiler

ShortExtendedValue expectedDescription
-v--variant=yesVariant (default 08m2) (alternatively use #PICAXE directive within the program. This option will be ignored if #PICAXE is used)
-s--syntaxnoSyntax check only (no download)
-f--firmwarenoFirmware check only (no download)
-c--comport=yesAssign COM/USB port device (default /dev/ttyUSB0). Alternately use #COM directive within program. This option will be ignored if #COM is used. There should be a space between the -c and the port on unix based systems, unlike the compilers.
-d--debugnoLeave port open for debug display (b0-13)
--debughexnoLeave port open for debug display (hex mode)
-e--edebugnoLeave port open for debug display (b14-b27)
--edebughexnoLeave port open for debug display (hex mode)
-t--termnoLeave port open for sertxd display
--termhexnoLeave port open for sertxd display (hex mode)
--termintnoLeave port open for sertxd display (int mode)
-p--passnoAdd pass message to error report file
--tidynoRemove the output file on completion if in upload mode.
-P--compilepath=yesSpecify the path to the compilers directory (defaults to /usr/local/lib/picaxe/)
--online-compilenoUse the online compiler and output a compiled .axe file (instead of the local compiler)
--online-syntaxnoUse the online compiler for a syntax check only (no download)

Table Sertxd/Serout Extension

Enable a non standard extension that will evaluate a ;#sertxd or ;#serout directive to automatically save, load and print a string from table memory on supported chips.

For example:

;#sertxd("Hello world", cr, lf)

Syntax is the same as the sertxd command, although dynamic content such as printing variables is not supported and may cause cryptic errors if attempted.

Two word and one byte variables are required for storing addresses and processing, as set by the defines outlined below. These variables can be modified between calls to ;#sertxd, although any call to ;#sertxd will modify them unless variable backup and restore to storage ram is enabled.

The folowing definitions can be used to change the default behaviour (and apply to both tablesertxd and tableserout:

DefinitionDefault ValueDescription
TABLE_SERTXD_ADDRESS_VARw0Changes the word used. If not backing up, the value in it will be lost when ;#sertxd is called.
TABLE_SERTXD_ADDRESS_END_VARw1Changes the word used.
TABLE_SERTXD_TMP_BYTEb4Changes the byte used.
TABLE_SERTXD_BACKUP_VARSDo not backupEnable saving & restoring the variables used to storage ram. This is slower as it uses peek & poke, but allows the variables to keep their value accross ;#sertxd calls.
TABLE_SERTXD_USE_EEPROMUse table memoryIf defined, uses eeprom to store strings instead of table memory, allowing this extension to be used on chips without table memory.
TABLE_SERTXD_MEM_OFFSET0Offset the start of the first string in case the first part of the memory is needed for something else.

Only required if backing up variables:

DefinitionDefault ValueDescription
TABLE_SERTXD_BACKUP_LOC121The location in storage ram to save the existing values of the general purpose variables. 5 bytes are required.
TABLE_SERTXD_ADDRESS_VAR_Lb0The lower byte (I haven't had much success in using peek and poke with words, so need the individual bytes)
TABLE_SERTXD_ADDRESS_VAR_Hb1The upper byte
TABLE_SERTXD_ADDRESS_END_VAR_Lb2The lower byte
TABLE_SERTXD_ADDRESS_END_VAR_Hb3The upper byte

This flag also enables the non standard ;#sertxdnl directive that prints a new line. When called many times, this uses less program space than sertxd(cr, lf)

Only required for tableserout:

DefinitionDefault ValueDescription
TABLE_SEROUT_PINnone(required)The output pin to send the serial message on (eg C.0).
TABLE_SEROUT_BAUDN4800The baud rate to output the serial signal (see the PICAXE Command Reference for list of valid baud rates)

Table Sertxd Extension usage example

Click to expand

As an example of what it does, a cutdown version of the simple.bas example:

#picaxe 14m2
main:
;#sertxd("Hello World", cr, lf)
;#sertxd("That's a really annoying line!", cr,lf,"(because of all the characters that have to be ignored when in a string;",cr, lf, "such as ',', ''', ';', ')', '(', ']', '[', ':', '#', ...")
;#sertxd("This line contains dynamic content that cannot be printed", #w0)
pause 5000goto main

is preprocessed (on Ubuntu) using:

cd Examples/SertxdTableExtension
# picaxepreprocess.py is copied into a folder on the path, so I can just call it as follows:
picaxepreprocess.py -s --tablesertxd simple.bas # Syntax check and enable tablesertxd extension.
'-----PREPROCESSED BY picaxepreprocess.py-----'----UPDATED AT 12:05PM, March 13, 2021----'----SAVING AS compiled.bas ----'---BEGIN simple.bas ---
#picaxe 14m2  'CHIP VERSION PARSED
main:
;#sertxd("Hello World", cr, lf) 'Evaluated below
w0 =0
w1 =12gosub print_table_sertxd
;#sertxd("That's a really annoying line!", cr,lf,"(because of all the characters that have to be ignored when in a string;",cr, lf, "such as ',', ''', ';', ')', '(', ']', '[', ':', '#', ...") 'Evaluated below
w0 =13
w1 =174gosub print_table_sertxd
;#sertxd("This line contains dynamic content that cannot be printed", #w0) 'Evaluated below
w0 =175
w1 =232gosub print_table_sertxd
pause 5000goto main
'---Extras added by the preprocessor---
print_table_sertxd:
for w0 = w0 to w1
readtable w0, b4
sertxd(b4)
next w0
return
table 0, ("Hello World",cr,lf) ;#sertxd
table 13, ("That's a really annoying line!",cr,lf,"(because of all the characters that have to be ignored when in a string;",cr,lf,"such as ',', ''', ';', ')', '(', ']', '[', ':', '#', ...") ;#sertxd
table 175, ("This line contains dynamic content that cannot be printed","?") ;#sertxd

And should print something like:

Hello World
That's a really annoying line!
(because of all the characters that have to be ignored when in a string;
such as ',', ''', ';', ')', '(', ']', '[', ':', '#', ... This line contains dynamic content that cannot be printed ?

Usage with a makefile

See the Makefile for an example of advanced usage. When properly configured, the makefile can automatically handle preprocessing the code, compiling it, and uploading to a picaxe chip by simply invoking make compile and run a syntax check with make syntax. The makefile also demonstrates usage with multiple picaxe chips with separate programs in the same project directory.

Online Compile

The online compiler feature is experimental and uses an undocumented API that may be changed and break at any time, but has been largely stable for the last several years. This allows programming even without any native compilers available for your operating system (such as on MacOS with Apple Silicon and without Rosetta 2).

If using the online compile functionality to produce a .axe file, you'll need the PICAXE programmer app or the online PICAXE programmer tool to upload it to the PICAXE chip.

As an additional experimental project, haxepad.html can be used to integrate with the online compiler as a functional yet very primitive graphical programming interface.

Other projects

If you're interested in this project, you may also like this similar PICAXE Preprocessor implementation, or this similar C-style preprocessor from ParksProjets.

About

A preprocessor for PICAXE basic files to bring feature-parity with PE6 to other text editors.

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages