PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
DeviceState.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/devices/DeviceState.cpp
4// Purpose: Data structure allowing a device's state to be tested.
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#include "PDevice.h"
28#include "DeviceState.h"
29#include <iostream>
30
31using std::ostream;
32
34 mInterface(intface),
35 mDeviceTest(test),
36 mDevice(device),
37 mPDT(pdt)
38{
39
40
41}
42
47
49{
50 return mInterface;
51}
52
53
54int DeviceState::GetState(int iface) const
55{
56 return false;
57}
58
60{
61 //cout << "devicestate Dummy device tester\n";
62 return true;
63}
64
65
66//Overload of the << operator
67ostream & operator <<(ostream & out, const DeviceState & device )
68{
69 device.SendToStream(out);
70 return out;
71}
72
73
74
75ostream & DeviceState::SendToStream(ostream & out) const
76{
77 out << "<Anonymous DeviceState>" << std::flush;
78 return out;
79}
80
82 DeviceTest test,
83 int intface,
84 PDevice * device,
86 DeviceState(test, intface,device, pdt),
87 mValue(value)
88{
89 //cout << "Creating new valuestate; interface:" << mInterface << std::endl;
90}
91
95
96
97
98
100{
101
102
103 int value = mDevice->GetState(mInterface);
104 //cout << "test value|" << mValue << "==" <<"device value|"<<value<< std::endl;
105
106 switch(mDeviceTest)
107 {
108
109 case DT_NOT_EQUAL:
110 case DT_OUTSIDE:
111 return value != mValue;
112
113 case DT_EQUAL:
114 case DT_NOT_OUTSIDE:
115 case DT_ON_EDGE:
116 return value == mValue;
117
118 case DT_LESS_THAN:
119 return value < mValue;
120
122 return value <= mValue;
123
124 case DT_GREATER_THAN:
125 return value > mValue;
126
128 return value >= mValue;
129
130 case DT_NOT_INSIDE: //a value is always 'not inside' another value (even if they are the same).
131 case DT_TRUE:
132 return 1;
133
134 case DT_INSIDE: //a value is never inside another value (even if they are the same).
135 case DT_FALSE:
136 default:
137 return 0;
138
139 }
140}
141
142
143ostream & ValueState::SendToStream(ostream & out) const
144{
145 out << "<ValueState: " << mValue << "|"<< mInterface << ">" << std::flush;
146 return out;
147}
148
149
150
151IntervalState::IntervalState(int low, int high, DeviceTest test, int intface, PDevice * device, PEBL_DEVICE_TYPE pdt):
152 DeviceState(test, intface, device, pdt),
153 mLow(low),
154 mHigh(high)
155{
156}
157
161
163{
164
165
166 long int value = mDevice->GetState(mInterface);
167 switch(mDeviceTest)
168 {
169
170 case DT_NOT_EQUAL:
171 case DT_OUTSIDE:
172 return value > mHigh || value < mLow;
173
174 case DT_EQUAL:
175 case DT_NOT_OUTSIDE:
176 return value <= mHigh && value > mLow;
177
178 case DT_NOT_INSIDE:
179 return value <= mLow || value >= mHigh;
180
181 case DT_INSIDE:
182 return value < mHigh && value > mLow;
183
184 case DT_ON_EDGE:
185 return value == mLow || value == mHigh;
186
187 case DT_LESS_THAN: //Interpretation: less than the lower bound
188 return value < mLow;
189
190 case DT_LESS_THAN_OR_EQUAL: //Interpretation: less than or equal to the upper bound
191 return value <= mHigh;
192
193 case DT_GREATER_THAN:
194 return value > mHigh; //Interpretation: greater than the upper bound
195
196 case DT_GREATER_THAN_OR_EQUAL: //Interpretation: greater than or equal to the lower bound
197 return value >= mLow;
198
199 case DT_TRUE:
200 return 1;
201 case DT_FALSE:
202 default:
203 return 0;
204 }
205}
206
207
208
209
210
211
212ostream & IntervalState::SendToStream(ostream & out) const
213{
214 out << "<IntervalState: "<< mLow << " to " << mHigh << ">" << std::flush;
215 return out;
216}
217
218
219
220RegionState::RegionState(int lowX, int highX, int lowY, int highY, DeviceTest test, int intface, PDevice * device, PEBL_DEVICE_TYPE pdt):
221 DeviceState(test, intface, device, pdt),
222 mLowX(lowX),
223 mHighX(highX),
224 mLowY(lowY),
225 mHighY(highY)
226{
227}
228
232
233
234
236{
237
238 int valueX = mDevice->GetState(mInterface);
239 int valueY = mDevice->GetState(mInterface+1);
240
241 switch(mDeviceTest)
242 {
243
244 case DT_NOT_EQUAL:
245 case DT_OUTSIDE:
246 return valueX > mHighX || valueX < mLowX || valueY > mHighY || valueY < mLowY;
247
248 case DT_EQUAL:
249 case DT_NOT_OUTSIDE:
250 return valueX <= mHighX && valueX >= mLowX && valueY <= mHighY && valueY >= mLowY;
251
252
253 case DT_NOT_INSIDE:
254 return valueX >= mHighX || valueX <= mLowX || valueY >= mHighY || valueY <= mLowY;
255
256 case DT_INSIDE:
257 return valueX < mHighX && valueX > mLowX && valueY < mHighY && valueY > mLowY;
258
259 case DT_ON_EDGE:
260 return ((valueX == mLowX || valueX == mHighX) && valueY >= mLowY && valueY <= mHighY) ||
261 ((valueY == mLowY || valueY == mHighY) && valueX >= mLowX && valueX <= mHighX);
262
263 case DT_LESS_THAN: //Interpretation: less than both lower bounds
264 return valueX < mLowX && valueY < mLowY;
265
266 case DT_LESS_THAN_OR_EQUAL: //Interpretation: less than or equal to both upper bounds
267 return valueX <= mHighX && valueY <= mHighY;
268
269 case DT_GREATER_THAN: //Interpretation: greater than the upper bounds
270 return valueX > mHighX && valueY > mHighY;
271
272 case DT_GREATER_THAN_OR_EQUAL: //Interpretation: greater than or equal to the lower bounds
273 return valueX >= mLowX && valueY >= mLowY;
274
275 case DT_TRUE:
276 return 1;
277 case DT_FALSE:
278 default:
279 return 0;
280 }
281
282}
283
284
285ostream & RegionState::SendToStream(ostream & out) const
286{
287 out << "<RegionState: x:"<< mLowX << " to " << mHighX <<" y: " << mLowY << " to " << mHighY << ">" << std::flush;
288 return out;
289}
ostream & operator<<(ostream &out, const DeviceState &device)
DeviceTest
Definition DeviceState.h:41
@ DT_NOT_OUTSIDE
Definition DeviceState.h:49
@ DT_INSIDE
Definition DeviceState.h:48
@ DT_NOT_EQUAL
Definition DeviceState.h:42
@ DT_ON_EDGE
Definition DeviceState.h:52
@ DT_LESS_THAN_OR_EQUAL
Definition DeviceState.h:45
@ DT_TRUE
Definition DeviceState.h:53
@ DT_LESS_THAN
Definition DeviceState.h:44
@ DT_OUTSIDE
Definition DeviceState.h:50
@ DT_FALSE
Definition DeviceState.h:54
@ DT_GREATER_THAN_OR_EQUAL
Definition DeviceState.h:47
@ DT_NOT_INSIDE
Definition DeviceState.h:51
@ DT_EQUAL
Definition DeviceState.h:43
@ DT_GREATER_THAN
Definition DeviceState.h:46
PEBL_DEVICE_TYPE
Definition PDevice.h:40
DeviceTest mDeviceTest
virtual int GetState(int iface) const
int mInterface
This is different from the value or values that an interface can take on.
Definition DeviceState.h:99
DeviceState(DeviceTest test, int interfaced, PDevice *device, PEBL_DEVICE_TYPE pdt)
PDevice * mDevice
virtual std::ostream & SendToStream(std::ostream &out) const
virtual int GetInterface() const
virtual ~DeviceState()
virtual int TestDevice() const
virtual int TestDevice() const
std::ostream & SendToStream(std::ostream &out) const
virtual ~IntervalState()
virtual int GetState(int iface) const
Definition PDevice.cpp:65
virtual int TestDevice() const
virtual std::ostream & SendToStream(std::ostream &out) const
RegionState(int lowx, int highx, int lowy, int highy, DeviceTest test, int interfaced, PDevice *device, PEBL_DEVICE_TYPE pdt)
ValueState(int value, DeviceTest test, int interfaced, PDevice *device, PEBL_DEVICE_TYPE pdt)
virtual ~ValueState()
virtual std::ostream & SendToStream(std::ostream &out) const
virtual int TestDevice() const