Skip to content

Commit a0d24fa

Browse files
Merge pull request #121 from stefanhaustein/master
Add a Crowpi2 Demo
2 parents 78799b5 + c4c43d8 commit a0d24fa

10 files changed

Lines changed: 167 additions & 227 deletions

File tree

src/main/java/com/pi4j/examples/games/snake/Snake.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Snake {
3838
private int headY;
3939
private int length;
4040
private int stepTimeMillis;
41+
private boolean armed = false;
4142

4243
private Entity[][] arena;
4344
private List<Segment> body = new ArrayList<>();
@@ -47,11 +48,6 @@ public Snake(GraphicsDisplay display, GameController controller) {
4748
this.display = display;
4849
this.controller = controller;
4950

50-
assignKey(GameController.Key.UP, value -> processDirectionalKey(value, 0, -1));
51-
assignKey(GameController.Key.DOWN, value -> processDirectionalKey(value, 0, 1));
52-
assignKey(GameController.Key.LEFT, value -> processDirectionalKey(value, -1, 0));
53-
assignKey(GameController.Key.RIGHT, value -> processDirectionalKey(value, 1, 0));
54-
5551
int displayWidth = display.getWidth();
5652
int displayHeight = display.getHeight();
5753

@@ -80,12 +76,6 @@ private void addSegment(int x, int y) {
8076
setEntity(x, y, Entity.SNAKE);
8177
}
8278

83-
private void assignKey(GameController.Key key, Consumer<Boolean> consumer) {
84-
ListenableOnOffRead<?> lor = controller.getKey(key);
85-
if (lor != null) {
86-
keys.put(lor, lor.addConsumer(consumer));
87-
}
88-
}
8979

9080
private void initialize() {
9181
arena = new Entity[AREA_SIZE][AREA_SIZE];
@@ -104,14 +94,6 @@ private void initialize() {
10494
addFood();
10595
}
10696

107-
108-
private void processDirectionalKey(boolean keyPressed, int dx, int dy) {
109-
if (keyPressed) {
110-
this.dx = dx;
111-
this.dy = dy;
112-
}
113-
}
114-
11597
private void renderColor(int x, int y, int color) {
11698
display.fillRect(x0 + x * scale, y0 + y * scale, scale, scale, color);
11799
}
@@ -120,6 +102,17 @@ public void run() {
120102
initialize();
121103
while (!exit) {
122104
deferredDelay.setDelayMillis(stepTimeMillis);
105+
GameController.Direction direction = controller.getDirection();
106+
if (!armed) {
107+
armed = direction == GameController.Direction.NONE;
108+
} else {
109+
switch (direction) {
110+
case NORTH -> setDirection(0, -1);
111+
case SOUTH -> setDirection(0, 1);
112+
case EAST -> setDirection(1, 0);
113+
case WEST -> setDirection(-1, 0);
114+
}
115+
}
123116
step();
124117
deferredDelay.materializeDelay();
125118
}
@@ -129,6 +122,11 @@ public void run() {
129122
}
130123
}
131124

125+
private void setDirection(int dx, int dy) {
126+
this.dx = dx;
127+
this.dy = dy;
128+
}
129+
132130
private void setEntity(int x, int y, Entity entity) {
133131
arena[x][y] = entity;
134132
int color = switch(entity) {

src/main/java/com/pi4j/examples/hat/DisplayHatDemo.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.pi4j.examples.hat;
22

3+
import com.pi4j.drivers.display.character.CharacterDisplay;
4+
import com.pi4j.drivers.display.graphics.GraphicsCharacterDisplay;
35
import com.pi4j.drivers.display.graphics.GraphicsDisplay;
46
import com.pi4j.drivers.input.GameController;
57
import com.pi4j.drivers.sensor.Sensor;
8+
import com.pi4j.drivers.sound.Note;
9+
import com.pi4j.drivers.sound.SoundDriver;
610
import com.pi4j.examples.games.bricks.Bricks;
711
import com.pi4j.examples.games.snake.Snake;
812
import com.pi4j.examples.ui.ListView;
@@ -13,33 +17,44 @@
1317
/** A generic demo for display hats */
1418
public class DisplayHatDemo {
1519

16-
private final GraphicsDisplay display;
20+
private final GraphicsDisplay graphicsDisplay;
21+
private final CharacterDisplay characterDisplay;
1722
private final GameController controller;
23+
private final SoundDriver soundDriver;
1824
private final List<Sensor> sensors;
1925

2026
public DisplayHatDemo(
21-
GraphicsDisplay display,
27+
GraphicsDisplay graphicsDisplay,
28+
CharacterDisplay characterDisplay,
2229
GameController controller,
30+
SoundDriver soundDriver,
2331
List<Sensor> sensors
2432
) {
25-
this.display = display;
33+
this.graphicsDisplay = graphicsDisplay;
34+
this.characterDisplay = characterDisplay != null ? characterDisplay
35+
: new GraphicsCharacterDisplay(graphicsDisplay);
2636
this.controller = controller;
37+
this.soundDriver = soundDriver;
2738
this.sensors = sensors;
2839
}
2940

3041
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());
3343

34-
ListView menu = new ListView(display, controller, scale);
44+
ListView menu = new ListView(characterDisplay, controller);
3545
if (sensors != null && !sensors.isEmpty()) {
36-
menu.add("Sensors", () -> new SensorView(display, controller, scale)
46+
menu.add("Sensors", () -> new SensorView(characterDisplay, controller)
3747
.addAll(sensors)
3848
.run());
3949
}
40-
menu.add("Snake", () -> new Snake(display, controller).run());
50+
menu.add("Snake", () -> new Snake(graphicsDisplay, controller).run());
4151
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+
});
4358
}
4459
menu.add("Exit", ListView.EXIT_ACTION);
4560
menu.run();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.pi4j.examples.hat.elecrow.crowpi2;
2+
3+
import com.pi4j.Pi4J;
4+
import com.pi4j.context.Context;
5+
import com.pi4j.drivers.hat.elecrow.CrowPi2;
6+
import com.pi4j.examples.hat.DisplayHatDemo;
7+
8+
public class CrowPi2Demo {
9+
10+
public static void main(String[] args) {
11+
Context pi4j = Pi4J.newAutoContext();
12+
CrowPi2 crowPi2 = new CrowPi2(pi4j);
13+
14+
DisplayHatDemo demo = new DisplayHatDemo(
15+
crowPi2.getGraphicsDisplay(),
16+
crowPi2.getTextDisplay(),
17+
crowPi2.getGameController(),
18+
crowPi2.getSoundDriver(),
19+
null);
20+
21+
demo.run();
22+
}
23+
24+
}

src/main/java/com/pi4j/examples/hat/raspberry/sensehat/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ Displays a scrolling line menu where a sensor demo and a snake game can be selec
44
using the joystick.
55

66
This demo requires that the Raspberry SenseHat linux driver is installed and that
7-
I2C is enabled in the Rasperry Pi configuration.
7+
I2C is enabled in the Raspberry Pi configuration.
88

src/main/java/com/pi4j/examples/hat/raspberry/sensehat/SenseHatDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void main(String[] args) {
1414
Context pi4j = Pi4J.newAutoContext();
1515
SenseHat senseHat = new SenseHat(pi4j);
1616

17-
new DisplayHatDemo(senseHat.getDisplay(), senseHat.getController(), senseHat.getAllSensors()).run();
17+
new DisplayHatDemo(senseHat.getDisplay(), null, senseHat.getController(), null, senseHat.getAllSensors()).run();
1818

1919
pi4j.shutdown();
2020
}

src/main/java/com/pi4j/examples/hat/waveshare/gamepi13/Demo.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ static void main(String[] args) {
1414
Context pi4j = Pi4J.newAutoContext();
1515
GamePi13 hat = new GamePi13(pi4j);
1616

17-
new DisplayHatDemo(hat.getDisplay(), hat.getController(), null).run();
17+
new DisplayHatDemo(
18+
hat.getDisplay(),
19+
null,
20+
hat.getController(),
21+
hat.getSoundDriver(),
22+
null).run();
1823

19-
hat.getDisplay().close();
20-
hat.getController().close();
24+
hat.close();
2125
pi4j.shutdown();
2226
}
2327

src/main/java/com/pi4j/examples/hat/waveshare/waveshare14972/Demo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ static void main(String[] args) {
1414
Context pi4j = Pi4J.newAutoContext();
1515
Waveshare14972 hat = new Waveshare14972(pi4j);
1616

17-
new DisplayHatDemo(hat.getDisplay(), hat.getController(), null).run();
17+
new DisplayHatDemo(
18+
hat.getDisplay(),
19+
null,
20+
hat.getController(),
21+
null,
22+
null).run();
1823

1924
hat.getDisplay().close();
2025
hat.getController().close();

0 commit comments

Comments
 (0)