-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.ino
120 lines (106 loc) · 2.91 KB
/
Main.ino
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
#include <Keyboard.h>
///////////////adjust for your own build///////////////////
const int sw_pins[3] = {7,8,9.}; // right, left, shifter
const int bp_pin = 6; //pin for speaker
const int bp_freq = 880; //speaker frequency
const int paris_m = 10; //how many PARIS(50dots) per min.
//check Wikipedia.
const int bp_span = 60000 / (50*paris_m);
//how many milliseconds a dot is.
////////////////////////////////////////////////////////////
long lastbtt, lastbtt_shift;
byte charbuffI = 0;
char charbuffC[24];
byte shiftbuff = 0;
bool sh_cap = false;
bool silent;
void setup(){
Keyboard.begin();
Serial.begin(9600);
for(int i = 0; i < 3; i++){
pinMode(sw_pins[i], INPUT_PULLUP);
}
silent = !digitalRead(sw_pins[2]);
if(!silent) switchon();
//lastbtt = false;
}
//
void loop() {
detectPaddles();
charEnter();
detectPRepeat();
charEnter();
detechShift();
shiftEnter();
Beep();
delay(10);
}
bool paddle_flag[2] = {false, false};
char tontsu[2] = {'.', '-'};
void detectPaddles(){
for(int i = 0; i < 2; i++){
int gauge = 0;
while(!paddle_flag[i] && digitalRead(sw_pins[i]) == LOW){
gauge++;
if(gauge > 1000){
gauge = 0;
Serial.println(i); //buffering
insMem((i+1)*2 -1); //{0,1] into [1,3]
charbuffC[charbuffI] = tontsu[i];
charbuffI++; //count up index
lastbtt = millis();
break;
}
}
paddle_flag[i] = !digitalRead(sw_pins[i]);
}
}
void detectPRepeat(){
for(int i = 0; i < 2; i++){
if(digitalRead(sw_pins[i]) == LOW
&&(millis() - lastbtt) > hl_Wait("repeat")){
insMem((i+1)*2 -1); //{0,1] into [1,3]
charbuffC[charbuffI] = tontsu[i];
charbuffI++; //count up index
lastbtt = millis();
}
}
}
void charEnter(){
if((millis() - lastbtt) > hl_Wait("char")
&& charbuffI > 0){
Serial.print("~~~");
char ch = CONVtoCHAR(reverseBuff());
Serial.print(ch);
Keyboard.write(ch);
if(sh_cap){
Keyboard.release(129);
sh_cap = false;
}
Serial.println("~~~");
charbuffI = 0;
}//
}
String reverseBuff(){
String res = "";
for(int i = 0; i < charbuffI; i++){
//res += charbuffC[charbuffI - (i+1)];
res += charbuffC[i];
}
return res;
}
int hl_Wait(String mode){
if(mode == "repeat"){
if(charbuffC[charbuffI - 1] == '.'){ //char timeout
return bp_span *2;
}else{
return bp_span *4;
}
}else if(mode == "char"){
if(charbuffC[charbuffI - 1] == '.'){ //char timeout
return bp_span *4;
}else{
return bp_span *6;
}
}
}