This repository was archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPwm.java
More file actions
380 lines (344 loc) · 12.2 KB
/
Copy pathPwm.java
File metadata and controls
380 lines (344 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (c) Steven P. Goldsmith. All rights reserved.
*/
package com.codeferm.periphery;
import static com.codeferm.periphery.Common.MAX_CHAR_ARRAY_LEN;
import static com.codeferm.periphery.Common.jString;
import static com.codeferm.periphery.Common.memMove;
import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT;
import org.fusesource.hawtjni.runtime.JniClass;
import org.fusesource.hawtjni.runtime.JniField;
import org.fusesource.hawtjni.runtime.JniMethod;
import org.fusesource.hawtjni.runtime.Library;
import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER;
/**
* c-periphery PWM wrapper functions for Linux userspace sysfs PWMs.
*
* @author Steven P. Goldsmith
* @version 1.0.0
* @since 1.0.0
*/
@JniClass
public class Pwm implements AutoCloseable {
/**
* Function was successful.
*/
public static final int PWM_SUCCESS = 0;
/**
* java-periphery library.
*/
private static final Library LIBRARY = new Library("java-periphery", Pwm.class);
/**
* PWM handle.
*/
final private long handle;
/**
* Load library.
*/
static {
LIBRARY.load();
init();
}
/**
* Load constants.
*/
@JniMethod(flags = {CONSTANT_INITIALIZER})
private static native void init();
/**
* Error constants.
*/
@JniField(flags = {CONSTANT})
public static int PWM_ERROR_ARG;
@JniField(flags = {CONSTANT})
public static int PWM_ERROR_OPEN;
@JniField(flags = {CONSTANT})
public static int PWM_ERROR_QUERY;
@JniField(flags = {CONSTANT})
public static int PWM_ERROR_CONFIGURE;
@JniField(flags = {CONSTANT})
public static int PWM_ERROR_CLOSE;
/**
* Polarity constants.
*/
@JniField(flags = {CONSTANT})
public static int PWM_POLARITY_NORMAL;
@JniField(flags = {CONSTANT})
public static int PWM_POLARITY_INVERSED;
/**
* Open the sysfs PWM with the specified chip and channel.
*
* @param chip PWM chip.
* @param channel PWM channel.
*/
public Pwm(final int chip, final int channel) {
// Allocate handle
handle = pwmNew();
if (handle == 0) {
throw new RuntimeException("Handle cannot be NULL");
}
// Open device
if (pwmOpen(handle, chip, channel) != PWM_SUCCESS) {
// Free handle before throwing exception
pwmFree(handle);
throw new RuntimeException(pwmErrMessage(handle));
}
}
/**
* Close and free handle.
*/
@Override
public void close() {
pwmClose(handle);
pwmFree(handle);
}
/**
* Handle accessor.
*
* @return Handle.
*/
public long getHandle() {
return handle;
}
/**
* Allocate an PWM handle.
*
* @return A valid handle on success, or NULL on failure.
*/
@JniMethod(accessor = "pwm_new")
public static final native long pwmNew();
/**
* Open the sysfs PWM with the specified chip and channel.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @param chip PWM chip.
* @param channel PWM channel.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_open")
public static native int pwmOpen(long pwm, int chip, int channel);
/**
* Enable the PWM output.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_enable")
public static native int pwmEnable(long pwm);
/**
* Disable the PWM output.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_disable")
public static native int pwmDisable(long pwm);
/**
* Close the PWM.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_close")
public static native int pwmClose(long pwm);
/**
* Free an PWM handle.
*
* @param pwm Valid pointer to an allocated LED handle structure.
*/
@JniMethod(accessor = "pwm_free")
public static native void pwmFree(long pwm);
/**
* Get the output state of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param enabled True if enabled, false if disabled.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_enabled")
public static native int pwmGetEnabled(long pwm, boolean[] enabled);
/**
* Get the period in nanoseconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param periodNs Period in nanoseconds.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_period_ns")
public static native int pwmGetPeriodNs(long pwm, long[] periodNs);
/**
* Get the duty cycle in nanoseconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param dutyCycleNs Duty cycle in nanoseconds of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_duty_cycle_ns")
public static native int pwmGetDutyCycleNs(long pwm, long[] dutyCycleNs);
/**
* Get the period in seconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param period Period in seconds of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_period")
public static native int pwmGetPeriod(long pwm, double[] period);
/**
* Get the duty cycle as a ratio between 0.0 to 1.0 of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param dutyCycle Duty cycle as a ratio between 0.0 to 1.0 of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_duty_cycle")
public static native int pwmGetDutyCycle(long pwm, double[] dutyCycle);
/**
* Get the frequency in Hz of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param frequency Frequency in Hz of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_frequency")
public static native int pwmGetFrequency(long pwm, double[] frequency);
/**
* Get the output polarity of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param polarity Polarity of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_get_polarity")
public static native int pwmGetPolarity(long pwm, int[] polarity);
/**
* Set the output state of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param enabled True to enable or false to disable.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_enabled")
public static native int pwmSetEnabled(long pwm, boolean enabled);
/**
* Set the period in nanoseconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param periodNs Period in nanoseconds of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_period_ns")
public static native int pwmSetPeriodNs(long pwm, long periodNs);
/**
* Set the duty cycle in nanoseconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param dutyCycle Duty cycle in nanoseconds of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_duty_cycle_ns")
public static native int pwmSetDutyCycleNs(long pwm, long dutyCycle);
/**
* Set the period in seconds of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param period period in seconds of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_period")
public static native int pwmSetPeriod(long pwm, double period);
/**
* Set the duty cycle as a ratio between 0.0 to 1.0 of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param dutyCycle duty cycle as a ratio between 0.0 to 1.0 of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_duty_cycle")
public static native int pwmSetDutyCycle(long pwm, double dutyCycle);
/**
* Set the frequency in Hz of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param frequency Frequency in Hz of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_frequency")
public static native int pwmSetFrequency(long pwm, double frequency);
/**
* Set the output polarity of the PWM.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @param polarity Output polarity of the PWM.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_set_polarity")
public static native int pwmSetPolarity(long pwm, int polarity);
/**
* Return the chip number of the PWM handle.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @return Chip number of the PWM handle.
*/
@JniMethod(accessor = "pwm_chip")
public static native int pwmChip(long pwm);
/**
* Return the channel number of the PWM handle.
*
* @param pwm Valid pointer to an allocated LED handle structure.
* @return Channel number of the PWM handle.
*/
@JniMethod(accessor = "pwm_channel")
public static native int pwmChannel(long pwm);
/**
* Return a string representation of the PWM handle.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @param str String representation of the PWM handle.
* @param len Length of char array.
* @return 0 on success, or a negative PWM error code on failure.
*/
@JniMethod(accessor = "pwm_tostring")
public static native int pwmToString(long pwm, byte[] str, long len);
/**
* Return a string representation of the PWM handle. Wraps native method and simplifies.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return PWM handle as String.
*/
public static String pwmToString(long pwm) {
var str = new byte[MAX_CHAR_ARRAY_LEN];
if (pwmToString(pwm, str, str.length) < 0) {
throw new RuntimeException(pwmErrMessage(pwm));
}
return jString(str);
}
/**
* Return the libc errno of the last failure that occurred.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return libc errno.
*/
@JniMethod(accessor = "pwm_errno")
public static native int pwmErrNo(long pwm);
/**
* Return a human readable error message pointer of the last failure that occurred.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return Error message pointer.
*/
@JniMethod(accessor = "pwm_errmsg")
public static native long pwmErrMsg(long pwm);
/**
* Return a human readable error message of the last failure that occurred. Converts const char * returned by pwm_errmsg to a
* Java String.
*
* @param pwm Valid pointer to an allocated PWM handle structure.
* @return Error message.
*/
public static String pwmErrMessage(long pwm) {
var ptr = pwmErrMsg(pwm);
var str = new byte[MAX_CHAR_ARRAY_LEN];
memMove(str, ptr, str.length);
return jString(str);
}
}