Alright.. this is nothing huge.. i just prepared it yesterday and wrote some lines today i stopped it today evening already again as i got some new 'project' at the university to attend.. hence i will give whoever wants it the code for a more or less standalone ardiono 'compiler' and uploader.
It compiles all libraries nicely and links them into the o's and a's but it doesnt want to compile the sketch. Uploading is not yet implemented, but i wanted to finish compiling first.
Done in VS10(beta) with Net3.5 - but should be compatible with every .Net version i know..
Main program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace ArduinoNPP
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Commadnline compiler");
Console.WriteLine("By martin@shalmirane.de");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Starting");
Console.WriteLine();
string gcc = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-gcc";
string gpp = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-g++";
string ar = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-ar";
string objcp = ConfigurationManager.AppSettings.Get("path_to_arduino") + @"\hardware\tools\avr\bin\avr-objcopy";
string mcu = ConfigurationManager.AppSettings.Get("mcu");
string df_cpu = ConfigurationManager.AppSettings.Get("df_cpu");
string temp = ConfigurationManager.AppSettings.Get("path_to_temp_files");
string ardu = ConfigurationManager.AppSettings.Get("path_to_arduino");
Console.WriteLine("");
Console.WriteLine("Parsing PDE");
string sketch = "";
if (args[0].EndsWith(".pde"))
{
sketch = File.ReadAllText(args[0]);
}
//Read file and convert from PDE to cpp...
/*
ArrayList list = new ArrayList();
//Find all functions
string pat = @"(?i)void.[^{]*"; //Need to change towards int, char etc....
string text = sketch;
foreach (Match match in Regex.Matches(text,pat))
{
int index = match.Index;
while (text[index++] != '{') ;
int bracketCount = 1;
while (bracketCount > 0)
{
if (text[index] == '{')
bracketCount++;
else if (text[index] == '}')
bracketCount--;
index++;
}
string methodContent = text.Substring(match.Index, index - match.Index);
list.Add(methodContent);
// Console.WriteLine(methodContent);
}
string setup = "";
string loop = "";
int i = -1;
foreach (string fn in list)
{
i++;
if(fn.IndexOf("setup()")>0)
{
setup = fn;
list.Remove(i);
}
if(fn.IndexOf("loop()")>0)
{
loop = fn;
list.Remove(i);
}
}
*/
Console.WriteLine("Compiling libraries");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu="+mcu+" -DF_CPU="+df_cpu+@" -I"+ardu+@"\hardware\cores\arduino "+ardu+@"\hardware\cores\arduino\pins_arduino.c -o"+temp+@"\pins_arduino.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\WInterrupts.c -o" + temp + @"\WInterrupts.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring.c -o" + temp + @"\wiring.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_analog.c -o" + temp + @"\wiring_analog.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_digital.c -o" + temp + @"\wiring_digital.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_pulse.c -o" + temp + @"\wiring_pulse.c.o");
run(gcc, @"-c -g -Os -w -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + @" -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\wiring_shift.c -o" + temp + @"\wiring_shift.c.o");
run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\HardwareSerial.cpp -o" + temp + @"\HardwareSerial.cpp.o");
run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\Print.cpp -o" + temp + @"\Print.cpp.o");
run(gpp, @"-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + @" -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + ardu + @"\hardware\cores\arduino\WMath.cpp -o" + temp + @"\WMath.cpp.o");
Console.WriteLine("");
Console.WriteLine("Linking");
run(ar,"rcs " + temp + @"core.a " + temp +@"pins_arduino.c.o");
run(ar,"rcs " + temp + @"core.a " + temp +@"WInterrupts.c.o" );
run(ar,"rcs " + temp + @"core.a " + temp +@"wiring.c.o" );
run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_analog.c.o");
run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_digital.c.o" );
run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_pulse.c.o" );
run(ar,"rcs " + temp + @"core.a " + temp +@"wiring_shift.c.o");
run(ar,"rcs " + temp + @"core.a " + temp +@"HardwareSerial.cpp.o");
run(ar,"rcs " + temp + @"core.a " + temp +@"Print.cpp.o");
run(ar,"rcs " + temp + @"core.a " + temp +@"WMath.cpp.o");
Console.WriteLine("");
Console.WriteLine("Compiling sketch");
FileInfo fi = new FileInfo(args[0]);
string fullpath = fi.FullName;
string filename = fi.Name;
Console.WriteLine(temp + filename);
run(gpp, "-c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -mmcu=" + mcu + " -DF_CPU=" + df_cpu + " -I" + ardu + @"\hardware\cores\arduino " + fullpath + " -o" + temp + filename + ".o");
run(gcc, " -Os -Wl,--gc-sections -mmcu=" + mcu + @" -o " + temp + filename + ".elf " + temp + filename + ".o " + temp + "core.a -L" + temp + @"file.tmp -lm ");
run(objcp, "-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 " + temp + @"\" + filename + ".elf " + temp + @"\" + filename + ".eep ");
run(objcp, " -O ihex -R .eeprom " + temp + filename + ".elf " + temp + filename + ".hex ");
Console.WriteLine("");
Console.WriteLine("Upload");
Console.WriteLine("...umm - not yet...");
Console.WriteLine("");
Console.WriteLine("Done. Press ENTER to exit");
Console.ReadLine();
}
public static string run(string filename, string arguments)
{
System.IO.StreamReader sr;
Process builder = new Process();
builder.StartInfo.RedirectStandardOutput = true;
builder.StartInfo.UseShellExecute = false;
builder.StartInfo.CreateNoWindow = true;
builder.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
builder.StartInfo.FileName = filename;
builder.StartInfo.Arguments = arguments;
builder.Start();
builder.WaitForExit();
sr = builder.StandardOutput;
Console.Write(".");
return "";
}
}