-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCom.pde
More file actions
157 lines (136 loc) · 4.47 KB
/
Com.pde
File metadata and controls
157 lines (136 loc) · 4.47 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
/*
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
//**********************************************************************************
#include "Config.h"
#include "Enum.h"
void comInit() {
#ifdef COM_SERIAL0
Serial.begin(SERIAL0_BAUDRATE);
//Always identify
if (logData)
logASCIIVersion();
#endif
#ifdef BTNIC_EMBEDDED
Wire.onReceive(btnicRX);
#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
}
/********************************************************************************************************************
* BTnic Instances
********************************************************************************************************************/
#ifdef BTNIC_PROTOCOL
#include "Com_BTnic.h"
#ifdef BTNIC_EMBEDDED
BTnic btnicI2C;
void updateI2CBTnic() {
if(btnicI2C.getState() == BTNIC_STATE_TX) {
//TX Ready
Wire.beginTransmission(BTNIC_I2C_ADDR);
char timestamp[11];
Wire.send(ultoa(millis(), timestamp, 10));
Wire.send(0x09);
while(btnicI2C.getState() == BTNIC_STATE_TX) Wire.send(btnicI2C.tx());
Wire.endTransmission();
}
}
void btnicRX(int numBytes) {
if(btnicI2C.getState() == BTNIC_STATE_RX) {
for (byte i = 0; i < numBytes; i++) {
btnicI2C.rx(Wire.receive());
if(btnicI2C.getState() != BTNIC_STATE_RX) break;
}
}
}
#endif
#ifdef COM_SERIAL0
#if COM_SERIAL0 == BTNIC /* BTnic over Serial0 */
BTnic btnicS0;
void updateS0BTnic() {
if(btnicS0.getState() == BTNIC_STATE_RX) {
while (Serial.available()) {
btnicS0.rx(Serial.read());
if(btnicS0.getState() != BTNIC_STATE_RX) break;
}
}
if(btnicS0.getState() == 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
}