Piezo buzzer

Let's make some noise. The buzzer also has its own driver. We can open it using the RainbowHat.openPiezo() method and it returns an object of type Speaker.

The Speaker class has two methods: play, which receives the frequency to play, and stop, which -unsurprisingly- makes the speaker stop.

To follow up from the previous section, let's build a three-tone piano where the buttons A, B, and C will play the frequencies of 1,000 Hz, 3,000 Hz, and 5,000 Hz, respectively. The buzzer will start sounding when the button is pressed and stop when the button is released.

For this example, we'll use button drivers for simplicity. The initialization and cleanup for PianoActivity looks like this:

class PianoActivity : Activity() {

private lateinit var buttonA: ButtonInputDriver
private lateinit var buttonB: ButtonInputDriver
private lateinit var buttonC: ButtonInputDriver

private lateinit var buzzer: Speaker

private val frequencies = hashMapOf(
KEYCODE_A to 1000.0,
KEYCODE_B to 3000.0,
KEYCODE_C to 5000.0)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

buzzer = RainbowHat.openPiezo()

buttonA = RainbowHat.createButtonAInputDriver(KEYCODE_A)
buttonB = RainbowHat.createButtonBInputDriver(KEYCODE_B)
buttonC = RainbowHat.createButtonCInputDriver(KEYCODE_C)

buttonA.register()
buttonB.register()
buttonC.register()
}

override fun onDestroy() {
super.onDestroy()
buzzer.close()
buttonA.unregister()
buttonB.unregister()
buttonC.unregister()
}

[...]
}

We can see again the standard lifetime of the peripherals, being opened inside onCreate and closed inside onDestroy. As mentioned before, we used the utility wrapper method to open the piezo buzzer.

The other interesting part of initialization is the way we store the frequencies to play. We will have a Map that uses the key codes as keys and has the frequencies as values. What is interesting here is how easy it is to read the initialization of such variable in Kotlin.

Now, for the interesting part, let's look into onKeyDown and onKeyUp:

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
val freqToPlay = frequencies.get(keyCode)
if (freqToPlay != null) {
buzzer.play(freqToPlay)
return true
}
else {
return false
}
}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
buzzer.stop()
return true
}

Besides the code of the button handling and finding the right frequency to play, the only code related to the piezo buzzer are the calls to play and stop.

Note that when we get the frequency from the map, it can be null. Kotlin enforces null validation because a key that is not referenced on the map may get pressed. We don't want our application to crash, so if there is no value for the key, we just pass the event up the chain to the parent class. This is a good example of how Kotlin handles nullables and why it is one of its best features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.29.219