-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDIRemoteSensor.cpp
156 lines (135 loc) · 4.23 KB
/
SDIRemoteSensor.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
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
#ifndef __SDI_REMOTE_SENSOR_CPP
#define __SDI_REMOTE_SENSOR_CPP
#include <stdlib.h>
#include "SDIStream.hpp"
#include "SDIRemoteSensor.hpp"
SDIRemoteSensor::SDIRemoteSensor(SDIStream &bus, char addr):
mySDIStream(bus) {
this->address = addr;
}
bool SDIRemoteSensor::isMyAddress(char otherAddress){
return (this->address == otherAddress) || ('?' == otherAddress);
}
//TODO implement this
int SDIRemoteSensor::listen(){
while(0 == mySDIStream.available()){
delay(1);
}
delay(10);
int numDelays = 0;
// Now that we've received at least 1 character, wait until received "\r\n"
while(mySDIStream.peek() != '!'){
delay(10);
numDelays++;
if(numDelays > 10){ // timeout
return -1;
}
}
// Now we have the command. Read it into a buffer, then act on the command.
int bufSize = mySDIStream.available()+1;
char *cmd = new char[mySDIStream.available()+1];
for(int i=0; i<bufSize-1; i++){
cmd[i] = (char) mySDIStream.read();
}
cmd[bufSize] = '\0'; // null terminator
printf("Received command: %s\n", cmd);
if(isMyAddress(cmd[0])){
printf("Addresses match\n");
// The command was addressed to us.
if(0 == strcmp(&cmd[1], "!")){
// Acknowledge Active && Address Query
//TODO write response
} else if(0 == strcmp(&cmd[1], "I!")){
// Send Identification
// TODO write response
} else if(0 == strcmp(&cmd[1], "C!")){
// Start Measurement
printf("Start Measurement Command Received\n");
SDIResponse res = this->startMeasurementHandler();
printf("From the handler, received: %s\n", res);
// delete[] theResponse; // I don't know why this is an invalid pointer?
// TODO: investigate numbers following C
} else if(0 == strcmp(&cmd[1], "D0!")){
// Send Data
printf("Get Data Command Received\n");
SDIResponse res = this->getDataHandler();
printf("From the handler, received: %s\n", res);
// TODO Investigate this (number following M could be 1-9)
} else if(0 == strcmp(&cmd[1], "Ab!")){
// Change Address
// TODO Investigate how to extend past just changing address to 'b'.
}
} else{
printf("Wrong Address.\n");
return -1;
}
return 0; // testing
}
int SDIRemoteSensor::setIdentification(SDIDeviceIdentification &id){
memcpy(&(this->mySDIDeviceIdentification), &id, sizeof(struct SDIDeviceIdentification));
return 0;
}
// TODO figure this out
int SDIRemoteSensor::registerStartMeasurementHandler(SDIResponse (*handler)(void)){
// Bind the startMeasurementHandler to the given function.
this->startMeasurementHandler = handler;
return 0;
}
int SDIRemoteSensor::registerStartAltMeasurementHandler(SDIResponse (*handler)(int altno)){
this->startAltMeasurementHandler = handler;
return 0;
}
int SDIRemoteSensor::registerGetDataHandler(SDIResponse (*handler)(void)){
handler();
this->getDataHandler = handler;
return 0;
}
/*
// status functions
int SDIRemoteSensor::busy() {
if (!mBusy) {
return 0;
}
// check to see if we are ready to get the data
if (millis() > mTimeReady) {
mBusy = false;
mBus.getData(mAddress, mData, mNumExpected)
return 0;
} else {
return 1;
}
}
// modification functions
int SDIRemoteSensor::changeAddress(char newAddress) {
return mBus.changeAddress(mAddress, newAddress);
}
// measurement triggers
unsigned long SDIRemoteSensor::startMeasurement() {
if (this->busy()) {
return -1;
}
mBus.refresh(mAddress, 0, &waitTime, &mNumExpected);
mTimeReady = millis() + ((unsigned long)waitTime)*1000L;
return mTimeReady;
}
unsigned long SDIRemoteSensor::startAltMeasurement(int altNo) {
if (this->busy()) {
return -1;
}
mBus.refresh(mAddress, altNo, &waitTime, &mNumExpected);
mTimeReady = millis() + ((unsigned long)waitTime)*1000L;
return mTimeReady;
}
// getters
float* SDIRemoteSensor::getData() {
if (this->busy()) {
return NULL;
} else {
return mData;
}
}
*/
struct SDIDeviceIdentification SDIRemoteSensor::getMySDIDeviceIdentification(){
return this->mySDIDeviceIdentification;
}
#endif