Microcontrollers and Java


I am interested in this question. Let's say I have some button that is lying on the table. What microcontrollers, devices, etc. can be used to implement the interaction of this button and java?

Author: Nofate, 2016-12-28

1 answers

Since Java was originally developed for programming refrigerators and coffee makers, it would be strange if the ability to work with iron was completely lost.

The easiest way to connect a button to a microcontroller is through GPIO ports, which are widely used in the embedded world. The button is connected to the port pin with one pin, and to the ground (GND) with the other. Then the microcontroller either does something itself, or sends it via UART/USB/Ethernet/WiFi/Bluetooth a message about a button being pressed somewhere else.

Directly on the hardware, there are the following options.

  1. Use a specialized microcontroller, sharpened for Java.

    Yes, there are some. For example, the Centerion family of 3G modules. The firmware is a Java ME midlet and can be filled in, including over the air. Listening to the port looks something like this:

    Vector inPins = new Vector();
    inPins.addElement("GPIO11");
    
    InPort inPort = new InPort(inPins);  
    inPort.addListener(new InPortListener() {
        public void portValueChanged(int val) {
            System.out.println("Port value: " + val);
        }
    });
    

  1. Use a full-fledged embedded system a general-purpose ARM-based processor, such as BeagleBone or RaspberryPi.

    There you will have a full-fledged Linux with the ability to install a full-fledged Java SE or Java SE Embedded. And the ports will be accessible via the file system as devices like /sys/class/gpio/gpio49. You can interact with the port using the usual file I / O tools (which will be relatively slow, although it is sufficient for many tasks), or via direct access to memory via a memory-mapped file /dev/mem (which will be quickly).

    But it will be much more pleasant to use the weighty API of the third-party library pi4j:

    GpioController gpioController = GpioFactory.getInstance();
    GpioPinDigitalInput pin02 = gpioController.provisionDigitalInputPin(RaspiPin.GPIO_02,PinPullResistance.PULL_DOWN);
    pin02.addListener(new GpioPinListenerDigital() {
        @Override
        public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent gpioPinDigitalStateChangeEvent) {
            System.out.println("state: " + gpioPinDigitalStateChangeEvent.getState());
        }
    });
    

    In addition, there is also Java ME Embedded, which provides a native API for accessing ports in the package com.oracle.deviceaccess:

    GPIOPin switchPin = null;
    switchPin = (GPIOPin) PeripheralManager.open(1);
    switchPin.setInputListener(new PinListener() {
        @Override
        public void valueChanged(PinEvent event) {
           // do something
        }
    });
    

    By the way, life here is not limited to ARM, there are also MIPS and Intel Atom. But the approaches are the same.

    Third-party Java-based software platforms are very interesting:

    • MicroEJ. On Youtube there is a video where a java application with graphics is executed on very weak hardware (Cortex-M0+ @48 MHz, 256 KB Flash, 32 KB RAM).

    • Google's Android Things, formerly known as Brillo.


  1. Surprisingly, you can write in Java for 8-bit AVR microcontrollers (the same ones that Arduino is built on)!

    This is made possible by HaikuVM, which translates Java bytecode from . class-files in C-structures, links the interpreter to them and forms the output of the usual HEX file for AVR microcontrollers, which is sewn into the hardware as usual.

    Still have:

    • NanoVM is a Java subset virtual machine that occupies 8 kB of flash memory.

    • UJ - this VM is larger (tens of kilobytes), but promises full bytecode support, multithreading, and synchronized.

    If you want to connect the nopka to Arduino, and it in turn connect to the computer and do something on the computer at the touch of a button, try JArduino. This API requires you to upload your firmware to the Arduino, after which you can interact with the device from a regular Java program on your computer.

 12
Author: Nofate, 2016-12-29 14:22:58