-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDISerial.cpp
93 lines (71 loc) · 1.84 KB
/
SDISerial.cpp
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
#ifndef __SDI_SERIAL_CPP
#define __SDI_SERIAL_CPP
#include "SDISerial.hpp"
#include "SDIBusController.hpp"
#include <string.h>
SDISerial::SDISerial(HardwareSerial &serial, int serialOutPin, int flowControlPin):
mSerial(serial){
// set the pin mode of the serial output
pinMode(flowControlPin, OUTPUT);
mSerialOutPin = serialOutPin;
mFlowControlPin = flowControlPin;
isHardwareSerial = false;
}
//
// SDISerial Implementation
//
void SDISerial::begin() {
mSerial.begin(1200, SERIAL_7E1);
}
void SDISerial::end() {
mSerial.end();
}
void SDISerial::sendPreamble() {
/*
Sends the required preamble according to the SDI-12 specification
*/
// TODO(colin): figure out to start and stop interface
this->end();
pinMode(mSerialOutPin, OUTPUT);
digitalWrite(mSerialOutPin, 0);
delay(SDI_BREAK_TIME_MS);
digitalWrite(mSerialOutPin, 1);
delay(SDI_MARKING_TIME_MS);
this->begin();
}
void SDISerial::setBufferWrite(){
digitalWrite(mFlowControlPin, 1);
}
void SDISerial::setBufferRead(){
// LOW is READ ******
digitalWrite(mFlowControlPin, 0);
}
int SDISerial::available() {
return mSerial.available();
}
int SDISerial::read() {
return mSerial.read();
}
int SDISerial::peek() {
return mSerial.peek();
}
void SDISerial::flush() {
mSerial.flush();
}
//
// Print implementation
//
/* This sets the buffer to write, sends the preamble, and writes to the serial
port. It is the caller's responsibility to set the buffer back to read */
int SDISerial::write(char chr) {
// TODO(colin): allow for hardware serial
// int out = mSerial.write((const uint8_t*)&chr, sizeof(chr));
int out = mSerial.write(chr);
return out;
}
int SDISerial::write(char* str) {
// TODO(colin): allow for enabling and disabling hardware serial
int out = mSerial.write((const uint8_t*)str, strlen(str));
return out;
}
#endif