Yellow Rabbit

Frozen

Here is an active version

Software Configuration Variant

Kotlin and GPIO in Raspberry Pi

Last time we calculated the electrical circuit and checked it on the breadboard. I had such an LED that we need to provide 4mA on any GPIO. Now it’s up to the software.

I have to say that I’m going to light the LED from Kotlin:smiley:

Kotlin

I specify USE flags just for reference, in most cases they are already so. We build Intellij for Raspberry Pi on a Linux machine, where Intellij is already installed.

 git clone https://github.com/JetBrains/intellij-community.git

 ant  -Dintellij.build.skip.build.steps=mac_artifacts,mac_dmg,mac_sign,windows_exe_installer

Screenshot of Intellij on Raspberry Pi

Pigpio


diff --git a/pigpio.c b/pigpio.c
index 710d20f..7e38063 100644
--- a/pigpio.c
+++ b/pigpio.c
@@ -13296,6 +13296,13 @@ unsigned gpioHardwareRevision(void)
                   pi_mem_flag  = 0x04;
                }
             }
+           // arm64 /proc/cpuinfo does not contain "model name"
+           if (!strncasecmp("CPU architecture: 8", buf, 19)) {
+                  piCores = 4;
+                  pi_peri_phys = 0x3F000000;
+                  pi_dram_bus  = 0xC0000000;
+                  pi_mem_flag  = 0x04;
+           }
          }
 
          if (!strncasecmp("revision\t:", buf, 10))

Jpigpio

Changing java.library.path on the fly

In order to use Jpigpio, or rather libJPigpioC.so, I use a quick and dirty hack from Sun engineers:


package io.github.yrabbit.java.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class AddDir {
    public static void addDir(String s) throws IOException {
        try {
            // This enables the java.library.path to be modified at runtime
            // From a Sun engineer at http://forums.sun.com/thread.jspa?threadID=707176
            //
            Field field = ClassLoader.class.getDeclaredField("usr_paths");
            field.setAccessible(true);
            String[] paths = (String[])field.get(null);
            for (int i = 0; i < paths.length; i++) {
                if (s.equals(paths[i])) {
                    return;
                }
            }
            String[] tmp = new String[paths.length+1];
            System.arraycopy(paths,0,tmp,0,paths.length);
            tmp[paths.length] = s;
            field.set(null,tmp);
            System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + s);
        } catch (IllegalAccessException e) {
            throw new IOException("Failed to get permissions to set library path");
        } catch (NoSuchFieldException e) {
            throw new IOException("Failed to get field handle to set library path");
        }
    }
}

Project

The actual creation of the project in Intellij is quite straightforward. I do not think there will be any difficulties, but just in case I took a few screenshots. What could I forget? A couple of commands in the console:


mkdir -p src/main/kotlin src/main/java libs
ln -s ~/local/lib/Pigpio.jar libs/

First program for Raspberry Pi


package io.github.yrabbit.kotlin.led

import io.github.yrabbit.java.util.AddDir
import jpigpio.JPigpio.*
import jpigpio.Pigpio
import jpigpio.PigpioException
import jpigpio.Utils

fun main(args: Array<String>) {
    // dirty hack: need to point java.library.path to the location of libJPigpioC.so
    AddDir.addDir("/usr/local/lib")

    println("Kotlin Raspberry Pi gentoo64")
    println("You must see a blinking universe :)")
    try {
        // Init lib
        val pigpio = Pigpio()
        pigpio.gpioInitialize()
        Utils.addShutdown(pigpio)

        // Mode output
        pigpio.gpioSetMode(LED_PIN, PI_OUTPUT)

        repeat(3) {
            // ON
            pigpio.gpioWrite(LED_PIN, PI_HIGH)
            pigpio.gpioDelay(500 * 1000)
            // OFF
            pigpio.gpioWrite(LED_PIN, PI_LOW)
            pigpio.gpioDelay(500 * 1000)
        }
    } catch(e: PigpioException) {
        e.printStackTrace()
    }
    pigpio.gpioSetMode(LED_PIN, PI_INPUT)
}

const val LED_PIN = 26 // GPIO26

Connect to Raspberry Pi

Breadboard: Breadboard

Reality: Real Raspberry Pi connection and breadboard

Start

Run the resulting JAR: Start

Well, isn’t it cool? I’m happy :smile:

  1. This is necessary to add fast indexing of files to Idea. About this after. 

  2. The directory must exist.