Skip to content

Commit

Permalink
Merge pull request #385 from ytai/KotlinConversion
Browse files Browse the repository at this point in the history
Kotlin conversion HelloIOIO
  • Loading branch information
hannesa2 authored Jun 11, 2024
2 parents f959501 + c1a7087 commit d387a8a
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 175 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package ioio.examples.hello

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import android.widget.ToggleButton
import ioio.lib.api.DigitalOutput
import ioio.lib.api.IOIO
import ioio.lib.api.IOIO.VersionType
import ioio.lib.api.exception.ConnectionLostException
import ioio.lib.util.BaseIOIOLooper
import ioio.lib.util.IOIOLooper
import ioio.lib.util.android.IOIOActivity

/**
* This is the main activity of the HelloIOIO example application.
*
*
* It displays a toggle button on the screen, which enables control of the
* on-board LED. This example shows a very simple usage of the IOIO, by using
* the [IOIOActivity] class. For a more advanced use case, see the
* HelloIOIOPower example.
*/
class MainActivity : IOIOActivity() {
private var toggleButton: ToggleButton? = null
private var numConnected = 0

/**
* Called when the activity is first created. Here we normally initialize
* our GUI.
*/
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
toggleButton = findViewById(R.id.button)
}

/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity.createIOIOThread
*/
override fun createIOIOLooper(): IOIOLooper {
return Looper()
}

private fun showVersions(ioio: IOIO, title: String) {
toast(
String.format(
"""
%s
IOIOLib: %s
Application firmware: %s
Bootloader firmware: %s
Hardware: %s
""".trimIndent(),
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER)
)
)
}

private fun toast(message: String) {
val context: Context = this
runOnUiThread { Toast.makeText(context, message, Toast.LENGTH_LONG).show() }
}

private fun enableUi(enable: Boolean) {
// This is slightly trickier than expected to support a multi-IOIO use-case.
runOnUiThread {
if (enable) {
if (numConnected++ == 0) {
toggleButton!!.isEnabled = true
}
} else {
if (--numConnected == 0) {
toggleButton!!.isEnabled = false
}
}
}
}

/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
internal inner class Looper : BaseIOIOLooper() {
/**
* The on-board LED.
*/
private var digitalOutput: DigitalOutput? = null

/**
* Called every time a connection with IOIO has been established.
* Typically used to open pins.
*
* @throws ConnectionLostException When IOIO connection is lost.
* @see ioio.lib.util.IOIOLooper.setup
*/
@Throws(ConnectionLostException::class)
override fun setup() {
showVersions(ioio_, "IOIO connected!")
digitalOutput = ioio_.openDigitalOutput(0, true)
enableUi(true)
}

/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException When IOIO connection is lost.
* @throws InterruptedException When the IOIO thread has been interrupted.
* @see ioio.lib.util.IOIOLooper.loop
*/
@Throws(ConnectionLostException::class, InterruptedException::class)
override fun loop() {
digitalOutput!!.write(!toggleButton!!.isChecked)
Thread.sleep(100)
}

/**
* Called when the IOIO is disconnected.
*
* @see ioio.lib.util.IOIOLooper.disconnected
*/
override fun disconnected() {
enableUi(false)
toast("IOIO disconnected")
}

/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see ioio.lib.util.IOIOLooper.incompatible
*/
@Deprecated("Deprecated in Java")
override fun incompatible() {
showVersions(ioio_, "Incompatible firmware version!")
}
}
}
29 changes: 17 additions & 12 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,26 +131,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -197,11 +198,15 @@ if "$cygwin" || "$msys" ; then
done
fi

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
Loading

0 comments on commit d387a8a

Please sign in to comment.