-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsensors.ts
More file actions
100 lines (94 loc) · 3.02 KB
/
sensors.ts
File metadata and controls
100 lines (94 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Abstraction for all available sensors.
* This class is extended by each of the concrete sensors which add on static methods for their name, getting their readings & optionally min/max readings
*/
class Sensor {
/** Immutable: Abs(minimum) + Abs(maximum); calculated once at start since min & max can't change */
private range: number
constructor(
private name: string,
private radioName: string,
private sensorFn: () => number,
private minimum: number,
private maximum: number,
private isJacdacSensor: boolean,
private setupFn?: () => void
) {
// Data from opts:
this.range = Math.abs(this.minimum) + this.maximum
// Could be additional functions required to set up the sensor (see Jacdac modules or Accelerometers):
if (this.setupFn != null) this.setupFn()
}
//------------------
// Factory Function:
//------------------
/**
* Factory function used to generate a Sensor from that sensors: .getName(), sensorSelect name, or its radio name
* This is a single factory within this abstract class to reduce binary size
* @param name either sensor.getName(), sensor.getRadioName() or the ariaID the button that represents the sensor in SensorSelect uses.
* @returns concrete sensor that the input name corresponds to.
*/
public static getFromName(name: string): Sensor {
if (name == "Light" || name == "L")
return new Sensor(
"Light",
"L",
() => input.lightLevel(),
0,
255,
false
)
else if (name == "Temp." || name == "Temperature" || name == "T")
return new Sensor(
"Temperature",
"T",
() => input.temperature(),
-40,
100,
false
)
else if (name == "Magnet" || name == "M")
return new Sensor(
"Magnet",
"M",
() => input.magneticForce(Dimension.Strength),
-5000,
5000,
false
)
else if (name == "Volume" || name == "Microphone" || name == "V")
return new Sensor(
"Microphone",
"V",
() => input.soundLevel(),
0,
255,
false
)
else return undefined
}
//---------------------
// Interface Functions:
//---------------------
getName(): string {
return this.name
}
getRadioName(): string {
return this.radioName
}
getReading(): number {
return this.sensorFn()
}
getNormalisedReading(): number {
return Math.abs(this.getReading()) / this.range
}
getMinimum(): number {
return this.minimum
}
getMaximum(): number {
return this.maximum
}
isJacdac(): boolean {
return this.isJacdacSensor
}
}