-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
144 lines (117 loc) · 2.83 KB
/
main.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
/**
* HappyHands Interactive
*
* By Set HM 2023.
*
*
* Designed for children to use, learn the animal species, different shapes and colors,
* and demonstrate their skill and memory.
*/
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
/**
* The Main window field.
*/
let mainWindow = null
/**
* Creates the main window and performs some tweaks on it.
*/
function createWindow() {
mainWindow = new BrowserWindow({
width: 1100,
height: 620,
show: false,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
})
mainWindow.loadFile('renderer/index.html')
mainWindow.maximize()
mainWindow.show()
}
app.whenReady().then(() => {
ipcMain.handle('get:ports', getAvailablePorts)
ipcMain.on('device-open', openDevice)
ipcMain.on('device-selected', openDevice)
createWindow()
})
/* Remote Control */
const colors = {
'V': 'green',
'A': 'yellow',
'B': 'blue'
}
const actions = {
'P': 'pressed',
'R': 'released'
}
let esp32
let prevMessage = {
color: '',
action: ''
}
// Ports logic
/**
* List all the available devices that can be opened via serial communication.
*/
async function getAvailablePorts() {
const ports = await SerialPort.list()
if (ports.length === 0) {
return "No devices"
}
return ports
}
/**
* Opens a serial communication at the desired path.
* @param event Received from the caller.
* @param path The selected port to be opened.
*/
function openDevice(event, path) {
console.log(`Selected port: ${path}`)
esp32 = new SerialPort({
path: path,
baudRate: 115200
}, err => {
if (err) {
console.log(err)
} else {
console.log('openDevice(): No errors detected')
esp32.write('')
const parser = esp32.pipe(new ReadlineParser())
parser.on('data', getDeviceMessage)
parser.on('error', getDeviceError)
}
})
}
/**
* Reads which button of the control was pushed down or released and its color.
* @param data A string that contains two characters one for the color and one for the action.
*/
function getDeviceMessage(data) {
console.log(data)
let message = data.toString().trim()
let color = colors[message.charAt(0)]
let action = actions[message.charAt(1)]
if (!(prevMessage.color === color && prevMessage.action === action)) {
console.log(color, action)
mainWindow.webContents.send(`button:${action}`, color)
prevMessage.color = color
prevMessage.action = action
}
setTimeout(() => {
prevMessage.color = ' '
prevMessage.action = ' '
}, 500)
}
/**
* Displays an error in the console given an error object.
* @param err The error object to be analyzed.
*/
function getDeviceError(err) {
if (err.disconnected == true) {
console.log('Device was disconnected')
}
}