|
17 | 17 |
|
18 | 18 | JNIEnv *env; |
19 | 19 | JavaVM *jvm; |
20 | | -jclass analogInputClass = nullptr; |
21 | | -jobject analogInput = nullptr; |
22 | 20 |
|
23 | | -jclass digitalOutputClass = nullptr; |
24 | | -jobject digitalOutput = nullptr; |
| 21 | +struct IOClass { |
| 22 | + IOClass(): _clazz(nullptr), _instance(nullptr) {} |
25 | 23 |
|
26 | | -jobject createInstance(jclass clazz) { |
27 | | - jobject result = nullptr; |
28 | | - if (clazz == nullptr) { |
29 | | - env->ExceptionDescribe(); |
30 | | - } else { |
31 | | - jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V"); |
32 | | - if (constructor == nullptr) { |
33 | | - env->ExceptionDescribe(); |
| 24 | + bool create(const char *path) { |
| 25 | + bool result; |
| 26 | + if (_instance != nullptr) { |
| 27 | + // error: already constructed |
| 28 | + result = false; |
34 | 29 | } else { |
35 | | - result = env->NewObject(clazz, constructor); |
| 30 | + _clazz = env->FindClass(path); |
| 31 | + if (_clazz == nullptr) { |
| 32 | + env->ExceptionDescribe(); |
| 33 | + } else { |
| 34 | + jmethodID constructor = env->GetMethodID(_clazz, "<init>", "()V"); |
| 35 | + if (constructor == nullptr) { |
| 36 | + env->ExceptionDescribe(); |
| 37 | + } else { |
| 38 | + _instance = env->NewObject(_clazz, constructor); |
| 39 | + } |
| 40 | + } |
| 41 | + result = _instance != nullptr; |
36 | 42 | } |
| 43 | + return result; |
37 | 44 | } |
38 | | - return result; |
39 | | -} |
40 | 45 |
|
41 | | -int invokeIV(jclass clazz, jobject instance, const char *name, int value) { |
42 | | - int result = 0; |
43 | | - if (instance != nullptr) { |
44 | | - jmethodID method = env->GetMethodID(clazz, name, "(I)V"); |
45 | | - if (method != nullptr) { |
46 | | - env->CallVoidMethod(instance, method, value); |
47 | | - result = 1; |
48 | | - } else { |
49 | | - env->ExceptionDescribe(); |
| 46 | + bool invoke(const char *name, int value) { |
| 47 | + bool result = false; |
| 48 | + if (_instance != nullptr) { |
| 49 | + jmethodID method = env->GetMethodID(_clazz, name, "(I)V"); |
| 50 | + if (method != nullptr) { |
| 51 | + env->CallVoidMethod(_instance, method, value); |
| 52 | + result = true; |
| 53 | + } else { |
| 54 | + env->ExceptionDescribe(); |
| 55 | + } |
50 | 56 | } |
| 57 | + return result; |
51 | 58 | } |
52 | | - return result; |
53 | | -} |
54 | 59 |
|
55 | | -int invokeOpen(jclass clazz, jobject instance, int pin) { |
56 | | - return invokeIV(clazz, instance, "open", pin); |
57 | | -} |
| 60 | + bool open(int pin) { |
| 61 | + return invoke("open", pin); |
| 62 | + } |
| 63 | + |
| 64 | + bool write(int value) { |
| 65 | + return invoke("write", value); |
| 66 | + } |
| 67 | + |
| 68 | + private: |
| 69 | + jclass _clazz; |
| 70 | + jobject _instance; |
| 71 | +}; |
| 72 | + |
| 73 | +IOClass analogInput; |
| 74 | +IOClass digitalOutput; |
58 | 75 |
|
59 | 76 | static void cmd_digital_output_write(var_s *self, var_s *retval) { |
60 | | - int value = 0; |
61 | | - if (digitalOutput != nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) { |
62 | | - invokeIV(digitalOutputClass, digitalOutput, "write", value); |
63 | | - jvm->DetachCurrentThread(); |
64 | | - } |
| 77 | + static int value = !value; |
| 78 | + digitalOutput.write(value); |
65 | 79 | } |
66 | 80 |
|
67 | 81 | static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) { |
| 82 | + int result; |
68 | 83 | int pin = get_param_int(argc, params, 0, 0); |
69 | | - int result = 0; |
70 | | - if (analogInput == nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) { |
71 | | - analogInputClass = env->FindClass("net/sourceforge/smallbasic/ioio/AnalogInput"); |
72 | | - analogInput = createInstance(analogInputClass); |
73 | | - result = invokeOpen(analogInputClass, analogInput, pin); |
74 | | - jvm->DetachCurrentThread(); |
75 | | - } |
76 | | - if (!result) { |
| 84 | + if (analogInput.create("net/sourceforge/smallbasic/ioio/AnalogInput") && |
| 85 | + analogInput.open(pin)) { |
| 86 | + result = 1; |
| 87 | + } else { |
77 | 88 | error(retval, "openAnalogInput() failed"); |
| 89 | + result = 0; |
78 | 90 | } |
79 | 91 | return result; |
80 | 92 | } |
81 | 93 |
|
82 | 94 | static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) { |
| 95 | + int result; |
83 | 96 | int pin = get_param_int(argc, params, 0, 0); |
84 | | - int result = 0; |
85 | | - if (digitalOutput == nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) { |
86 | | - digitalOutputClass = env->FindClass("net/sourceforge/smallbasic/ioio/DigitalOutput"); |
87 | | - digitalOutput = createInstance(digitalOutputClass); |
88 | | - if (digitalOutput != nullptr) { |
89 | | - result = invokeOpen(digitalOutputClass, digitalOutput, pin); |
90 | | - } |
91 | | - jvm->DetachCurrentThread(); |
92 | | - } |
93 | | - if (!result) { |
94 | | - error(retval, "openDigitalOutput() failed"); |
95 | | - } else { |
| 97 | + if (digitalOutput.create("net/sourceforge/smallbasic/ioio/DigitalOutput") && |
| 98 | + digitalOutput.open(pin)) { |
96 | 99 | map_init(retval); |
97 | 100 | v_create_func(retval, "write", cmd_digital_output_write); |
| 101 | + result = 1; |
| 102 | + } else { |
| 103 | + error(retval, "openDigitalOutput() failed"); |
| 104 | + result = 0; |
98 | 105 | } |
99 | 106 | return result; |
100 | 107 | } |
@@ -124,17 +131,17 @@ int sblib_init(const char *sourceFile) { |
124 | 131 | vm_args.nOptions = 2; |
125 | 132 | vm_args.options = options; |
126 | 133 | vm_args.ignoreUnrecognized = 0; |
127 | | - int result = (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) == JNI_OK); |
| 134 | + int result = (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) == JNI_OK && |
| 135 | + jvm->AttachCurrentThread((void **)&env, nullptr) == JNI_OK); |
128 | 136 | if (!result) { |
129 | | - fprintf(stderr, "Unable to create JVM\n"); |
| 137 | + fprintf(stderr, "Failed to create JVM\n"); |
130 | 138 | } |
131 | 139 | return result; |
132 | 140 | } |
133 | 141 |
|
134 | 142 | void sblib_close(void) { |
| 143 | + jvm->DetachCurrentThread(); |
135 | 144 | jvm->DestroyJavaVM(); |
136 | | - analogInputClass = nullptr; |
137 | | - analogInput = nullptr; |
138 | 145 | env = nullptr; |
139 | 146 | jvm = nullptr; |
140 | 147 | } |
|
0 commit comments