/
k9os.ino
461 lines (407 loc) · 14.1 KB
/
k9os.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**
* K9 OS v1.0
*
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <aJSON.h>
#include <Servo.h>
#define I2C_ADDR 0x3F // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
byte U4PortAP=0, U4PortBP=0, U4PortA=0, U4PortB =0; // Define variables to hold I/O port readings.
int testModeCounter=0;
int n = 1;
int debug=0;
int testmodeStatus=0;
unsigned long last_print = 0;
//aJsonStream serial_stream(&Serial);
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
int pattern=0;
Servo myservoVertical; // create servo object to control a servo
Servo myservoHorizontal;
int pos = 0; // variable to store the servo position
int wag=0;
void setup()
{
setTailToNeutral();
lcd.begin (20,4);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.print(" *** K9 OS 0.1a *** ");
lcd.setCursor ( 0, 1 ); // go to the 2nd line
lcd.print(" SYSTEM BOOTING ");
lcd.setCursor ( 0, 2 ); // go to the third line
lcd.print(" PLEASE WAIT");
lcd.setCursor ( 0, 3 ); // go to the fourth line
lcd.print("");
// Setup Serial Communications
Serial.begin(115200);
setupKeypad();
}
void setupKeypad()
{
Wire.begin(); // Begin I2C bus
/*
* The following code starts I2C communication with MCP23017(U2).Please refer MCP23017 datasheet
* for register addresses. Sets all GPIOs of this IC to OUTPUT
*/
Wire.beginTransmission(0x20); // Starts communication with MCP23017(U2) IC
Wire.write(0x00); // Set MCP23017 memory pointer to IODIRA address
Wire.write(0x00); // Set all pins of PORTA to outputs
Wire.endTransmission(); // Ends I2C communication with MCP23017(U2) IC
Wire.beginTransmission(0x20); // Starts communication with MCP23017(U2) IC
Wire.write(0x01); // Set MCP23017 memory pointer to IODIRB address
Wire.write(0x00); // Set all pins of PORTB to outputs
Wire.endTransmission(); // Ends I2C communication with MCP23017(U2) IC
/*
* The following code starts I2C communication with MCP23017(U4).Please refer MCP23017 datasheet
* for register addresses. Sets all GPIOs of this IC to OUTPUT
*/
Wire.beginTransmission(0x21); // Starts communication with MCP23017(U4) IC
Wire.write(0x00); // Set MCP23017 memory pointer to IODIRA address
Wire.write(0xFF); // Set all PORTA pins to OUTPUT
Wire.endTransmission(); // Ends I2C communication with MCP23017(U4) IC
Wire.beginTransmission(0x21); // Starts communication with MCP23017(U4) IC
Wire.write(0x01); // Set MCP23017 memory pointer to IODIRB address
Wire.write(0xFF); // Set all PORTB pins to OUTPUT
Wire.endTransmission(); // Ends I2C communication with MCP23017(U4) IC
DigitalIO(0);
}
void clearlcdnow(){
// lcd.setCursor ( 0, 0 );
// lcd.print(" ");
lcd.setCursor ( 0, 1 );
lcd.print(" ");
lcd.setCursor ( 0, 2 );
lcd.print(" ");
lcd.setCursor ( 0, 3 );
lcd.print(" ");
}
void sendKeyboardEvents(byte a, byte ax,byte b,byte bx){
String response="";
if((bx&1)==1){
if((b&1)==1){
response=response+"\"key1\":\"down\",";
} else {
response=response+"\"key1\":\"up\",";
}
}
if((bx&2)==2){
if((b&2)==2){
response=response+"\"key2\":\"down\",";
} else {
response=response+"\"key2\":\"up\",";
}
}
if((bx&4)==4){
if((b&4)==4){
response=response+"\"key3\":\"down\",";
} else {
response=response+"\"key3\":\"up\",";
}
}
if((bx&8)==8){
if((b&8)==8){
response=response+"\"key4\":\"down\",";
} else {
response=response+"\"key4\":\"up\",";
}
}
if((bx&16)==16){
if((b&&16)==16){
response=response+"\"key5\":\"down\",";
} else {
response=response+"\"key5\":\"up\",";
}
}
if((bx&32)==32){
if((b&32)==32){
response=response+"\"key6\":\"down\",";
} else {
response=response+"\"key6\":\"up\",";
}
}
if((bx&64)==64){
if((b&64)==64){
response=response+"\"key7\":\"down\",";
} else {
response=response+"\"key7\":\"up\",";
}
}
if((bx&128)==128){
if((b&128)==128){
response=response+"\"key8\":\"down\",";
} else {
response=response+"\"key8\":\"up\",";
}
}
if((ax&16)==16){
if((a&16)==16){
response=response+"\"key9\":\"down\",";
} else {
response=response+"\"key9\":\"up\",";
}
}
if((ax&32)==32){
if((a&32)==32){
response=response+"\"key10\":\"down\",";
} else {
response=response+"\"key10\":\"up\",";
}
}
if((ax&64)==64){
if((a&64)==64){
response=response+"\"key11\":\"down\",";
} else {
response=response+"\"key11\":\"up\",";
}
}
if((ax&128)==128){
if((a&128)==128){
response=response+"\"key12\":\"down\",";
} else {
response=response+"\"key12\":\"up\",";
}
}
response="{"+response.substring(0, response.length() - 1)+"}";
Serial.println(response);
}
void readKeyboard(){
Wire.beginTransmission(0x21); // Start I2C communication with MCP23017(U4)
Wire.write(0x12); // Set MCP23017 memory pointer to PORTA address
Wire.endTransmission(); // Ends I2C communication with U4
Wire.requestFrom(0x21, 1); // Request one byte of data from MCP20317(U4)
U4PortA=Wire.read(); // Store the incoming byte of PORTA into "U4PortA"
Wire.beginTransmission(0x21); // Start I2C communication with U4
Wire.write(0x13); // Set MCP23017 memory pointer to PORTB address
Wire.endTransmission(); // Ends I2C communication with U4
Wire.requestFrom(0x21, 1); // Request one byte of data from MCP20317
U4PortB=Wire.read(); // Store the incoming byte of PORTB into "U4PortB"
// if((U4PortA^U4PortAP)>0){
// Serial.print("U4 PORTA: " );
// Serial.print(U4PortAP, BIN);
// Serial.print("|");
// Serial.print(U4PortA, BIN); // Print the contents of the PORTA register in binary
// Serial.print("+");
// Serial.println(U4PortA^U4PortAP); // Print the contents of the PORTA register in binary
// }
// if((U4PortB^U4PortBP)>0){
// Serial.print("U4 PORTB: " );
// Serial.print(U4PortBP, BIN); // Print the contents of the PORTB register in binary
// Serial.print("|");
// Serial.print(U4PortB, BIN); // Print the contents of the PORTB register in binary
// Serial.print("+");
// Serial.println(U4PortBP^U4PortB); // Print the contents of the PORTB register in binary
// }
if((U4PortA^U4PortAP)>0){
sendKeyboardEvents(U4PortA,U4PortA^U4PortAP,0,0);
}
if((U4PortB^U4PortBP)>0){
sendKeyboardEvents(0,0,U4PortB,(U4PortB^U4PortBP));
}
U4PortAP=U4PortA;
U4PortBP=U4PortB;
}
void DigitalIO(int i)
{
Wire.beginTransmission(0x20);
Wire.write(0x12);
Wire.write(highByte(i)); // Set or clear PORTA pins of U2
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x13);
Wire.write(lowByte(i)); // Set or clear PORTB pins of U2
Wire.endTransmission();
}
int readline(int readch, char *buffer, int len)
{
static int pos = 0;
int rpos;
if (readch > 0) {
switch (readch) {
case '\r':
rpos = pos;
pos = 0; // Reset position index ready for next time
return rpos;
default:
if (pos < len-1) {
buffer[pos++] = readch;
buffer[pos] = 0;
}
}
}
// No end of line has been found, so return -1.
return -1;
}
void processReceivedMessages(){
static char buffer[200];
if (readline(Serial.read(), buffer, 200) > 0) {
aJsonObject *msg = aJson.parse(buffer);
processMessage(msg);
aJson.deleteItem(msg);
}
}
void loop(){
//Serial.print(".");
readKeyboard();
processReceivedMessages();
if(testmodeStatus>0){
if(testModeCounter>50){
clearlcdnow();
lcd.setCursor ( 0, 1 );
lcd.print(" KEYBOARD TEST MODE ");
int curent=pow(2,pattern);
lcd.setCursor ( 13, 2 ); // go to the third line
lcd.print(curent);
if(pattern>1)
curent=curent+1;
lcd.setCursor ( 6, 2 ); // go to the third line
lcd.print(pattern);
DigitalIO(curent);
pattern++;
if(pattern>11){
pattern=0;
testModeCounter=0;
}
} else {
testModeCounter=testModeCounter+1;
}
}
if(wag>0){
wagTailHorizontal();
}
if(wag<0){
wagTailVertical();
}
return;
}
void processMessage(aJsonObject *msg){
aJsonObject *line1 = aJson.getObjectItem(msg, "line1");
aJsonObject *line2 = aJson.getObjectItem(msg, "line2");
aJsonObject *line3 = aJson.getObjectItem(msg, "line3");
aJsonObject *line4 = aJson.getObjectItem(msg, "line4");
aJsonObject *backlighton = aJson.getObjectItem(msg, "backlighton");
aJsonObject *backlightoff = aJson.getObjectItem(msg, "backlightoff");
aJsonObject *testmodeon = aJson.getObjectItem(msg, "testmodeon");
aJsonObject *testmodeoff = aJson.getObjectItem(msg, "testmodeoff");
aJsonObject *lightcode = aJson.getObjectItem(msg, "lightcode");
aJsonObject *clearlcd = aJson.getObjectItem(msg, "clearlcd");
aJsonObject *waghorizontal = aJson.getObjectItem(msg, "waghorizontal");
aJsonObject *wagvertical = aJson.getObjectItem(msg, "wagvertical");
if (waghorizontal->type == aJson_True) {
wag=1;
Serial.println("{\"response\":\"OK\"}");
}
if (wagvertical->type == aJson_True) {
wag=-1;
Serial.println("{\"response\":\"OK\"}");
}
if (clearlcd->type == aJson_True) {
clearlcdnow();
Serial.println("{\"response\":\"OK\"}");
}
if (line1->type == aJson_String) {
lcd.setCursor (0,0);
lcd.print(String(line1->valuestring));
Serial.println("{\"response\":\"OK\"}");
}
if (line2->type == aJson_String) {
lcd.setCursor (0,1);
lcd.print(String(line2->valuestring));
Serial.println("{\"response\":\"OK\"}");
}
if (line3->type == aJson_String) {
lcd.setCursor (0,2);
lcd.print(String(line3->valuestring));
Serial.println("{\"response\":\"OK\"}");
}
if (line4->type == aJson_String) {
lcd.setCursor (0,3);
lcd.print(String(line4->valuestring));
Serial.println("{\"response\":\"OK\"}");
}
if (backlightoff->type == aJson_True) {
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
Serial.println("{\"response\":\"OK\"}");
}
if (backlighton->type == aJson_True) {
lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
Serial.println("{\"response\":\"OK\"}");
}
if (testmodeoff->type == aJson_True) {
testmodeStatus=0;
clearlcdnow();
DigitalIO(0);
Serial.println("{\"response\":\"OK\"}");
}
if (testmodeon->type == aJson_True) {
testmodeStatus=1;
Serial.println("{\"response\":\"OK\"}");
}
if (lightcode->type == aJson_Int) {
// Serial.print("lightcode=");
// Serial.print(lightcode->valueint);
DigitalIO(lightcode->valueint);
Serial.println("{\"response\":\"OK\"}");
}
}
void wagTailVertical(){
myservoVertical.attach(9);
for(pos = 95; pos < 120; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservoVertical.write(pos);
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 120; pos>=70; pos-=1) // goes from 180 degrees to 0 degrees
{
myservoVertical.write(pos); // tell servo to go to position in variable 'pos'
//myservo1.write(pos);
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 70; pos < 95; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservoVertical.write(pos);
delay(10); // waits 15ms for the servo to reach the position
}
myservoVertical.detach();
wag=0;
}
void wagTailHorizontal(){
myservoHorizontal.attach(10);
for(pos = 55; pos < 70; pos += 1)
{
myservoHorizontal.write(pos);
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 70; pos>=40; pos-=1) // goes from 180 degrees to 0 degrees
{
myservoHorizontal.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for(pos = 40; pos < 55; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservoHorizontal.write(pos);
delay(10); // waits 15ms for the servo to reach the position
}
myservoHorizontal.detach();
wag=0;
}
void setTailToNeutral(){
myservoVertical.attach(9);
myservoHorizontal.attach(10);
myservoVertical.write(55);
//myservoHorizontal.write(85);
myservoVertical.detach();
myservoHorizontal.detach();
wag=0;
}