-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine-shop-calc.js
141 lines (108 loc) · 2.87 KB
/
machine-shop-calc.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var mainMenu = {
"" : {
"title" : "-- Main Menu --"
},
"MACHINE SHOP CALCULATOR" : function() { E.emit('initUnitCalc'); },
"TEMP COMP CALC" : function() { E.emit('initTempCalc'); },
"> BACKLIGHT" : function() { Pixl.menu(subMenu); },
};
var subMenu = {
"" : {
"title" : "-- SubMenu --"
},
"ON" : function() { LED1.set(); },
"OFF" : function() { LED1.reset(); },
"< BACK" : function() { Pixl.menu(mainMenu); },
};
var UnitCalc = function() {
var self = this;
this.keys = '123456789*0#';
this.rows = [D6,D5,D4,D3];
this.cols = [D2,D1,D0];
this.input = '';
this.value = 0;
this.in = 0;
this.mm = 0;
this.keypad = require('KeyPad').connect(this.cols, this.rows, (index) => {
//print(self.keys[index]);
var keystroke = self.keys[index];
if (keystroke == '*') keystroke = '.';
if (keystroke == '#') {
keystroke = '';
self.input = '';
}
self.input += keystroke;
self.calc(self.input);
self.display();
});
this.calc = function(value) {
self.in = self.round(self.mmToInch(value), 4);
self.mm = self.round(self.inchToMm(value), 4);
self.value = self.round(value, 4);
};
this.mmToInch = function(mm) {
return mm / 25.4;
};
this.inchToMm = function(inch) {
return inch * 25.4;
};
this.round = function(value, n) {
return Number(value).toFixed(n);
};
this.display = function() {
// Clear the LCD
g.clear();
// Draw Screen Divisions
g.drawLine(0, 10, 128, 10);
g.drawLine(64, 10, 64, 64);
// Set Font Type
g.setFont6x8();
// Draw Header
g.drawString('MACHINE SHOP CALCULATOR', 1, 0);
// Draw mm to inch values
g.drawString(self.value + ' mm', 2, 22);
g.drawString(self.in + ' in', 2, 36);
// Draw inch to mm values
g.drawString(self.value + ' in', 66, 22);
g.drawString(self.mm + ' mm', 66, 36);
// Set Font Type
g.setFontBitmap();
// Draw "TEMP COMP CALC" text
g.drawString('TEMP COMP CALC', 2, 58);
// Update the display
g.flip();
};
this.init = function() {
require("Font6x8").add(Graphics);
self.calc(0);
self.display();
};
};
var TempCompCalc = function() {
var self = this;
this.tempC = E.getTemperature();
this.tempF = (this.tempC * 1.8 + 32).toFixed(1);
this.getTemp = function() {
self.tempC = E.getTemperature();
self.tempF = (self.tempC * 1.8 + 32).toFixed(1);
print('tempF: ' + self.tempF);
};
this.init = function() {
setInterval(self.getTemp, 2000);
};
};
var initTimer = setTimeout(() => {
E.emit('init');
}, 2000);
E.on('init', function() {
Pixl.menu(mainMenu);
initTimer = null;
});
E.on('initUnitCalc', function() {
var unitCalc = new UnitCalc();
unitCalc.init();
});
E.on('initTempCalc', function() {
var tempCompCalc = new TempCompCalc();
tempCompCalc.init();
});