mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Turns out we don't actually need 30MB of bloated jars to make a single HTTP post to get a Google SSO auth token. Don't need them for Firebase either. And not for Apple SSO. Shoot while we're at it, might as well get rid of pi4j too since making a JNI wrapper for PiGPio is easy enough.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.lanternsoftware.pigpio;
|
||||
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class PIGPIO {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(PIGPIO.class);
|
||||
|
||||
private PIGPIO() {
|
||||
}
|
||||
|
||||
static {
|
||||
try {
|
||||
String osArch = System.getProperty("os.arch").toLowerCase();
|
||||
if (osArch.equals("arm"))
|
||||
osArch = "armhf";
|
||||
String path = "/lib/" + osArch + "/lantern-pigpio.so";
|
||||
byte[] file = ResourceLoader.getByteArrayResource(PIGPIO.class, path);
|
||||
String target = Files.createTempFile("lantern-pigpio", "so").toAbsolutePath().toString();
|
||||
ResourceLoader.writeFile(target, file);
|
||||
System.load(target);
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Failed to load lantern-pigpio.so from resource", _e);
|
||||
}
|
||||
}
|
||||
|
||||
public static native int gpioInitialise();
|
||||
|
||||
public static native void gpioTerminate();
|
||||
|
||||
public static native int gpioSetMode(int gpio, int mode);
|
||||
|
||||
public static native int gpioGetMode(int gpio);
|
||||
|
||||
public static native int gpioSetPullUpDown(int gpio, int pud);
|
||||
|
||||
public static native int gpioRead(int gpio);
|
||||
|
||||
public static native int gpioWrite(int gpio, int level);
|
||||
|
||||
public static native int spiOpen(int spiChan, int baud, int spiFlags);
|
||||
|
||||
public static native int spiClose(int handle);
|
||||
|
||||
public static native int spiRead(int handle, byte[] buf, int offset, int count);
|
||||
|
||||
public static native int spiWrite(int handle, byte[] buf, int offset, int count);
|
||||
|
||||
public static native int spiXfer(int handle, byte[] txBuf, int txOffset, byte[] rxBuf, int rxOffset, int count);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.lanternsoftware.pigpio;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PiGpioFactory {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PIGPIO.class);
|
||||
private static final Map<Integer, Spi> spiHandles = new HashMap<>();
|
||||
private static boolean initialized = false;
|
||||
|
||||
public static Spi getSpiChannel(int _channel, int _baud, boolean _auxiliary) {
|
||||
ensureInitialized();
|
||||
int channelId = (0xff & _channel);
|
||||
if (_auxiliary)
|
||||
channelId |= 0x100;
|
||||
Spi handle = spiHandles.get(channelId);
|
||||
if (handle == null) {
|
||||
int h = PIGPIO.spiOpen(_channel, _baud, _auxiliary ? 0x100 : 0);
|
||||
if (h >= 0) {
|
||||
handle = new Spi(h);
|
||||
spiHandles.put(channelId, handle);
|
||||
}
|
||||
else {
|
||||
LOG.error("Failed to get SPI handle");
|
||||
}
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
private static void ensureInitialized() {
|
||||
if (initialized)
|
||||
return;
|
||||
int init = PIGPIO.gpioInitialise();
|
||||
LOG.info("GPIO init: {}", init);
|
||||
if (init < 0)
|
||||
LOG.error("Failed to initialize PiGpio");
|
||||
else
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public static void shutdown() {
|
||||
for (Spi handle : spiHandles.values()) {
|
||||
PIGPIO.spiClose(handle.getHandle());
|
||||
}
|
||||
spiHandles.clear();
|
||||
PIGPIO.gpioTerminate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.lanternsoftware.pigpio;
|
||||
|
||||
public class Spi {
|
||||
private final int handle;
|
||||
|
||||
public Spi(int _handle) {
|
||||
handle = _handle;
|
||||
}
|
||||
|
||||
public int getHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
public int read(byte[] buf) {
|
||||
return read(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
public int read(byte[] buf, int offset, int count) {
|
||||
return PIGPIO.spiRead(handle, buf, offset, count);
|
||||
}
|
||||
|
||||
public int write(byte[] buf) {
|
||||
return write(buf, 0, buf.length);
|
||||
}
|
||||
|
||||
public int write(byte[] buf, int offset, int count) {
|
||||
return PIGPIO.spiWrite(handle, buf, offset, count);
|
||||
}
|
||||
|
||||
public int transfer(byte[] txBuf, byte[] rxBuf) {
|
||||
return transfer(txBuf, 0, rxBuf, 0, rxBuf.length);
|
||||
}
|
||||
|
||||
public int transfer(byte[] txBuf, int txOffset, byte[] rxBuf, int rxOffset, int count) {
|
||||
return PIGPIO.spiXfer(handle, txBuf, txOffset, rxBuf, rxOffset, count);
|
||||
}
|
||||
}
|
||||
54
pigpio/lantern-pigpio/src/main/native/Makefile
Normal file
54
pigpio/lantern-pigpio/src/main/native/Makefile
Normal file
@@ -0,0 +1,54 @@
|
||||
ARCH := armhf
|
||||
DEBUG = -O3
|
||||
CC = $(CROSS_PREFIX)gcc
|
||||
AR = $(CROSS_PREFIX)ar
|
||||
RANLIB = $(CROSS_PREFIX)ranlib
|
||||
SIZE = $(CROSS_PREFIX)size
|
||||
STRIP = $(CROSS_PREFIX)strip
|
||||
SHLIB = $(CC) -shared
|
||||
STRIPLIB = $(STRIP) --strip-unneeded
|
||||
INCLUDE = -I. -Ipigpio \
|
||||
-I/$(JAVA_HOME)/include \
|
||||
-I/$(JAVA_HOME)/include/linux \
|
||||
-I/usr/local/include -I/usr/local/include/linux
|
||||
|
||||
CFLAGS := $(DEBUG) -Wall $(INCLUDE) -Winline -pipe $(CARGS) -fPIC
|
||||
LIBS = -L lib/$(ARCH) -L pigpio -lpigpio -lrt
|
||||
|
||||
TARGET=lantern-pigpio.so
|
||||
|
||||
###############################################################################
|
||||
|
||||
SRC = com_lanternsoftware_pigpio_PIGPIO.c
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
all: $(OBJ)
|
||||
@echo [LINK with DYNAMICALLY linked libraries]
|
||||
@$(CC) $(OBJ) -shared -o $(TARGET) $(INCLUDE) $(LIBS)
|
||||
|
||||
.c.o:
|
||||
@echo [COMPILE] $<
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET) *~ core tags Makefile.bak
|
||||
|
||||
tags: $(SRC)
|
||||
@echo [ctags]
|
||||
@ctags $(SRC)
|
||||
|
||||
depend:
|
||||
makedepend -Y $(SRC)
|
||||
|
||||
install: $(TARGET)
|
||||
@echo [install]
|
||||
install -m 0755 -d /usr/local/lib
|
||||
install -m 0755 -d /usr/local/include
|
||||
install -m 0644 $(TARGET) /usr/local/lib
|
||||
|
||||
uninstall:
|
||||
@echo [uninstall]
|
||||
rm -f /usr/local/lib/$(TARGET)
|
||||
|
||||
com_lanternsoftware_pigpio_PIGPIO.o: com_lanternsoftware_pigpio_PIGPIO.h
|
||||
@@ -0,0 +1,117 @@
|
||||
#include <jni.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pigpio.h>
|
||||
#include "com_lanternsoftware_pigpio_PIGPIO.h"
|
||||
|
||||
|
||||
JavaVM *callback_jvm;
|
||||
|
||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
|
||||
{
|
||||
JNIEnv *env;
|
||||
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2))
|
||||
{
|
||||
return JNI_ERR;
|
||||
}
|
||||
callback_jvm = jvm;
|
||||
return JNI_VERSION_1_2;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioInitialise
|
||||
(JNIEnv *env, jclass class)
|
||||
{
|
||||
gpioCfgSetInternals (gpioCfgGetInternals () | PI_CFG_NOSIGHANDLER);
|
||||
return gpioInitialise();
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioTerminate(JNIEnv *env, jclass class)
|
||||
{
|
||||
return gpioTerminate();
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioSetMode
|
||||
(JNIEnv *env, jclass class, jint gpio, jint mode)
|
||||
{
|
||||
return gpioSetMode((unsigned)gpio, (unsigned)mode);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioGetMode
|
||||
(JNIEnv *env, jclass class, jint gpio)
|
||||
{
|
||||
return gpioGetMode((unsigned)gpio);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioSetPullUpDown
|
||||
(JNIEnv *env, jclass class, jint gpio, jint pud)
|
||||
{
|
||||
return gpioSetPullUpDown((unsigned)gpio, (unsigned)pud);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioRead
|
||||
(JNIEnv *env, jclass class, jint gpio)
|
||||
{
|
||||
return gpioRead((unsigned)gpio);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioWrite
|
||||
(JNIEnv *env, jclass class, jint gpio, jint level)
|
||||
{
|
||||
return gpioWrite((unsigned)gpio, (unsigned)level);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiOpen
|
||||
(JNIEnv *env, jclass class, jint spiChan, jint baud, jint spiFlags)
|
||||
{
|
||||
return spiOpen((unsigned)spiChan, (unsigned)baud, (unsigned)spiFlags);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiClose
|
||||
(JNIEnv *env, jclass class, jint handle)
|
||||
{
|
||||
return spiClose((unsigned)handle);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiRead
|
||||
(JNIEnv *env, jclass class, jint handle, jbyteArray data, jint offset, jint count)
|
||||
{
|
||||
jbyte *buffer = (*env)->GetByteArrayElements(env, data, 0);
|
||||
jsize max_length = (*env)->GetArrayLength(env, data) - offset;
|
||||
int length = (count > max_length) ? max_length : count;
|
||||
jbyte *offsetBuffer = buffer + offset;
|
||||
jint result = spiRead((unsigned)handle, (char *)offsetBuffer, (unsigned)length);
|
||||
(*env)->ReleaseByteArrayElements(env, data, buffer, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiWrite
|
||||
(JNIEnv *env, jclass class, jint handle, jbyteArray data, jint offset, jint count)
|
||||
{
|
||||
jbyte *buffer = (*env)->GetByteArrayElements(env, data, 0);
|
||||
jsize max_length = (*env)->GetArrayLength(env, data) - offset;
|
||||
int length = (count > max_length) ? max_length : count;
|
||||
jbyte *offsetBuffer = buffer + offset;
|
||||
jint result = spiWrite((unsigned)handle, (char *)offsetBuffer, (unsigned)length);
|
||||
(*env)->ReleaseByteArrayElements(env, data, buffer, JNI_ABORT);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiXfer
|
||||
(JNIEnv *env, jclass class, jint handle, jbyteArray writeData, jint writeOffset, jbyteArray readData, jint readOffset, jint count)
|
||||
{
|
||||
jbyte *writeBuffer = (*env)->GetByteArrayElements(env, writeData, 0);
|
||||
jbyte *readBuffer = (*env)->GetByteArrayElements(env, readData, 0);
|
||||
jsize max_length = (*env)->GetArrayLength(env, writeData) - writeOffset;
|
||||
int length = (count > max_length) ? max_length : count;
|
||||
jbyte *offsetWriteBuffer = writeBuffer + writeOffset;
|
||||
jbyte *offsetReadBuffer = readBuffer + readOffset;
|
||||
jint result = spiXfer((unsigned)handle, (char *)offsetWriteBuffer, (char *)offsetReadBuffer, (unsigned)length);
|
||||
(*env)->ReleaseByteArrayElements(env, writeData, writeBuffer, JNI_ABORT);
|
||||
(*env)->ReleaseByteArrayElements(env, readData, readBuffer, 0);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class com_lanternsoftware_pigpio_PIGPIO */
|
||||
|
||||
#ifndef _Included_com_lanternsoftware_pigpio_PIGPIO
|
||||
#define _Included_com_lanternsoftware_pigpio_PIGPIO
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioInitialise
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioInitialise
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioTerminate
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioTerminate
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioSetMode
|
||||
* Signature: (II)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioSetMode
|
||||
(JNIEnv *, jclass, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioGetMode
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioGetMode
|
||||
(JNIEnv *, jclass, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioSetPullUpDown
|
||||
* Signature: (II)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioSetPullUpDown
|
||||
(JNIEnv *, jclass, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioRead
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioRead
|
||||
(JNIEnv *, jclass, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: gpioWrite
|
||||
* Signature: (II)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_gpioWrite
|
||||
(JNIEnv *, jclass, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: spiOpen
|
||||
* Signature: (III)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiOpen
|
||||
(JNIEnv *, jclass, jint, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: spiClose
|
||||
* Signature: (I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiClose
|
||||
(JNIEnv *, jclass, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: spiRead
|
||||
* Signature: (I[BII)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiRead
|
||||
(JNIEnv *, jclass, jint, jbyteArray, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: spiWrite
|
||||
* Signature: (I[BII)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiWrite
|
||||
(JNIEnv *, jclass, jint, jbyteArray, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_lanternsoftware_pigpio_PIGPIO
|
||||
* Method: spiXfer
|
||||
* Signature: (I[BI[BII)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_lanternsoftware_pigpio_PIGPIO_spiXfer
|
||||
(JNIEnv *, jclass, jint, jbyteArray, jint, jbyteArray, jint, jint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
Binary file not shown.
Reference in New Issue
Block a user