|
| 1 | +// This file is part of SmallBASIC |
| 2 | +// |
| 3 | +// This program is distributed under the terms of the GPL v2.0 or later |
| 4 | +// Download the GNU Public License (GPL) from www.gnu.org |
| 5 | +// |
| 6 | +// Copyright(C) 2020 Chris Warren-Smith |
| 7 | + |
| 8 | +#include "config.h" |
| 9 | +#include "include/var.h" |
| 10 | +#include "include/module.h" |
| 11 | +#include "include/param.h" |
| 12 | + |
| 13 | +#include <cstring> |
| 14 | +#include <cstdio> |
| 15 | +#include <jni.h> |
| 16 | +#include <pthread.h> |
| 17 | + |
| 18 | +JNIEnv *env; |
| 19 | +JavaVM *jvm; |
| 20 | +jclass analogInputClass = nullptr; |
| 21 | +jobject analogInput = nullptr; |
| 22 | + |
| 23 | +// void callVoidMethod(jmethodID method, int n) { |
| 24 | +// env->CallVoidMethod(instance, method, n); |
| 25 | +// } |
| 26 | + |
| 27 | +// jmethodID getMethodID(const char *name, const char *signature) { |
| 28 | +// return env->GetMethodID(clazz, name, signature); |
| 29 | +// } |
| 30 | + |
| 31 | +// jmethodID getStaticMethodId(const char *name, const char *signature) { |
| 32 | +// return env->GetStaticMethodID(clazz, name, signature); |
| 33 | +// } |
| 34 | + |
| 35 | +jobject createInstance(jclass clazz) { |
| 36 | + jobject result = nullptr; |
| 37 | + if (clazz == nullptr) { |
| 38 | + env->ExceptionDescribe(); |
| 39 | + } else { |
| 40 | + jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V"); |
| 41 | + if (constructor == nullptr) { |
| 42 | + env->ExceptionDescribe(); |
| 43 | + } else { |
| 44 | + result = env->NewObject(clazz, constructor); |
| 45 | + } |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +int sblib_init(const char *sourceFile) { |
| 51 | + JavaVMInitArgs vm_args; |
| 52 | + JavaVMOption options[2]; |
| 53 | + options[0].optionString = (char *)"-Djava.class.path=./target/ioio-1.0-jar-with-dependencies.jar"; |
| 54 | + options[1].optionString = (char *)"-Dioio.SerialPorts=ACM0"; |
| 55 | + vm_args.version = JNI_VERSION_1_8; |
| 56 | + vm_args.nOptions = 2; |
| 57 | + vm_args.options = options; |
| 58 | + vm_args.ignoreUnrecognized = 0; |
| 59 | + int result = (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) == JNI_OK); |
| 60 | + if (!result) { |
| 61 | + fprintf(stderr, "Unable to create JVM\n"); |
| 62 | + } |
| 63 | + return result; |
| 64 | +} |
| 65 | + |
| 66 | +void sblib_close(void) { |
| 67 | + jvm->DestroyJavaVM(); |
| 68 | + analogInputClass = nullptr; |
| 69 | + analogInput = nullptr; |
| 70 | + env = nullptr; |
| 71 | + jvm = nullptr; |
| 72 | +} |
| 73 | + |
| 74 | +static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) { |
| 75 | + int pin = get_param_int(argc, params, 0, 0); |
| 76 | + int result = 0; |
| 77 | + if (analogInput == nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) { |
| 78 | + analogInputClass = env->FindClass("net/sourceforge/smallbasic/ioio/AnalogInput"); |
| 79 | + analogInput = createInstance(analogInputClass); |
| 80 | + if (analogInput != nullptr) { |
| 81 | + jmethodID method = env->GetMethodID(analogInputClass, "openInput", "(I)V"); |
| 82 | + if (method != nullptr) { |
| 83 | + env->CallVoidMethod(analogInput, method, pin); |
| 84 | + result = 1; |
| 85 | + } else { |
| 86 | + env->ExceptionDescribe(); |
| 87 | + } |
| 88 | + } |
| 89 | + jvm->DetachCurrentThread(); |
| 90 | + } |
| 91 | + if (!result) { |
| 92 | + error(retval, "openAnalogInput() failed"); |
| 93 | + } |
| 94 | + return result; |
| 95 | +} |
| 96 | + |
| 97 | +FUNC_SIG lib_func[] = { |
| 98 | + {1, 1, "OPENANALOGINPUT", cmd_openanaloginput}, |
| 99 | +}; |
| 100 | + |
| 101 | +FUNC_SIG lib_proc[] = { |
| 102 | +}; |
| 103 | + |
| 104 | +SBLIB_API int sblib_proc_count() { |
| 105 | + return (sizeof(lib_proc) / sizeof(lib_proc[0])); |
| 106 | +} |
| 107 | + |
| 108 | +SBLIB_API int sblib_func_count() { |
| 109 | + return (sizeof(lib_func) / sizeof(lib_func[0])); |
| 110 | +} |
| 111 | + |
| 112 | + |
0 commit comments