-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathachsubscriber.cpp
74 lines (61 loc) · 1.9 KB
/
achsubscriber.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
#include "achshm.h"
#include "timer.h"
cshmSubscriber::cshmSubscriber(const char* channel_name) {
last_index_read = 0;
last_sequence_read = 0;
if(cshm::ChannelInfo(channel_name, &total_frames, &frame_size)) {
shm = new cshm(channel_name, total_frames, frame_size);
is_open = 1;
return;
}
is_open = 0;
}
size_t cshmSubscriber::getFrameSize() {
return frame_size;
}
int cshmSubscriber::getTotalFrames() {
return total_frames;
}
cshm_error cshmSubscriber::open() {
if(!is_open) return ACH_BAD_SHM_FILE;
err = shm->Open();
if(err != ACH_OK)
return err;
shm->getLastWrittenBufferInfo(&last_index_read, &last_sequence_read);
// Bug.. last_index_read should be something different
return err;
}
void cshmSubscriber::reset() {
last_index_read = 0;
last_sequence_read = 0;
}
cshm_error cshmSubscriber::getNext(void* buffer, cshm_buffer_info_t* info) {
if(!is_open) return ACH_BAD_SHM_FILE;
err = shm->Read(buffer, info, last_index_read);
if(err != ACH_OK) return err;
// Deprecated, For future release
// Check if we are slow/fast
// May be use getLatest and reinitialize last_index_read and last_sequence_read
// if(info->seq_num < last_sequence_read - total_frames) err = ACH_READER_SLOW;
// if(info->seq_num == last_sequence_read) err = ACH_READER_FAST;
last_sequence_read++;
last_index_read = last_sequence_read % total_frames;
last_time_read = Timer::getCurrentTime();
return err;
}
cshm_error cshmSubscriber::getLatest(void* buffer, cshm_buffer_info_t *info) {
if(!is_open) return ACH_BAD_SHM_FILE;
last_time_read = Timer::getCurrentTime();
return shm->ReadLatest(buffer, info);
}
void cshmSubscriber::setFrequencyErrorCallBack(void (*func)(void), int _frequency) {
callback = func;
last_time = &last_time_read;
frequency = 1/_frequency;
interval = _frequency;
Timer::startTimer();
Timer::subscribeToTimer(callback, interval, last_time);
}
cshmSubscriber::~cshmSubscriber() {
delete shm;
}