Browse files

Use system and user preferences instead of hacking in our own

  • Loading branch information...
1 parent 7b285ef commit 738e5274db4d7d2c873df1e1ab59705e0b76aa4e @tclem committed Jun 20, 2012
Showing with 84 additions and 25 deletions.
  1. +84 −25 src/main/java/com/github/tclem/arduinocli/MyBase.java
View
109 src/main/java/com/github/tclem/arduinocli/MyBase.java
@@ -1,10 +1,14 @@
package com.github.tclem.arduinocli;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FilenameFilter;
+import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Arrays;
+import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Hashtable;
import java.util.Map;
import processing.app.Base;
@@ -25,6 +29,16 @@ public MyBase(String[] args) {
static public String init(String[] args) {
+ // On OS X this should be: /Applications/Arduino.app/Contents/Resources/Java/
+ String path = System.getProperty("java.library.path");
+ if (!path.endsWith("/")) path = path + "/";
+ System.out.printf("javaroot: %s\n", path);
+ System.setProperty("user.dir", path); // Used by *nix
+ System.setProperty("javaroot", path); // Used by OS X
+
+ initPlatform();
+ loadPreferences();
+
if (args.length < 1) {
throw new IllegalArgumentException(
"You must specify -c (compile) or -d (compile and deploy).");
@@ -46,45 +60,90 @@ static public String init(String[] args) {
System.out.printf("Compiling%s file: %s\n", deploy ? " and deploying"
: "", pdeFile);
- System.out.printf("Using board: %s\n", board);
- System.out.printf("Using serial port: %s\n", serialPort);
+ System.out.printf("board: %s\n", board);
+ System.out.printf("serial port: %s\n", serialPort);
// Mock out the Arduino IDE minimal setup
imports = new HashMap<String, File>();
targetsTable = new HashMap<String, Target>();
- // On OS X this should be: /Applications/Arduino.app/Contents/Resources/Java/
- String path = System.getProperty("java.library.path");
- if (!path.endsWith("/")) path = path + "/";
- System.out.printf("Using javaroot: %s\n", path);
- System.setProperty("user.dir", path); // Used by *nix
- System.setProperty("javaroot", path); // Used by OS X
-
- Preferences.set("sketchbook.path", System.getProperty("arduino.sketchbook"));
-
loadHardware2(getHardwareFolder());
loadHardware2(getSketchbookHardwareFolder());
+ addLibraries();
+
+ return pdeFile;
+ }
+
+ private static void loadPreferences() {
+ try {
+ load(getLibStream("preferences.txt"));
+ load(new FileInputStream(getSettingsFile("preferences.txt")));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
- Preferences.setInteger("editor.tabs.size", 2);
+ private static void load(InputStream input) {
+ Hashtable table = new Hashtable();
+
+ String[] lines = MySketchCode.loadStrings(input); // Reads as UTF-8
+ for (String line : lines) {
+ if ((line.length() == 0) ||
+ (line.charAt(0) == '#')) continue;
+
+ // this won't properly handle = signs being in the text
+ int equals = line.indexOf('=');
+ if (equals != -1) {
+ String key = line.substring(0, equals).trim();
+ String value = line.substring(equals + 1).trim();
+ table.put(key, value);
+ }
+ }
+
+ // check for platform-specific properties in the defaults
+ String platformExt = "." + platformNames[pa_platform];
+ int platformExtLength = platformExt.length();
+ Enumeration e = table.keys();
+ while (e.hasMoreElements()) {
+ String key = (String) e.nextElement();
+ String value = (String) table.get(key);
+ if (key.endsWith(platformExt)) {
+ // this is a key specific to a particular platform
+ key = key.substring(0, key.length() - platformExtLength);
+ }
+
+ Preferences.set(key, value);
+ System.out.printf("%s = %s\n", key, value);
+ }
+ }
- Preferences.set("target", "arduino");
- Preferences.set("upload.using", "bootloader");
+ // platform IDs for PApplet.platform
- Preferences.set("board", board);
- Preferences.set("serial.port", serialPort);
+ static final int OTHER = 0;
+ static final int WINDOWS = 1;
+ static final int MACOSX = 2;
+ static final int LINUX = 3;
- Preferences.setInteger("serial.databits", 8);
- Preferences.setInteger("serial.stopbits", 1);
- Preferences.set("serial.parity", "N");
- Preferences.setInteger("serial.debug_rate", 9600);
+ static final String[] platformNames = {
+ "other", "windows", "macosx", "linux"
+ };
+ static public int pa_platform;
+ static {
+ String osname = System.getProperty("os.name");
- Preferences.setBoolean("upload.verbose", true);
- Preferences.set("programmer", "avrispmkii");
+ if (osname.indexOf("Mac") != -1) {
+ pa_platform = MACOSX;
- addLibraries();
+ } else if (osname.indexOf("Windows") != -1) {
+ pa_platform = WINDOWS;
- return pdeFile;
- }
+ } else if (osname.equals("Linux")) { // true for the ibm vm
+ pa_platform = LINUX;
+
+ } else {
+ pa_platform = OTHER;
+ }
+ }
private static void addLibraries() {
try {

0 comments on commit 738e527

Please sign in to comment.