FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/java/libraries/io/src/processing/io/PWM.java at master · lpasqualis/processing · GitHub
lpasqualis
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
java
/
libraries
/
io
/
src
/
processing
/
io
/
PWM.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
215 lines (182 loc) · 6.21 KB
Breadcrumbs
processing
/
java
/
libraries
/
io
/
src
/
processing
/
io
/
PWM.java
Copy path
File metadata and controls
215 lines (182 loc) · 6.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Copyright (c) The Processing Foundation 2015
Hardware I/O library developed by Gottfried Haider as part of GSoC 2015
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package
processing
.
io
;
import
processing
.
io
.
NativeInterface
;
import
java
.
io
.
File
;
import
java
.
nio
.
file
.
Files
;
import
java
.
nio
.
file
.
Path
;
import
java
.
nio
.
file
.
Paths
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
/**
* @webref
*/
public
class
PWM
{
int
channel
;
String
chip
;
/**
* Opens a PWM channel
* @param channel PWM channel
* @see list
* @webref
*/
public
PWM
(
String
channel
) {
NativeInterface
.
loadLibrary
();
int
pos
=
channel
.
indexOf
(
"/pwm"
);
if
(
pos
== -
1
) {
throw
new
IllegalArgumentException
(
"Unsupported channel"
);
}
chip
=
channel
.
substring
(
0
,
pos
);
this
.
channel
=
Integer
.
parseInt
(
channel
.
substring
(
pos
+
4
));
if
(
NativeInterface
.
isSimulated
()) {
return
;
}
// export channel through sysfs
String
fn
=
"/sys/class/pwm/"
+
chip
+
"/export"
;
int
ret
=
NativeInterface
.
writeFile
(
fn
,
Integer
.
toString
(
this
.
channel
));
if
(
ret
<
0
) {
if
(
ret
== -
2
) {
// ENOENT
System
.
err
.
println
(
"Make sure your kernel is compiled with PWM_SYSFS enabled and you have the necessary PWM driver for your platform"
);
}
// XXX: check
if
(
ret
== -
22
) {
// EINVAL
System
.
err
.
println
(
"PWM channel "
+
channel
+
" does not seem to be available on your platform"
);
}
// XXX: check
if
(
ret
!= -
16
) {
// EBUSY, returned when the pin is already exported
throw
new
RuntimeException
(
fn
+
": "
+
NativeInterface
.
getError
(
ret
));
}
}
// delay to give udev a chance to change the file permissions behind our back
// there should really be a cleaner way for this
try
{
Thread
.
sleep
(
500
);
}
catch
(
InterruptedException
e
) {
Thread
.
currentThread
().
interrupt
();
}
}
/**
* Disables the PWM output
* @webref
*/
public
void
clear
() {
if
(
NativeInterface
.
isSimulated
()) {
return
;
}
String
fn
=
String
.
format
(
"/sys/class/pwm/%s/pwm%d/enable"
,
chip
,
channel
);
int
ret
=
NativeInterface
.
writeFile
(
fn
,
"0"
);
if
(
ret
<
0
) {
throw
new
RuntimeException
(
NativeInterface
.
getError
(
ret
));
}
}
/**
* Gives ownership of a channel back to the operating system
* @webref
*/
public
void
close
() {
if
(
NativeInterface
.
isSimulated
()) {
return
;
}
// XXX: implicit clear()?
// XXX: also check GPIO
String
fn
=
"/sys/class/pwm/"
+
chip
+
"/unexport"
;
int
ret
=
NativeInterface
.
writeFile
(
fn
,
Integer
.
toString
(
channel
));
if
(
ret
<
0
) {
if
(
ret
== -
2
) {
// ENOENT
System
.
err
.
println
(
"Make sure your kernel is compiled with PWM_SYSFS enabled and you have the necessary PWM driver for your platform"
);
}
// XXX: check
// EINVAL is also returned when trying to unexport pins that weren't exported to begin with
throw
new
RuntimeException
(
NativeInterface
.
getError
(
ret
));
}
}
/**
* Lists all available PWM channels
* @return String array
* @webref
*/
public
static
String
[]
list
() {
if
(
NativeInterface
.
isSimulated
()) {
return
new
String
[]{
"pwmchip0/pwm0"
,
"pwmchip0/pwm1"
};
}
ArrayList
<
String
>
devs
=
new
ArrayList
<
String
>();
File
dir
=
new
File
(
"/sys/class/pwm"
);
File
[]
chips
=
dir
.
listFiles
();
if
(
chips
!=
null
) {
for
(
File
chip
:
chips
) {
// get the number of supported channels
try
{
Path
path
=
Paths
.
get
(
"/sys/class/pwm/"
+
chip
.
getName
() +
"/npwm"
);
String
tmp
=
new
String
(
Files
.
readAllBytes
(
path
));
int
npwm
=
Integer
.
parseInt
(
tmp
.
trim
());
for
(
int
i
=
0
;
i
<
npwm
;
i
++) {
devs
.
add
(
chip
.
getName
() +
"/pwm"
+
i
);
}
}
catch
(
Exception
e
) {
}
}
}
// listFiles() does not guarantee ordering
String
[]
tmp
=
devs
.
toArray
(
new
String
[
devs
.
size
()]);
Arrays
.
sort
(
tmp
);
return
tmp
;
}
/**
* Enables the PWM output
* @param period cycle period in Hz
* @param duty duty cycle, 0.0 (always off) to 1.0 (always on)
* @webref
*/
public
void
set
(
int
period
,
float
duty
) {
if
(
NativeInterface
.
isSimulated
()) {
return
;
}
// set period
String
fn
=
fn
=
String
.
format
(
"/sys/class/pwm/%s/pwm%d/period"
,
chip
,
channel
);
// convert to nanoseconds
int
ret
=
NativeInterface
.
writeFile
(
fn
,
String
.
format
(
"%d"
, (
int
)(
1000000000
/
period
)));
if
(
ret
<
0
) {
throw
new
RuntimeException
(
fn
+
": "
+
NativeInterface
.
getError
(
ret
));
}
// set duty cycle
fn
=
fn
=
String
.
format
(
"/sys/class/pwm/%s/pwm%d/duty_cycle"
,
chip
,
channel
);
if
(
duty
<
0.0
||
1.0
<
duty
) {
System
.
err
.
println
(
"Duty cycle must be between 0.0 and 1.0."
);
throw
new
IllegalArgumentException
(
"Illegal argument"
);
}
// convert to nanoseconds
ret
=
NativeInterface
.
writeFile
(
fn
,
String
.
format
(
"%d"
, (
int
)((
1000000000
*
duty
) /
period
)));
if
(
ret
<
0
) {
throw
new
RuntimeException
(
fn
+
": "
+
NativeInterface
.
getError
(
ret
));
}
// enable output
fn
=
String
.
format
(
"/sys/class/pwm/%s/pwm%d/enable"
,
chip
,
channel
);
ret
=
NativeInterface
.
writeFile
(
fn
,
"1"
);
if
(
ret
<
0
) {
throw
new
RuntimeException
(
fn
+
": "
+
NativeInterface
.
getError
(
ret
));
}
}
/**
* Enables the PWM output with a preset period of 1 kHz
* @param duty duty cycle, 0.0 (always off) to 1.0 (always on)
* @webref
*/
public
void
set
(
float
duty
) {
set
(
1000
,
duty
);
}
}
Back
|
FazBrowse Home
|
New Git URL