-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
166 lines (149 loc) · 3.41 KB
/
Copy pathtest.js
File metadata and controls
166 lines (149 loc) · 3.41 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
require("dotenv").config();
const keypress = require("keypress");
const Mapper = require("./Mapper");
const Serial = require("./Serial");
const { timer } = require("./Utilities");
const { pixelMap } = require("./pixelMap");
const delay = +process.env.TEST_DELAY;
const gridWidth = +process.env.GRID_WIDTH;
const gridHeight = +process.env.GRID_HEIGHT;
let forward = true;
let running = false;
let initialized = false;
let index = 0;
const frame = new Array(gridWidth * gridHeight).fill(0);
const initializePrompt = "l to list ports, s initialize and start";
Mapper.randomPixels = false;
// make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// listen for the "keypress" event
process.stdin.on("keypress", async function(ch, key) {
if (!key) return;
switch (key.name) {
case "space":
running = !running;
break;
case "backspace":
forward = !forward;
break;
case "right":
indexForward();
break;
case "left":
indexBackward();
break;
case "up":
indexBackward(true);
break;
case "down":
indexForward(true);
break;
case "o":
allOn();
break;
case "f":
allOff();
break;
case "l":
process.stdout.write('\n');
await Serial.list();
process.stdout.write(initializePrompt);
break;
case "s":
process.stdout.write('\n');
await Serial.init();
initialized = true;
break;
case "c":
if (key.ctrl) {
process.stdin.pause();
process.exit();
}
break;
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
const indexForward = (moveRow = false) => {
if (moveRow) {
let newIndex = index + gridWidth;
if (newIndex >= frame.length - 1) {
newIndex = newIndex - frame.length;
}
index = newIndex;
} else {
if (index >= frame.length - 1) {
index = 0;
} else {
index++;
}
}
setPixel();
};
const indexBackward = (moveRow = false) => {
if (moveRow) {
let newIndex = index - gridWidth;
if (newIndex < 0) {
newIndex = frame.length - 1 + newIndex;
}
index = newIndex;
} else {
if (index <= 0) {
index = frame.length - 1;
} else {
index--;
}
}
setPixel();
};
const setPixel = () => {
frame.fill(1);
frame[index] = 0;
const pixel = pixelMap[index];
const row = Math.ceil((index + 1) / 48);
const column = (index + 1) % 48 || 48;
console.log(`Row: ${row} | Column: ${column}`);
if (pixel) {
console.log(`IO${pixel.io} | address ${pixel.index}`);
} else {
console.log("Null");
}
console.log("--------");
Serial.output(Mapper.getIOArrays(frame));
};
const allOn = () => {
running = false;
frame.fill(0);
console.log("all on\n--------");
Serial.output(Mapper.getIOArrays(frame));
};
const allOff = () => {
running = false;
frame.fill(1);
console.log("all off\n--------");
Serial.output(Mapper.getIOArrays(frame));
};
// Main Loop
const loop = async () => {
process.stdout.write(initializePrompt);
let prevTime = Date.now();
while (true) {
if (!initialized) {
const curTime = Date.now();
if (curTime - prevTime > 1000) {
process.stdout.write(".");
prevTime = curTime;
}
}
if (running && initialized) {
if (forward) {
indexForward();
} else {
indexBackward();
}
}
await timer(delay);
}
};
loop();