Skip to content
1 change: 1 addition & 0 deletions Examples/syntaxError.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pause 50000000000
74 changes: 74 additions & 0 deletions haxepad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>
<body>

<h1>hAXEPad</h1>

<form method="post" id="compileform">
<label for="code">code:</label><br>
<textarea id="code" name="code" rows="20" cols="80"></textarea><br><br>
<label for="platform">platform:</label>
<select id='platform' name='platform'>
<option value="08m2">08M2</option>
<option value="14m2">14M2</option>
<option value="18m2">18M2</option>
<option value="20m2">20M2</option>
<option value="20x2">20X2</option>
<option value="28x1">28X1</option>
<option value="28x2">28X2</option>
<option value="40x1">40X1</option>
<option value="40x2">40X2</option>
</select><br><br>
<button value="compile" onclick="sendCompile(); return false;">compile</button>
<button value="syntax check" onclick="sendSyntax(); return false;">syntax check</button>
</form>
<p>Click on the submit button, and the input will compiled.</p>
<br><br>
raw output:<br>
<div id="resultsDisplay" style='width: 90%; max-height: 20vh; line-height: 1.2em; overflow-wrap: break-word ;overflow:scroll; background-color:lightgreen;'></div>
<div id="responseDisplay" style='width: 90%; max-height: 20vh; line-height: 1.2em; overflow-wrap: break-word ;overflow:scroll; background-color:skyblue;'></div>

<script>
form=document.getElementById("compileform");
function sendCompile(){
form.action="https://picaxecloud.com/compiler/compile.json";
getResponse();
//form.submit();
}

function sendSyntax(){
form.action="https://picaxecloud.com/compiler/check.json";
getResponse();
//form.submit();
}

//document.getElementById('compileform').addEventListener('submit', (event) => {
//event.preventDefault();
function getResponse(){
var xhr = new XMLHttpRequest();
xhr.open("POST", form.action);
xhr.onload = function(event){
var resp=JSON.parse(event.target.response);
document.getElementById("responseDisplay").innerText=event.target.response;
//alert("Success, server responded with: " + event.target.response); // raw response
if(resp.hasOwnProperty('axe')){ //compile successful
document.getElementById("resultsDisplay").innerText="compile successful";
//TODO offer downloadable axe file
}if(resp.hasOwnProperty('status')){ //syntax check passed
document.getElementById("resultsDisplay").innerText="syntax check ok";
}if(resp.hasOwnProperty('errors')){ //syntax error
document.getElementById("resultsDisplay").innerText="error: "+resp.errors;

}

};
// or onerror, onabort
var formData = new FormData(document.getElementById("compileform"));
xhr.send(formData);
};

</script>

</body>
</html>

115 changes: 83 additions & 32 deletions picaxepreprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
# TODO: Debug levels
# TODO: Remove extra comment ; added for ifs

import sys, getopt, os, datetime, re, os.path, subprocess
import sys, getopt, os, datetime, re, os.path, subprocess, json, base64
has_requests = False

inputfilename = 'main.bas'
outputfilename = 'compiled.bas'
outputpath = ""
Expand Down Expand Up @@ -38,6 +40,8 @@
compiler_extension = "" # File extension (.exe...). For linux anyway, there is none, but including
# just in case it is different for other platforms.
send_to_compiler = False
online_compiler = False
syntax_check_only = False
command = [""] # Empty string at the first position will be replaced by the compiler name and path.
tidy = False

Expand All @@ -47,36 +51,38 @@ def print_help():
picaxepreprocess.py [OPTIONS] [INPUTFILE]

Optional switches
-i, --ifile= Input file (default main.bas). Flag not required if it is
the last argument given.
-o, --ofile= Output file (default compiled.bas)
-u, --upload Send the file to the compiler if this option is included.
-s, --syntax Send the file to the compiler for a syntax check only (no download)
--nocolor Disable terminal colour for systems that do not support it (Windows).
--noifs Disable evaluation of #if and #ifdef - this will be left to the compiler if present.
--verbose Print preproccessor debugging info
-h, --help Display this help
-i, --ifile= Input file (default main.bas). Flag not required if it is
the last argument given.
-o, --ofile= Output file (default compiled.bas)
-u, --upload Send the file to the compiler if this option is included.
--online-compile Use the online compiler and output a compiled .axe file
-s, --syntax Send the file to the compiler for a syntax check only (no download)
--online-syntax Use the online compiler for a syntax check only (no download)
--nocolor Disable terminal colour for systems that do not support it (Windows).
--noifs Disable evaluation of #if and #ifdef - this will be left to the compiler if present.
--verbose Print preprocessor debugging info
-h, --help Display this help

Optional switches only used if sending to the compiler
-v, --variant= Variant (default 08m2)
(alternatively use #PICAXE directive within the program.
This option will be ignored if #PICAXE is used)
-s, --syntax Syntax check only (no download)
-f, --firmware Firmware check only (no download)
-c, --comport= Assign 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, unlike the compilers.
-d, --debug Leave port open for debug display (b0-13)
--debughex Leave port open for debug display (hex mode)
-e --edebug Leave port open for debug display (b14-b27)
--edebughex Leave port open for debug display (hex mode)
-t, --term Leave port open for sertxd display
--termhex Leave port open for sertxd display (hex mode)
--termint Leave port open for sertxd display (int mode)
-p, --pass Add pass message to error report file
--tidy Remove the output file on completion if in upload mode.
-P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/)
-v, --variant= Variant (default 08m2)
(alternatively use #PICAXE directive within the program.
This option will be ignored if #PICAXE is used)
-s, --syntax Syntax check only (no download)
-f, --firmware Firmware check only (no download)
-c, --comport= Assign 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, unlike the compilers.
-d, --debug Leave port open for debug display (b0-13)
--debughex Leave port open for debug display (hex mode)
-e --edebug Leave port open for debug display (b14-b27)
--edebughex Leave port open for debug display (hex mode)
-t, --term Leave port open for sertxd display
--termhex Leave port open for sertxd display (hex mode)
--termint Leave port open for sertxd display (int mode)
-p, --pass Add pass message to error report file
--tidy Remove the output file on completion if in upload mode.
-P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/)

Preprocessor for PICAXE microcontrollers.
See https://github.com/Patronics/PicaxePreprocess for more info.
Expand All @@ -88,6 +94,8 @@ def main(argv):
global outputfilename
global outputpath
global send_to_compiler
global online_compiler
global syntax_check_only
global port
global command
global tidy
Expand All @@ -105,7 +113,7 @@ def main(argv):
argv.pop() # Remove the -i option as it has been parsed here.

try:
opts, _ = getopt.getopt(argv,"hi:o:uv:sfc:detpP:",["help", "ifile=","ofile=","upload","variant=","syntax","firmware","comport=","debug","debughex","edebug","edebughex","term","termhex","termint", "pass", "tidy", "compilepath=", "nocolor", "noifs", "verbose"])
opts, _ = getopt.getopt(argv,"hi:o:uv:sfc:detpP:",["help", "ifile=","ofile=","upload","variant=","syntax","firmware","comport=","debug","debughex","edebug","edebughex","term","termhex","termint", "pass", "tidy", "compilepath=", "nocolor", "noifs", "verbose", "online-syntax", "online-compile"])
except getopt.GetoptError:
print_help()
sys.exit(2)
Expand All @@ -123,11 +131,19 @@ def main(argv):
outputfilename = arg
elif opt in ("-u", "--upload"):
send_to_compiler = True
elif opt in ("--online-compile"):
online_compiler = True
compiler_path = "https://picaxecloud.com/compiler/compile.json"
elif opt in ("-v", "--variant"): # Picaxe variant
set_chip(arg)
elif opt in ("-s", "--syntax"): # Syntax only
send_to_compiler = True
syntax_check_only = True #currently unused in this path
command.append("-s")
elif opt in ("--online-syntax"):
online_compiler = True
syntax_check_only = True
compiler_path = "https://picaxecloud.com/compiler/check.json"
elif opt in ("-f", "--firmware"): # Firmware check
command.append("-f")
elif opt in ("-c", "--comport"): # Serial port given
Expand Down Expand Up @@ -203,8 +219,32 @@ def main(argv):
os.remove(outputfilename)
err_file = outputfilename.replace("."+outputfilename.split(".")[-1],"") + ".err" # Calculate the name of the error file
os.remove(err_file)


if online_compiler:
try:
import requests
except ImportError:
preprocessor_error("""Using the online compiler requires the python 'requests' module
Install this module with the command
python3 -m pip install requests
and try again, or use the offline compiler""")
with open (outputfilename, 'r') as processed_file:
#produce 'form' layout online compiler expects
compileFormData = {'platform':chip, 'code':processed_file.read()}
compile_request = requests.post(compiler_path, json=compileFormData)
compile_result = json.loads(compile_request.text)
if "status" in compile_result.keys():
print(f"\u001b[1m\u001b[32mSYNTAX CHECK SUCCESS: {compile_result['status']}\u001b[0m")
elif "errors" in compile_result.keys():
print(f"""
\u001b[1m\u001b[31mSYNTAX CHECK FAILED\u001b[0m\u001b[1m on line:
{compile_result['errors'][0]}
{compile_result['errors'][1]}
{compile_result['errors'][2]}\u001b[0m""")
elif "axe" in compile_result:
#base64 decode the result into the .axe file expected
with open (f'{outputfilename}.axe', 'wb') as compiled_file:
compiled_file.write(base64.b64decode(compile_result['axe']))
preprocessor_success(f"\u001b[1monline compile sucessful, saved to {outputfilename}.axe\u001b[0m")
print()
print("Done.")

Expand Down Expand Up @@ -537,5 +577,16 @@ def preprocessor_info(*values, **kwargs):
if verbose:
print(*values, **kwargs)

def preprocessor_success(msg):
""" Prints a success status message. Similar to warning. """
if use_colour:
print("\u001b[1m\u001b[32m", end="") # Bold Green
print("Successs")
if use_colour:
print("\u001b[0m", end="") # Reset
print(msg)


if __name__ == "__main__":
main(sys.argv[1:])

41 changes: 22 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,28 @@ Optional switches
-h, --help Display this help

Optional switches only used if sending to the compiler
-v, --variant= Variant (default 08m2)
(alternatively use #PICAXE directive within the program.
This option will be ignored if #PICAXE is used)
-s, --syntax Syntax check only (no download)
-f, --firmware Firmware check only (no download)
-c, --comport= Assign 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, unlike the compilers.
-d, --debug Leave port open for debug display (b0-13)
--debughex Leave port open for debug display (hex mode)
-e --edebug Leave port open for debug display (b14-b27)
--edebughex Leave port open for debug display (hex mode)
-t, --term Leave port open for sertxd display
--termhex Leave port open for sertxd display (hex mode)
--termint Leave port open for sertxd display (int mode)
-p, --pass Add pass message to error report file
--tidy Remove the output file on completion if in upload mode.
-P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/)
-v, --variant= Variant (default 08m2)
(alternatively use #PICAXE directive within the program.
This option will be ignored if #PICAXE is used)
-s, --syntax Syntax check only (no download)
-f, --firmware Firmware check only (no download)
-c, --comport= Assign 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, unlike the compilers.
-d, --debug Leave port open for debug display (b0-13)
--debughex Leave port open for debug display (hex mode)
-e --edebug Leave port open for debug display (b14-b27)
--edebughex Leave port open for debug display (hex mode)
-t, --term Leave port open for sertxd display
--termhex Leave port open for sertxd display (hex mode)
--termint Leave port open for sertxd display (int mode)
-p, --pass Add pass message to error report file
--tidy Remove the output file on completion if in upload mode.
-P --compilepath= specify the path to the compilers directory (defaults to /usr/local/lib/picaxe/)
--online-compile Use the online compiler and output a compiled .axe file (instead of the local compiler)
--online-syntax Use the online compiler for a syntax check only (no download)

```

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.
Expand Down