-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuttpit.ino
More file actions
347 lines (269 loc) · 8.51 KB
/
buttpit.ino
File metadata and controls
347 lines (269 loc) · 8.51 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "LedControl.h"
#include <rotary.h>
#include <Keyboard.h>
// PINS for LED
#define DINPIN 10 //PIN to DIN
#define CSPIN 16 //PIN to CS
#define CLKPIN 14 //PIN to CLK
// 1st encoder - for fuel
#define ROTARY_PIN1 3
#define ROTARY_PIN2 2
// 2nd encoder - for next/prev control
#define ROTARY_PIN3 0
#define ROTARY_PIN4 1
// 3rd encoder - for change value
#define ROTARY_PIN5 4
#define ROTARY_PIN6 5
// define to enable weak pullups.
#define ENABLE_PULLUPS
#define NumSwitches 4
#define ShortPressTime 200
#define ShowPressuresSwitchTime 2000
#define FT B00100010 // BOTH FRONT TIRES
#define RT B00010100 // BOTH REAR TYRES
#define WDS B00000001 // WINDSCREEN
#define FRP B01000000 // FAST REPAIR
#define KeysPin1 A3
#define KeysPin2 A2
#define NumAnalogWires 2
int FrontTiresPress=0, RearTiresPress=0;
long TimeTmp;//,TTmpKeys;
boolean ShowPressuresFlag = 0;
boolean ChangeFlag = 0;
struct AnalogKeysStruct {
int PIN;
int AnalogKeyValue;
int PrevAnalogKeyValue;
long TTmpKeys;
};
AnalogKeysStruct buttons[NumAnalogWires] = {
{KeysPin1, 0, 0, 0},
{KeysPin2, 0, 0, 0},
};
Rotary r1 = Rotary(ROTARY_PIN1, ROTARY_PIN2);
Rotary r2 = Rotary(ROTARY_PIN3, ROTARY_PIN4);
Rotary r3 = Rotary(ROTARY_PIN5, ROTARY_PIN6);
volatile unsigned int encoderPos = 0; // a counter for the dial
struct SwitchStates { // struct's def for holding states of our switches
int PIN; // switch's PIN
boolean State;
boolean PrevState;
long TimePressed;
boolean ShortPress;
};
SwitchStates Switches[] = {
{6,false,false,0,false},
{7,false,false,0,false},
{8,false,false,0,false},
{9,false,false,0,false},
};
byte PitCombo,PrevPitCombo = 0;
LedControl lc=LedControl(DINPIN,CLKPIN,CSPIN,1);
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
for (byte i=0;i<NumSwitches;i++) {
pinMode(Switches[i].PIN, INPUT_PULLUP);
}
pinMode(ROTARY_PIN1, INPUT_PULLUP);
pinMode(ROTARY_PIN2, INPUT_PULLUP);
pinMode(ROTARY_PIN3, INPUT_PULLUP);
pinMode(ROTARY_PIN4, INPUT_PULLUP);
pinMode(ROTARY_PIN5, INPUT_PULLUP);
pinMode(ROTARY_PIN6, INPUT_PULLUP);
Keyboard.begin();
TimeTmp = millis();
}
void loop() {
CheckSwitches(); //check current switches' states and populate structure
ProcessSwitches(); //process switches' structure and show corresponding pictograph
CheckFuelEncoder(); //check position of fuel encoder and show requested number of fuel
CheckControlEncoder(); //check and process control encoder
CheckChangeEncoder(); //check and process change encoder
CheckButtons();
ShowPressures();
}
void ProcessPitCombo(void) {
String StrResult = "#clear";
String tmp;
Keyboard.write('t');
delay(500);
// Keyboard.print("#clear");
// delay (50);
if (FrontTiresPress) {
FrontTiresPress > 0 ? tmp = "+" : tmp = "";
StrResult = String(StrResult + " lf " + tmp + FrontTiresPress + " rf " + tmp + FrontTiresPress);
}
if (RearTiresPress) {
RearTiresPress > 0 ? tmp = "+" : tmp = "";
StrResult = String(StrResult + " lr " + tmp + RearTiresPress + " rr " + tmp + RearTiresPress);
}
if (PitCombo) {
if (PitCombo & FT && !FrontTiresPress) {
StrResult = String(StrResult + " lf rf");
}
if (PitCombo & RT && !RearTiresPress) {
StrResult = String(StrResult + " lr rr");
}
}
if (encoderPos) {
StrResult = String(StrResult + " fuel " + encoderPos);
}
if (PitCombo) {
if (PitCombo & FRP) {
StrResult = String(StrResult + " fr");
}
if (PitCombo & WDS) {
StrResult = String(StrResult + " ws");
}
}
// StrResult = String(StrResult + "$");
Keyboard.print(StrResult);
delay(ShortPressTime);
Keyboard.write(KEY_RETURN);
// delay(ShortPressTime);
}
void ShowPressures(void) {
if ((millis() - TimeTmp) > ShowPressuresSwitchTime) {
TimeTmp = millis();
ShowPressuresFlag = ShowPressuresFlag ^ 1;
}
if ((FrontTiresPress != 0) && (RearTiresPress == FrontTiresPress)) {
ShowNumber(RearTiresPress,4);
lc.setRow(0,7,RT|FT);
return;
}
if (ShowPressuresFlag && FrontTiresPress) {
ShowNumber(FrontTiresPress,4);
lc.setRow(0,7,FT);
ChangeFlag = 0;
return;
}
if (!ShowPressuresFlag && RearTiresPress) {
ShowNumber(RearTiresPress,4);
lc.setRow(0,7,RT);
ChangeFlag = 0;
return;
}
if ((FrontTiresPress == 0) && (RearTiresPress == FrontTiresPress)) {
//ShowNumber(RearTiresPress,4);
lc.setRow(0,7,0);
lc.setRow(0,6,0);
lc.setRow(0,5,0);
lc.setRow(0,4,0);
return;
}
}
void CheckButtons(void) {
for (int i=0;i<NumAnalogWires;i++) {
buttons[i].AnalogKeyValue = analogRead(buttons[i].PIN);
if (buttons[i].AnalogKeyValue>150 && buttons[i].PrevAnalogKeyValue>150 && (millis() - buttons[i].TTmpKeys) > ShortPressTime) {
buttons[i].TTmpKeys = millis();
if (buttons[i].AnalogKeyValue > 600 && buttons[i].PrevAnalogKeyValue > 600) {
if (!i) FrontTiresPress --; ShowPressuresFlag=1;
if (i) Keyboard.write(KEY_ESC);
delay(ShortPressTime);
}
if (buttons[i].AnalogKeyValue > 350 && buttons[i].AnalogKeyValue <= 600 && buttons[i].PrevAnalogKeyValue > 350 && buttons[i].PrevAnalogKeyValue <= 600) {
if (!i) FrontTiresPress ++; ShowPressuresFlag=1;
if (i) Keyboard.write(KEY_TAB);
delay(ShortPressTime);
}
if (buttons[i].AnalogKeyValue > 250 && buttons[i].AnalogKeyValue <= 350 && buttons[i].PrevAnalogKeyValue > 250 && buttons[i].PrevAnalogKeyValue <= 350) {
if (!i) RearTiresPress --; ShowPressuresFlag=0;
if (i) Keyboard.write(0x20);
delay(ShortPressTime);
}
if (buttons[i].AnalogKeyValue > 200 && buttons[i].AnalogKeyValue <= 250 && buttons[i].PrevAnalogKeyValue > 200 && buttons[i].PrevAnalogKeyValue <= 250) {
if (!i) RearTiresPress ++; ShowPressuresFlag=0;
if (i) ProcessPitCombo();
delay(ShortPressTime);
}
if (!i) TimeTmp = millis();
}
if (buttons[i].AnalogKeyValue>150 && buttons[i].PrevAnalogKeyValue<150) {
buttons[i].TTmpKeys = millis();
}
if (buttons[i].AnalogKeyValue != buttons[i].PrevAnalogKeyValue) buttons[i].PrevAnalogKeyValue = buttons[i].AnalogKeyValue;
}
}
void CheckChangeEncoder(void) {
unsigned char result = r3.process();
if (result) {
Keyboard.write(result == DIR_CCW ? KEY_LEFT_ARROW : KEY_RIGHT_ARROW );
}
}
void CheckFuelEncoder(void) {
unsigned char result = r1.process();
if (result) {
if (result == DIR_CCW && encoderPos < 999) encoderPos++ ;
if (result == DIR_CW && encoderPos > 0) encoderPos--;
ShowNumber(encoderPos,0);
}
}
void CheckControlEncoder(void) {
unsigned char result = r2.process();
if (result) {
Keyboard.write(result == DIR_CCW ? KEY_UP_ARROW : KEY_DOWN_ARROW);
}
}
// Shows 3 digit number beginning from _pos position
void ShowNumber (int _encoderPos, byte _pos) {
int tmp;
tmp = abs(_encoderPos);
// if (_encoderPos != 0) {
lc.setDigit(0,_pos,tmp%10,false);
// } else lc.setRow(0,_pos,0);
tmp /= 10;
if (tmp != 0) {
lc.setDigit(0,_pos+1,tmp%10,false);
tmp /= 10; //_encoderPos/100;
if (tmp != 0) {
lc.setDigit(0,_pos+2,tmp%10,false);
if (_encoderPos < 0) lc.setRow(0,_pos+3,WDS);
} else {
lc.setRow(0,_pos+2,0);
if (_encoderPos < 0) lc.setRow(0,_pos+2,WDS);
}
} else {
lc.setRow(0,_pos+1,0);
if (_encoderPos < 0) lc.setRow(0,_pos+1,WDS);
}
}
void ProcessSwitches(void) {
for (byte i=0;i<NumSwitches;i++) {
switch (i) {
case 0:
if (Switches[i].State) PitCombo |= FT; else PitCombo &= ~FT; // switch 0 - request to change front tyres
break;
case 1:
if (Switches[i].State) PitCombo |= RT; else PitCombo &= ~RT; // switch 1 - request to change rear tyres
break;
case 2:
if (Switches[i].State) PitCombo |= WDS; else PitCombo &= ~WDS; // switch 2 - request to clean wildscrean
break;
case 3:
if (Switches[i].State) PitCombo |= FRP; else PitCombo &= ~FRP; // switch 3 - request to make fast repair
}
}
if (PitCombo != PrevPitCombo) {
lc.setRow(0,3,PitCombo);
PrevPitCombo = PitCombo;
}
}
void CheckSwitches(void) {
long CheckTime;
for (byte i=0;i<NumSwitches;i++) {
if (digitalRead(Switches[i].PIN) == LOW) Switches[i].State = true;
else {
Switches[i].State = false;
}
}
}