-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMinimalExample.kt
152 lines (142 loc) · 6.18 KB
/
MinimalExample.kt
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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.pi4j.io.gpio.digital.DigitalState
import com.pi4j.io.gpio.digital.PullResistance
import com.pi4j.ktx.*
import com.pi4j.ktx.io.digital.digitalInput
import com.pi4j.ktx.io.digital.digitalOutput
import com.pi4j.ktx.io.digital.onLow
import com.pi4j.ktx.io.digital.piGpioProvider
import java.lang.Thread.sleep
/*-
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J-Kotlin :: EXAMPLE :: Kotlin Sample Code
* FILENAME : MinimalExample.kt
*
* This file is part of the Pi4J project. More information about
* this project can be found here: https://pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2021 Pi4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
private const val PIN_BUTTON = 24 // PIN 18 = BCM 24
private const val PIN_LED = 22 // PIN 15 = BCM 22
private var pressCount = 0
/**
* This application blinks a led and counts the number the button is pressed. The blink speed increases with each
* button press, and after 5 presses the application finishes.
*
* @throws java.lang.Exception if any.
*
* This example fully describes the base usage of Pi4J-Kotlin by providing extensive comments in each step.
*
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>)
* @author Frank Delporte (<a href="https://www.webtechie.be">https://www.webtechie.be</a>)
*/
fun main() {
// Use Pi4J-Kotlin com.pi4j.pi4k.console wrapper/helper
console {
// Print program title/header
title("<-- The Pi4J Project -->", "Minimal Example project")
// ************************************************************
//
// WELCOME TO Pi4J:
//
// Here we will use this getting started example to
// demonstrate the basic fundamentals of the Pi4J library with the Pi4K Kotlin DSL.
//
// This example is to introduce you to the boilerplate
// logic and concepts required for all applications using
// the Pi4J library. This example will do use some basic I/O.
// Check the pi4j-examples project to learn about all the I/O
// functions of Pi4J.
//
// ************************************************************
// ------------------------------------------------------------
// Initialize the Pi4J Runtime Context
// ------------------------------------------------------------
// Before you can use Pi4J you must initialize a new runtime
// context.
//
// The 'com.pi4j.pi4k.withinAutoContext'
// method will automatically load all available Pi4J
// extensions found in the application's classpath which
// may include 'Platforms' and 'I/O Providers'
pi4j {
// ------------------------------------------------------------
// Output Pi4J Context information
// ------------------------------------------------------------
// The created Pi4J Context initializes platforms, providers
// and the I/O registry. To help you to better understand this
// approach, we print out the info of these. This can be removed
// from your own application.
// OPTIONAL
printLoadedPlatforms(this)
printDefaultPlatform(this)
printProviders(this)
// Here we will create I/O interfaces for a (GPIO) digital output
// and input pin. We define the 'provider' to use PiGpio to control
// the GPIO.
digitalInput(PIN_BUTTON) {
id("button")
name("Press button")
pull(PullResistance.PULL_DOWN)
debounce(3000L)
}.onLow {
pressCount++
+"Button was pressed for the ${pressCount}th time"
}
digitalOutput(PIN_LED) {
id("led")
name("LED Flasher")
shutdown(DigitalState.LOW)
initial(DigitalState.LOW)
}.run {
// OPTIONAL: print the registry
printRegistry(this@pi4j)
while (pressCount < 5) {
+"LED ${state()}"
toggle()
sleep(500L / (pressCount + 1))
}
}
// ------------------------------------------------------------
// Terminate the Pi4J library
// ------------------------------------------------------------
// We we are all done and want to exit our application, we must
// call the 'shutdown()' function on the Pi4J static helper class.
// This will ensure that all I/O instances are properly shutdown,
// released by the the system and shutdown in the appropriate
// manner. Terminate will also ensure that any background
// threads/processes are cleanly shutdown and any used memory
// is returned to the system.
// shutdown() is called automatically after the block execution
}
}
}