summaryrefslogtreecommitdiff
path: root/lib/MIMOSA.pm
blob: 54a5e15cb45b8ac9906c09c181eed82fca3ef60b (plain)
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
package MIMOSA;

use strict;
use warnings;
use 5.020;

use Carp;
use Carp::Assert::More;
use File::Slurp qw(read_dir);
use IPC::Run qw(harness);
use List::Util qw(max);

our $VERSION = '0.00';

sub new {
	my ( $class, %opt ) = @_;

	my $self = \%opt;

	bless( $self, $class );

	return $self;
}

sub start {
	my ($self) = @_;
	my $buf;

	my $mim_daemon = harness(
		[ 'MimosaCMD', '--start' ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_p1 = harness(
		[ 'MimosaCMD', '--parameter', 'offset', $self->{mimosa_offset} ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_p2 = harness(
		[ 'MimosaCMD', '--parameter', 'shunt', $self->{mimosa_shunt} ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_p3 = harness(
		[ 'MimosaCMD', '--parameter', 'voltage', $self->{mimosa_voltage} ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_p4 = harness(
		[ 'MimosaCMD', '--parameter', 'directory', 'src/apps/DriverEval' ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_start = harness(
		[ 'MimosaCMD', '--mimosa-start' ],
		'<'  => \undef,
		'>&' => \$buf
	);

	if ( $self->is_running ) {
		croak("MIMOSA daemon is already running");
	}

	$mim_daemon->run or croak();
	$mim_p1->run     or croak();
	$mim_p2->run     or croak();
	$mim_p3->run     or croak();
	$mim_start->run  or croak();
}

sub is_running {
	my ($self) = @_;

	my $buf;

	my $mim_check = harness(
		[ 'pidof', 'MimosaCMD' ],
		'<'  => \undef,
		'>&' => \$buf
	);

	return $mim_check->run;
}

sub stop {
	my ($self) = @_;
	my $buf;

	my $mim_stop = harness(
		[ 'MimosaCMD', '--mimosa-stop' ],
		'<'  => \undef,
		'>&' => \$buf
	);
	my $mim_kill = harness(
		[ 'MimosaCMD', '--stop' ],
		'<'  => \undef,
		'>&' => \$buf
	);

	# make sure MIMOSA has all teh data
	sleep(5);

	$mim_stop->run or croak();

	$self->wait_for_save;

	$mim_kill->run or croak();

	while ( $self->is_running ) {
		sleep(1);
	}
}

sub wait_for_save {
	my ($self) = @_;

	my $mtime         = 0;
	my $mtime_changed = 1;

	while ($mtime_changed) {
		sleep(3);
		my @mim_files = grep { m{ \. mim $ }x } read_dir('.');
		my @mtimes    = map  { ( stat($_) )[9] } @mim_files;
		my $new_mtime = max @mtimes;
		if ( $new_mtime != $mtime ) {
			$mtime = $new_mtime;
		}
		else {
			$mtime_changed = 0;
		}
	}

	return $self;
}

sub kill {
	my ($self) = @_;
	my $buf;

	my $mim_kill = harness(
		[ 'MimosaCMD', '--stop' ],
		'<'  => \undef,
		'>&' => \$buf
	);

	$mim_kill->run or croak();
}

sub calibrate {
	my ($self) = @_;

	$self->mimosactl('disconnect');
	sleep(2);
	$self->mimosactl('1k');      # actually 987 Ohm
	sleep(2);
	$self->mimosactl('100k');    # actually 99.3 kOhm
	sleep(2);
	$self->mimosactl('connect');
}

sub mimosactl {
	my ( $self, $arg ) = @_;
	my $buf;

	my $mimosactl = harness(
		[ 'mimosactl', $arg ],
		'<'  => \undef,
		'>&' => \$buf
	);

	$mimosactl->run
	  or croak( "mimosactl $arg returned " . $mimosactl->full_result );

	return $self;
}

1;