-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursorReal.js
148 lines (112 loc) · 3.05 KB
/
CursorReal.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
142
143
144
145
146
147
148
const _ = require('lodash');
const fs = require('fs');
const inquirer = require('inquirer')
const SerialPort = require('serialport')
const sleep = require('./sleep')
const CursorFile = require('./CursorFile')
const SerialChannel = require('./SerialChannel')
module.exports = class CursorReal{
constructor(comPort) {
// super('examples/collision.turin', 0)
// super('examples/collision.turin', 0)
this.port = new SerialPort(comPort, {
baudRate: 115200,
})
// Ready promise
this.promiseReady = new Promise((resolve, reject) => {
this.port.on('open', () => setTimeout(() => resolve(), 2000))
})
this.channel = new SerialChannel(this.port, {
// debug: true
})
}
static async getPort(prefered) {
let ports = await SerialPort.list()
let found = _.find(ports, {comName: prefered})
if (found)
return prefered
console.log(`Port '${prefered}' not available`)
let answer = await inquirer.prompt({
type: 'list',
name: 'port',
choices: _.map(ports, 'comName'),
message: 'Select Cursor port'
})
return answer.port
}
async ready() {
return this.promiseReady
}
async goToStart() {
await this.channel.execute('r')
// await super.goToStart()
}
async read() {
let raw = await this.channel.execute('c')
let color1 = raw.split(':')[2]
let color2 = raw.split(':')[3]
return [parseHexToColor(color1), parseHexToColor(color2)]
}
async next() {
let result = await this.channel.execute('n')
result = result.replace(':n:', '')
if (result != 'ok') {
return false
}
return true
// return await super.next()
}
async previous() {
let result = await this.channel.execute('b')
result = result.replace(':b:', '')
if (result != 'ok')
throw new Error('Failed to go to previous: '+result)
// await super.previous()
}
halt() {
// throw new Error("Something bad happened!")
}
}
const colors = {
'EMPTY': hex2rgb('B64A4A'),
'RED': hex2rgb('C03830'),
'BLUE': hex2rgb('416754'),
'BEIGE': hex2rgb('63642D'),
'WHITE': hex2rgb('5C6435'),
'BROWN': hex2rgb('755731'),
'PURPLE': hex2rgb('5C5866'),
'LGREEN': hex2rgb('577825'),
'DGREEN': hex2rgb('3C8E3B'),
'ORANGE': hex2rgb('9A4420'),
}
function parseHexToColor(hex) {
let rgb = hex2rgb(hex)
let bestDiff = 100000
let bestColor = null
for (let i in colors) {
let color = colors[i]
let diff = diffColors(rgb, color)
if (diff > bestDiff)
continue
bestDiff = diff
bestColor = i
}
return bestColor
}
function diffColors(c1, c2) {
return Math.sqrt(
Math.pow(c1[0] - c2[0], 2) +
Math.pow(c1[1] - c2[1], 2) +
Math.pow(c1[2] - c2[2], 2))
}
function hex2rgb(hex) {
if (typeof hex !== 'string') {
throw new TypeError('Expected a string');
}
hex = hex.replace(/^#/, '');
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
var num = parseInt(hex, 16);
return [num >> 16, num >> 8 & 255, num & 255];
};