forked from OSCSYS/brewtroller
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Com.pde
186 lines (166 loc) · 5.47 KB
/
Com.pde
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
/*
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
*/
//**********************************************************************************
//Code Shared by all Schemas
//**********************************************************************************
void comInit() {
#ifdef COM_SERIAL0
Serial.begin(SERIAL0_BAUDRATE);
//Always identify
if (logData)
logASCIIVersion();
#endif
#ifdef RGBIO8_ENABLE
RGBIO8_Init();
#endif
}
void logASCIIVersion() {
printFieldUL(millis()); // timestamp
printFieldPS(LOGSYS); // keyword "SYS"
Serial.print("VER\t"); // Version record
printFieldPS(BTVER); // BT Version
printFieldUL(BUILD); // Build #
#if COM_SERIAL0 == BTNIC || (COM_SERIAL0 == ASCII && COMSCHEMA > 0)
printFieldUL(COM_SERIAL0); // Protocol Type
printFieldUL(COMSCHEMA);// Protocol Schema
#ifdef USEMETRIC // Metric or US units
Serial.print("0");
#else
Serial.print("1");
#endif
#endif
Serial.println();
}
void printFieldUL (unsigned long uLong) {
Serial.print(uLong, DEC);
Serial.print("\t");
}
void printFieldPS (const char *sText) {
while (pgm_read_byte(sText) != 0) Serial.print(pgm_read_byte(sText++));
Serial.print("\t");
}
void updateCom() {
#ifdef COM_SERIAL0
#if COM_SERIAL0 == ASCII
updateS0ASCII(); /* Log_ASCII.pde */
#elif COM_SERIAL0 == BTNIC
updateS0BTnic();
#endif
#endif
#ifdef BTNIC_EMBEDDED
updateI2CBTnic();
#endif
//BTPD Support
#ifdef BTPD_SUPPORT
updateBTPD();
#endif
#ifdef RGBIO8_ENABLE
RGBIO8_Update();
#endif
}
/********************************************************************************************************************
* BTnic Instances
********************************************************************************************************************/
#ifdef BTNIC_PROTOCOL
#include "Com_BTnic.h"
#ifdef BTNIC_EMBEDDED
BTnic btnicI2C;
void updateI2CBTnic() {
switch (btnicI2C.getState()) {
case BTNIC_STATE_IDLE:
case BTNIC_STATE_RX:
while (1) {
Wire.requestFrom(BTNIC_I2C_ADDR, 1);
if (! Wire.available())
break; //No data
byte data = Wire.receive();
if (!data)
break; //Null return: No data
#ifdef DEBUG_BTNIC
Serial.print("btnicEmb RX: ");
Serial.print(data);
Serial.print("(0x");
Serial.print(data, HEX);
Serial.print(", ");
Serial.print(data, DEC);
Serial.println(")");
#endif
btnicI2C.rx(data);
if(btnicI2C.getState() != BTNIC_STATE_RX) break;
}
case BTNIC_STATE_TX:
//TX Ready
while(btnicI2C.getState() == BTNIC_STATE_TX) {
byte maxLength = 32;
Wire.beginTransmission(BTNIC_I2C_ADDR);
while(maxLength-- && btnicI2C.getState() == BTNIC_STATE_TX) {
byte data = btnicI2C.tx();
#ifdef DEBUG_BTNIC
Serial.print("btnicEmb TX: ");
Serial.print(data);
Serial.print("(0x");
Serial.print(data, HEX);
Serial.print(", ");
Serial.print(data, DEC);
Serial.println(")");
#endif
Wire.send(data);
}
Wire.endTransmission();
}
break;
}
}
#endif
#ifdef COM_SERIAL0
#if COM_SERIAL0 == BTNIC /* BTnic over Serial0 */
BTnic btnicS0;
void updateS0BTnic() {
switch (btnicS0.getState()) {
case BTNIC_STATE_IDLE:
case BTNIC_STATE_RX:
while (Serial.available()) {
btnicS0.rx(Serial.read());
if(btnicS0.getState() != BTNIC_STATE_RX) break;
}
if(btnicS0.getState() != BTNIC_STATE_TX) break;
case BTNIC_STATE_TX:
//TX Ready
Serial.print(millis(),DEC);
Serial.write(0x09);
while(btnicS0.getState() == BTNIC_STATE_TX) Serial.write(btnicS0.tx());
//Serial.write(0x0D); //Carriage Return
//Serial.write(0x0A); //New Line
}
}
#endif
#endif
#endif
void comEvent(byte eventID, int eventParam) {
#ifdef BTNIC_PROTOCOL
#ifdef BTNIC_EMBEDDED
btnicI2C.eventHandler(eventID, eventParam);
#endif
#ifdef COM_SERIAL0
#if COM_SERIAL0 == BTNIC /* BTnic over Serial0 */
btnicS0.eventHandler(eventID, eventParam);
#endif
#endif
#endif
}