-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssbtgpio.cpp
127 lines (114 loc) · 3.16 KB
/
ssbtgpio.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
#include "ssbtgpio.h"
#include <wiringPi.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm> // for count()
using namespace std;
SSBTGPIO::SSBTGPIO(){
datafile.open("dummy.txt");
lines_done = 0;
}
SSBTGPIO::~SSBTGPIO(){
datafile.close();
}
int SSBTGPIO::setUpPins(){
if(wiringPiSetup() == -1) return -1;
pinMode(MPLXR0, OUTPUT);
pinMode(MPLXR1, OUTPUT);
pinMode(IN0, INPUT);
pinMode(IN1, INPUT);
pinMode(IN2, INPUT);
pinMode(IN3, INPUT);
pullUpDnControl(IN0, PUD_OFF);
pullUpDnControl(IN1, PUD_OFF);
pullUpDnControl(IN2, PUD_OFF);
pullUpDnControl(IN3, PUD_OFF);
numlines = 0;
DAQ_flag = false;
return 0; // no error
}
// request one of five channels from mux, return integer value of 4-bit read 0-15
int SSBTGPIO::readADC(int chn){
// select channel from multiplexer
if(chn == 3){
digitalWrite(MPLXR1,1);
digitalWrite(MPLXR0,1);
}else if(chn == 2){
digitalWrite(MPLXR1,1);
digitalWrite(MPLXR0,0);
}else if(chn == 1){
digitalWrite(MPLXR1,0);
digitalWrite(MPLXR0,1);
}else if(chn == 0){
digitalWrite(MPLXR1,0);
digitalWrite(MPLXR0,0);
}
// read from gpio pins
delay(100);
int val0 = digitalRead(IN0);
int val1 = digitalRead(IN1);
int val2 = digitalRead(IN2);
int val3 = digitalRead(IN3);
// convert 4-bit binary to decimal integer
int ret = (1*val0) + (2*val1) + (4*val2) + (8*val3);
return ret;
}
int SSBTGPIO::getPoint1(){
return point1;
}
int SSBTGPIO::getPoint2(){
return point2;
}
int SSBTGPIO::getNumLines(){
df_cur = datafile.tellg();
datafile.seekg(0, ios::beg);
df_begin = datafile.tellg();
datafile.seekg (0, ios::end);
df_end = datafile.tellg();
datafile.seekg(df_begin);
numlines = count(istreambuf_iterator<char>(datafile), istreambuf_iterator<char>(), '\n') - lines_done;
datafile.seekg(df_cur);
return numlines;
}
void SSBTGPIO::incLinesDone(){
lines_done++;
}
int SSBTGPIO::readFile(){
// how big is file now? did the DAQ hardware add more data points?
// iterate through entire file and count lines
df_cur = datafile.tellg();
datafile.seekg(0, ios::beg);
df_begin = datafile.tellg();
datafile.seekg (0, ios::end);
df_end = datafile.tellg();
datafile.seekg(df_begin);
// cout << "file length: " << df_end - df_cur << "\n";
numlines = count(istreambuf_iterator<char>(datafile), istreambuf_iterator<char>(), '\n') - lines_done;
// cout << "numlines: " << numlines << "\n";
datafile.seekg(df_cur);
// get some points from the file and put them in point1 and point2
bool done = false;
if (datafile){
string s;
if (getline( datafile, s )){
istringstream ss( s );
if (ss){
string s;
if (getline( ss, s, ',' )) point1 = stoi(s);
if (getline( ss, s, ',' )) point2 = stoi(s);
}
}else return 1; // finished file
}else return -1; // file read error
return 0; // got one line fine
}
void SSBTGPIO::DAQisr(){
DAQ_flag = !DAQ_flag; // switch triggers isr despite position
if(DAQ_flag){
df_cur = datafile.tellg();
datafile.seekg (0, ios::end);
datafile << readADC(0) << ", " << readADC(1) << "\n";
datafile.seekg(df_cur);
}
}