Arduino und Java?

Ich wollte mal Fragen ob man die
Arduinos mit Java programmieren kann. Die Ide
ist soweit ich weiß in Java programmiert.

Addi

Ich wollte mal Fragen ob man die
Arduinos mit Java programmieren kann.

Nein, kann man nicht. Java ist für eine Plattform mit 2kB Hauptspeicher nicht geeignet, schon alleine die Speicherverwaltung (Garbage Collection) ist auf solchen Systemen absolut unbrauchbar.

Also wenn man vorsichtig ist und ein paar Konventionen beachtet, dann ist C/C++ eh das Gleiche wie Java. ]:smiley:

Meine Motorrad-Ganganzeige z.B. läuft sowohl auf dem Arduino als auch als Java-Applet im Web, für die abschließende Konvertierung hab ich ein kleines Script geschrieben und die Hardware-nahen Funktionen eben doppelt, aber über 70% des gesamten Codes und 100% der eigentlichen Logik wird von beiden Seiten benutzt. :slight_smile:

Mann müsste eben recht atomar bleiben und sollte nicht übermäßig Objekte benutzen, sonst wirds unötig kompliziert...

Daran habe ich auh gedacht, kann man
aber vielleicht den Code noch auf dem Computer
von "Java-Code" zu Maschinencode konvertieren?

Das ist zum Beispiel meine Java-Klasse um EEPROM zu emulieren:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class EEPROM {

        private static final String FILE = "eeprom_properties.xml";

        private static final Properties properties = new Properties();

        public static void setProperty(final String key, final String value) {
                properties.setProperty(key, value);
                try {
                        properties.storeToXML(new FileOutputStream(FILE),
                                        "Local properties file for Joghurts 'BikeComp' project. " +
                                                        "See 'http://arduino.dg4sfw.de/bikecomp/' for further information.");
                } catch (Exception e) {
                        System.out.println("Eeprom.setProperty(" + key + ", " + value + "): " + e.getMessage());
                }
        }

        public static String getProperty(final String key) {
                try {
                        properties.loadFromXML(new FileInputStream(FILE));
                } catch (Exception e) {
                        System.out.println("Eeprom.getProperty(" + key + "): " + e.getMessage());
                }
                return properties.getProperty(key);
        }

        public static char read(int pos) {
                final String value = getProperty("" + pos);
                return value != null ? value.charAt(0) : 0;
        }

        public static void write(int pos, char value) {
                setProperty("" + pos, "" + value);
        }
}

Ds1307

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

public class Ds1307 extends Thread {

        private static final String FILE = "ds1307_properties.xml";

        private final Properties properties = new Properties();
        private byte[] storage = new byte[64];

        /**
         * 0 -> Seconds         00..59 / uppermost bit is start(0)/stop(1)
         * 1 -> Minutes         00..59
         * 2 -> Hours           00..23
         * 3 -> Weekday         0(Monday)..6(Sunday)
         * 4 -> Day                     1..28|29|30|31
         * 5 -> Month           1..12
         * 6 -> Year            00..99
         * 7 -> Settings
         * 8..63 can be used for whatever you want!
         */

        public Ds1307() {
                try {
                        properties.loadFromXML(new FileInputStream(FILE));
                        for (int t = 8; t < 64; t++) {
                                String value = null;
                                try {
                                        value = "" + t;
                                        if (properties.getProperty(value) != null)
                                                storage[t] = (byte) Integer.parseInt(properties.getProperty(value));
                                } catch (Exception e) {
                                        System.out.println("Ds1307.getProperty(" + value + "): " + e.getMessage());
                                }
                        }
                } catch (Exception e) {
                        System.out.println("Ds1307.loadFromXML(): " + e.getMessage());
                }

                // The RTC runs in Standard Time!! Daylight Saving Time has one more hour!
                // final Calendar calendar = new GregorianCalendar(2010, 9, 31, 2, 59, 50); // Switch from (Daylight Savings Time)
                // final Calendar calendar = new GregorianCalendar(2010,2,28,1,59,50); // Switch from (Standard Time)
                final Calendar calendar = new GregorianCalendar();

                // Remove daylight savings compensation because the DS1307 won't compensate, too!
                calendar.add(Calendar.HOUR_OF_DAY, -calendar.get(Calendar.DST_OFFSET) / 3600000);

                storage[0] = decToBcd(calendar.get(Calendar.SECOND));
                storage[1] = decToBcd(calendar.get(Calendar.MINUTE));
                storage[2] = decToBcd(calendar.get(Calendar.HOUR_OF_DAY));
                storage[3] = decToBcd((calendar.get(Calendar.DAY_OF_WEEK) + 6) % 7);
                storage[4] = decToBcd(calendar.get(Calendar.DAY_OF_MONTH));
                storage[5] = decToBcd(calendar.get(Calendar.MONTH) + 1);
                storage[6] = decToBcd(calendar.get(Calendar.YEAR) % 100);
                start();
        }

        public void run() {
                //noinspection InfiniteLoopStatement
                while (true) {
                        try {
                                Thread.sleep(1000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }

                        int second = bcdToDec(storage[0]);
                        int minute = bcdToDec(storage[1]);
                        int hour = bcdToDec(storage[2]);
                        int weekday = bcdToDec(storage[3]);
                        int day = bcdToDec(storage[4]);
                        int month = bcdToDec(storage[5]);
                        int year = bcdToDec(storage[6]);

                        // Clock only runs if start/stop-bit is NOT set!
                        if ((second & 0x80) == 0)
                                second++;

                        if (second == 60) {
                                second = 0;
                                minute++;
                        }
                        if (minute == 60) {
                                minute = 0;
                                hour++;
                        }
                        if (hour == 24) {
                                hour = 0;
                                day++;
                                weekday = (weekday + 1) % 7;
                        }
                        if (day == 30 && month == 2 && year % 4 == 0 ||
                                        day == 29 && month == 2 && year % 4 != 0 ||
                                        day == 31 && (month == 4 || month == 6 || month == 9 || month == 11) ||
                                        day == 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
                                day = 1;
                                month++;
                        }
                        if (month == 13) {
                                month = 1;
                                year++;
                        }
                        if (year == 100) {
                                year = 0;
                        }

                        storage[0] = decToBcd(second);
                        storage[1] = decToBcd(minute);
                        storage[2] = decToBcd(hour);
                        storage[3] = decToBcd(weekday);
                        storage[4] = decToBcd(day);
                        storage[5] = decToBcd(month);
                        storage[6] = decToBcd(year);
                }
        }

        public void send(final int address, final byte data) {
                storage[address % 64] = data;
                if (address % 64 > 7) {
                        properties.setProperty("" + (address % 64), "" + ((int) data & 0xff));
                        try {
                                properties.storeToXML(new FileOutputStream(FILE),
                                                "Local properties file for Joghurts 'BikeComp' project. " +
                                                                "See 'http://arduino.dg4sfw.de/bikecomp/' for further information.");
                        } catch (Exception e) {
                                System.out.println("Ds1307.storeToXML():" + e.getMessage());
                        }
                }
        }

        public byte receive(final int address) {
                return storage[address % 64];
        }

        private int bcdToDec(final byte bcd) {
                return bcd / 16 * 10 + (bcd % 16);
        }

        private byte decToBcd(final int dec) {
                return (byte) ((dec / 10 << 4) + dec % 10);
        }
}

Wire

public class Wire extends Thread {

        public static final int DS1307 = 0x68;

        private static Ds1307 ds1307;
        private static int activeDevice = -1, activeAddress = -1, activeAmount;

        public static void begin() {
        }

        public static void beginTransmission(final int device) {
                activeDevice = device;
                activeAddress = -1;
        }

        public static void send(final byte data) {
                if (activeAddress == -1) {
                        activeAddress = data; // the first byte is used for addressing!!
                } else {
                        if (activeDevice == DS1307) {
                                if (ds1307 == null)
                                        ds1307 = new Ds1307();
                                if (activeAddress > 63)
                                        System.out.println("Wire: Address overflow! " + Thread.currentThread().getStackTrace()[2]);
                                ds1307.send(activeAddress++, data);
                        } else
                                throw new RuntimeException("No or unknown active device: " + activeDevice);
                }
        }

        public static void endTransmission() {
                activeDevice = -1;
                // Do NOT reset "activeAddress"-pointer or this will crash when trying to receive something!
                activeAmount = 0;
        }

        public static void requestFrom(final int device, final int amount) {
                activeDevice = device;
                activeAmount = amount;
        }

        public static byte receive() {
                if (activeAddress == -1)
                        throw new RuntimeException("Address not set!");
                if (activeDevice == DS1307) {
                        if (ds1307 == null)
                                ds1307 = new Ds1307();
                        if (--activeAmount < 0)
                                throw new RuntimeException("Amount too small!");
                        else {
                                if (activeAddress > 63)
                                        System.out.println("Wire: Address overflow! " + Thread.currentThread().getStackTrace()[2]);
                                return ds1307.receive(activeAddress++);
                        }
                } else
                        throw new RuntimeException("No or unknown active device: " + activeDevice);
        }
}

Addi:
kann man aber vielleicht den Code noch auf dem Computer von "Java-Code" zu Maschinencode konvertieren?

Wie meinst Du das, Java-Sourcecode in Arduino-Binary umwandeln?

Guck Dir mal meinen Source an...
Ich hab da noch zwei kleine Scripte, die die voranstehenden Kommentare entsprechend umstellen, damit es dann anschließend für Java oder Arduino compiliert.

Was verstehst du unter Java ?
Geschweifte Klammern und Semikolons sind leicht nach c zu konvertieren.
Die Java Runtime ( import java.io.* ) kannst du vergessen.