|
1 | 1 | package com.pi4j.examples.hat; |
2 | 2 |
|
| 3 | +import com.pi4j.drivers.display.character.CharacterDisplay; |
| 4 | +import com.pi4j.drivers.display.graphics.GraphicsCharacterDisplay; |
3 | 5 | import com.pi4j.drivers.display.graphics.GraphicsDisplay; |
4 | 6 | import com.pi4j.drivers.input.GameController; |
5 | 7 | import com.pi4j.drivers.sensor.Sensor; |
| 8 | +import com.pi4j.drivers.sound.Note; |
| 9 | +import com.pi4j.drivers.sound.SoundDriver; |
6 | 10 | import com.pi4j.examples.games.bricks.Bricks; |
7 | 11 | import com.pi4j.examples.games.snake.Snake; |
8 | 12 | import com.pi4j.examples.ui.ListView; |
|
13 | 17 | /** A generic demo for display hats */ |
14 | 18 | public class DisplayHatDemo { |
15 | 19 |
|
16 | | - private final GraphicsDisplay display; |
| 20 | + private final GraphicsDisplay graphicsDisplay; |
| 21 | + private final CharacterDisplay characterDisplay; |
17 | 22 | private final GameController controller; |
| 23 | + private final SoundDriver soundDriver; |
18 | 24 | private final List<Sensor> sensors; |
19 | 25 |
|
20 | 26 | public DisplayHatDemo( |
21 | | - GraphicsDisplay display, |
| 27 | + GraphicsDisplay graphicsDisplay, |
| 28 | + CharacterDisplay characterDisplay, |
22 | 29 | GameController controller, |
| 30 | + SoundDriver soundDriver, |
23 | 31 | List<Sensor> sensors |
24 | 32 | ) { |
25 | | - this.display = display; |
| 33 | + this.graphicsDisplay = graphicsDisplay; |
| 34 | + this.characterDisplay = characterDisplay != null ? characterDisplay |
| 35 | + : new GraphicsCharacterDisplay(graphicsDisplay); |
26 | 36 | this.controller = controller; |
| 37 | + this.soundDriver = soundDriver; |
27 | 38 | this.sensors = sensors; |
28 | 39 | } |
29 | 40 |
|
30 | 41 | public void run() { |
31 | | - int resolution = Math.min(display.getWidth(), display.getHeight()); |
32 | | - int scale = Math.max(1, resolution) / 64; |
| 42 | + int resolution = Math.min(graphicsDisplay.getWidth(), graphicsDisplay.getHeight()); |
33 | 43 |
|
34 | | - ListView menu = new ListView(display, controller, scale); |
| 44 | + ListView menu = new ListView(characterDisplay, controller); |
35 | 45 | if (sensors != null && !sensors.isEmpty()) { |
36 | | - menu.add("Sensors", () -> new SensorView(display, controller, scale) |
| 46 | + menu.add("Sensors", () -> new SensorView(characterDisplay, controller) |
37 | 47 | .addAll(sensors) |
38 | 48 | .run()); |
39 | 49 | } |
40 | | - menu.add("Snake", () -> new Snake(display, controller).run()); |
| 50 | + menu.add("Snake", () -> new Snake(graphicsDisplay, controller).run()); |
41 | 51 | if (resolution >= 64) { |
42 | | - menu.add("Bricks", () -> new Bricks(display, controller).run()); |
| 52 | + menu.add("Bricks", () -> new Bricks(graphicsDisplay, controller).run()); |
| 53 | + } |
| 54 | + if (soundDriver != null) { |
| 55 | + menu.add("Play Demo Sound", () -> { |
| 56 | + soundDriver.playNotes(103, null, Note.G4, 8, Note.G4, 8, Note.G4, 8, Note.DS4, 6, Note.AS4, 2, Note.G4, 8, Note.DS4, 6, Note.AS4, 2, Note.G4, 16); |
| 57 | + }); |
43 | 58 | } |
44 | 59 | menu.add("Exit", ListView.EXIT_ACTION); |
45 | 60 | menu.run(); |
|
0 commit comments