Skip to content

Commit 6294ce5

Browse files
Merge pull request #122 from stefanhaustein/master
Waveshare 13891 demo, Calculator Keypad demo
2 parents a0d24fa + 205dbd0 commit 6294ce5

10 files changed

Lines changed: 154 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The following table the currently supported devices within this project:
5656
| A/D | SPI | (1)(3) | [MCP3008 A/D 10bit converter](src/main/java/com/pi4j/devices/mcp3008/README.md)
5757
| A/D | I2C | (1) | [MCP4725 12 bit DAC](src/main/java/com/pi4j/devices/mcp4725/README.md)
5858
| HAT | | | [Raspberry SenseHat](src/main/java/com/pi4j/examples/hat/raspberry/sensehat/)
59+
| HAT | | | [Waveshare 13891 Mini Display Hat](src/main/java/com/pi4j/examples/hat/waveshare/waveshare13891/)
5960
| HAT | | | [Waveshare 14972 Mini Display Hat](src/main/java/com/pi4j/examples/hat/waveshare/waveshare14972/)
6061
| HAT | | | [Waveshare GamePi13 Hat](src/main/java/com/pi4j/examples/hat/waveshare/gamepi13/)
6162
| IO Expander | I2C | | [MCP23008 drive and read chip GPIOs](src/main/java/com/pi4j/devices/mcp23008/README.md)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.pi4j.examples.apps.calculator;
2+
3+
import com.pi4j.drivers.display.character.CharacterDisplay;
4+
import com.pi4j.drivers.input.KeyPad;
5+
6+
public class Calculator {
7+
private final CharacterDisplay display;
8+
private final KeyPad keyPad;
9+
10+
private char lastKey = '\0';
11+
private char pendingOp = ' ';
12+
private long keyStart = 0;
13+
private double accumulator = 0;
14+
private long input = 0;
15+
private int divisor = 0;
16+
17+
public Calculator(CharacterDisplay display, KeyPad keyPad) {
18+
this.display = display;
19+
this.keyPad = keyPad;
20+
}
21+
22+
public void run() {
23+
display.clear();
24+
display.writeAt(0, 0, "Exit: Hold '='");
25+
renderInput();
26+
27+
while (true) {
28+
char key = keyPad.getKey();
29+
if (key == '\0') {
30+
boolean longPress = System.currentTimeMillis() - keyStart > 1000;
31+
if (longPress && lastKey == '=') {
32+
break;
33+
}
34+
if (lastKey >= '0' && lastKey <= '9') {
35+
input = input * 10 + (lastKey - '0');
36+
divisor = divisor * 10;
37+
renderInput();
38+
} else if (lastKey == '#') {
39+
divisor = 1;
40+
renderInput();
41+
} else if ("+-*=/".indexOf(lastKey) != -1) {
42+
commit(lastKey, longPress);
43+
}
44+
} else if (lastKey != key) {
45+
keyStart = System.currentTimeMillis();
46+
}
47+
lastKey = key;
48+
49+
try {
50+
Thread.sleep(50);
51+
} catch (InterruptedException e) {
52+
53+
}
54+
}
55+
}
56+
57+
private void commit(char nextOp, boolean longPress) {
58+
if (pendingOp != ' ' || nextOp == '=' || input != 0) {
59+
double value = input / Math.max(1.0, divisor);
60+
switch (pendingOp) {
61+
case '+' -> accumulator += value;
62+
case '-' -> accumulator -= value;
63+
case '/' -> accumulator /= value;
64+
case '*' -> accumulator *= value;
65+
default -> accumulator = value;
66+
}
67+
divisor = 0;
68+
input = 0;
69+
}
70+
display.clear();
71+
String s = String.valueOf(accumulator);
72+
display.writeAt(
73+
0,
74+
display.getHeight() - 2,
75+
" ".repeat(Math.max(0, display.getWidth() - s.length())) + s);
76+
pendingOp = nextOp == '=' ? ' ' : nextOp;
77+
renderInput();
78+
}
79+
80+
private void renderInput() {
81+
String s = String.valueOf(input);
82+
if (divisor != 0) {
83+
int digits = String.valueOf(divisor).length();
84+
if (digits > s.length()) {
85+
s = "0".repeat(digits - s.length()) + s;
86+
}
87+
int cut = s.length() - digits + 1;
88+
s = s.substring(0, cut) + "." + s.substring(cut);
89+
}
90+
display.writeAt(
91+
0,
92+
display.getHeight() - 1,
93+
pendingOp + " ".repeat(Math.max(0, display.getWidth() - s.length() - 1)) + s);
94+
}
95+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.pi4j.drivers.display.graphics.GraphicsCharacterDisplay;
55
import com.pi4j.drivers.display.graphics.GraphicsDisplay;
66
import com.pi4j.drivers.input.GameController;
7+
import com.pi4j.drivers.input.KeyPad;
78
import com.pi4j.drivers.sensor.Sensor;
89
import com.pi4j.drivers.sound.Note;
910
import com.pi4j.drivers.sound.SoundDriver;
11+
import com.pi4j.examples.apps.calculator.Calculator;
1012
import com.pi4j.examples.games.bricks.Bricks;
1113
import com.pi4j.examples.games.snake.Snake;
1214
import com.pi4j.examples.ui.ListView;
@@ -20,20 +22,23 @@ public class DisplayHatDemo {
2022
private final GraphicsDisplay graphicsDisplay;
2123
private final CharacterDisplay characterDisplay;
2224
private final GameController controller;
25+
private final KeyPad keyPad;
2326
private final SoundDriver soundDriver;
2427
private final List<Sensor> sensors;
2528

2629
public DisplayHatDemo(
2730
GraphicsDisplay graphicsDisplay,
2831
CharacterDisplay characterDisplay,
2932
GameController controller,
33+
KeyPad keyPad,
3034
SoundDriver soundDriver,
3135
List<Sensor> sensors
3236
) {
3337
this.graphicsDisplay = graphicsDisplay;
3438
this.characterDisplay = characterDisplay != null ? characterDisplay
3539
: new GraphicsCharacterDisplay(graphicsDisplay);
3640
this.controller = controller;
41+
this.keyPad = keyPad;
3742
this.soundDriver = soundDriver;
3843
this.sensors = sensors;
3944
}
@@ -51,6 +56,9 @@ public void run() {
5156
if (resolution >= 64) {
5257
menu.add("Bricks", () -> new Bricks(graphicsDisplay, controller).run());
5358
}
59+
if (keyPad != null) {
60+
menu.add("Calculator", () -> new Calculator(characterDisplay, keyPad).run());
61+
}
5462
if (soundDriver != null) {
5563
menu.add("Play Demo Sound", () -> {
5664
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);

src/main/java/com/pi4j/examples/hat/elecrow/crowpi2/CrowPi2Demo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static void main(String[] args) {
1515
crowPi2.getGraphicsDisplay(),
1616
crowPi2.getTextDisplay(),
1717
crowPi2.getGameController(),
18+
crowPi2.getKeyPad(),
1819
crowPi2.getSoundDriver(),
1920
null);
2021

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

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

17-
new DisplayHatDemo(senseHat.getDisplay(), null, senseHat.getController(), null, senseHat.getAllSensors()).run();
17+
new DisplayHatDemo(
18+
senseHat.getDisplay(),
19+
null,
20+
senseHat.getController(),
21+
null,
22+
null,
23+
senseHat.getAllSensors()).run();
1824

1925
pi4j.shutdown();
2026
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.pi4j.examples.hat.DisplayHatDemo;
77

88
/**
9-
* A simple demo running a "Snake" game. Exit by pressing the select key.
9+
* A small demo allowing the user to play two mini games.
1010
*/
1111
public class Demo {
1212

@@ -18,6 +18,7 @@ static void main(String[] args) {
1818
hat.getDisplay(),
1919
null,
2020
hat.getController(),
21+
null,
2122
hat.getSoundDriver(),
2223
null).run();
2324

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.pi4j.examples.hat.waveshare.waveshare13891;
2+
3+
import com.pi4j.Pi4J;
4+
import com.pi4j.context.Context;
5+
import com.pi4j.drivers.hat.waveshare.Waveshare13891;
6+
import com.pi4j.examples.hat.DisplayHatDemo;
7+
8+
/**
9+
* A small demo allowing the user to play two mini games.
10+
*/
11+
public class Demo {
12+
13+
static void main(String[] args) {
14+
Context pi4j = Pi4J.newAutoContext();
15+
Waveshare13891 hat = new Waveshare13891(pi4j);
16+
17+
new DisplayHatDemo(
18+
hat.getDisplay(),
19+
null,
20+
hat.getController(),
21+
null,
22+
null,
23+
null).run();
24+
25+
hat.getDisplay().close();
26+
hat.getController().close();
27+
pi4j.shutdown();
28+
}
29+
30+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Waveshare 13891 Display Hat Demo
2+
3+
A small demo allowing the user to play two mini games.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.pi4j.examples.hat.DisplayHatDemo;
77

88
/**
9-
* A simple demo running a "Snake" game. Exit by holding the center button for one second.
9+
* A small demo allowing the user to play two mini games.
1010
*/
1111
public class Demo {
1212

@@ -19,6 +19,7 @@ static void main(String[] args) {
1919
null,
2020
hat.getController(),
2121
null,
22+
null,
2223
null).run();
2324

2425
hat.getDisplay().close();

src/main/java/com/pi4j/examples/ui/ListView.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public void run() {
7272
}
7373
}
7474
triggeredDirection = GameController.Direction.NONE;
75-
} else {
76-
triggeredDirection = direction;
7775
if (anyPressed(GameController.Key.CENTER, GameController.Key.START, GameController.Key.A)) {
7876
trigger();
7977
}
78+
} else {
79+
triggeredDirection = direction;
8080
}
8181
if (scroll) {
8282
x0 = x0 - 1.0f / 6;
@@ -104,8 +104,9 @@ private boolean anyPressed(GameController.Key... keys) {
104104
if (onOff != null) {
105105
if (onOff.isOn()) {
106106
triggeredKey = key;
107-
} else {
108-
return triggeredKey == key;
107+
} else if (triggeredKey == key) {
108+
triggeredKey = null;
109+
return true;
109110
}
110111
}
111112
}

0 commit comments

Comments
 (0)