Skip to content

Commit 2a1c437

Browse files
Merge pull request #119 from stefanhaustein/master
Add an example for the Waveshare GamePi13 hat
2 parents 14672d9 + 5fe35a8 commit 2a1c437

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/main/java/com/pi4j/devices/cp2102n/CP2102N.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Serial createSerialDevice() {
7373
.flowControl(FlowControl.NONE)
7474
.id(id)
7575
.name(name)
76-
.device("/dev/ttyAMA0")
76+
.port("/dev/ttyAMA0")
7777
.provider("linuxfsserial")
7878
.build());
7979

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* A simple "Snake" game implementation for a 8x8 playing field.
1313
* <p>
14-
* Hold the center key for one second to exit.
14+
* Press the select key or hold the center key for one second to exit.
1515
* <p>
1616
* This class doesn't include a main() method; please find an examples for wiring this up in the waveshare 14972
1717
* dispay hat demo.
@@ -42,6 +42,7 @@ public class Snake {
4242
private Entity[][] arena;
4343
private List<Segment> body = new ArrayList<>();
4444
private long longPressStart = Long.MAX_VALUE;
45+
private boolean exit = false;
4546

4647
public Snake(GraphicsDisplay display, GameController controller) {
4748
this.display = display;
@@ -52,6 +53,7 @@ public Snake(GraphicsDisplay display, GameController controller) {
5253
assignKey(GameController.Key.LEFT, value -> processDirectionalKey(value, -1, 0));
5354
assignKey(GameController.Key.RIGHT, value -> processDirectionalKey(value, 1, 0));
5455
assignKey(GameController.Key.CENTER, value -> processCenterKey(value));
56+
assignKey(GameController.Key.SELECT, _ -> { exit = true; });
5557

5658
int displayWidth = display.getWidth();
5759
int displayHeight = display.getHeight();
@@ -122,10 +124,13 @@ private void renderColor(int x, int y, int color) {
122124

123125
public void run() {
124126
initialize();
125-
while (System.currentTimeMillis() - longPressStart < 1000) {
127+
while (!exit) {
126128
deferredDelay.setDelayMillis(stepTimeMillis);
127129
step();
128130
deferredDelay.materializeDelay();
131+
if (System.currentTimeMillis() - longPressStart > 1000) {
132+
exit = true;
133+
}
129134
}
130135
display.fillRect(0, 0, display.getWidth(), display.getHeight(), 0xff000000);
131136
for (Map.Entry<ListenableOnOffRead<?>, Consumer<Boolean>> entry : keys.entrySet()) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.pi4j.examples.hat.waveshare.gamepi13;
2+
3+
import com.pi4j.Pi4J;
4+
import com.pi4j.context.Context;
5+
import com.pi4j.drivers.display.graphics.GraphicsDisplay;
6+
import com.pi4j.drivers.hat.waveshare.GamePi13;
7+
import com.pi4j.drivers.input.GameController;
8+
9+
/**
10+
* A simple demo running a "Snake" game. Exit by pressing the select key.
11+
*/
12+
public class Demo {
13+
14+
static void main(String[] args) {
15+
Context pi4j = Pi4J.newAutoContext();
16+
GamePi13 hat = new GamePi13(pi4j);
17+
GraphicsDisplay display = hat.getDisplay();
18+
GameController controller = hat.getController();
19+
20+
new com.pi4j.examples.games.snake.Snake(display, controller).run();
21+
22+
display.close();
23+
controller.close();
24+
pi4j.shutdown();
25+
}
26+
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Waveshare GamePi13 Demo
2+
3+
Runs a small snake game on the display.
4+
5+
Exit by pressing the select key.

0 commit comments

Comments
 (0)