Go Down

Topic: Upload Sketches directly from Geany (Read 685 times) previous topic - next topic

Robin2

Feb 01, 2015, 06:57 pm Last Edit: Feb 01, 2015, 07:04 pm by Robin2
This was developed on Linux (Xubuntu and PuppyLinux Tahrpup 6.0 CE). It may be possible to adapt it for Windows or Mac.

I have been using Geany as my external editor for Arduino programming for some time.

Recently I have been trying out Puppy Linux - which has a lot of attractive simplicities. However I discovered that the Arduino IDE does not display properly - the menus get scrunched up and become unusable.

That got me wondering about compiling and uploading without using the Arduino IDE, which, in turn, got me wondering if I could make use of the Build menu in Geany.

I discovered that since version 1.5 there is a command line mechanism for Arduino. However the command line is long and error prone and I don't think there is any way to pass all of the Arduino specific values to it from Geany.

I have written a very short Python program which takes values from comments on the first 5 lines of an Arduino sketch and creates and executes the necessary arduino command line. The Python script is called from Geany which passes the path and filename of the sketch to it. Initial tests are positive.

This is a very simple Python program with no error checking other than to ensure that lines 1 and 5 have the required data - just to avoid havoc in case it is passed a file that is not intended for it. It expects lines 2,3 and 4 to have upload (or verify), the board name and the port name respectively. It does no checking of these values.

The Python code and a demo Arduino program are attached. The normal requirement for the .ino file to be in a directory of the same name applies.

In Geany's Build menu there is an option Set Build Commands which brings up a box where you can enter commands for different actions. I added a command called Upload and in the command box I entered
Code: [Select]
/mnt/sdb1/SGT-Prog/Arduino/GeanyPythonBuild/PythonBuildPuppy.py "%d/%f"
 The first part is the path and filename for my Python script (obviously change that to whatever is appropriate for your system) and the second part produces the path and filename for the Arduino sketch.

I have added .ino files to Geany's filetype_extensions.conf and as far as I can tell it only associates my Build mods with .ino files.

Demo Arduino Sketch
Code: [Select]
// python-build-start
// upload
// arduino:avr:uno
// /dev/ttyACM0
// python-build-end


void setup() {
Serial.begin(9600);
Serial.println("Testino starting");
}

void loop() {

}


Python Program
Code: [Select]
#!/usr/bin/env python

# simple program to compile and upload Arduino code using the Arduino command line

import subprocess
import sys

#       next line must be edited to be appropriate for your PC
arduinoProg = "/mnt/sdb1/Programs/arduino-1.5.6-r2/arduino"

projectFile = sys.argv[1]

codeFile = open(projectFile, 'r')
startLine = codeFile.readline()[3:].strip()
actionLine = codeFile.readline()[3:].strip()
boardLine = codeFile.readline()[3:].strip()
portLine = codeFile.readline()[3:].strip()
endLine = codeFile.readline()[3:].strip()
codeFile.close()

#~ print projectFile
#~ print startLine
#~ print actionLine
#~ print boardLine
#~ print portLine
#~ print endLine

if (startLine != "python-build-start" or endLine != "python-build-end"):
print "Sorry, can't process file"
sys.exit()

arduinoCommand = arduinoProg + " --" + actionLine + " --board " + boardLine + " --port " + portLine + " " + projectFile

print "\n\n -- Arduino Command --"
print arduinoCommand

print "-- Starting %s --\n" %(actionLine)

presult = subprocess.call(arduinoCommand, shell=True)

if presult != 0:
print "\n Failed - result code = %s --" %(presult)
else:
print "\n-- Success --"


Have fun.

...R

AndiTheVaultDweller

#1
Mar 24, 2015, 02:47 pm Last Edit: Mar 24, 2015, 02:55 pm by AndiTheVaultDweller
Hi Robin2

your code works like a charm and made my day!

After getting frustrated with ms-"free-but-register"-license-crap while using the execellent plugin for visualstudio and then getting even more frustrated configuring the eclipse-enviroment for arduino - even while eclipse is my main ide at work - it feels like coming home to a cosy fire & hot cocoa using geany and just hit a a key to verify or upload :D

I tweaked your code a little bit because i felt it more natural to have the switch for verify or upload in the command-line, so one can have for example verify on F8 and upload on F9.  

In place of the action-command i moved the arduino-prog line into the build-macro.

Kind of taste-induced changes but i thought maybe somebody else would like these too:

Demo Arduino Sketch
Code: [Select]
// python-build-start
// E:\\Arduino\\arduino.exe
// arduino:avr:uno
// /dev/ttyACM0
// python-build-end


void setup() {
 Serial.begin(9600);
 Serial.println("Testino starting");
}

void loop() {
 
}


Python Program
Code: [Select]
#!/usr/bin/env python

# simple program to compile and upload Arduino code using the Arduino command line

import subprocess
import sys

actionLine = sys.argv[1]
projectFile = sys.argv[2]

codeFile = open(projectFile, 'r')
startLine = codeFile.readline()[3:].strip()
arduinoProg = codeFile.readline()[3:].strip()
boardLine = codeFile.readline()[3:].strip()
portLine = codeFile.readline()[3:].strip()
endLine = codeFile.readline()[3:].strip()
codeFile.close()

#~ print projectFile
#~ print startLine
#~ print actionLine
#~ print boardLine
#~ print portLine
#~ print endLine

if (startLine != "python-build-start" or endLine != "python-build-end"):
 print("\n Error in build-commands - can't process file")
 sys.exit()

arduinoCommand = arduinoProg + " --" + actionLine + " --board " + boardLine + " --port " + portLine + " --verbose " + projectFile

print("\n\n -- Arduino Command --")
print(arduinoCommand)

print("-- Starting %s --\n" %(actionLine))

presult = subprocess.call(arduinoCommand, shell=True)

if presult != 0:
 print("\n Failed - result code = %s --" %(presult))
else:
 print("\n-- Success --")


geany commands:
Code: [Select]

E:\Arduino\PythonBuildPuppy.py verify "%d\%f"

E:\Arduino\PythonBuildPuppy.py upload "%d\%f"


Many thanks for that code Robin2!

atvd

Robin2

#2
Mar 24, 2015, 04:58 pm Last Edit: Mar 24, 2015, 05:01 pm by Robin2
Thanks for your interest and your kind words. I must try that.

...R

Go Up