/
bSimulateScope.h
241 lines (215 loc) · 6.34 KB
/
bSimulateScope.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
//20160528
//Robert H Cudmore
//
// A class to simulate a trigger and frame pins of a microscope
//
// Two version
// - scanimage, one pin that goes high on trigger and then pulses low for frame
// - two pins, two different pins, one for trigger and another for frame
//
// When this code is running on an Arduino, connect triggerPin and framePin to devices to be triggered
// The beauty is these ppins can be looped back to the same Arduino as inputs
//
// Usage:
//
// int triggerPin = 2;
// int framePin = 3;
// int ledPin = 13;
// bSimulateScope myScope(triggerPin, framePin, ledPin);
//
// myScope.set("frameInterval", 30);
// myScope.set("numFrames", 150);
//
// void loop()
// {
// myScope.Update();
// }
#include "Arduino.h"
class bSimulateScope
{
// Class Member Variables
int triggerPin;
int framePin;
int ledPin; // the number of the LED pin
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
//copied from v2.cpp
String inString; //to receive serial input
boolean useTwoTriggerPins;
boolean isRunning;
int currentFrame;
unsigned long trialStartMillis;
unsigned long lastFrameMillis;
int frameInterval; //ms
int framePinDur; //ms, duration of pulse for frame pin, 0 for none
int numFrames;
int firstFrameDelay;
// Constructor - creates a bSimulateScope
// and initializes the member variables and state
public:
bSimulateScope(int theTriggerPin, int theFramePin, int theLedPin)
{
triggerPin = theTriggerPin; //goes high during scanning
framePin = theFramePin;
pinMode(triggerPin, OUTPUT);
pinMode(framePin, OUTPUT);
ledPin = theLedPin;
pinMode(ledPin, OUTPUT);
previousMillis = 0;
//Serial.begin(115200);
Serial.begin(9600);
//copied from v2.cpp
inString = "";
useTwoTriggerPins = true;
isRunning = false;
currentFrame = 0;
trialStartMillis = 0; // not sure on 0 ???
lastFrameMillis = 0;
frameInterval = 30; //ms
framePinDur = 5;
numFrames = 300;
firstFrameDelay = 100; //ms
}
/////////////////////////////////////////////////////////////////
void help() {
Serial.println("====================");
Serial.println("bSimulateScope help, serial commands are:");
Serial.println("start : start a trial");
Serial.println("help : print this help");
Serial.println("state : print state variables");
}
/////////////////////////////////////////////////////////////////
void state() {
Serial.println("====================");
Serial.println("bSimulateScope state");
Serial.println("triggerPin=" + String(triggerPin));
Serial.println("framePin=" + String(framePin));
Serial.println("ledPin=" + String(ledPin));
Serial.println("useTwoTriggerPins=" + String(useTwoTriggerPins));
Serial.println("frameInterval=" + String(frameInterval));
Serial.println("numFrames=" + String(numFrames));
Serial.println("framePinDur=" + String(framePinDur));
}
/////////////////////////////////////////////////////////////
void set(String param, int value) {
if (param == "frameInterval") {
frameInterval = value;
} else if (param == "numFrames") {
numFrames = value;
} else if (param == "framePinDur") {
framePinDur = value;
} else {
Serial.println("bSimulateScope::set() did not understand '" + param + "'");
}
}
/////////////////////////////////////////////////////////////
void serialOut(unsigned long now, String str, unsigned long val) {
Serial.println(String(now) + "," + str + "," + val);
}
/////////////////////////////////////////////////////////////////
boolean SerialIn(unsigned long now, String str) {
str.replace("\n","");
str.replace("\r","");
String delimStr = ",";
if (str.length()==0) {
return false;
}
else if (str == "help") {
help();
}
else if (str == "state") {
state();
}
else if (str == "start") {
trialStart(now);
} else if (str.indexOf("set") == 0) {
int firstComma = str.indexOf(",");
if (firstComma >= 0) {
int secondComma = str.indexOf(",", firstComma+1);
if (secondComma >= 0) {
String param = str.substring(firstComma+1,secondComma-1);
int value = str.substring(secondComma+1,str.length()-1).toInt();
set(param,value);
}
}
}
return true;
}
/////////////////////////////////////////////////////////////
void trialStart(unsigned long now) {
if (isRunning == false) {
if (useTwoTriggerPins) {
digitalWrite(triggerPin, HIGH); //
} else {
digitalWrite(framePin, HIGH); //
}
isRunning = true;
currentFrame = 0;
trialStartMillis = now;
//serialOut(now, "trialStart", currentFrame);
}
}
/////////////////////////////////////////////////////////////
void trialStop(unsigned long now) {
if (isRunning) {
if (useTwoTriggerPins) {
digitalWrite(triggerPin, LOW); //
} else {
digitalWrite(framePin, LOW); //
}
isRunning = false;
//serialOut(now, "trialStop", currentFrame);
}
}
/////////////////////////////////////////////////////////////////
void Update()
{
unsigned long now = millis();
//if (Serial.available() > 0) {
// inString = Serial.readStringUntil('\n');
// SerialIn(now, inString);
//}
//update scope frames
if (isRunning) {
if (now > (trialStartMillis + firstFrameDelay)) {
if (now >= (lastFrameMillis + frameInterval)) {
lastFrameMillis = now;
currentFrame += 1;
digitalWrite(ledPin, LOW); //so we can see if the code is running
if (useTwoTriggerPins) {
digitalWrite(framePin, HIGH); //so we can see if the code is running
if (framePinDur > 0) delay(framePinDur);
digitalWrite(framePin, LOW); //so we can see if the code is running
} else {
digitalWrite(framePin, LOW); //so we can see if the code is running
if (framePinDur > 0) delay(framePinDur);
digitalWrite(framePin, HIGH); //so we can see if the code is running
}
//serialOut(now, "newFrame", currentFrame);
if (currentFrame == numFrames) {
trialStop(now);
}
} else {
digitalWrite(ledPin, HIGH); //so we can see if the code is running
}
}
} else {
digitalWrite(ledPin, LOW); //so we can see if the code is running
} //isRunning
} //update
}; // class
// start of main Arduino code
/*
int triggerPin = 2;
int framePin = 3;
int ledPin = 13;
bSimulateScope myScope(triggerPin, framePin, ledPin);
void setup()
{
}
void loop()
{
myScope.Update();
}
*/