Subversion Repositories HomeAutomation

Rev

Rev 2164 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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