forked from brewtrollergit/brewtroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Com_BTnic.h
923 lines (815 loc) · 27.2 KB
/
Com_BTnic.h
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*
Copyright (C) 2009, 2010 Matt Reba, Jeremiah Dillingham
This file is part of BrewTroller.
BrewTroller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BrewTroller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BrewTroller. If not, see <http://www.gnu.org/licenses/>.
BrewTroller - Open Source Brewing Computer
Software Lead: Matt Reba (matt_AT_brewtroller_DOT_com)
Hardware Lead: Jeremiah Dillingham (jeremiah_AT_brewtroller_DOT_com)
Documentation, Forums and more information available at http://www.brewtroller.com
Update 9/22/2010 to support enhanced functions and mutiple schemas.
*/
#ifdef BTNIC_PROTOCOL
/********************************************************************************************************************
* BTnic Class
********************************************************************************************************************/
// Using Values 65-90 & 97-122 for command codes to make terminal input easier
// but any value between 0-255 can be used with the following exceptions
// Command Field Char 9 (Tab)
// Command Term Char 10 (Line Feed)
// Command Term Char 13 (Carriage Return)
//Command codes for special responses
#define CMD_REJECT 33 //!
#define CMD_REJECT_PARAM 35 //#
#define CMD_REJECT_INDEX 36 //$
#define CMD_REJECT_CRC 42 //*
#define CMD_GET_PROGNAMES 62 //%3E
#define CMD_SET_PROGRAM 63 //%3F
#define CMD_GET_PROGRAM 64 //%40
#define CMD_GET_BOIL 65 //A
#define CMD_GET_CAL 66 //B
#define CMD_GET_EVAP 67 //C
#define CMD_GET_OSET 68 //D
#define CMD_GET_PROG 69 //E
#define CMD_GET_TS 70 //F
#define CMD_GET_VER 71 //G
#define CMD_GET_VSET 72 //H
#define CMD_INIT_EEPROM 73 //I
#define CMD_SCAN_TS 74 //J
#define CMD_SET_BOIL 75 //K
#define CMD_SET_CAL 76 //L
#define CMD_SET_EVAP 77 //M
#define CMD_SET_OSET 78 //N
#define CMD_SET_PROG 79 //O
#define CMD_SET_TS 80 //P
#define CMD_SET_VLVCFG 81 //Q
#define CMD_SET_VSET 82 //R
#define CMD_ADV_STEP 83 //S
#define CMD_EXIT_STEP 84 //T
#define CMD_INIT_STEP 85 //U
#define CMD_SET_ALARM 86 //V
#define CMD_SET_AUTOVLV 87 //W
#define CMD_SET_SETPOINT 88 //X
#define CMD_SET_TIMERSTATUS 89 //Y
#define CMD_SET_TIMERVALUE 90 //Z
#define CMD_GET_PROGNAME 91 //'['
#define CMD_SET_PROGNAME 92 //'\'
#define CMD_GET_PROGTEMPS 93 //']'
#define CMD_SET_PROGTEMPS 94 //'^'
#define CMD_GET_PROGMINS 95 //'_'
#define CMD_SET_PROGMINS 96 //'`'
#define CMD_GET_STATUS 97 //a
#define CMD_SET_VLVPRF 98 //b
#define CMD_RESET 99 //c
#define CMD_GET_VLVCFG 100 //d
#define CMD_GET_ALARM 101 //e
#define CMD_GET_BOILPWR 102 //f
#define CMD_GET_DELAYTIME 103 //g
#define CMD_GET_GRAINTEMP 104 //h
#define CMD_SET_BOILPWR 105 //i
#define CMD_SET_DELAYTIME 106 //j
#define CMD_SET_GRAINTEMP 107 //k
#define CMD_STEPPRG 110 //n
#define CMD_TIMER 111 //o
#define CMD_VOL 112 //p
#define CMD_TEMP 113 //q
#define CMD_STEAM 114 //r
#define CMD_HEATPWR 115 //s
#define CMD_SETPOINT 116 //t
#define CMD_AUTOVLV 117 //u
#define CMD_VLVBITS 118 //v
#define CMD_VLVPRF 119 //w
#define CMD_GET_PROGVOLS 120 //x
#define CMD_SET_PROGVOLS 121 //y
#define CMD_SET_TGTVOL 123 //{
#define CMD_GET_TGTVOL 124 //|
#define CMD_SET_BOILCTL 125 //}
#define CMD_GET_BOILCTL 126 //~
typedef enum {
BTNIC_STATE_IDLE,
BTNIC_STATE_RX,
BTNIC_STATE_EXE,
BTNIC_STATE_TX,
} BTNICState;
#define BTNIC_BUF_LEN 1024
#define CMDCODE_MIN 62
#define CMDCODE_MAX 126
#define NO_CMDINDEX -1
static byte CMD_PARAM_COUNTS[] PROGMEM =
{
0, //CMD_GET_PROGNAMES (All)
22, //CMD_SET_PROGRAM (Full)
0, //CMD_GET_PROGRAM (Full)
0, //CMD_GET_BOIL
0, //CMD_GET_CAL
0, //CMD_GET_EVAP
0, //CMD_GET_OSET
0, //CMD_GET_PROG
0, //CMD_GET_TS
0, //CMD_GET_VER
0, //CMD_GET_VSET
1, //CMD_INIT_EEPROM
0, //CMD_SCAN_TS
1, //CMD_SET_BOIL
2, //CMD_SET_CAL
1, //CMD_SET_EVAP
8, //CMD_SET_OSET
6, //CMD_SET_PROG
8, //CMD_SET_TS
1, //CMD_SET_VLVCFG
2, //CMD_SET_VSET
0, //CMD_ADV_STEP
0, //CMD_EXIT_STEP
1, //CMD_INIT_STEP
1, //CMD_SET_ALARM
1, //CMD_SET_AUTOVLV
1, //CMD_SET_SETPOINT
1, //CMD_SET_TIMERSTATUS
1, //CMD_SET_TIMERVALUE
0, //CMD_GET_PROGNAME
1, //CMD_SET_PROGNAME
0, //CMD_GET_PROGMASHTEMPS
6, //CMD_SET_PROGMASHTEMPS
0, //CMD_GET_PROGMASHMINS
6, //CMD_SET_PROGMASHMINS
0, //CMD_GET_STATUS
2, //CMD_SET_VLVPRF
0, //CMD_RESET
0, //CMD_GET_VLVCFG
0, //CMD_GET_ALARM
0, //CMD_GET_BOILPWR
0, //CMD_GET_DELAYTIME
0, //CMD_GET_GRAINTEMP
1, //CMD_SET_BOILPWR
1, //CMD_SET_DELAYTIME
1, //CMD_SET_GRAINTEMP
0, //Unused
0, //Unused
0, //CMD_STEPPRG
0, //CMD_TIMER
0, //CMD_VOL
0, //CMD_TEMP
0, //CMD_STEAM
0, //CMD_HEATPWR
0, //CMD_SETPOINT
0, //CMD_AUTOVLV
0, //CMD_VLVBITS
0, //CMD_VLVPRF
0, //CMD_GET_PROGVOLS
3, //CMD_SET_PROGVOLS
0, //Unused
1, //CMD_SET_TGTVOL
0, //CMD_GET_TGTVOL
2, //CMD_SET_BOILCNT
0 //CMD_GET_BOILCNT
};
static byte CMD_INDEX_MAXVALUE[] PROGMEM =
{
0, //CMD_GET_PROGNAMES (All)
NUM_PROGRAMS - 1, //CMD_SET_PROGRAM (Full)
NUM_PROGRAMS - 1, //CMD_GET_PROGRAM (Full)
0, //CMD_GET_BOIL
29, //CMD_GET_CAL (0-9 HLT, 10-19 Mash, 20-29 Kettle)
0, //CMD_GET_EVAP
VS_STEAM, //CMD_GET_OSET
NUM_PROGRAMS - 1, //CMD_GET_PROG
NUM_TS - 1, //CMD_GET_TS
0, //CMD_GET_VER
VS_KETTLE, //CMD_GET_VSET
0, //CMD_INIT_EEPROM
0, //CMD_SCAN_TS
0, //CMD_SET_BOIL
29, //CMD_SET_CAL (0-9 HLT, 10-19 Mash, 20-29 Kettle)
0, //CMD_SET_EVAP
VS_STEAM, //CMD_SET_OSET
NUM_PROGRAMS - 1, //CMD_SET_PROG
NUM_TS - 1, //CMD_SET_TS
NUM_VLVCFGS - 1, //CMD_SET_VLVCFG
VS_KETTLE, //CMD_SET_VSET
NUM_BREW_STEPS - 1, //CMD_ADV_STEP
NUM_BREW_STEPS - 1, //CMD_EXIT_STEP
NUM_BREW_STEPS - 1, //CMD_INIT_STEP
0, //CMD_SET_ALARM
0, //CMD_SET_AUTOVLV
VS_STEAM, //CMD_SET_SETPOINT
TIMER_BOIL, //CMD_SET_TIMERSTATUS
TIMER_BOIL, //CMD_SET_TIMERVALUE
NUM_PROGRAMS - 1, //CMD_GET_PROGNAME
NUM_PROGRAMS - 1, //CMD_SET_PROGNAME
NUM_PROGRAMS - 1, //CMD_GET_PROGTEMPS
NUM_PROGRAMS - 1, //CMD_SET_PROGTEMPS
NUM_PROGRAMS - 1, //CMD_GET_PROGMINS
NUM_PROGRAMS - 1, //CMD_SET_PROGMINS
0, //CMD_GET_STATUS
0, //CMD_SET_VLVPRF
1, //CMD_RESET
NUM_VLVCFGS - 1, //CMD_GET_VLVCFG
0, //CMD_GET_ALARM
0, //CMD_GET_BOILPWR
0, //CMD_GET_DELAYTIME
0, //CMD_GET_GRAINTEMP
0, //CMD_SET_BOILPWR
0, //CMD_SET_DELAYTIME
0, //CMD_SET_GRAINTEMP
0, //Unused
0, //Unused
0, //CMD_STEPPRG
TIMER_BOIL, //CMD_TIMER
VS_KETTLE, //CMD_VOL
VS_KETTLE, //CMD_TEMP
0, //CMD_STEAM
VS_STEAM, //CMD_HEATPWR
VS_STEAM, //CMD_SETPOINT
0, //CMD_AUTOVLV
0, //CMD_VLVBITS
0, //CMD_VLVPRF
NUM_PROGRAMS - 1, //CMD_GET_PROGVOLS
NUM_PROGRAMS - 1, //CMD_SET_PROGVOLS
0, //Unused
VS_KETTLE, //CMD_SET_TGTVOL
VS_KETTLE, //CMD_GET_TGTVOL
0, //CMD_SET_BOILCTL
0 //CMD_GET_BOILCTL
};
class BTnic
{
public:
BTnic(void); /* Constructor */
void rx(byte); /* Receive a byte into buffer and returns true of tx ready*/
byte tx(void); /* Return a byte from buffer or '/0' if end of buffer */
void reset(void); /* Sets buf len to 0 */
BTNICState getState(void); /* Return current state RX / EXE / TX */
void eventHandler(byte, int);
private:
void setState(BTNICState);
void execCmd(void);
void rejectCmd(byte);
void chkBuf(void);
void logFieldCmd(byte, int);
void logField(char*);
void logField_P(const char*);
void logFieldI(unsigned long);
void logEnd(void);
void logStepPrg(byte zone, byte startStep, byte endStep);
int getCmdIndex(void);
byte getCmdParamCount(void);
char* getCmdParam(byte, char*, byte);
unsigned long getCmdParamNum(byte);
BTNICState _state; /* Current state: IDLE/RX/EXE/TX */
unsigned int _bufLen; /* Length of data in buffer */
byte _bufData[BTNIC_BUF_LEN]; /* Buffer */
unsigned int _bufCur; /* Cursor position in buffer for tx */
};
BTnic::BTnic(void) {
_bufLen = _bufCur = 0;
_state = BTNIC_STATE_IDLE;
}
void BTnic::rx(byte byteIn) {
if (_state == BTNIC_STATE_IDLE)
setState(BTNIC_STATE_RX);
if (byteIn == 0x01)
_bufLen = _bufCur = 0;
else if (byteIn == 0x0D) execCmd();
else {
_bufData[_bufLen++] = byteIn;
if (_bufLen == BTNIC_BUF_LEN) execCmd();
}
}
byte BTnic::tx(void) {
if (_bufCur < _bufLen) return _bufData[_bufCur++];
else return '\0';
}
void BTnic::reset(void) {
_bufLen = _bufCur = 0;
setState(BTNIC_STATE_IDLE);
}
BTNICState BTnic::getState(void) {
if (_state == BTNIC_STATE_TX && _bufCur == _bufLen) reset();
return _state;
}
void BTnic::eventHandler(byte eventID, int eventParam) {
//Not Implemented
}
void BTnic::setState(BTNICState state) {
#ifdef DEBUG_BTNIC
Serial.print("BTNIC State Change: ");
Serial.print(_state, DEC);
Serial.print(" -> ");
Serial.println(state, DEC);
#endif
_state = state;
}
//Check and process command. Return error code (0 if OK)
void BTnic::execCmd(void) {
setState(BTNIC_STATE_EXE);
// log ASCII version "GET_VER"
if (strcasecmp(getCmdParam(0, buf, 20), "GET_VER") == 0) {
logASCIIVersion();
return reset();
}
if(!_bufLen || _bufData[0] < CMDCODE_MIN || _bufData[0] > CMDCODE_MAX) return rejectCmd(CMD_REJECT);
if(getCmdParamCount() != pgm_read_byte(CMD_PARAM_COUNTS + _bufData[0] - CMDCODE_MIN)) return rejectCmd(CMD_REJECT_PARAM);
int cmdIndex = getCmdIndex();
if(cmdIndex == NO_CMDINDEX) return rejectCmd(CMD_REJECT_INDEX);
switch (_bufData[0]) {
case CMD_GET_STATUS: //a
logFieldCmd(CMD_GET_STATUS, NO_CMDINDEX);
logFieldI(alarmStatus);
logFieldI(autoValveBitmask());
logFieldI(actProfiles);
logFieldI(computeValveBits());
for (byte vessel = VS_HLT; vessel <= VS_KETTLE; vessel++) {
logFieldI(setpoint[vessel]);
logFieldI(temp[vessel]);
logFieldI(getHeatPower(vessel));
logFieldI(tgtVol[vessel]);
logFieldI(volAvg[vessel]);
#ifdef FLOWRATE_CALCS
logFieldI(flowRate[vessel]);
#else
logFieldI(0);
#endif
}
for (byte timer = TIMER_MASH; timer <= TIMER_BOIL; timer++) {
logFieldI(timerValue[timer]);
logFieldI(timerStatus[timer]);
}
logFieldI(boilControlState);
logStepPrg(ZONE_MASH, STEP_FILL, STEP_SPARGE);
logStepPrg(ZONE_BOIL, STEP_BOIL, STEP_CHILL);
break;
case CMD_SET_BOIL: //K
setBoilTemp(getCmdParamNum(1));
case CMD_GET_BOIL: //A
logFieldCmd(CMD_GET_BOIL, NO_CMDINDEX);
logFieldI(getBoilTemp());
break;
case CMD_SET_CAL: //L
{
byte vessel = cmdIndex / 10;
byte calIndex = cmdIndex - vessel * 10;
setVolCalib(vessel, calIndex, getCmdParamNum(2), getCmdParamNum(1));
}
case CMD_GET_CAL: //B
{
logFieldCmd(CMD_GET_CAL, cmdIndex);
byte vessel = cmdIndex / 10;
byte calIndex = cmdIndex - vessel * 10;
logFieldI(calibVols[vessel][calIndex]);
logFieldI(calibVals[vessel][calIndex]);
}
break;
case CMD_SET_EVAP: //M
setEvapRate(min(getCmdParamNum(1), 100));
case CMD_GET_EVAP: //C
logFieldCmd(CMD_GET_EVAP, NO_CMDINDEX);
logFieldI(getEvapRate());
break;
case CMD_SET_OSET: //N
setPIDEnabled(cmdIndex, getCmdParamNum(1));
setPIDCycle(cmdIndex, getCmdParamNum(2));
setPIDp(cmdIndex, getCmdParamNum(3));
setPIDi(cmdIndex, getCmdParamNum(4));
setPIDd(cmdIndex, getCmdParamNum(5));
if (cmdIndex == VS_STEAM) {
setSteamZero(getCmdParamNum(6));
setSteamTgt(getCmdParamNum(7));
setSteamPSens(getCmdParamNum(8));
}
else setHysteresis(cmdIndex, getCmdParamNum(6));
case CMD_GET_OSET: //D
logFieldCmd(CMD_GET_OSET, cmdIndex);
logFieldI(PIDEnabled[cmdIndex]);
logFieldI(PIDCycle[cmdIndex]);
logFieldI(getPIDp(cmdIndex));
logFieldI(getPIDi(cmdIndex));
logFieldI(getPIDd(cmdIndex));
if (cmdIndex == VS_STEAM) {
logFieldI(getSteamTgt());
#ifndef PID_FLOW_CONTROL
logFieldI(steamZero);
logFieldI(steamPSens);
#endif
}
else {
logFieldI(hysteresis[cmdIndex]);
logFieldI(0);
logFieldI(0);
}
break;
case CMD_SET_PROGNAME: //'['
{
char pName[20];
getCmdParam(1, pName, 19);
setProgName(cmdIndex, pName);
}
case CMD_GET_PROGNAME: //'\'
logFieldCmd(CMD_GET_PROGNAME, cmdIndex);
{
char pName[20];
getProgName(cmdIndex, pName);
logField(pName);
}
break;
case CMD_GET_PROGNAMES: //%3E (All Program Names)
logFieldCmd(CMD_GET_PROGNAMES, NO_CMDINDEX);
for (byte i = 0; i < NUM_PROGRAMS; i++) {
char pName[20];
getProgName(i, pName);
logField(pName);
}
break;
case CMD_SET_PROGTEMPS: //']'
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
setProgMashTemp(cmdIndex, i, getCmdParamNum(i + 1) * SETPOINT_DIV);
}
case CMD_GET_PROGTEMPS: //'^'
logFieldCmd(CMD_GET_PROGTEMPS, cmdIndex);
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
logFieldI(getProgMashTemp(cmdIndex, i) / SETPOINT_DIV);
}
break;
case CMD_SET_PROGMINS: //'_'
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
setProgMashMins(cmdIndex, i, getCmdParamNum(i + 1));
}
case CMD_GET_PROGMINS: //'`'
logFieldCmd(CMD_GET_PROGMINS, cmdIndex);
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
logFieldI(getProgMashMins(cmdIndex, i));
}
break;
case CMD_SET_PROGVOLS: //x
setProgBatchVol(cmdIndex, getCmdParamNum(1));
setProgGrain(cmdIndex, getCmdParamNum(2));
setProgRatio(cmdIndex, getCmdParamNum(3));
case CMD_GET_PROGVOLS: //y
logFieldCmd(CMD_GET_PROGVOLS, cmdIndex);
logFieldI(getProgBatchVol(cmdIndex));
logFieldI(getProgGrain(cmdIndex));
logFieldI(getProgRatio(cmdIndex));
break;
case CMD_SET_PROG: //O (Partial program data)
setProgSparge(cmdIndex, getCmdParamNum(1) * SETPOINT_DIV);
setProgHLT(cmdIndex, getCmdParamNum(2) * SETPOINT_DIV);
setProgBoil(cmdIndex, getCmdParamNum(3));
setProgPitch(cmdIndex, getCmdParamNum(4) * SETPOINT_DIV);
setProgAdds(cmdIndex, getCmdParamNum(5));
setProgMLHeatSrc(cmdIndex, getCmdParamNum(6));
case CMD_GET_PROG: //E (partial program data)
logFieldCmd(CMD_GET_PROG, cmdIndex);
logFieldI(getProgSparge(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgHLT(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgBoil(cmdIndex));
logFieldI(getProgPitch(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgAdds(cmdIndex));
logFieldI(getProgMLHeatSrc(cmdIndex));
break;
case CMD_SET_PROGRAM:
{
char pName[20];
getCmdParam(1, pName, 19);
setProgName(cmdIndex, pName);
}
setProgBatchVol(cmdIndex, getCmdParamNum(2));
setProgGrain(cmdIndex, getCmdParamNum(3));
setProgRatio(cmdIndex, getCmdParamNum(4));
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
setProgMashTemp(cmdIndex, i, getCmdParamNum(i * 2 + 5) * SETPOINT_DIV);
setProgMashMins(cmdIndex, i, getCmdParamNum(i * 2 + 6));
}
setProgSparge(cmdIndex, getCmdParamNum(17) * SETPOINT_DIV);
setProgHLT(cmdIndex, getCmdParamNum(18) * SETPOINT_DIV);
setProgBoil(cmdIndex, getCmdParamNum(19));
setProgPitch(cmdIndex, getCmdParamNum(20) * SETPOINT_DIV);
setProgAdds(cmdIndex, getCmdParamNum(21));
setProgMLHeatSrc(cmdIndex, getCmdParamNum(22));
case CMD_GET_PROGRAM:
logFieldCmd(CMD_GET_PROGRAM, cmdIndex);
{
char pName[20];
getProgName(cmdIndex, pName);
logField(pName);
}
logFieldI(getProgBatchVol(cmdIndex));
logFieldI(getProgGrain(cmdIndex));
logFieldI(getProgRatio(cmdIndex));
for (byte i = MASH_DOUGHIN; i <= MASH_MASHOUT; i++) {
logFieldI(getProgMashTemp(cmdIndex, i) / SETPOINT_DIV);
logFieldI(getProgMashMins(cmdIndex, i));
}
logFieldI(getProgSparge(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgHLT(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgBoil(cmdIndex));
logFieldI(getProgPitch(cmdIndex) / SETPOINT_DIV);
logFieldI(getProgAdds(cmdIndex));
logFieldI(getProgMLHeatSrc(cmdIndex));
logFieldI(calcStrikeTemp(cmdIndex) / SETPOINT_DIV);
logFieldI(getFirstStepTemp(cmdIndex) / SETPOINT_DIV);
logFieldI(calcPreboilVol(cmdIndex));
logFieldI(calcStrikeVol(cmdIndex));
logFieldI(calcSpargeVol(cmdIndex));
logFieldI(calcGrainVolume(cmdIndex));
logFieldI(calcGrainLoss(cmdIndex));
break;
case CMD_SET_TS: //P
{
byte addr[8];
for (byte i=0; i<8; i++) addr[i] = (byte)getCmdParamNum(i+1);
setTSAddr(cmdIndex, addr);
}
case CMD_GET_TS: //F
logFieldCmd(CMD_GET_TS, cmdIndex);
for (byte i=0; i<8; i++) logFieldI(tSensor[cmdIndex][i]);
break;
case CMD_GET_VER: //G
logFieldCmd(CMD_GET_VER, NO_CMDINDEX);
logField_P(BTVER);
logFieldI(BUILD);
logFieldI(BTNIC);
logFieldI(COMSCHEMA);
#ifdef USEMETRIC
logFieldI(0);
#else
logFieldI(1);
#endif
break;
case CMD_SET_VSET: //R
setCapacity(cmdIndex, getCmdParamNum(1));
setVolLoss(cmdIndex, getCmdParamNum(2));
case CMD_GET_VSET: //H
logFieldCmd(CMD_GET_VSET, cmdIndex);
logFieldI(getCapacity(cmdIndex));
logFieldI(getVolLoss(cmdIndex));
break;
case CMD_INIT_EEPROM: //I
if (getCmdParamNum(1) != 210) return rejectCmd(CMD_REJECT_PARAM);
logFieldCmd(CMD_INIT_EEPROM, NO_CMDINDEX);
initEEPROM();
break;
case CMD_SCAN_TS: //J
{
logFieldCmd(CMD_SCAN_TS, NO_CMDINDEX);
byte tsAddr[8];
getDSAddr(tsAddr);
for (byte i=0; i<8; i++) logFieldI(tsAddr[i]);
}
break;
case CMD_SET_VLVCFG: //Q
setValveCfg(cmdIndex, getCmdParamNum(1));
case CMD_GET_VLVCFG: //d
logFieldCmd(CMD_GET_VLVCFG, cmdIndex);
logFieldI(vlvConfig[cmdIndex]);
break;
case CMD_INIT_STEP: //U
case CMD_ADV_STEP: //S
case CMD_EXIT_STEP: //T
if (_bufData[0] == CMD_INIT_STEP) {
byte progNum = getCmdParamNum(1);
if (progNum >= 20) return rejectCmd(CMD_REJECT_PARAM);
stepInit(progNum, cmdIndex);
}
else if (_bufData[0] == CMD_ADV_STEP) stepAdvance(cmdIndex);
else if (_bufData[0] == CMD_EXIT_STEP) stepExit(cmdIndex);
case CMD_STEPPRG: //n
logFieldCmd(CMD_STEPPRG, NO_CMDINDEX);
logStepPrg(ZONE_MASH, STEP_FILL, STEP_SPARGE);
logStepPrg(ZONE_BOIL, STEP_BOIL, STEP_CHILL);
break;
case CMD_SET_ALARM: //V
setAlarm(getCmdParamNum(1));
case CMD_GET_ALARM: //e
logFieldCmd(CMD_GET_ALARM, NO_CMDINDEX);
logFieldI(alarmStatus);
break;
case CMD_SET_AUTOVLV: //W
{
byte actModes = getCmdParamNum(1);
for (byte i = AV_FILL; i <= AV_HLT; i++)
autoValve[i] = (actModes & (1<<i));
}
case CMD_AUTOVLV: //u
{
logFieldCmd(CMD_AUTOVLV, NO_CMDINDEX);
logFieldI(autoValveBitmask());
}
break;
case CMD_SET_SETPOINT: //X
setSetpoint(cmdIndex, getCmdParamNum(1) * SETPOINT_DIV);
case CMD_SETPOINT: //t
logFieldCmd(CMD_SETPOINT, cmdIndex);
logFieldI(setpoint[cmdIndex] / (SETPOINT_MULT * SETPOINT_DIV));
break;
case CMD_SET_TIMERSTATUS: //Y
case CMD_SET_TIMERVALUE: //Z
if (_bufData[0] == CMD_SET_TIMERSTATUS) {
if (getCmdParamNum(1) != timerStatus[cmdIndex])
pauseTimer(cmdIndex);
} else {
timerValue[cmdIndex] = getCmdParamNum(1);
lastTime[cmdIndex] = millis();
}
case CMD_TIMER: //o
logFieldCmd(CMD_TIMER, cmdIndex);
logFieldI(timerValue[cmdIndex]);
logFieldI(timerStatus[cmdIndex]);
break;
case CMD_SET_VLVPRF: //b
//Check param 2 (value) and set/unset specified active profiles
if (getCmdParamNum(2)) actProfiles |= getCmdParamNum(1);
else actProfiles &= ~getCmdParamNum(1);
case CMD_VLVPRF: //w
logFieldCmd(CMD_VLVPRF, NO_CMDINDEX);
logFieldI(actProfiles);
break;
case CMD_RESET: //c
//Reboot (1) or just Reset Outputs?
if (cmdIndex == 1) softReset();
else {
logFieldCmd(CMD_RESET, cmdIndex);
resetOutputs();
clearTimer(TIMER_MASH);
clearTimer(TIMER_BOIL);
}
break;
case CMD_SET_BOILPWR: //i
setBoilPwr(getCmdParamNum(1));
case CMD_GET_BOILPWR: //f
logFieldCmd(CMD_GET_BOILPWR, NO_CMDINDEX);
logFieldI(boilPwr);
break;
case CMD_SET_DELAYTIME: //j
setDelayMins(getCmdParamNum(1));
case CMD_GET_DELAYTIME: //g
logFieldCmd(CMD_GET_DELAYTIME, NO_CMDINDEX);
logFieldI(getDelayMins());
break;
case CMD_SET_GRAINTEMP: //k
setGrainTemp(getCmdParamNum(1));
case CMD_GET_GRAINTEMP: //h
logFieldCmd(CMD_GET_GRAINTEMP, NO_CMDINDEX);
logFieldI(getGrainTemp());
break;
case CMD_VOL: //p
logFieldCmd(CMD_VOL, cmdIndex);
logFieldI(volAvg[cmdIndex]);
#ifdef FLOWRATE_CALCS
logFieldI(flowRate[cmdIndex]);
#else
logFieldI(0);
#endif
break;
case CMD_TEMP: //q
logFieldCmd(CMD_TEMP, cmdIndex);
logFieldI(temp[cmdIndex]);
break;
case CMD_STEAM: //r
logFieldCmd(CMD_STEAM, NO_CMDINDEX);
#ifdef PID_FLOW_CONTROL
logFieldI(flowRate[VS_MASH]);
#else
logFieldI(steamPressure);
#endif
break;
case CMD_HEATPWR: //s
logFieldCmd(CMD_HEATPWR, cmdIndex);
logFieldI(getHeatPower(cmdIndex));
break;
case CMD_VLVBITS: //v
logFieldCmd(CMD_VLVBITS, NO_CMDINDEX);
logFieldI(computeValveBits());
break;
case CMD_SET_TGTVOL: //{
tgtVol[cmdIndex] = min(getCmdParamNum(1), getCapacity(cmdIndex));
case CMD_GET_TGTVOL: //|
logFieldCmd(CMD_GET_TGTVOL, cmdIndex);
logFieldI(tgtVol[cmdIndex]);
break;
case CMD_SET_BOILCTL: //}
boilControlState = (ControlState)getCmdParamNum(1);
switch (boilControlState) {
case CONTROLSTATE_OFF:
PIDOutput[VS_KETTLE] = 0;
setpoint[VS_KETTLE] = 0;
break;
case CONTROLSTATE_AUTO:
setpoint[VS_KETTLE] = getBoilTemp();
break;
case CONTROLSTATE_ON:
setpoint[VS_KETTLE] = 1;
PIDOutput[VS_KETTLE] = PIDCycle[VS_KETTLE] * getCmdParamNum(2);
break;
}
case CMD_GET_BOILCTL: //~
logFieldCmd(CMD_GET_BOILCTL, NO_CMDINDEX);
logFieldI(boilControlState);
logFieldI(PIDOutput[VS_KETTLE] / PIDCycle[VS_KETTLE]);
break;
default:
return rejectCmd(CMD_REJECT); //Reject Command Code (CMD_REJECT);
}
logEnd();
}
void BTnic::logStepPrg(byte zone, byte startStep, byte endStep) {
if (zoneIsActive(zone)) {
for (byte i = startStep; i <= endStep; i++) {
if (stepProgram[i] != PROGRAM_IDLE){
logFieldI(i);
logFieldI(stepProgram[i]);
}
}
} else {
logFieldI(PROGRAM_IDLE);
logFieldI(PROGRAM_IDLE);
}
}
void BTnic::rejectCmd(byte rejectCode) {
//Unknown Command: Shift buffer two chars and insert rejectCode and field delimeter
if (_bufLen) {
_bufLen = min(_bufLen + 2, BTNIC_BUF_LEN);
_bufCur = _bufLen - 1;
while (_bufCur > 1) _bufData[_bufCur] = _bufData[(_bufCur--) - 2];
_bufData[1] = 0x09;
} else _bufLen = 1;
_bufData[0] = rejectCode;
logEnd();
}
void BTnic::logFieldCmd(byte cmdCode, int cmdIndex) {
char tmpbuf[8];
tmpbuf[0] = cmdCode;
tmpbuf[1] = '\0';
if (cmdIndex != -1) {
char indexBuf[7];
itoa(cmdIndex, indexBuf, 10);
strcat(tmpbuf, indexBuf);
}
_bufLen = 0;
logField(tmpbuf);
}
void BTnic::logField(char *string) {
if (_bufLen) _bufData[_bufLen++] = 0x09; //Tab Char if not being used for cmd field
while (*string != '\0') _bufData[_bufLen++] = *(string++);
}
void BTnic::logField_P(const char *string) {
_bufData[_bufLen++] = 0x09; //Tab Char
while (pgm_read_byte(string) != 0) _bufData[_bufLen++] = pgm_read_byte(string++);
}
void BTnic::logFieldI(unsigned long param) {
char tmpbuf[11];
ultoa(param, tmpbuf, 10);
logField(tmpbuf);
}
void BTnic::logEnd(void) {
_bufLen = min(_bufLen, BTNIC_BUF_LEN - 2);
_bufData[_bufLen++] = 0x0D; //Carriage Return
_bufData[_bufLen++] = 0x0A; //New Line
_bufCur = 0;
setState(BTNIC_STATE_TX);
}
int BTnic::getCmdIndex() {
byte maxValue = pgm_read_byte(CMD_INDEX_MAXVALUE +(_bufData[0] - CMDCODE_MIN));
if (!maxValue) return 0;
_bufCur = 1;
char tmpbuf[11];
while (_bufCur < _bufLen && _bufCur < 11 && _bufData[_bufCur] != 0x09) {
if(!isdigit(_bufData[_bufCur]))
return NO_CMDINDEX;
tmpbuf[_bufCur - 1] = _bufData[_bufCur++];
}
if (_bufCur == 1) return NO_CMDINDEX; //Missing Index
tmpbuf[_bufCur - 1] = '\0';
int cmdIndex = atoi(tmpbuf);
if (cmdIndex < 0 || cmdIndex > maxValue) return NO_CMDINDEX;
else return cmdIndex;
}
byte BTnic::getCmdParamCount() {
byte paramCount = 0;
for (byte pos = 0; pos < _bufLen; pos++) if (_bufData[pos] == 0x09) paramCount++;
return paramCount;
}
char* BTnic::getCmdParam(byte paramNum, char *retStr, byte limit) {
byte pos = 0;
byte param = 0;
while (pos < _bufLen && param < paramNum) { if (_bufData[pos++] == 0x09) param++; }
byte retPos = 0;
while (pos < _bufLen && _bufData[pos] != 0x09 && retPos < limit) { retStr[retPos++] = _bufData[pos++]; }
retStr[retPos] = '\0';
return retStr;
}
unsigned long BTnic::getCmdParamNum(byte paramNum) {
char tmpbuf[20];
getCmdParam(paramNum, tmpbuf, 10);
return strtoul(tmpbuf, NULL, 10);
}
/********************************************************************************************************************
* End of BTnic Class
********************************************************************************************************************/
#endif //BTNIC_PROTOCOL