-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyserial.js
executable file
·150 lines (133 loc) · 3.89 KB
/
keyserial.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
// [keyserial.js]
// keyboard serial command receiver
//
// CCA3.0 License 2015 by D.F.Mac.
// See also http://creativecommons.org/licenses/by/3.0/
var keyserial = function(param){
this.isActive = false; // true: handling key | false:thru key
this.isConnect = false; // true: connecting | false: disconnecting
this.mode = 0; // 0:keycode | 1:number | 2:str
this.isWDT = false; // true: WDT on | false: WDT off
if(param){
if((param.polling == true)||(param.polling == false)){
this.isWDT = param.polling;
}
}
this.WDEXPIRE = 4; // 1000 * 4 ms
this.wdcount = this.WDEXPIRE;
this.str = "";
this.value = 0;
////////// interfaces
// start handling key
this.start = function(){
console.log("keyserial.start");
this.isActive = true;
if(this.isWDT == false){
this.onConnect();
this.isConnect = true;
}
}.bind(this);
// stop handling key
this.stop = function(){
console.log("keyserial.stop");
this.isActive = false;
if(this.isWDT == false){
this.onDisconnect();
this.isConnect = false;
}
}.bind(this);
////////// Event Handler
// Connection state change : connect
this.onConnect = function(){
console.log("keyserial.onConnect");
}.bind(this);
// Connection state change : disconnect
this.onDisconnect = function(){
console.log("keyserial.onDisconnect");
}.bind(this);
// key event handler
this.onKey = function(keyCode){
console.log("keyserial.onKey:"+keyCode);
}.bind(this);
// number handler (0-99999)
this.onNumber = function(value){
console.log("keyserial.onValue:"+value);
}.bind(this);
// string handler
this.onString = function(str){
console.log("keyserial.onString:"+str);
}.bind(this);
////////// Internal Functions
// hander:number
this.handleNumber = function(keyCode){
if(keyCode == 13){
this.onValue(this.value);
this.mode = 0;
}else{
if((keyCode >= 48) && (keyCode <= 57)){
var val = keyCode - 48;
this.value = (this.value * 10)+val;
if(this.value >= 99999){ // 本当は65565にしたいけど面倒w
console.log("keyserial.handleNumber:error-001");
this.mode = 0;
}
}else{
console.log("keyserial.handleNumber:error-002");
this.mode = 0;
}
}
}.bind(this);
// hander:str
this.handleStr = function(keyCode){
if(keyCode == 13){
this.onString(this.str);
this.mode = 0;
}else{
this.str = this.str+String.fromCharCode(keyCode);
}
}.bind(this);
// Watch Dog Timer
window.setInterval(function(){
if(this.isWDT == true){
if(this.wdcount > 0){
this.wdcount --;
if(this.wdcount == 0){
this.onDisconnect();
this.isConnect = false;
}
}
}
}.bind(this),1000);
// KeyEvent dispatcher
// 'Enter':13 : mode=0 : key oneshot mode
// ',':188 : mode=1 : number mode ('0':48 - '9':57)
// '.':190 : mode=2 : strings mode
document.onkeydown = function(ev){
if(this.isActive == true){
// polling
if(ev.keyCode == 13){
this.wdcount = this.WDEXPIRE;
if(this.isConnect == false){
this.onConnect();
}
this.isConnect = true;
}
// handle key
if(this.mode == 0){
if(ev.keyCode == 190){ // ',' comma
this.str = "";
this.mode = 2; // str
}else if(ev.keyCode == 188){ // '.' period
this.value = 0;
this.mode = 1; // number
}else{
this.onKey(ev.keyCode);
}
}else if(this.mode == 1){
this.handleNumber(ev.keyCode);
}else if(this.mode == 2){
this.handleStr(ev.keyCode);
}
}
}.bind(this);
};