-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.cpp
197 lines (163 loc) · 4.57 KB
/
devices.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
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
/*
Classes malfunctionRecord and device for car embedded system project.
Author: Larry Henschen
Last Modified: Nov 26, 2016 (Sara Sood)
*/
#include "definitions.h"
#include "devices.h"
#include "events.h"
#include <iostream>
#include <cmath>
using namespace std;
extern int globalTime;
extern LIST* eventList;
extern bool checkEngine;
// malfunctionRecord functions.
malfunctionRecord::malfunctionRecord() {
count = 0;
lastMalfunction = NO_MALFUNCTION;
lastMalfunctionTime = 0;
}
void malfunctionRecord::display() const {
if(count==0) cout << "Device has never malfunctioned.\n";
else {
cout << "Malfunction record:\n";
cout << count << " malfunctions.\n";
cout << "Most recent: " << lastMalfunctionTime << "\n";
cout << "Malfunction type: ";
switch(lastMalfunction) {
case MALFUNCTION_OVER_RANGE:
{
cout << "reading over limit.\n";
break;
}
case MALFUNCTION_UNDER_RANGE:
{
cout << "reading under limit.\n";
break;
}
case MALFUNCTION_STOPPED_WORKING:
{
cout << "device stopped working.\n";
break;
}
default:
{
cout << "unspecified error.\n";
break;
}
}
}
}
void malfunctionRecord::malfunction(int mtype, int time) {
count += 1;
lastMalfunction = mtype;
lastMalfunctionTime = time;
}
int malfunctionRecord::getRecord(int& m, int& t) {
m = lastMalfunction;
t = lastMalfunctionTime;
return count;
}
// device class functions
device::device(string n, int i) {
// NOTE: The default constructor for mr should be called automatically.
name = n;
id = i;
value = 0;
type = GENERIC;
}
void device::display() const {
cout << "Device: " << name << ". ID: " << id << ". Value: " << value << "\n";
mr.display();
}
void device::recordMalfunction(int m, int t) {
mr.malfunction(m,t);
}
int device::getMalfunctionRecord(int& m, int& t) {
return mr.getRecord(m,t);
}
int device::amIThisDevice(string n) {
if(name == n)
return 1;
else
return 0;
}
int device::myType() {
return type;
}
int device::getValue() {
return value;
}
digitalSensorDevice::digitalSensorDevice(string n, int i, int v) : device(n, i) {
value = v;
type = DIGITAL_SENSOR;
}
void digitalSensorDevice::display() const {
device::display();
cout << "This is a digital sensor device." << endl;
cout << "The device is currently ";
if(value==ON) cout << "ON." << endl;
else cout << "OFF." << endl;
}
void digitalSensorDevice::setValue(int v) {
value = v;
}
analogSensorDevice::analogSensorDevice(string n, int i, int v, int bor) : device(n, i) {
type = ANALOG_SENSOR;
bitsOfResolution = bor;
if (bitsOfResolution < 1 || bitsOfResolution > 16)
bitsOfResolution = 8;
analogSensorDevice::setValue(v);
}
void analogSensorDevice::display() const {
device::display();
cout << "This is an analog sensor device." << endl;
cout << "The resolution is "<< bitsOfResolution <<", and the value is "<< value << "."<< endl;
}
void analogSensorDevice::setValue(int v) {
int big_x = pow(2, bitsOfResolution) - 1;
if (v < 0)
value = 0;
else if (v > big_x)
value = big_x;
else
value = v;
}
digitalControllerDevice::digitalControllerDevice(string n, int i) :device(n, i) {
value = OFF;
type = DIGITAL_CONTROLLER;
}
void digitalControllerDevice::display() const {
device::display();
cout << "This is a digital controller device." << endl;
cout << "The device is currently ";
if(value==ON) cout << "ON." << endl;
else cout << "OFF." << endl;
}
void digitalControllerDevice::setValue(int v) {
value = v;
}
analogControllerDevice::analogControllerDevice(string n, int i, int v, int up, int low) : device(n, i) {
lowerLimit = low;
upperLimit = up;
type = ANALOG_CONTROLLER;
analogControllerDevice::setValue(v);
}
void analogControllerDevice::display() const {
device::display();
cout << "This is an analog controller device with range "<< lowerLimit
<<" to "<< upperLimit <<"." << endl;
cout << "The value is "<< value <<"." << endl;
}
void analogControllerDevice::setValue(int v) {
if (v < lowerLimit) {
value = lowerLimit;
cout << "***ERROR: Controller " << name << " assigned value below range." << endl;
} else if (v > upperLimit) {
value = upperLimit;
cout << "***ERROR: Controller " << name << " assigned value above range." << endl;
} else {
value = v;
}
}