Interested in such a question. Suppose I have some button that lies on the table. What are microcontrollers, devices, etc. can i use to realize the interaction of this button and java ?
- Do you want to run java on a microcontroller? - Mikhail Vaysman
- Well, I heard that there is such a card arduino, on which jvm works - Dmitriy Mironov
- there is one, but why? decide exactly what you want. - Mikhail Vaysman
- I need, for example, to read button presses, time and coordinates on the global map - Dmitriy Mironov
- oneadd this information to the question. - Mikhail Vaysman
1 answer
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 the GPIO ports, which are widely distributed in the embedded world. The button is connected with one pin to the port pin, the other to the ground (GND). Next, the microcontroller either does something itself, or via UART / USB / Ethernet / WiFi / Bluetooth sends a message about the pressed button to somewhere else.
Directly on the gland there are the following options.
Use a specialized microcontroller, sharpened by Java.
Yes, there are such. For example, the family of 3G modules Centerion. The firmware is a Java ME midlet and can also be flooded 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); } });
Use a full-fledged general-purpose embedded system based on an ARM processor, such as BeagleBone or RaspberryPi.
There you will have a full-fledged Linux with the ability to install full-fledged Java SE or Java SE Embedded. And the ports will be accessible through the file system as devices like
/sys/class/gpio/gpio49. You can interact with the port using the usual file I / O facilities (which will be relatively slow, although it is enough for many tasks) or via direct memory access through the memory-mapped file/dev/mem(which will be fast).But it will be much nicer to use the spread 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 the native port access API in the
com.oracle.deviceaccesspackage: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 both MIPS and Intel Atom. But the approaches there are the same.
Third-party Java-based software platforms look very interesting:
MicroEJ . There is a video on Youtube, where a java-application with graphics is executed on a very weak hardware ( Cortex ™ -M0 + @ 48 MHz, 256 KB Flash, 32 KB RAM ).
Google's Android Things , formerly known as Brillo.
Surprisingly, Java can be written under 8-bit AVR-microcontrollers (the ones that Arduino is built on)!
This was made possible by HaikuVM , which translates Java bytecode from .class files to C-structures, links the interpreter to them and forms the output for the usual AVR microcontroller HEX file, which is sewn into a piece of iron as usual.
There is also:
NanoVM is a Java subset virtual machine that occupies 8 KB in flash memory.
uJ - this VM is larger (tens of kilobytes), but promises full bytecode support, multithreading and
synchronized.
If you want to connect a button to an Arduino, and in turn connect it to a computer and do something on the computer by pressing a button, try JArduino . This API requires you to upload your firmware to Arduino, after which you can interact with the device from a regular Java program on your computer.