|
| 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 | +} |
0 commit comments