Skip to content

Commit

Permalink
Follow the morse spec more closely
Browse files Browse the repository at this point in the history
Signed-off-by: Geetesh Kalakoti <[email protected]>
  • Loading branch information
geeteshk committed Nov 10, 2018
1 parent a933baf commit ccdf2c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
36 changes: 24 additions & 12 deletions app/src/main/java/io/geeteshk/dot/ui/fragment/SignalFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,25 @@ class SignalFragment : RestoreStateFragment() {
val delay = getDelay(element)

// If it's a space or unknown character we ignore it
if (delay == -1) return@forEachIndexed
if (delay == -1) {
// Check if we are delaying a word or in between a letter
if (view!!.rootView.morseInput.text.toString()[numSpaces] == ' ') {
delay(WORD_SPACE_LENGTH)
} else {
delay(LETTER_SPACE_LENGTH)
}

return@forEachIndexed
}

// Update the spans and enable the flash for the respective delay
spanner.addCharSpans(viewModel.currentSpannable, index)
flashlight.flashAndWait(true, delay)

// Clear the spans and wait a 'dot length' before
// Clear the spans and wait space length before
// moving onto the next character
spanner.removeSpans(viewModel.currentSpannable)
flashlight.flashAndWait(false, Constants.DOT_DELAY)
flashlight.flashAndWait(false, PART_SPACE_LENGTH)
}

// We have finished looping so we can cleanup
Expand Down Expand Up @@ -270,17 +279,20 @@ class SignalFragment : RestoreStateFragment() {
/** Utility to get how long to wait for a given character */
private fun getDelay(char: Char): Int {
return when (char) {
'.' -> Constants.DOT_DELAY
'-' -> Constants.DOT_DELAY * 3
' ' -> {
// Since we return on space we need to delay here
delay(Constants.DOT_DELAY)

-1
}

'.' -> DOT_LENGTH
'-' -> DASH_LENGTH
' ' -> -1
else -> -1
}
}
}

companion object {
private const val TIME_UNIT = 250
const val DOT_LENGTH = TIME_UNIT
const val DASH_LENGTH = TIME_UNIT * 3
const val PART_SPACE_LENGTH = TIME_UNIT
const val LETTER_SPACE_LENGTH = TIME_UNIT * 3
const val WORD_SPACE_LENGTH = TIME_UNIT * 7
}
}
3 changes: 0 additions & 3 deletions app/src/main/java/io/geeteshk/dot/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package io.geeteshk.dot.utils
/** Constant values that are used throughout the app */
class Constants {
companion object {
/** The duration of a single dot in morse code */
const val DOT_DELAY = 250

/** Request code used when opening app Settings */
const val RC_SETTINGS = 123

Expand Down

0 comments on commit ccdf2c9

Please sign in to comment.