Skip to content

Commit a915442

Browse files
committed
sw_concepts: rewrite abstractions.py to java
1 parent e088b5b commit a915442

File tree

4 files changed

+332
-120
lines changed

4 files changed

+332
-120
lines changed

topics/sw_concepts/code/abstractions.py

-120
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package ch.scs.jumpstart.pattern.examples;
2+
3+
import java.io.*;
4+
import java.nio.file.*;
5+
import java.util.*;
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class Abstractions {
9+
10+
private static final int INPUT_MIN = 0;
11+
private static final int INPUT_MAX_TSL2550 = 544;
12+
private static final int INPUT_THRESHOLD = 11;
13+
14+
private static final double OUTPUT_MIN_FACTOR = 0.2;
15+
private static final double OUTPUT_CHANGE_MAX_FACTOR = 0.005;
16+
17+
private static final int[] SENSOR_VALUE_LUX_APPROX_MAP = {
18+
0, 1, 2, 3, 4, 5, 6, 7,
19+
8, 9, 10, 11, 12, 13, 14, 15,
20+
16, 18, 20, 22, 24, 26, 28, 30,
21+
32, 34, 36, 38, 40, 42, 44, 46,
22+
49, 53, 57, 61, 65, 69, 73, 77,
23+
81, 85, 89, 93, 97, 101, 105, 109,
24+
115, 123, 131, 139, 147, 155, 163, 171,
25+
179, 187, 195, 203, 211, 219, 227, 235,
26+
247, 263, 279, 295, 311, 327, 343, 359,
27+
375, 391, 407, 423, 439, 455, 471, 487,
28+
511, 543, 575, 607, 639, 671, 703, 735,
29+
767, 799, 831, 863, 895, 927, 959, 991,
30+
1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
31+
1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
32+
2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
33+
3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015
34+
};
35+
36+
public static void main(String[] args) throws IOException {
37+
if (args.length != 3) {
38+
throw new IllegalArgumentException("Wrong number of arguments");
39+
}
40+
41+
String path = args[0];
42+
String inputPath = args[1];
43+
String outputPath = args[2];
44+
boolean opt3001 = Files.isSymbolicLink(Paths.get(inputPath)) &&
45+
Files.readSymbolicLink(Paths.get(inputPath)).toString().contains("in_illuminance_input");
46+
47+
int inputMax = INPUT_MAX_TSL2550;
48+
int inputLastValue = INPUT_MIN - INPUT_THRESHOLD;
49+
Integer outputLastValue = null;
50+
51+
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
52+
int outputMax = Integer.parseInt(reader.readLine().trim());
53+
int outputMin = (int) Math.ceil(outputMax * OUTPUT_MIN_FACTOR);
54+
int outputChangeMax = (int) Math.ceil(outputMax * OUTPUT_CHANGE_MAX_FACTOR);
55+
56+
while (true) {
57+
try {
58+
int inputValue;
59+
if (opt3001) {
60+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
61+
inputValue = (int) Double.parseDouble(inputReader.readLine().trim());
62+
} catch (IOException e) {
63+
inputValue = INPUT_MIN;
64+
}
65+
} else {
66+
try (BufferedReader inputReader = new BufferedReader(new FileReader(inputPath))) {
67+
inputValue = Integer.parseInt(inputReader.readLine().trim());
68+
}
69+
if (0 <= inputValue && inputValue < SENSOR_VALUE_LUX_APPROX_MAP.length) {
70+
inputValue = SENSOR_VALUE_LUX_APPROX_MAP[inputValue];
71+
} else {
72+
inputValue = inputLastValue;
73+
}
74+
}
75+
76+
inputValue = Math.min(inputValue, inputMax);
77+
78+
if (Math.abs(inputValue - inputLastValue) < INPUT_THRESHOLD) {
79+
inputValue = inputLastValue;
80+
}
81+
82+
double a = (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN);
83+
int value1 = (int) (a * (outputMax - outputMin) + outputMin);
84+
int outputValue = Math.min(value1, outputMax);
85+
86+
if (outputLastValue == null) {
87+
outputValue = outputValue;
88+
} else if (outputValue >= outputLastValue) {
89+
outputValue = Math.min(outputValue, outputLastValue + outputChangeMax);
90+
} else {
91+
outputValue = Math.max(outputValue, outputLastValue - outputChangeMax);
92+
}
93+
int dimmedValue = outputValue;
94+
95+
if (!Objects.equals(outputValue, outputLastValue)) {
96+
System.out.printf("input: %4d (%4.1f%%), output: %4d (%4.1f%%), dimmed: %4d (%4.1f%%)%n",
97+
inputValue, 100 * (inputValue - INPUT_MIN) / (double) (inputMax - INPUT_MIN),
98+
outputValue, 100 * (outputValue - outputMin) / (double) (outputMax - outputMin),
99+
dimmedValue, 100 * (dimmedValue - outputMin) / (double) (outputMax - outputMin));
100+
System.out.flush();
101+
}
102+
103+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath))) {
104+
outputLastValue = outputValue;
105+
writer.write(String.valueOf(outputValue));
106+
}
107+
108+
inputLastValue = inputValue;
109+
} catch (IOException e) {
110+
if (!(e instanceof FileNotFoundException)) {
111+
throw e;
112+
}
113+
}
114+
115+
TimeUnit.MILLISECONDS.sleep(10);
116+
}
117+
} catch (IOException | InterruptedException e) {
118+
e.printStackTrace();
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)