-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNextion.js
155 lines (123 loc) · 3.83 KB
/
Nextion.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
149
150
151
152
153
154
155
/* Copyright (c) 2016 HyGy. See the file LICENSE for copying permission. */
/*
This is a basic nextion hmi lcd handling module. Tested with NX3224T028_011R 2.8” 320*240 LCD.
http://wiki.iteadstudio.com/Nextion_HMI_Solution
*/
var C = {};
function NEXTION() { }
/** 'public' constants here */
NEXTION.prototype.C = {};
/** Set the given page */
exports.setPage = function(pageNum) {
this.serialPort.print('page ' + pageNum + '\xff\xff\xff');
};
/** Set the given page */
exports.get = function(att) {
this.serialPort.print('get ' + att + '\xff\xff\xff');
};
exports.sendme = function() {
this.serialPort.print('sendme\xff\xff\xff');
}
exports.refreshComponent = function(componentId) {
this.serialPort.print('ref '+componentId+'\xff\xff\xff');
}
// cov: variable type conversion
exports.cov = function(att1, att2, length)
{
this.serialPort.print('cov '+att1+','+att2+','+length+'\xff\xff\xff');
}
// enter touch calibration
exports.touch_j = function ()
{
this.serialPort.print('touch_j'+'\xff\xff\xff');
}
// sets the given variable, if new Val is string then send as string if number then send as number
exports.setVal = function(varName, newVal) {
if (typeof(newVal)=='string')
{
this.serialPort.print(varName+'="'+newVal+'"'+'\xff\xff\xff');
}
else {
this.serialPort.print(varName+'='+newVal+'\xff\xff\xff');
}
}
/** Put most of my comments outside the functions... */
exports.commandRecived = function() {
var lastNextionCommand=this.lastNextionCommand;
var me = this;
console.log('nextionCommandRecived called');
console.log(lastNextionCommand);
switch (lastNextionCommand[0]) {
case 0x1a: // variable name is invalid
console.log('0x1a: variable name is invalid');
break;
case 0x1b: // variable name is invalid
console.log('0x1b: variable operation invalid');
break;
case 0x65: // touch event
console.log('touch event');
me.emit(
'touchevent',
lastNextionCommand[1], // pageId
lastNextionCommand[2], // componentId
lastNextionCommand[3] === 0x0 ? 'release' : 'press' // touchEvent press: 0x01, release 0x00
);
break;
case 0x00:
console.log('0x00: Invalid instruction sent.');
break;
case 0x66: // Current page ID number returns
me.emit(
'getpageid',
lastNextionCommand[1]
);
break;
case 0X70: // String variable data returns
console.log('String variable data returns');
var stringToSend = "";
for (var cikl = 1; cikl < lastNextionCommand.length; cikl++) {
stringToSend += E.toString(lastNextionCommand[cikl]);
}
me.emit(
'stringdatareturned',
stringToSend
);
break;
case 0X71: // Numeric data returns
console.log('Numeric variable data returns');
var numToSend = lastNextionCommand[1]+ (lastNextionCommand[2] << 8) + (lastNextionCommand[3] << 16) + (lastNextionCommand << 24);
me.emit(
'numericdatareturned',
numToSend
);
break;
default:
console.log('unknown command started with: 0x' + lastNextionCommand[0].toString(16));
}
};
var dataArrived = function(_data) {
var c;
for (var cikl = 0; cikl < _data.length; cikl++) {
c = _data[cikl];
if (c == "\xff") {
this.instructionEndCount++;
} else {
this.nextionCommandCollector.push(c);
}
if (this.instructionEndCount == 3) {
this.instructionEndCount = 0;
this.lastNextionCommand = E.toUint8Array(this.nextionCommandCollector);
this.nextionCommandCollector = [];
this.commandRecived();
}
}
};
exports.connect = function(_port) {
var me=this;
this.serialPort = _port;
this.instructionEndCount=0;
this.nextionCommandCollector=[];
this.lastNextionCommand=[];
Serial1.on('data', dataArrived.bind(me));
return new NEXTION();
}