Skip to content

Commit 967e527

Browse files
committed
More Kotlin examples
1 parent ae14ce8 commit 967e527

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

examples/monitor_kotlin/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

examples/monitor_kotlin/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>net.codecrete.usb.examples</groupId>
6+
<artifactId>monitor_kotlin</artifactId>
7+
<version>0.7.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>monitor_kotlin</name>
11+
<url>https://github.com/manuelbl/JavaDoesUSB/examples/kotlin</url>
12+
13+
<properties>
14+
<kotlin.version>1.9.10</kotlin.version>
15+
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
21+
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
22+
23+
<plugins>
24+
<plugin>
25+
<groupId>org.jetbrains.kotlin</groupId>
26+
<artifactId>kotlin-maven-plugin</artifactId>
27+
<version>${kotlin.version}</version>
28+
<extensions>true</extensions>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.jetbrains.kotlin</groupId>
36+
<artifactId>kotlin-stdlib</artifactId>
37+
<version>${kotlin.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>net.codecrete.usb</groupId>
41+
<artifactId>java-does-usb</artifactId>
42+
<version>0.7.0-SNAPSHOT</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.tinylog</groupId>
46+
<artifactId>tinylog-api</artifactId>
47+
<version>2.6.2</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.tinylog</groupId>
51+
<artifactId>tinylog-impl</artifactId>
52+
<version>2.6.2</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.tinylog</groupId>
56+
<artifactId>jsl-tinylog</artifactId>
57+
<version>2.6.2</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<version>4.13.2</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Java Does USB
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
// Licensed under MIT License
5+
// https://opensource.org/licenses/MIT
6+
//
7+
package net.codecrete.usb.examples
8+
9+
import net.codecrete.usb.Usb
10+
import net.codecrete.usb.UsbDevice
11+
12+
/**
13+
* Sample application monitoring USB devices as they are connected and disconnected.
14+
*/
15+
16+
fun main() {
17+
Monitor().monitor()
18+
}
19+
20+
class Monitor {
21+
fun monitor() {
22+
// register callbacks for events
23+
Usb.setOnDeviceConnected { device -> printDetails(device, "Connected") }
24+
Usb.setOnDeviceDisconnected { device ->
25+
printDetails( device, "Disconnected")
26+
}
27+
28+
// display the already present USB devices
29+
for (device in Usb.getDevices())
30+
printDetails(device, "Present")
31+
32+
// wait for ENTER to quit program
33+
println("Monitoring... Press ENTER to quit.")
34+
System.`in`.read()
35+
}
36+
37+
private fun printDetails(device: UsbDevice, event: String) {
38+
print(String.format("%-14s", "$event:"))
39+
println(device.toString())
40+
}
41+
}

0 commit comments

Comments
 (0)