-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan.ts
81 lines (74 loc) · 1.49 KB
/
fan.ts
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
import { GCode, GCommand } from './gcode';
/**
* Fan class can process GCode related to the part cooling fan
*
* @export
* @class Fan
*/
export class Fan {
/**
* Gcodes supported by this class
*
* @public
* @static
* @readonly
*/
public static readonly SUPPORTED_GCODES = [GCommand.SET_FAN_SPEED, GCommand.TURN_OFF_FAN];
/**
* Current fan speed [0-255]
*
* @private
* @type {number}
*/
private speed: number = 0;
/**
* Change in fan speed after last gcode
*
* @public
* @type {number}
*/
public speedDelta: number = 0;
/**
* Executes provided gcode if it is supported
*
* @param {GCode} gcode
*/
exec(gcode: GCode): void {
if (gcode.command === GCommand.SET_FAN_SPEED) {
const { S } = gcode.params;
if (S) {
this.setSpeed(Number(S));
}
} else if (gcode.command === GCommand.TURN_OFF_FAN) {
this.setSpeed(0);
}
}
/**
* Sets fan speed
*
* @param {number} newSpeed
*/
setSpeed(newSpeed: number): void {
this.speedDelta = newSpeed - this.speed;
this.speed = newSpeed;
}
/**
* Returns current fan speed [0-255]
*
* @returns {number}
*/
getSpeed(): number {
return this.speed;
}
/**
* Bakes current fan speed into fan gcode (M106)
*
* @returns {GCode}
*/
getSpeedCommand(): GCode {
const gcode = new GCode();
gcode.command = GCommand.SET_FAN_SPEED;
gcode.params.S = this.speed.toFixed(2).toString();
return gcode;
}
}