[go: up one dir, main page]

Menu

[5c152e]: / test / Trigger.cc  Maximize  Restore  History

Download this file

136 lines (115 with data), 2.6 kB

  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
#include <iostream>
#include <cstdio>
#include "Trigger.h"
Trigger::Trigger(double f)
: fd(-1), freq(f), fname("/tmp/trigger.dat"), running(false)
{
if (! access(fname.c_str(), F_OK) )
unlink(fname.c_str());
if ( mkfifo(fname.c_str(), 0660) )
{
perror("Could not create the fifo:\n\t");
return;
}
fd = open(fname.c_str(), O_RDWR);
if (freq <= 0)
freq = 50.;
set_freq(freq);
}
Trigger::~Trigger()
{
if (fd >= 0)
{
close(fd);
unlink(fname.c_str());
}
}
void Trigger::run()
{
char val = 'A';
running = true;
while (running)
{
loop();
write(fd, &val, sizeof(char));
test_cancel();
}
}
void Trigger::loop()
{
// volatile unsigned long iloop;
// for (iloop = 0;iloop < nloop;iloop++);
DAQpp::wait(period);
}
void Trigger::set_freq(double x)
{
DAQpp::GTimer timer;
if (x > 0.)
{
freq = x;
period = 1./freq;
}
// nloop = 100000000;
// timer.start();
// loop();
// timer.stop();
// nloop = (unsigned long)(((double)nloop) / timer() / freq);
// std::cout << "nloop " << nloop << std::endl;
}
/*
Function to enable and disable the external trigger, and the trigger
function.
*/
bool Trigger::operator()()
{
int rc=0;
char val;
if ( fdlist.fd < 0 )
return false;
while (rc == 0)
{
// we poll with a timeout to avoid hang ups
rc = poll(&fdlist, 1, 100);
if ( ! DAQpp::DAQmanager::theDAQmanager()->isRunning() )
break;
if (rc == 0)
continue;
if (rc > 0)
{
if ( fdlist.revents & (POLLNVAL | POLLERR | POLLHUP | POLLERR) )
return false;
if ( fdlist.revents & (POLLIN | POLLPRI) )
{
read(fdlist.fd, &val, sizeof(char));
return true;
}
}
else
{
std::cout << "InterruptTrigger: something happened" << std::endl;
}
}
return false;
}
void Trigger::enableInterrupts()
{
fdlist.fd = open(get_file_name().c_str(), O_RDONLY);
fdlist.events = POLLIN | POLLPRI;
// Make DAQmanager aware of this
DAQpp::DAQmanager::theDAQmanager()->set_trigger(this);
// start the thread that generates the data
start();
return;
}
void Trigger::disableInterrupts()
{
DAQpp::DAQmanager::theDAQmanager()->set_trigger(0);
running = false;
DAQpp::wait(0.001);
if (is_running())
stop();
if ( fdlist.fd >= 0 )
close(fdlist.fd);
fdlist.fd = -1;
return;
}