Subversion Repositories HomeAutomation

Rev

Rev 2164 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1666 runge 1
#!/usr/bin/perl
2
 
3
# Written by Mattias Runge 2008-2010
4
 
5
use Cwd 'abs_path';
1885 arune 6
use File::Basename;
1890 arune 7
use Time::HiRes qw( usleep );
1666 runge 8
 
9
$| = 1;
10
 
11
$numArgs = $#ARGV + 1;
12
 
13
my $hostname = "localhost";
14
my $port = 1201;
15
my $filename = "";
16
my $hwid = "";
17
my $bios = 0;
18
my $reset = "false";
19
 
20
foreach $argnum (0 .. $#ARGV)
21
{
22
	if ($ARGV[$argnum] eq "-h")
23
	{
24
		$hostname = $ARGV[$argnum+1];
25
		next;
26
 
27
	}
28
	elsif ($ARGV[$argnum] eq "-p")
29
	{
30
		$port = $ARGV[$argnum+1];
31
		next;
32
 
33
	}
34
	elsif ($ARGV[$argnum] eq "-i")
35
	{
36
		$hwid = $ARGV[$argnum+1];
37
		next;
38
 
39
	}
40
	elsif ($ARGV[$argnum] eq "-f")
41
	{
42
		$filename = abs_path($ARGV[$argnum+1]);
43
		next;
44
 
45
	}
46
	elsif ($ARGV[$argnum] eq "-b")
47
	{
48
		$bios = 1;
49
	}
50
	elsif ($ARGV[$argnum] eq "-r")
51
	{
52
		$reset = "true";
53
	}
54
}
55
 
56
if ($hwid eq "")
57
{
58
	die "You must specify a hardware id!\n";
59
	exit 1;
60
}
61
 
1885 arune 62
my $path = dirname(abs_path($0));
63
 
64
# Read atom functions from separate file
65
if (-e $path."/setConfig/AtomLib.pl") {
66
	require $path."/setConfig/AtomLib.pl";
67
} else {
68
	print "Error: No AtomLib.pl was found in ".$path.". \n\n";
69
	exit 1;
70
}
71
 
1666 runge 72
#0x14f90d1f to 0x1f0df914
73
@hwidChars = split(//, $hwid);
1877 arune 74
$hwid = $hwidChars[0]. $hwidChars[1]. uc($hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3]);
1666 runge 75
 
76
print "atomDude settings:\n";
77
print "Hardware id: " . $hwid . "\n";
78
print "Atom address: " . $hostname . ":" . $port . "\n";
79
print "Hexfile: " . $filename . "\n";
2159 arune 80
#print "Parameters: " . "\n";
1666 runge 81
 
2161 arune 82
############################## RESET ##############################
1666 runge 83
if ($reset eq "true" && $filename eq "")
84
{
2187 arune 85
	$socket = atomd_initialize($hostname, $port);
2158 arune 86
	if ($port == 1203)
87
	{
88
		atomjs_write($socket, "Node_ResetNative('".$hwid."');\n");
2161 arune 89
		$response = atomjs_read_line($socket);
90
		if ($response =~ m/Error/)
91
		{
92
			die $response;
93
		}
94
		print $response;
95
 
2159 arune 96
		$success = 0;
2158 arune 97
		for ($count = 0; $count < 15; $count++)
98
		{
99
			atomjs_write($socket, "Node_ResetPollNative();\n");
100
			if (atomjs_read_line($socket) =~ m/true/)
101
			{
102
				print "\033[32m" . $hwid . " started okay.\033[0m\n";
2159 arune 103
				$success = 1;
2158 arune 104
				last;
105
			}
106
			usleep(200000);
107
		}
2159 arune 108
		if ($success == 0)
109
		{
110
			print "\033[31mTimed out waiting for reset.\033[0m\n";
111
		}
2158 arune 112
	}
113
	else
114
	{
115
		usleep(100000);
116
		atomd_kill_promt($socket);
117
		atomd_send_command($socket, "Node_Reset $hwid");
118
		print atomd_read_command_response($socket);
119
		#my $result = system("atomic -s $hostname -p $port -c \"Node_Reset $hwid\"");
120
		#exit($result);
121
	}
1666 runge 122
}
2161 arune 123
############################## REPROGRAM ##############################
1666 runge 124
else
125
{
2187 arune 126
	open (FP, "+<".$filename) or die "Error: Cannot open file $filename\n";
127
	@filecontents = <FP>;
128
	close FP;
129
	$socket = atomd_initialize($hostname, $port);
130
 
2158 arune 131
	if ($port == 1203)
132
	{
2159 arune 133
		atomjs_write($socket, "Node_ClearHex();\n");
134
		atomjs_read_line($socket);
135
		my $linenums = 0;
136
		foreach $filerow (@filecontents) 
137
		{
2158 arune 138
			$filerow =~ s/^\s+//;
139
			$filerow =~ s/\s+$//;
2159 arune 140
			$linenums += 1;
141
			$command = "Node_AppendHexNative('".$filerow."');\n";
142
			atomjs_write($socket, $command);
2161 arune 143
			$response = atomjs_read_line($socket);
144
			if ($response =~ m/Error/)
145
			{
146
				die $response;
147
			}
2158 arune 148
		}
2159 arune 149
		$command = "Node_ProgramNative('".$hwid."', '".$bios."', '".$linenums."');\n";
150
		atomjs_write($socket, $command);
151
		#print $command;
2161 arune 152
		$response = atomjs_read_line($socket);
153
		if ($response =~ m/Error/)
154
		{
155
			die $response;
156
		}
157
		print $response;
158
#atomd_disconnect($socket);
159
		$success = 0;
2164 arune 160
		$alreadyprintfull = 0;
2161 arune 161
		for ($count = 0; $count < 150; $count++)
162
		{
163
			atomjs_write($socket, "Node_ProgramPollNative();\n");
164
			if (atomjs_read_line($socket) =~ m/true/)
165
			{
2164 arune 166
				print progress_bar( 100, 100, 40, '=' );
167
				print "\n\033[32m" . $hwid . " started okay.\033[0m\n";
2161 arune 168
				$success = 1;
169
				last;
170
			}
2164 arune 171
			atomjs_write($socket, "Node_GetProgramProgress('".$hwid."');\n");
172
			$response = atomjs_read_line($socket);
173
			if ($response =~ m/Error/)
174
			{
175
				die $response;
176
			}
177
			$response =~ s/^\s+//;
178
			$response =~ s/\s+$//;
179
			if ($response > 99)
180
			{
181
				$response = 99;
182
			}
183
			#print $response."\n";
184
			print progress_bar( $response, 100, 40, '=' );
185
 
2161 arune 186
			usleep(200000);
187
		}
188
		if ($success == 0)
189
		{
2164 arune 190
			print "\n\033[31mTimed out waiting for program to finish.\033[0m\n";
2161 arune 191
		}
2158 arune 192
	}
193
	else
194
	{
195
		usleep(100000);
196
		atomd_kill_promt($socket);
1890 arune 197
 
2158 arune 198
		#print "Sending Node_ClearHex\n";
199
		atomd_send_command($socket, "Node_ClearHex");
200
		atomd_kill_promt($socket);
1967 arune 201
 
2158 arune 202
		my $linenums = 0;
2159 arune 203
		foreach $filerow (@filecontents) 
204
		{
2158 arune 205
			#print "Sending Node_AppendHex $filerow \n";
206
			#$filerow =~ s/^\s+//;
207
			#$filerow =~ s/\s+$//;
208
			atomd_send_command($socket, "Node_AppendHex $filerow");
209
			atomd_kill_promt($socket);
210
			$linenums += 1;
211
		}
212
		#print "Sending Node_ProgramHex $hwid $bios $linenums \n";
213
		atomd_send_command($socket, "Node_ProgramHex $hwid $bios $linenums");
1890 arune 214
 
2158 arune 215
		print atomd_read_command_response($socket);
1913 arune 216
 
2158 arune 217
#		my $result = system("atomic -s $hostname -p $port -c \"Node_Program $hwid $bios $filename\"");
218
#		exit($result);
2159 arune 219
	}
1666 runge 220
}
221
 
222
exit(0);
2164 arune 223
 
224
# wget-style. routine by tachyon
225
# at http://tachyon.perlmonk.org/
226
sub progress_bar {
227
    my ( $got, $total, $width, $char ) = @_;
228
    $width ||= 25; $char ||= '=';
229
    my $num_width = length $total;
230
    sprintf "|%-${width}s| (%.2f%%)\r", 
231
        $char x (($width-1)*$got/$total). '>', 
232
        $got, $total, 100*$got/+$total;
233
}
234